4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "timestretchengine.h"
34 TimeStretchEngine::TimeStretchEngine(double scale, int sample_rate, int window_time)
37 output_allocation = 0;
41 update(scale, sample_rate, window_time);
44 TimeStretchEngine::~TimeStretchEngine()
46 if(output) delete [] output;
47 if(input) delete [] input;
50 void TimeStretchEngine::reset()
58 void TimeStretchEngine::update(double scale, int sample_rate, int window_time)
61 this->sample_rate = sample_rate;
62 this->window_time = window_time;
64 window_size = (int64_t)sample_rate * (int64_t)window_time / (int64_t)1000;
65 window_skirt = window_size / 2;
66 //printf("TimeStretchEngine::update %d %d\n", __LINE__, window_size);
67 if(output) delete [] output;
68 if(input) delete [] input;
69 //printf("TimeStretchEngine::update %d %d\n", __LINE__, window_size);
72 output_allocation = 0;
78 void TimeStretchEngine::overlay(double *out, double *in, int size, int skirt)
81 for(int i = 0; i < skirt; i++)
83 *out = *out * (1.0 - (double)i / skirt) + *in * ((double)i / skirt);
89 for(int i = 0; i < size - skirt; i++)
95 for(int i = 0; i < skirt; i++)
101 int TimeStretchEngine::process(Samples *in_buffer, int in_size)
103 // printf("TimeStretchEngine::process %d in_buffer=%p in_size=%d\n",
107 // Stack on input buffer
108 if(!input || input_size + in_size > input_allocation)
110 int new_input_allocation = input_size + in_size;
111 double *new_input = new double[new_input_allocation];
114 memcpy(new_input, input, input_size * sizeof(double));
118 input_allocation = new_input_allocation;
121 // printf("TimeStretchEngine::process %d input=%p input_size=%d input_allocation=%d\n",
125 // input_allocation);
127 memcpy(input + input_size, in_buffer->get_data(), in_size * sizeof(double));
128 input_size += in_size;
130 //printf("TimeStretchEngine::process %d\n", __LINE__);
131 // Overlay windows from input buffer into output buffer
135 int64_t current_out_sample = output_sample + output_size;
136 int64_t current_in_sample = (int64_t)((double)current_out_sample / scale);
138 if(current_in_sample - input_sample + window_size + window_skirt > input_size)
140 // Shift input buffer so the fragment that would have been copied now will be
141 // in the next iteration.
144 // printf("TimeStretchEngine::process %d %lld %d\n",
146 // current_in_sample - input_sample,
149 if(current_in_sample - input_sample < input_size)
152 input + current_in_sample - input_sample,
153 (input_size - (current_in_sample - input_sample)) * sizeof(double));
154 input_size -= current_in_sample - input_sample;
155 input_sample = current_in_sample;
159 //printf("TimeStretchEngine::process %d input_size=%d\n", __LINE__, input_size);
163 //printf("TimeStretchEngine::process %d\n", __LINE__);
164 // Allocate output buffer
165 if(output_size + window_size + window_skirt > output_allocation)
167 int new_allocation = output_size + window_size + window_skirt;
168 double *new_output = new double[new_allocation];
169 bzero(new_output, new_allocation * sizeof(double));
174 (output_size + window_skirt) * sizeof(double));
175 // printf("TimeStretchEngine::process %d this=%p output=%p new_allocation=%d\n",
181 //printf("TimeStretchEngine::process %d\n", __LINE__);
184 output_allocation = new_allocation;
187 // Overlay new window
188 overlay(output + output_size,
189 input + current_in_sample - input_sample,
192 output_size += window_size;
193 //printf("TimeStretchEngine::process 30 %d\n", output_size);
197 //printf("TimeStretchEngine::process %d %d\n", __LINE__, output_size);
201 void TimeStretchEngine::read_output(Samples *buffer, int size)
203 memcpy(buffer->get_data(), output, size * sizeof(double));
204 memcpy(output, output + size, (output_size + window_skirt - size) * sizeof(double));
206 output_sample += size;
209 int TimeStretchEngine::get_output_size()
214 double* TimeStretchEngine::get_samples()