-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibresamp.cpp
53 lines (40 loc) · 1.67 KB
/
libresamp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <thread>
#include "libsigproc.h"
#include "libresamp.h"
double* resample(double* signal, unsigned* length, unsigned channels, double resamplingFactor, unsigned sampleRadius) {
unsigned inputLength = *length;
unsigned outputLength = (unsigned)(resamplingFactor*inputLength);
double* output = new double[channels*outputLength];
unsigned channel;
std::thread *threads = new std::thread[channels];
//Starts a new thread for each channel...
for (channel = 0; channel < channels; channel++)
threads[channel] = std::thread(
// ...passing a lambda that receives arrays corresponding to the
//respective channel of the thread and makes the convolution.
[&](double *signal, double *output) {
unsigned spot, start, stop, m, n;
// For each sample "m" of the new signal...
for (m = 0; m < outputLength; m++) {
// ...determines the corresponding sample in the original signal...
spot = (unsigned)(m/resamplingFactor);
// ...and evaluates the relevant range of original samples...
start = (spot <= sampleRadius) ? 0 : spot - sampleRadius;
stop = (inputLength - spot > sampleRadius) ? spot+sampleRadius : inputLength;
// ...then convolutes the original signal in the range.
output[m] = 0;
for (n = start; n < stop; n++) {
output[m] += sinc((m/resamplingFactor) - n)*signal[n];
}
}
},
//Passes to the lambda function pointers positioned at the begining of
//each channel in the signal arrays.
(signal + (channel*inputLength)), (output + (channel*outputLength))
);
for (channel = 0; channel < channels; channel++)
threads[channel].join();
delete[] threads;
*length = outputLength;
return output;
}