1 #include "timestretchengine.h"
7 // Size of window in milliseconds
11 TimeStretchEngine::TimeStretchEngine(double scale, int sample_rate)
14 output_allocation = 0;
22 this->sample_rate = sample_rate;
23 window_time = WINDOW_TIME;
24 window_size = (int64_t)sample_rate * (int64_t)window_time / (int64_t)1000;
25 window_skirt = window_size / 2;
28 TimeStretchEngine::~TimeStretchEngine()
30 if(output) delete [] output;
31 if(input) delete [] input;
34 void TimeStretchEngine::overlay(double *out, double *in, int size, int skirt)
37 for(int i = 0; i < skirt; i++)
39 *out = *out * (1.0 - (double)i / skirt) + *in * ((double)i / skirt);
45 for(int i = 0; i < size - skirt; i++)
51 for(int i = 0; i < skirt; i++)
57 int TimeStretchEngine::process(double *in_buffer, int in_size)
59 //printf("TimeStretchEngine::process 1\n");
60 // Stack on input buffer
61 if(input_size + in_size > input_allocation)
63 int new_input_allocation = input_size + in_size;
64 double *new_input = new double[new_input_allocation];
67 memcpy(new_input, input, input_size * sizeof(double));
71 input_allocation = new_input_allocation;
74 //printf("TimeStretchEngine::process 10\n");
76 memcpy(input + input_size, in_buffer, in_size * sizeof(double));
77 input_size += in_size;
79 //printf("TimeStretchEngine::process 20\n");
80 // Overlay windows from input buffer into output buffer
84 int64_t current_out_sample = output_sample + output_size;
85 int64_t current_in_sample = (int64_t)((double)current_out_sample / scale);
87 if(current_in_sample - input_sample + window_size + window_skirt > input_size)
89 // Shift input buffer so the fragment that would have been copied now will be
90 // in the next iteration.
91 // printf("TimeStretchEngine::process 1 %lld %d\n",
92 // current_in_sample - input_sample,
94 if(current_in_sample - input_sample < input_size)
96 input + current_in_sample - input_sample,
97 (input_size - (current_in_sample - input_sample)) * sizeof(double));
98 input_size -= current_in_sample - input_sample;
99 input_sample = current_in_sample;
101 //printf("TimeStretchEngine::process 10\n");
105 //printf("TimeStretchEngine::process 20\n");
106 // Allocate output buffer
107 if(output_size + window_size + window_skirt > output_allocation)
109 int new_allocation = output_size + window_size + window_skirt;
110 double *new_output = new double[new_allocation];
111 bzero(new_output, new_allocation * sizeof(double));
116 (output_size + window_skirt) * sizeof(double));
120 output_allocation = new_allocation;
123 // Overlay new window
124 overlay(output + output_size,
125 input + current_in_sample - input_sample,
128 output_size += window_size;
129 //printf("TimeStretchEngine::process 30 %d\n", output_size);
133 //printf("TimeStretchEngine::process 100 %d\n", output_size);
137 void TimeStretchEngine::read_output(double *buffer, int size)
139 memcpy(buffer, output, size * sizeof(double));
140 memcpy(output, output + size, (output_size + window_skirt - size) * sizeof(double));
142 output_sample += size;
145 double* TimeStretchEngine::get_samples()