react-native-audio-api 0.6.5 → 0.7.0-nightly-fba4835-20250717
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/cpp/audioapi/android/core/AudioDecoder.cpp +44 -33
- package/android/src/main/java/com/swmansion/audioapi/system/MediaSessionCallback.kt +2 -2
- package/common/cpp/audioapi/HostObjects/BaseAudioContextHostObject.h +24 -23
- package/common/cpp/audioapi/core/AudioParam.cpp +5 -4
- package/common/cpp/audioapi/core/BaseAudioContext.cpp +2 -2
- package/common/cpp/audioapi/core/BaseAudioContext.h +1 -1
- package/common/cpp/audioapi/core/utils/AudioDecoder.h +38 -1
- package/common/cpp/audioapi/libs/audio-stretch/stretch.c +610 -0
- package/common/cpp/audioapi/libs/audio-stretch/stretch.h +49 -0
- package/ios/audioapi/ios/AudioAPIModule.mm +11 -8
- package/ios/audioapi/ios/core/AudioDecoder.mm +37 -31
- package/lib/commonjs/core/BaseAudioContext.js +7 -6
- package/lib/commonjs/core/BaseAudioContext.js.map +1 -1
- package/lib/module/core/BaseAudioContext.js +7 -6
- package/lib/module/core/BaseAudioContext.js.map +1 -1
- package/lib/typescript/core/BaseAudioContext.d.ts +4 -1
- package/lib/typescript/core/BaseAudioContext.d.ts.map +1 -1
- package/lib/typescript/interfaces.d.ts +1 -1
- package/lib/typescript/interfaces.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/core/BaseAudioContext.ts +12 -9
- package/src/interfaces.ts +4 -1
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// **** AUDIO-STRETCH **** //
|
|
3
|
+
// Time Domain Harmonic Scaler //
|
|
4
|
+
// Copyright (c) 2022 David Bryant //
|
|
5
|
+
// All Rights Reserved. //
|
|
6
|
+
// Distributed under the BSD Software License (see license.txt) //
|
|
7
|
+
////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
|
|
9
|
+
// stretch.c
|
|
10
|
+
|
|
11
|
+
// Time Domain Harmonic Compression and Expansion
|
|
12
|
+
//
|
|
13
|
+
// This library performs time domain harmonic scaling with pitch detection
|
|
14
|
+
// to stretch the timing of a 16-bit PCM signal (either mono or stereo) from
|
|
15
|
+
// 1/2 to 2 times its original length. This is done without altering any of
|
|
16
|
+
// the tonal characteristics.
|
|
17
|
+
//
|
|
18
|
+
// Use stereo (num_chans = 2), when both channels are from same source
|
|
19
|
+
// and should contain approximately similar content.
|
|
20
|
+
// For independent channels, prefer using multiple StretchHandle-instances.
|
|
21
|
+
// see https://github.com/dbry/audio-stretch/issues/6
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
#include <stdio.h>
|
|
25
|
+
#include <stdlib.h>
|
|
26
|
+
#include <stdint.h>
|
|
27
|
+
#include <string.h>
|
|
28
|
+
#include <math.h>
|
|
29
|
+
|
|
30
|
+
#include "stretch.h"
|
|
31
|
+
|
|
32
|
+
#define MIN_PERIOD 24 /* minimum allowable pitch period */
|
|
33
|
+
#define MAX_PERIOD 2400 /* maximum allowable pitch period */
|
|
34
|
+
|
|
35
|
+
#if INT_MAX == 32767
|
|
36
|
+
#define MERGE_OFFSET 32768L /* promote to long before offset */
|
|
37
|
+
#define abs32 labs /* use long abs to avoid UB */
|
|
38
|
+
#else
|
|
39
|
+
#define MERGE_OFFSET 32768
|
|
40
|
+
#define abs32 abs
|
|
41
|
+
#endif
|
|
42
|
+
|
|
43
|
+
#define MAX_CORR UINT32_MAX /* maximum value for correlation ratios */
|
|
44
|
+
|
|
45
|
+
struct stretch_cnxt {
|
|
46
|
+
int num_chans, inbuff_samples, shortest, longest, tail, head, fast_mode;
|
|
47
|
+
int16_t *inbuff, *calcbuff;
|
|
48
|
+
float outsamples_error;
|
|
49
|
+
uint32_t *results;
|
|
50
|
+
|
|
51
|
+
struct stretch_cnxt *next;
|
|
52
|
+
int16_t *intermediate;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
static void merge_blocks (int16_t *output, int16_t *input1, int16_t *input2, int samples);
|
|
56
|
+
static int find_period_fast (struct stretch_cnxt *cnxt, int16_t *samples);
|
|
57
|
+
static int find_period (struct stretch_cnxt *cnxt, int16_t *samples);
|
|
58
|
+
|
|
59
|
+
/*
|
|
60
|
+
* Initialize a context of the time stretching code. The shortest and longest periods
|
|
61
|
+
* are specified here. The longest period determines the lowest fundamental frequency
|
|
62
|
+
* that can be handled correctly. Note that higher frequencies can be handled than the
|
|
63
|
+
* shortest period would suggest because multiple periods can be combined, and the
|
|
64
|
+
* worst-case performance will suffer if too short a period is selected. The flags are:
|
|
65
|
+
*
|
|
66
|
+
* STRETCH_FAST_FLAG 0x1 Use the "fast" version of the period calculation
|
|
67
|
+
*
|
|
68
|
+
* STRETCH_DUAL_FLAG 0x2 Cascade two instances of the stretcher to expand
|
|
69
|
+
* available ratios to 0.25X to 4.00X
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
StretchHandle stretch_init (int shortest_period, int longest_period, int num_channels, int flags)
|
|
73
|
+
{
|
|
74
|
+
struct stretch_cnxt *cnxt;
|
|
75
|
+
int max_periods = 3;
|
|
76
|
+
|
|
77
|
+
if (flags & STRETCH_FAST_FLAG) {
|
|
78
|
+
longest_period = (longest_period + 1) & ~1;
|
|
79
|
+
shortest_period &= ~1;
|
|
80
|
+
max_periods = 4;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (longest_period <= shortest_period || shortest_period < MIN_PERIOD || longest_period > MAX_PERIOD) {
|
|
84
|
+
fprintf (stderr, "stretch_init(): invalid periods!\n");
|
|
85
|
+
return NULL;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
cnxt = (struct stretch_cnxt *) calloc (1, sizeof (struct stretch_cnxt));
|
|
89
|
+
|
|
90
|
+
if (cnxt) {
|
|
91
|
+
cnxt->inbuff_samples = longest_period * num_channels * max_periods;
|
|
92
|
+
cnxt->inbuff = calloc (cnxt->inbuff_samples, sizeof (*cnxt->inbuff));
|
|
93
|
+
|
|
94
|
+
if (num_channels == 2 || (flags & STRETCH_FAST_FLAG))
|
|
95
|
+
cnxt->calcbuff = calloc (longest_period * num_channels, sizeof (*cnxt->calcbuff));
|
|
96
|
+
|
|
97
|
+
if ((flags & STRETCH_FAST_FLAG))
|
|
98
|
+
cnxt->results = calloc (longest_period, sizeof (*cnxt->results));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!cnxt || !cnxt->inbuff || (num_channels == 2 && (flags & STRETCH_FAST_FLAG) && !cnxt->calcbuff) || ((flags & STRETCH_FAST_FLAG) && !cnxt->results)) {
|
|
102
|
+
fprintf (stderr, "stretch_init(): out of memory!\n");
|
|
103
|
+
return NULL;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
cnxt->head = cnxt->tail = cnxt->longest = longest_period * num_channels;
|
|
107
|
+
cnxt->fast_mode = (flags & STRETCH_FAST_FLAG) ? 1 : 0;
|
|
108
|
+
cnxt->shortest = shortest_period * num_channels;
|
|
109
|
+
cnxt->num_chans = num_channels;
|
|
110
|
+
|
|
111
|
+
if (flags & STRETCH_DUAL_FLAG) {
|
|
112
|
+
cnxt->next = stretch_init (shortest_period, longest_period, num_channels, flags & ~STRETCH_DUAL_FLAG);
|
|
113
|
+
cnxt->intermediate = calloc (longest_period * num_channels * max_periods, sizeof (*cnxt->intermediate));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return (StretchHandle) cnxt;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/*
|
|
120
|
+
* Re-Initialize a context of the time stretching code - as if freshly created
|
|
121
|
+
* with stretch_init(). This drops all internal state.
|
|
122
|
+
*/
|
|
123
|
+
|
|
124
|
+
void stretch_reset (StretchHandle handle)
|
|
125
|
+
{
|
|
126
|
+
struct stretch_cnxt *cnxt = (struct stretch_cnxt *) handle;
|
|
127
|
+
|
|
128
|
+
cnxt->head = cnxt->tail = cnxt->longest;
|
|
129
|
+
memset (cnxt->inbuff, 0, cnxt->tail * sizeof (*cnxt->inbuff));
|
|
130
|
+
|
|
131
|
+
if (cnxt->next)
|
|
132
|
+
stretch_reset (cnxt->next);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/*
|
|
136
|
+
* Determine how many samples (per channel) should be reserved in 'output'-array
|
|
137
|
+
* for stretch_samples() and stretch_flush(). max_num_samples and max_ratio are the
|
|
138
|
+
* maximum values that will be passed to stretch_samples().
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
int stretch_output_capacity (StretchHandle handle, int max_num_samples, float max_ratio)
|
|
142
|
+
{
|
|
143
|
+
struct stretch_cnxt *cnxt = (struct stretch_cnxt *) handle;
|
|
144
|
+
int max_period = cnxt->longest / cnxt->num_chans;
|
|
145
|
+
int max_expected_samples;
|
|
146
|
+
float next_ratio;
|
|
147
|
+
|
|
148
|
+
if (cnxt->next) {
|
|
149
|
+
if (max_ratio < 0.5) {
|
|
150
|
+
next_ratio = max_ratio / 0.5;
|
|
151
|
+
max_ratio = 0.5;
|
|
152
|
+
}
|
|
153
|
+
else if (max_ratio > 2.0) {
|
|
154
|
+
next_ratio = max_ratio / 2.0;
|
|
155
|
+
max_ratio = 2.0;
|
|
156
|
+
}
|
|
157
|
+
else
|
|
158
|
+
next_ratio = 1.0;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
max_expected_samples = (int) ceil (max_num_samples * ceil (max_ratio * 2.0) / 2.0) +
|
|
162
|
+
max_period * (cnxt->fast_mode ? 4 : 3);
|
|
163
|
+
|
|
164
|
+
if (cnxt->next)
|
|
165
|
+
max_expected_samples = stretch_output_capacity (cnxt->next, max_expected_samples, next_ratio);
|
|
166
|
+
|
|
167
|
+
return max_expected_samples;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/*
|
|
171
|
+
* Process the specified samples with the given ratio (which is normally clipped to
|
|
172
|
+
* the range 0.5 to 2.0, or 0.25 to 4.00 for the "dual" mode). Note that in stereo
|
|
173
|
+
* the number of samples refers to the samples for one channel (i.e., not the total
|
|
174
|
+
* number of values passed) and can be as large as desired (samples are buffered here).
|
|
175
|
+
* The ratio may change between calls, but there is some latency to consider because
|
|
176
|
+
* audio is buffered here and a new ratio may be applied to previously sent samples.
|
|
177
|
+
*
|
|
178
|
+
* The exact number of samples output is not easy to determine in advance, so a function
|
|
179
|
+
* is provided (stretch_output_capacity()) that calculates the maximum number of samples
|
|
180
|
+
* that can be generated from a single call to this function (or stretch_flush()) given
|
|
181
|
+
* a number of samples and maximum ratio. It is reccomended that that function be used
|
|
182
|
+
* after initialization to allocate in advance the buffer size required. Be sure to
|
|
183
|
+
* multiply the return value by the number channels!
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
int stretch_samples (StretchHandle handle, const int16_t *samples, int num_samples, int16_t *output, float ratio)
|
|
187
|
+
{
|
|
188
|
+
struct stretch_cnxt *cnxt = (struct stretch_cnxt *) handle;
|
|
189
|
+
int out_samples = 0, next_samples = 0;
|
|
190
|
+
int16_t *outbuf = output;
|
|
191
|
+
float next_ratio;
|
|
192
|
+
|
|
193
|
+
/* if there's a cascaded instance after this one, try to do as much of the ratio here and the rest in "next" */
|
|
194
|
+
|
|
195
|
+
if (cnxt->next) {
|
|
196
|
+
outbuf = cnxt->intermediate;
|
|
197
|
+
|
|
198
|
+
if (ratio < 0.5) {
|
|
199
|
+
next_ratio = ratio / 0.5;
|
|
200
|
+
ratio = 0.5;
|
|
201
|
+
}
|
|
202
|
+
else if (ratio > 2.0) {
|
|
203
|
+
next_ratio = ratio / 2.0;
|
|
204
|
+
ratio = 2.0;
|
|
205
|
+
}
|
|
206
|
+
else
|
|
207
|
+
next_ratio = 1.0;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
num_samples *= cnxt->num_chans;
|
|
211
|
+
|
|
212
|
+
/* this really should not happen, but a good idea to clamp in case */
|
|
213
|
+
|
|
214
|
+
if (ratio < 0.5)
|
|
215
|
+
ratio = 0.5;
|
|
216
|
+
else if (ratio > 2.0)
|
|
217
|
+
ratio = 2.0;
|
|
218
|
+
|
|
219
|
+
/* while we have pending samples to read into our buffer */
|
|
220
|
+
|
|
221
|
+
while (num_samples) {
|
|
222
|
+
|
|
223
|
+
/* copy in as many samples as we have room for */
|
|
224
|
+
|
|
225
|
+
int samples_to_copy = num_samples;
|
|
226
|
+
|
|
227
|
+
if (samples_to_copy > cnxt->inbuff_samples - cnxt->head)
|
|
228
|
+
samples_to_copy = cnxt->inbuff_samples - cnxt->head;
|
|
229
|
+
|
|
230
|
+
memcpy (cnxt->inbuff + cnxt->head, samples, samples_to_copy * sizeof (cnxt->inbuff [0]));
|
|
231
|
+
num_samples -= samples_to_copy;
|
|
232
|
+
samples += samples_to_copy;
|
|
233
|
+
cnxt->head += samples_to_copy;
|
|
234
|
+
|
|
235
|
+
/* while there are enough samples to process (3 or 4 times the longest period), do so */
|
|
236
|
+
|
|
237
|
+
while (cnxt->tail >= cnxt->longest && cnxt->head - cnxt->tail >= cnxt->longest * (cnxt->fast_mode ? 3 : 2)) {
|
|
238
|
+
float process_ratio;
|
|
239
|
+
int period;
|
|
240
|
+
|
|
241
|
+
if (ratio != 1.0 || cnxt->outsamples_error)
|
|
242
|
+
period = cnxt->fast_mode ? find_period_fast (cnxt, cnxt->inbuff + cnxt->tail) :
|
|
243
|
+
find_period (cnxt, cnxt->inbuff + cnxt->tail);
|
|
244
|
+
else
|
|
245
|
+
period = cnxt->longest;
|
|
246
|
+
|
|
247
|
+
/*
|
|
248
|
+
* Once we have calculated the best-match period, there are 4 possible transformations
|
|
249
|
+
* available to convert the input samples to output samples. Obviously we can simply
|
|
250
|
+
* copy the samples verbatim (1:1). Standard TDHS provides algorithms for 2:1 and
|
|
251
|
+
* 1:2 scaling, and I have created an obvious extension for 2:3 scaling. To achieve
|
|
252
|
+
* intermediate ratios we maintain a "error" term (in samples) and use that here to
|
|
253
|
+
* calculate the actual transformation to apply.
|
|
254
|
+
*/
|
|
255
|
+
|
|
256
|
+
if (cnxt->outsamples_error == 0.0)
|
|
257
|
+
process_ratio = floor (ratio * 2.0 + 0.5) / 2.0;
|
|
258
|
+
else if (cnxt->outsamples_error > 0.0)
|
|
259
|
+
process_ratio = floor (ratio * 2.0) / 2.0;
|
|
260
|
+
else
|
|
261
|
+
process_ratio = ceil (ratio * 2.0) / 2.0;
|
|
262
|
+
|
|
263
|
+
if (process_ratio == 0.5) {
|
|
264
|
+
merge_blocks (outbuf + out_samples, cnxt->inbuff + cnxt->tail,
|
|
265
|
+
cnxt->inbuff + cnxt->tail + period, period);
|
|
266
|
+
cnxt->outsamples_error += period - (period * 2.0 * ratio);
|
|
267
|
+
out_samples += period;
|
|
268
|
+
cnxt->tail += period * 2;
|
|
269
|
+
}
|
|
270
|
+
else if (process_ratio == 1.0) {
|
|
271
|
+
memcpy (outbuf + out_samples, cnxt->inbuff + cnxt->tail, period * 2 * sizeof (cnxt->inbuff [0]));
|
|
272
|
+
|
|
273
|
+
if (ratio != 1.0)
|
|
274
|
+
cnxt->outsamples_error += (period * 2.0) - (period * 2.0 * ratio);
|
|
275
|
+
else
|
|
276
|
+
cnxt->outsamples_error = 0; /* if the ratio is 1.0, we can never cancel the error, so just do it now */
|
|
277
|
+
|
|
278
|
+
out_samples += period * 2;
|
|
279
|
+
cnxt->tail += period * 2;
|
|
280
|
+
}
|
|
281
|
+
else if (process_ratio == 1.5) {
|
|
282
|
+
memcpy (outbuf + out_samples, cnxt->inbuff + cnxt->tail, period * sizeof (cnxt->inbuff [0]));
|
|
283
|
+
merge_blocks (outbuf + out_samples + period, cnxt->inbuff + cnxt->tail + period,
|
|
284
|
+
cnxt->inbuff + cnxt->tail, period);
|
|
285
|
+
memcpy (outbuf + out_samples + period * 2, cnxt->inbuff + cnxt->tail + period, period * sizeof (cnxt->inbuff [0]));
|
|
286
|
+
cnxt->outsamples_error += (period * 3.0) - (period * 2.0 * ratio);
|
|
287
|
+
out_samples += period * 3;
|
|
288
|
+
cnxt->tail += period * 2;
|
|
289
|
+
}
|
|
290
|
+
else if (process_ratio == 2.0) {
|
|
291
|
+
merge_blocks (outbuf + out_samples, cnxt->inbuff + cnxt->tail,
|
|
292
|
+
cnxt->inbuff + cnxt->tail - period, period * 2);
|
|
293
|
+
|
|
294
|
+
cnxt->outsamples_error += (period * 2.0) - (period * ratio);
|
|
295
|
+
out_samples += period * 2;
|
|
296
|
+
cnxt->tail += period;
|
|
297
|
+
|
|
298
|
+
if (cnxt->fast_mode) {
|
|
299
|
+
merge_blocks (outbuf + out_samples, cnxt->inbuff + cnxt->tail,
|
|
300
|
+
cnxt->inbuff + cnxt->tail - period, period * 2);
|
|
301
|
+
|
|
302
|
+
cnxt->outsamples_error += (period * 2.0) - (period * ratio);
|
|
303
|
+
out_samples += period * 2;
|
|
304
|
+
cnxt->tail += period;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else
|
|
308
|
+
fprintf (stderr, "stretch_samples: fatal programming error: process_ratio == %g\n", process_ratio);
|
|
309
|
+
|
|
310
|
+
/* if there's another cascaded instance after this, pass the just stretched samples into that */
|
|
311
|
+
|
|
312
|
+
if (cnxt->next) {
|
|
313
|
+
next_samples += stretch_samples (cnxt->next, outbuf, out_samples / cnxt->num_chans, output + next_samples * cnxt->num_chans, next_ratio);
|
|
314
|
+
out_samples = 0;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/* finally, left-justify the samples in the buffer leaving one longest period of history */
|
|
318
|
+
|
|
319
|
+
int samples_to_move = cnxt->inbuff_samples - cnxt->tail + cnxt->longest;
|
|
320
|
+
|
|
321
|
+
memmove (cnxt->inbuff, cnxt->inbuff + cnxt->tail - cnxt->longest,
|
|
322
|
+
samples_to_move * sizeof (cnxt->inbuff [0]));
|
|
323
|
+
|
|
324
|
+
cnxt->head -= cnxt->tail - cnxt->longest;
|
|
325
|
+
cnxt->tail = cnxt->longest;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/*
|
|
330
|
+
* This code is not strictly required, but will reduce latency, especially in the dual-instance case, by
|
|
331
|
+
* always flushing all pending samples if no actual stretching is desired (i.e., ratio is 1.0 and there's
|
|
332
|
+
* no error to compensate for). This case is more common now than previously because of the gap detection
|
|
333
|
+
* and cascaded instances.
|
|
334
|
+
*/
|
|
335
|
+
|
|
336
|
+
if (ratio == 1.0 && !cnxt->outsamples_error && cnxt->head != cnxt->tail) {
|
|
337
|
+
int samples_leftover = cnxt->head - cnxt->tail;
|
|
338
|
+
|
|
339
|
+
if (cnxt->next)
|
|
340
|
+
next_samples += stretch_samples (cnxt->next, cnxt->inbuff + cnxt->tail, samples_leftover / cnxt->num_chans,
|
|
341
|
+
output + next_samples * cnxt->num_chans, next_ratio);
|
|
342
|
+
else {
|
|
343
|
+
memcpy (outbuf + out_samples, cnxt->inbuff + cnxt->tail, samples_leftover * sizeof (*output));
|
|
344
|
+
out_samples += samples_leftover;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
memmove (cnxt->inbuff, cnxt->inbuff + cnxt->head - cnxt->longest, cnxt->longest * sizeof (cnxt->inbuff [0]));
|
|
348
|
+
cnxt->head = cnxt->tail = cnxt->longest;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return cnxt->next ? next_samples : out_samples / cnxt->num_chans;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/*
|
|
355
|
+
* Flush any leftover samples out at normal speed. For cascaded dual instances this must be called
|
|
356
|
+
* twice to completely flush, or simply call it until it returns zero samples. The maximum number
|
|
357
|
+
* of samples that can be returned from each call of this function can be determined in advance with
|
|
358
|
+
* stretch_output_capacity().
|
|
359
|
+
*/
|
|
360
|
+
|
|
361
|
+
int stretch_flush (StretchHandle handle, int16_t *output)
|
|
362
|
+
{
|
|
363
|
+
struct stretch_cnxt *cnxt = (struct stretch_cnxt *) handle;
|
|
364
|
+
int samples_leftover = cnxt->head - cnxt->tail;
|
|
365
|
+
int samples_flushed = 0;
|
|
366
|
+
|
|
367
|
+
if (cnxt->next) {
|
|
368
|
+
if (samples_leftover)
|
|
369
|
+
samples_flushed = stretch_samples (cnxt->next, cnxt->inbuff + cnxt->tail, samples_leftover / cnxt->num_chans, output, 1.0);
|
|
370
|
+
|
|
371
|
+
if (!samples_flushed)
|
|
372
|
+
samples_flushed = stretch_flush (cnxt->next, output);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
memcpy (output, cnxt->inbuff + cnxt->tail, samples_leftover * sizeof (*output));
|
|
376
|
+
samples_flushed = samples_leftover / cnxt->num_chans;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
cnxt->tail = cnxt->head;
|
|
380
|
+
memset (cnxt->inbuff, 0, cnxt->tail * sizeof (*cnxt->inbuff));
|
|
381
|
+
|
|
382
|
+
return samples_flushed;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/* free handle */
|
|
386
|
+
|
|
387
|
+
void stretch_deinit (StretchHandle handle)
|
|
388
|
+
{
|
|
389
|
+
struct stretch_cnxt *cnxt = (struct stretch_cnxt *) handle;
|
|
390
|
+
|
|
391
|
+
free (cnxt->calcbuff);
|
|
392
|
+
free (cnxt->results);
|
|
393
|
+
free (cnxt->inbuff);
|
|
394
|
+
|
|
395
|
+
if (cnxt->next) {
|
|
396
|
+
stretch_deinit (cnxt->next);
|
|
397
|
+
free (cnxt->intermediate);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
free (cnxt);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/*
|
|
404
|
+
* The pitch detection is done by finding the period that produces the
|
|
405
|
+
* maximum value for the following correlation formula applied to two
|
|
406
|
+
* consecutive blocks of the given period length:
|
|
407
|
+
*
|
|
408
|
+
* sum of the absolute values of each sample in both blocks
|
|
409
|
+
* ---------------------------------------------------------------------
|
|
410
|
+
* sum of the absolute differences of each corresponding pair of samples
|
|
411
|
+
*
|
|
412
|
+
* This formula was chosen for two reasons. First, it produces output values
|
|
413
|
+
* that can directly compared regardless of the pitch period. Second, the
|
|
414
|
+
* numerator can be accumulated for successive periods, and only the
|
|
415
|
+
* denominator need be completely recalculated.
|
|
416
|
+
*/
|
|
417
|
+
|
|
418
|
+
static int find_period (struct stretch_cnxt *cnxt, int16_t *samples)
|
|
419
|
+
{
|
|
420
|
+
uint32_t sum, diff, factor, scaler, best_factor = 0;
|
|
421
|
+
int16_t *calcbuff = samples;
|
|
422
|
+
int period, best_period;
|
|
423
|
+
int i, j;
|
|
424
|
+
|
|
425
|
+
period = best_period = cnxt->shortest / cnxt->num_chans;
|
|
426
|
+
|
|
427
|
+
// convert stereo to mono, and accumulate sum for longest period
|
|
428
|
+
|
|
429
|
+
if (cnxt->num_chans == 2) {
|
|
430
|
+
calcbuff = cnxt->calcbuff;
|
|
431
|
+
|
|
432
|
+
for (sum = i = j = 0; i < cnxt->longest * 2; i += 2)
|
|
433
|
+
sum += abs32 (calcbuff [j++] = ((int32_t) samples [i] + samples [i+1]) >> 1);
|
|
434
|
+
}
|
|
435
|
+
else
|
|
436
|
+
for (sum = i = 0; i < cnxt->longest; ++i)
|
|
437
|
+
sum += abs32 (calcbuff [i]) + abs32 (calcbuff [i+cnxt->longest]);
|
|
438
|
+
|
|
439
|
+
// if silence return longest period, else calculate scaler based on largest sum
|
|
440
|
+
|
|
441
|
+
if (sum)
|
|
442
|
+
scaler = (MAX_CORR - 1) / sum;
|
|
443
|
+
else
|
|
444
|
+
return cnxt->longest;
|
|
445
|
+
|
|
446
|
+
/* accumulate sum for shortest period size */
|
|
447
|
+
|
|
448
|
+
for (sum = i = 0; i < period; ++i)
|
|
449
|
+
sum += abs32 (calcbuff [i]) + abs32 (calcbuff [i+period]);
|
|
450
|
+
|
|
451
|
+
/* this loop actually cycles through all period lengths */
|
|
452
|
+
|
|
453
|
+
while (1) {
|
|
454
|
+
int16_t *comp = calcbuff + period * 2;
|
|
455
|
+
int16_t *ref = calcbuff + period;
|
|
456
|
+
|
|
457
|
+
/* compute sum of absolute differences */
|
|
458
|
+
|
|
459
|
+
diff = 0;
|
|
460
|
+
|
|
461
|
+
while (ref != calcbuff)
|
|
462
|
+
diff += abs32 ((int32_t) *--ref - *--comp);
|
|
463
|
+
|
|
464
|
+
/*
|
|
465
|
+
* Here we calculate and store the resulting correlation
|
|
466
|
+
* factor. Note that we must watch for a difference of
|
|
467
|
+
* zero, meaning a perfect match. Also, for increased
|
|
468
|
+
* precision using integer math, we scale the sum.
|
|
469
|
+
*/
|
|
470
|
+
|
|
471
|
+
factor = diff ? (sum * scaler) / diff : MAX_CORR;
|
|
472
|
+
|
|
473
|
+
if (factor >= best_factor) {
|
|
474
|
+
best_factor = factor;
|
|
475
|
+
best_period = period;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/* see if we're done */
|
|
479
|
+
|
|
480
|
+
if (period * cnxt->num_chans == cnxt->longest)
|
|
481
|
+
break;
|
|
482
|
+
|
|
483
|
+
/* update accumulating sum and current period */
|
|
484
|
+
|
|
485
|
+
sum += abs32 (calcbuff [period * 2]) + abs32 (calcbuff [period * 2 + 1]);
|
|
486
|
+
period++;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return best_period * cnxt->num_chans;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/*
|
|
493
|
+
* This pitch detection function is similar to find_period() above, except that it
|
|
494
|
+
* is optimized for speed. The audio data corresponding to two maximum periods is
|
|
495
|
+
* averaged 2:1 into the calculation buffer, and then the calulations are done
|
|
496
|
+
* for every other period length. Because the time is essentially proportional to
|
|
497
|
+
* both the number of samples and the number of period lengths to try, this scheme
|
|
498
|
+
* can reduce the time by a factor approaching 4x. The correlation results on either
|
|
499
|
+
* side of the peak are compared to calculate a more accurate center of the period.
|
|
500
|
+
*/
|
|
501
|
+
|
|
502
|
+
static int find_period_fast (struct stretch_cnxt *cnxt, int16_t *samples)
|
|
503
|
+
{
|
|
504
|
+
uint32_t sum, diff, scaler, best_factor = 0;
|
|
505
|
+
int period, best_period;
|
|
506
|
+
int i, j;
|
|
507
|
+
|
|
508
|
+
best_period = period = cnxt->shortest / (cnxt->num_chans * 2);
|
|
509
|
+
|
|
510
|
+
/* first step is compressing data 2:1 into calcbuff, and calculating maximum sum */
|
|
511
|
+
|
|
512
|
+
if (cnxt->num_chans == 2)
|
|
513
|
+
for (sum = i = j = 0; i < cnxt->longest * 2; i += 4)
|
|
514
|
+
sum += abs32 (cnxt->calcbuff [j++] = ((int32_t) samples [i] + samples [i+1] + samples [i+2] + samples [i+3]) >> 2);
|
|
515
|
+
else
|
|
516
|
+
for (sum = i = j = 0; i < cnxt->longest * 2; i += 2)
|
|
517
|
+
sum += abs32 (cnxt->calcbuff [j++] = ((int32_t) samples [i] + samples [i+1]) >> 1);
|
|
518
|
+
|
|
519
|
+
// if silence return longest period, else calculate scaler based on largest sum
|
|
520
|
+
|
|
521
|
+
if (sum)
|
|
522
|
+
scaler = (MAX_CORR - 1) / sum;
|
|
523
|
+
else
|
|
524
|
+
return cnxt->longest;
|
|
525
|
+
|
|
526
|
+
/* accumulate sum for shortest period */
|
|
527
|
+
|
|
528
|
+
for (sum = i = 0; i < period; ++i)
|
|
529
|
+
sum += abs32 (cnxt->calcbuff [i]) + abs32 (cnxt->calcbuff [i+period]);
|
|
530
|
+
|
|
531
|
+
/* this loop actually cycles through all period lengths */
|
|
532
|
+
|
|
533
|
+
while (1) {
|
|
534
|
+
int16_t *comp = cnxt->calcbuff + period * 2;
|
|
535
|
+
int16_t *ref = cnxt->calcbuff + period;
|
|
536
|
+
|
|
537
|
+
/* compute sum of absolute differences */
|
|
538
|
+
|
|
539
|
+
diff = 0;
|
|
540
|
+
|
|
541
|
+
while (ref != cnxt->calcbuff)
|
|
542
|
+
diff += abs32 ((int32_t) *--ref - *--comp);
|
|
543
|
+
|
|
544
|
+
/*
|
|
545
|
+
* Here we calculate and store the resulting correlation
|
|
546
|
+
* factor. Note that we must watch for a difference of
|
|
547
|
+
* zero, meaning a perfect match. Also, for increased
|
|
548
|
+
* precision using integer math, we scale the sum.
|
|
549
|
+
*/
|
|
550
|
+
|
|
551
|
+
cnxt->results [period] = diff ? (sum * scaler) / diff : MAX_CORR;
|
|
552
|
+
|
|
553
|
+
if (cnxt->results [period] >= best_factor) { /* check if best yet */
|
|
554
|
+
best_factor = cnxt->results [period];
|
|
555
|
+
best_period = period;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/* see if we're done */
|
|
559
|
+
|
|
560
|
+
if (period * cnxt->num_chans * 2 == cnxt->longest)
|
|
561
|
+
break;
|
|
562
|
+
|
|
563
|
+
/* update accumulating sum and current period */
|
|
564
|
+
|
|
565
|
+
sum += abs32 (cnxt->calcbuff [period * 2]) + abs32 (cnxt->calcbuff [period * 2 + 1]);
|
|
566
|
+
period++;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
if (best_period * cnxt->num_chans * 2 != cnxt->shortest && best_period * cnxt->num_chans * 2 != cnxt->longest) {
|
|
570
|
+
uint32_t high_side_diff = cnxt->results [best_period] - cnxt->results [best_period+1];
|
|
571
|
+
uint32_t low_side_diff = cnxt->results [best_period] - cnxt->results [best_period-1];
|
|
572
|
+
|
|
573
|
+
if ((low_side_diff + 1) / 2 > high_side_diff)
|
|
574
|
+
best_period = best_period * 2 + 1;
|
|
575
|
+
else if ((high_side_diff + 1) / 2 > low_side_diff)
|
|
576
|
+
best_period = best_period * 2 - 1;
|
|
577
|
+
else
|
|
578
|
+
best_period *= 2;
|
|
579
|
+
}
|
|
580
|
+
else
|
|
581
|
+
best_period *= 2; /* shortest or longest use as is */
|
|
582
|
+
|
|
583
|
+
return best_period * cnxt->num_chans;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/*
|
|
587
|
+
* To combine the two periods into one, each corresponding pair of samples
|
|
588
|
+
* are averaged with a linearly sliding scale. At the beginning of the period
|
|
589
|
+
* the first sample dominates, and at the end the second sample dominates. In
|
|
590
|
+
* this way the resulting block blends with the previous and next blocks.
|
|
591
|
+
*
|
|
592
|
+
* The signed values are offset to unsigned for the calculation and then offset
|
|
593
|
+
* back to signed. This is done to avoid the compression around zero that occurs
|
|
594
|
+
* with calculations of this type on C implementations that round division toward
|
|
595
|
+
* zero.
|
|
596
|
+
*
|
|
597
|
+
* The maximum period handled here without overflow possibility is 65535 samples.
|
|
598
|
+
* This corresponds to a maximum calculated period of 16383 samples (2x for stereo
|
|
599
|
+
* and 2x for the "2.0" version of the stretch algorithm). Since the maximum
|
|
600
|
+
* calculated period is currently set for 2400 samples, we have plenty of margin.
|
|
601
|
+
*/
|
|
602
|
+
|
|
603
|
+
static void merge_blocks (int16_t *output, int16_t *input1, int16_t *input2, int samples)
|
|
604
|
+
{
|
|
605
|
+
int i;
|
|
606
|
+
|
|
607
|
+
for (i = 0; i < samples; ++i)
|
|
608
|
+
output [i] = (int32_t)(((uint32_t)(input1 [i] + MERGE_OFFSET) * (samples - i) +
|
|
609
|
+
(uint32_t)(input2 [i] + MERGE_OFFSET) * i) / samples) - MERGE_OFFSET;
|
|
610
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// **** AUDIO-STRETCH **** //
|
|
3
|
+
// Time Domain Harmonic Scaler //
|
|
4
|
+
// Copyright (c) 2022 David Bryant //
|
|
5
|
+
// All Rights Reserved. //
|
|
6
|
+
// Distributed under the BSD Software License (see license.txt) //
|
|
7
|
+
////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
|
|
9
|
+
// stretch.h
|
|
10
|
+
|
|
11
|
+
// Time Domain Harmonic Compression and Expansion
|
|
12
|
+
//
|
|
13
|
+
// This library performs time domain harmonic scaling with pitch detection
|
|
14
|
+
// to stretch the timing of a 16-bit PCM signal (either mono or stereo) from
|
|
15
|
+
// 1/2 to 2 times its original length. This is done without altering any of
|
|
16
|
+
// its tonal characteristics.
|
|
17
|
+
//
|
|
18
|
+
// Use stereo (num_chans = 2), when both channels are from same source
|
|
19
|
+
// and should contain approximately similar content.
|
|
20
|
+
// For independent channels, prefer using multiple StretchHandle-instances.
|
|
21
|
+
// see https://github.com/dbry/audio-stretch/issues/6
|
|
22
|
+
|
|
23
|
+
#ifndef STRETCH_H
|
|
24
|
+
#define STRETCH_H
|
|
25
|
+
|
|
26
|
+
#include <stdint.h>
|
|
27
|
+
|
|
28
|
+
#define STRETCH_FAST_FLAG 0x1 // use "fast" version of period determination code
|
|
29
|
+
#define STRETCH_DUAL_FLAG 0x2 // cascade two instances (doubles usable ratio range)
|
|
30
|
+
|
|
31
|
+
#ifdef __cplusplus
|
|
32
|
+
extern "C" {
|
|
33
|
+
#endif
|
|
34
|
+
|
|
35
|
+
typedef void *StretchHandle;
|
|
36
|
+
|
|
37
|
+
StretchHandle stretch_init (int shortest_period, int longest_period, int num_chans, int flags);
|
|
38
|
+
int stretch_output_capacity (StretchHandle handle, int max_num_samples, float max_ratio);
|
|
39
|
+
int stretch_samples (StretchHandle handle, const int16_t *samples, int num_samples, int16_t *output, float ratio);
|
|
40
|
+
int stretch_flush (StretchHandle handle, int16_t *output);
|
|
41
|
+
void stretch_reset (StretchHandle handle);
|
|
42
|
+
void stretch_deinit (StretchHandle handle);
|
|
43
|
+
|
|
44
|
+
#ifdef __cplusplus
|
|
45
|
+
}
|
|
46
|
+
#endif
|
|
47
|
+
|
|
48
|
+
#endif
|
|
49
|
+
|