add auto zoombar/status color, fix 3 batchrender boobies, rotate plugin tweaks, add...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / timestretch / timestretchengine.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "samples.h"
23 #include "timestretchengine.h"
24
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29
30
31
32
33
34 TimeStretchEngine::TimeStretchEngine(double scale, int sample_rate, int window_time)
35 {
36         output = 0;
37         output_allocation = 0;
38         input = 0;
39         input_allocation = 0;
40         reset();
41         update(scale, sample_rate, window_time);
42 }
43
44 TimeStretchEngine::~TimeStretchEngine()
45 {
46         if(output) delete [] output;
47         if(input) delete [] input;
48 }
49
50 void TimeStretchEngine::reset()
51 {
52         input_size = 0;
53         input_sample = 0;
54         output_size = 0;
55         output_sample = 0;
56 }
57
58 void TimeStretchEngine::update(double scale, int sample_rate, int window_time)
59 {
60         this->scale = scale;
61         this->sample_rate = sample_rate;
62         this->window_time = window_time;
63
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);
70
71         output = 0;
72         output_allocation = 0;
73         input = 0;
74         input_allocation = 0;
75 }
76
77
78 void TimeStretchEngine::overlay(double *out, double *in, int size, int skirt)
79 {
80 // Fade in
81         for(int i = 0; i < skirt; i++)
82         {
83                 *out = *out * (1.0 - (double)i / skirt) + *in * ((double)i / skirt);
84                 out++;
85                 in++;
86         }
87
88 // Center
89         for(int i = 0; i < size - skirt; i++)
90         {
91                 *out++ = *in++;
92         }
93
94 // New skirt
95         for(int i = 0; i < skirt; i++)
96         {
97                 *out++ = *in++;
98         }
99 }
100
101 int TimeStretchEngine::process(Samples *in_buffer, int in_size)
102 {
103 // printf("TimeStretchEngine::process %d in_buffer=%p in_size=%d\n",
104 // __LINE__,
105 // in_buffer,
106 // in_size);
107 // Stack on input buffer
108         if(!input || input_size + in_size > input_allocation)
109         {
110                 int new_input_allocation = input_size + in_size;
111                 double *new_input = new double[new_input_allocation];
112                 if(input)
113                 {
114                         memcpy(new_input, input, input_size * sizeof(double));
115                         delete [] input;
116                 }
117                 input = new_input;
118                 input_allocation = new_input_allocation;
119         }
120
121 // printf("TimeStretchEngine::process %d input=%p input_size=%d input_allocation=%d\n",
122 // __LINE__,
123 // input,
124 // input_size,
125 // input_allocation);
126
127         memcpy(input + input_size, in_buffer->get_data(), in_size * sizeof(double));
128         input_size += in_size;
129
130 //printf("TimeStretchEngine::process %d\n", __LINE__);
131 // Overlay windows from input buffer into output buffer
132         int done = 0;
133         do
134         {
135                 int64_t current_out_sample = output_sample + output_size;
136                 int64_t current_in_sample = (int64_t)((double)current_out_sample / scale);
137
138                 if(current_in_sample - input_sample + window_size + window_skirt > input_size)
139                 {
140 // Shift input buffer so the fragment that would have been copied now will be
141 // in the next iteration.
142
143
144 // printf("TimeStretchEngine::process %d %lld %d\n",
145 // __LINE__,
146 // current_in_sample - input_sample,
147 // input_size);
148
149                         if(current_in_sample - input_sample < input_size)
150                         {
151                                 memcpy(input,
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;
156                         }
157
158                         done = 1;
159 //printf("TimeStretchEngine::process %d input_size=%d\n", __LINE__, input_size);
160                 }
161                 else
162                 {
163 //printf("TimeStretchEngine::process %d\n", __LINE__);
164 // Allocate output buffer
165                         if(output_size + window_size + window_skirt > output_allocation)
166                         {
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));
170                                 if(output)
171                                 {
172                                         memcpy(new_output,
173                                                 output,
174                                                 (output_size + window_skirt) * sizeof(double));
175 // printf("TimeStretchEngine::process %d this=%p output=%p new_allocation=%d\n",
176 // __LINE__,
177 // this,
178 // output,
179 // new_allocation);
180                                         delete [] output;
181 //printf("TimeStretchEngine::process %d\n", __LINE__);
182                                 }
183                                 output = new_output;
184                                 output_allocation = new_allocation;
185                         }
186
187 // Overlay new window
188                         overlay(output + output_size,
189                                 input + current_in_sample - input_sample,
190                                 window_size,
191                                 window_skirt);
192                         output_size += window_size;
193 //printf("TimeStretchEngine::process 30 %d\n", output_size);
194                 }
195         }while(!done);
196
197 //printf("TimeStretchEngine::process %d %d\n", __LINE__, output_size);
198         return output_size;
199 }
200
201 void TimeStretchEngine::read_output(Samples *buffer, int size)
202 {
203         memcpy(buffer->get_data(), output, size * sizeof(double));
204         memcpy(output, output + size, (output_size + window_skirt - size) * sizeof(double));
205         output_size -= size;
206         output_sample += size;
207 }
208
209 int TimeStretchEngine::get_output_size()
210 {
211         return output_size;
212 }
213
214 double* TimeStretchEngine::get_samples()
215 {
216         return output;
217 }
218
219
220
221
222
223
224
225
226