Mercurial > audlegacy-plugins
comparison src/sndstretch/sndstretch.c @ 881:6bb0e52df3d6 trunk
[svn] - Made port of XMMS plugin SndStretch
author | mf0102 |
---|---|
date | Tue, 20 Mar 2007 12:22:25 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
880:eb5fe37b785d | 881:6bb0e52df3d6 |
---|---|
1 // sndstretch.c | |
2 // | |
3 // sndstretch - algorithm for adjusting pitch and speed of s16le data | |
4 // Copyright (C) 2000 Florian Berger | |
5 // Email: florian.berger@jk.uni-linz.ac.at | |
6 // | |
7 // This program is free software; you can redistribute it and/or modify | |
8 // it under the terms of the GNU General Public License Version 2 as | |
9 // published by the Free Software Foundation; | |
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., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 // | |
20 // | |
21 | |
22 //#define DEBUG | |
23 | |
24 #include<stdio.h> | |
25 #include<stdlib.h> | |
26 #include<math.h> | |
27 #include"sndstretch.h" | |
28 | |
29 | |
30 static double _1_div_e = 0.367879441; // 1/e | |
31 static double _1_m_1_div_e = 0.632120558; // 1-1/e | |
32 | |
33 #define RESMAX 65536 | |
34 #define RESMAXVC 32768 | |
35 #define LOG2RESMAX 16 | |
36 #define LOG2RESMAXVC 15 | |
37 static int _1_div_e_i = 24109; // 1/e * 2^LOG2RESMAX | |
38 static int _1_m_1_div_e_i = 41427; // 1-1/e * 2^LOG2RESMAX | |
39 static int _1_div_e_i_vc = 12055; // 1/e * 2^LOG2RESMAXVC | |
40 static int _1_m_1_div_e_i_vc = 28333; // sqrt(1-1/e^2) * 2^LOG2RESMAXVC | |
41 //static int _1_div_e_i = 12055; // 1/e * 2^LOG2RESMAX | |
42 //static int _1_m_1_div_e_i = 20713; // 1-1/e * 2^LOG2RESMAX | |
43 | |
44 | |
45 void InitScaleJob( ScaleJob * job ) | |
46 { | |
47 /* nothing to do */ | |
48 } | |
49 | |
50 | |
51 void InitStretchJob( StretchJob * job ) | |
52 { | |
53 job->is_initialized=0; | |
54 job->snr_rest=0.0; | |
55 } | |
56 | |
57 | |
58 void InitPitchSpeedJob( PitchSpeedJob * job ) | |
59 { | |
60 job->ring_buff = (s16*)0; // init me = (s16*)0 | |
61 job->ring_buff_old = (s16*)0; // init me = (s16*)0 | |
62 job->buff_help = (s16*)0; // init me = (s16*)0 | |
63 job->ring_size = 1; // init me = 1 | |
64 job->ring_size_old = 0; // init me = 0 | |
65 job->ring_pos_w = 0; // init me = 0 | |
66 job->ring_pos_r = 0; // init me = 0 | |
67 job->is_init = 0; // init me = 0 | |
68 job->speed_act = 0; // init me = 0 | |
69 job->pitch_act = 0; // init me = 0 | |
70 job->fade_shift_act = 0; | |
71 InitStretchJob(&(job->stretch_job)); | |
72 InitScaleJob(&(job->scale_job)); | |
73 } | |
74 | |
75 | |
76 | |
77 void CleanupPitchSpeedJob( PitchSpeedJob * job ) | |
78 { | |
79 free(job->ring_buff); | |
80 free(job->ring_buff_old); | |
81 free(job->buff_help); | |
82 } | |
83 | |
84 | |
85 | |
86 inline int ringpos( int pos, int size ) | |
87 { | |
88 while ( pos >= size ) pos-=size; | |
89 while ( pos < 0 ) pos+=size; | |
90 return( pos ); | |
91 } | |
92 | |
93 | |
94 void ringload( s16 * ringbuff, int ring_size, int pos, s16 *buffer, int size ) | |
95 /* put <size> samples to ring with size <ring_size> on position <pos> */ | |
96 { | |
97 int i,j; | |
98 | |
99 j=0; | |
100 if( pos+size > ring_size ){ | |
101 for( i=pos; i<ring_size; i++,j++ ) ringbuff[i]=buffer[j]; | |
102 for( i=0; i<size-ring_size+pos; i++,j++ ) ringbuff[i]=buffer[j]; | |
103 }else{ | |
104 for( i=pos; i<size+pos; i++,j++ ) ringbuff[i]=buffer[j]; | |
105 } | |
106 } | |
107 | |
108 | |
109 void ringcopy( s16 * src_ring, int src_size, int src_from, int src_to, | |
110 s16 * dest_ring, int dest_size, int dest_pos ) | |
111 { | |
112 int is,id; | |
113 | |
114 is=src_from; id=dest_pos; | |
115 while( is!=src_to ){ | |
116 dest_ring[id]=src_ring[is]; | |
117 is=ringpos(is+1,src_size); | |
118 id=ringpos(id+1,dest_size); | |
119 } | |
120 } | |
121 | |
122 | |
123 void ringload_IIR_1_div_e_echo_d( s16 * ringbuff, int ring_size, int pos, s16 *buffer, int size , int delay) | |
124 /* put <size> samples to ring with size <ring_size> on position <pos> */ | |
125 { | |
126 int i,p1,p2; | |
127 | |
128 p1=pos, p2=ringpos(p1-delay, ring_size); | |
129 for( i=0; i<size; i++ ){ | |
130 ringbuff[p1] =(s16)( (double)buffer[i]*_1_m_1_div_e + (double)ringbuff[p2]*_1_div_e ); | |
131 p1++; if(p1>=ring_size) p1-=ring_size; | |
132 p2++; if(p2>=ring_size) p2-=ring_size; | |
133 } | |
134 } | |
135 | |
136 | |
137 void ringload_IIR_1_div_e_echo_i( s16 * ringbuff, int ring_size, int pos, s16 *buffer, int size , int delay) | |
138 /* put <size> samples to ring with size <ring_size> on position <pos> */ | |
139 { | |
140 int i,p1,p2; | |
141 | |
142 p1=pos, p2=ringpos(p1-delay, ring_size); | |
143 for( i=0; i<size; i++ ){ | |
144 ringbuff[p1] =(s16)( ( | |
145 _1_m_1_div_e_i * buffer[i] + | |
146 _1_div_e_i * ringbuff[p2] | |
147 ) >> LOG2RESMAX ); | |
148 p1++; if(p1>=ring_size) p1-=ring_size; | |
149 p2++; if(p2>=ring_size) p2-=ring_size; | |
150 } | |
151 } | |
152 | |
153 | |
154 void ringload_IIR_1_div_e_echo_i_vc( s16 * ringbuff, int ring_size, int pos, s16 *buffer, int size , int delay) | |
155 /* put <size> samples to ring with size <ring_size> on position <pos> */ | |
156 { | |
157 int i,p1,p2; | |
158 int actval; | |
159 | |
160 p1=pos, p2=ringpos(p1-delay, ring_size); | |
161 for( i=0; i<size; i++ ){ | |
162 /* ringbuff[p1] =(s16)( ( | |
163 _1_m_1_div_e_i_vc * buffer[i] + | |
164 _1_div_e_i_vc * ringbuff[p2] | |
165 ) >> LOG2RESMAXVC );*/ | |
166 actval = _1_m_1_div_e_i_vc * buffer[i] + | |
167 _1_div_e_i_vc * ringbuff[p2]; | |
168 /* prevent overflow */ | |
169 if( actval > (int)0x3FFFFFFF ) actval=(int)0x3FFFFFFF; | |
170 if( actval < (int)0xC0000000 ) actval=(int)0xC0000000; | |
171 ringbuff[p1] = actval >> LOG2RESMAXVC; | |
172 p1++; if(p1>=ring_size) p1-=ring_size; | |
173 p2++; if(p2>=ring_size) p2-=ring_size; | |
174 } | |
175 } | |
176 | |
177 | |
178 /*void ringget ( s16 * ringbuff, int ring_size, int pos, s16 *buffer, int size ) | |
179 { | |
180 int i; | |
181 | |
182 if( pos+size > ring_size ){ | |
183 for( i=pos; i<ring_size; i++ ) buffer[i]=ringbuff[i]; | |
184 for( i=0; i<size-ring_size+pos; i++ ) buffer[i]=ringbuff[i]; | |
185 }else{ | |
186 for( i=pos; i<size+pos; i++ ) buffer[i]=ringbuff[i]; | |
187 } | |
188 } | |
189 */ | |
190 | |
191 //int sndstretch( // not optimized | |
192 int sndstretch_not_optimized( | |
193 /* stretches the sound (not changing pitch !!!) */ | |
194 /* returns number of output samples produced */ | |
195 /* chnr simply gives multiples of chnr as snr_prod */ | |
196 s16 * buffer, int buff_size, /* ring buffer */ | |
197 int pos_init, /* only initial pos in ringbuffer */ | |
198 int snr_i, int snr_o, /* enlarge - spec */ | |
199 int chnr, /* # of channels */ | |
200 s16 * outbuff, /* output */ | |
201 int * out_prod, /* # of output-samples produced */ | |
202 int snr_proc, /* # of in-samples to process */ | |
203 int initialize /* bool */ | |
204 ) | |
205 { | |
206 static int is_initialized=0; | |
207 static int snr_o_prod; | |
208 static int snr_i_act; | |
209 static int snr_o_act; | |
210 /* static int snr_offs; */ | |
211 static int pos_act; | |
212 static int dsnr; | |
213 /* static int p1,p2; */ | |
214 static double snr_rest=0.0; | |
215 | |
216 int snr; | |
217 double snr_d; | |
218 | |
219 int i,p1,p2; | |
220 double fade_in, fade_out; | |
221 double outd; | |
222 | |
223 /* s16 * outbuff; */ | |
224 | |
225 /* reset */ | |
226 if( !is_initialized || initialize || | |
227 snr_i!=snr_i_act || snr_o!=snr_o_act ){ | |
228 | |
229 snr_rest = 0.0; | |
230 snr_o_prod = 0; | |
231 snr_i_act = snr_i; | |
232 snr_o_act = snr_o; | |
233 dsnr = snr_o_act-snr_i_act; | |
234 pos_act = pos_init; | |
235 | |
236 is_initialized = 1; | |
237 | |
238 } | |
239 | |
240 /* fprintf(stderr,"pos_act=%d\n",pos_act); | |
241 */ | |
242 snr_d = (double)snr_proc*(double)snr_o_act/(double)snr_i_act | |
243 + snr_rest; | |
244 snr = (int) (snr_d) / 2 * 2; | |
245 snr_rest = snr_d - (double)snr; | |
246 | |
247 /* fprintf(stderr,"snr=%d\n",snr); | |
248 */ | |
249 /* outbuff=malloc(snr*sizeof(s16)); | |
250 */ | |
251 /* produce snr samples */ | |
252 i=0; | |
253 do { | |
254 if( snr_o_prod == snr_o_act ) { | |
255 snr_o_prod=0; | |
256 /* fprintf(stderr,"!!!!!!!!!!!!!!!!!!!!!!!!!! dsnr = %d\n",dsnr);*/ | |
257 pos_act = ringpos( pos_act-dsnr, buff_size ); | |
258 } | |
259 for ( ; snr_o_prod < snr_o_act && i<snr ; snr_o_prod++,i++ ){ | |
260 p1=pos_act; | |
261 p2=ringpos( p1-dsnr, buff_size ); | |
262 // p3=ringpos( p2-dsnr, buff_size ); | |
263 // p4=ringpos( p3-dsnr, buff_size ); | |
264 // p2=ringpos( p1-abs(dsnr), buff_size ); | |
265 // p3=ringpos( p2-abs(dsnr), buff_size ); | |
266 // p4=ringpos( p3-abs(dsnr), buff_size ); | |
267 fade_in = (double)snr_o_prod/(double)snr_o_act; | |
268 fade_out = 1.0-fade_in; | |
269 // fade1 = (dsnr>0)?fade_out:fade_in; | |
270 // fade2 = (dsnr>0)?fade_in:fade_out; | |
271 outd = ( | |
272 (double)buffer[p1] * fade_out /* fade out */ | |
273 + (double)buffer[p2] * fade_in /* fade in */ | |
274 // +( 0.67 * (double)buffer[p1] /* fade out for lengthen */ | |
275 // + 0.24 * (double)buffer[p2] /* fade out */ | |
276 // + 0.09 * (double)buffer[p3]) * fade_out /* fade out */ | |
277 // +( 0.67 * (double)buffer[p2] /* fade in */ | |
278 // + 0.24 * (double)buffer[p3] /* fade in */ | |
279 // + 0.09 * (double)buffer[p4]) * fade_in /* fade in */ | |
280 ); | |
281 outbuff[i] = outd+0.5; | |
282 pos_act=ringpos( pos_act+1, buff_size ); | |
283 } | |
284 } while( i<snr ); | |
285 | |
286 /* fwrite( outbuff, sizeof(s16), snr, stdout ); | |
287 */ | |
288 /* free(outbuff); | |
289 */ | |
290 *out_prod = snr; | |
291 | |
292 /* fprintf(stderr,"snr = %d\n",snr); | |
293 */ | |
294 return( *out_prod ); | |
295 } | |
296 | |
297 | |
298 int sndstretch( // optimized | |
299 //int sndstretch_optimized( // optimized | |
300 /* stretches the sound (not changing pitch !!!) */ | |
301 /* returns number of output samples produced */ | |
302 /* chnr simply gives multiples of chnr as snr_prod */ | |
303 s16 * buffer, int buff_size, /* ring buffer */ | |
304 int pos_init, /* only initial pos in ringbuffer */ | |
305 int snr_i, int snr_o, /* enlarge - spec */ | |
306 int chnr, /* # of channels */ | |
307 s16 * outbuff, /* output */ | |
308 int * out_prod, /* # of output-samples produced */ | |
309 int snr_proc, /* # of in-samples to process */ | |
310 int initialize /* bool */ | |
311 ) | |
312 { | |
313 static int is_initialized=0; | |
314 static int snr_o_prod; | |
315 static int snr_i_act; | |
316 static int snr_o_act; | |
317 static int pos_act; | |
318 static int dsnr; | |
319 static double snr_rest=0.0; | |
320 | |
321 static int _RESMAX_div_max, _RESMAX_mod_max; | |
322 static int fade_in_i, fade_out_i, fade_rest_i; | |
323 | |
324 /* not necessarily static */ | |
325 static int snr; | |
326 static double snr_d; | |
327 static int i,p2; | |
328 | |
329 /* reset */ | |
330 if( !is_initialized || initialize || | |
331 snr_i!=snr_i_act || snr_o!=snr_o_act ){ | |
332 | |
333 snr_rest = 0.0; | |
334 snr_o_prod = 0; | |
335 snr_i_act = snr_i; | |
336 snr_o_act = snr_o; | |
337 dsnr = snr_o_act-snr_i_act; | |
338 pos_act = pos_init; | |
339 | |
340 is_initialized = 1; | |
341 | |
342 } | |
343 | |
344 snr_d = (double)snr_proc*(double)snr_o_act/(double)snr_i_act | |
345 + snr_rest; | |
346 snr = (int) (snr_d) / 2 * 2; | |
347 snr_rest = snr_d - (double)snr; | |
348 | |
349 /* produce snr samples */ | |
350 i=0; | |
351 do { | |
352 if( snr_o_prod == snr_o_act ) { | |
353 snr_o_prod=0; | |
354 pos_act = ringpos( pos_act-dsnr, buff_size ); | |
355 } | |
356 fade_in_i = RESMAX*((double)snr_o_prod/(double)snr_o_act); | |
357 fade_out_i = RESMAX-fade_in_i; | |
358 fade_rest_i= (RESMAX*snr_o_prod)%snr_o_act; | |
359 _RESMAX_div_max=RESMAX/snr_o_act; | |
360 _RESMAX_mod_max=RESMAX%snr_o_act; | |
361 p2=ringpos( pos_act-dsnr, buff_size ); | |
362 for ( ; snr_o_prod < snr_o_act && i<snr ; snr_o_prod++,i++ ){ | |
363 fade_in_i +=_RESMAX_div_max; | |
364 fade_out_i -=_RESMAX_div_max; | |
365 fade_rest_i+=_RESMAX_mod_max; | |
366 if(fade_rest_i>snr_o_act){ | |
367 fade_rest_i-=snr_o_act; | |
368 fade_in_i++; | |
369 fade_out_i--; | |
370 } | |
371 outbuff[i] = (s16)( ( | |
372 + (int)buffer[pos_act] * fade_out_i | |
373 + (int)buffer[p2] * fade_in_i | |
374 ) >> LOG2RESMAX ); | |
375 pos_act++; if(pos_act>=buff_size) pos_act-=buff_size; | |
376 p2++; if(p2 >=buff_size) p2 -=buff_size; | |
377 } | |
378 } while( i<snr ); | |
379 | |
380 *out_prod = snr; | |
381 | |
382 return( *out_prod ); | |
383 } | |
384 | |
385 | |
386 int sndstretch_job( // optimized | |
387 //int sndstretch_optimized( // optimized | |
388 /* stretches the sound (not changing pitch !!!) */ | |
389 /* returns number of output samples produced */ | |
390 /* chnr simply gives multiples of chnr as snr_prod */ | |
391 s16 * buffer, int buff_size, /* ring buffer */ | |
392 int pos_init, /* only initial pos in ringbuffer */ | |
393 int snr_i, int snr_o, /* enlarge - spec */ | |
394 int chnr, /* # of channels */ | |
395 s16 * outbuff, /* output */ | |
396 int * out_prod, /* # of output-samples produced */ | |
397 int snr_proc, /* # of in-samples to process */ | |
398 int initialize, /* bool */ | |
399 StretchJob * job | |
400 ) | |
401 { | |
402 /* static int is_initialized=0; | |
403 static int snr_o_prod; | |
404 static int snr_i_act; | |
405 static int snr_o_act; | |
406 static int pos_act; | |
407 static int dsnr; | |
408 static double snr_rest=0.0; | |
409 | |
410 static int _RESMAX_div_max, _RESMAX_mod_max; | |
411 static int fade_in_i, fade_out_i, fade_rest_i;*/ | |
412 | |
413 /* not necessarily static */ | |
414 static int snr; | |
415 static double snr_d; | |
416 static int i,p2; | |
417 | |
418 #define is_initialized (job->is_initialized ) | |
419 #define snr_o_prod (job->snr_o_prod ) | |
420 #define snr_i_act (job->snr_i_act ) | |
421 #define snr_o_act (job->snr_o_act ) | |
422 #define pos_act (job->pos_act ) | |
423 #define dsnr (job->dsnr ) | |
424 #define snr_rest (job->snr_rest ) | |
425 #define _RESMAX_div_max (job->_RESMAX_div_max) | |
426 #define _RESMAX_mod_max (job->_RESMAX_mod_max) | |
427 #define fade_in_i (job->fade_in_i ) | |
428 #define fade_out_i (job->fade_out_i ) | |
429 #define fade_rest_i (job->fade_rest_i ) | |
430 | |
431 /* reset */ | |
432 if( !is_initialized || initialize || | |
433 snr_i!=snr_i_act || snr_o!=snr_o_act ){ | |
434 | |
435 snr_rest = 0.0; | |
436 snr_o_prod = 0; | |
437 snr_i_act = snr_i; | |
438 snr_o_act = snr_o; | |
439 dsnr = snr_o_act-snr_i_act; | |
440 pos_act = pos_init; | |
441 | |
442 is_initialized = 1; | |
443 | |
444 } | |
445 | |
446 snr_d = (double)snr_proc*(double)snr_o_act/(double)snr_i_act | |
447 + snr_rest; | |
448 snr = (int) (snr_d) / 2 * 2; | |
449 snr_rest = snr_d - (double)snr; | |
450 | |
451 /* produce snr samples */ | |
452 i=0; | |
453 do { | |
454 if( snr_o_prod == snr_o_act ) { | |
455 snr_o_prod=0; | |
456 pos_act = ringpos( pos_act-dsnr, buff_size ); | |
457 } | |
458 fade_in_i = RESMAX*((double)snr_o_prod/(double)snr_o_act); | |
459 fade_out_i = RESMAX-fade_in_i; | |
460 fade_rest_i= (RESMAX*snr_o_prod)%snr_o_act; | |
461 _RESMAX_div_max=RESMAX/snr_o_act; | |
462 _RESMAX_mod_max=RESMAX%snr_o_act; | |
463 p2=ringpos( pos_act-dsnr, buff_size ); | |
464 for ( ; snr_o_prod < snr_o_act && i<snr ; snr_o_prod++,i++ ){ | |
465 fade_in_i +=_RESMAX_div_max; | |
466 fade_out_i -=_RESMAX_div_max; | |
467 fade_rest_i+=_RESMAX_mod_max; | |
468 if(fade_rest_i>snr_o_act){ | |
469 fade_rest_i-=snr_o_act; | |
470 fade_in_i++; | |
471 fade_out_i--; | |
472 } | |
473 outbuff[i] = (s16)( ( | |
474 + (int)buffer[pos_act] * fade_out_i | |
475 + (int)buffer[p2] * fade_in_i | |
476 ) >> LOG2RESMAX ); | |
477 pos_act++; if(pos_act>=buff_size) pos_act-=buff_size; | |
478 p2++; if(p2 >=buff_size) p2 -=buff_size; | |
479 } | |
480 } while( i<snr ); | |
481 | |
482 *out_prod = snr; | |
483 | |
484 return( *out_prod ); | |
485 | |
486 #undef is_initialized | |
487 #undef snr_o_prod | |
488 #undef snr_i_act | |
489 #undef snr_o_act | |
490 #undef pos_act | |
491 #undef dsnr | |
492 #undef snr_rest | |
493 #undef _RESMAX_div_max | |
494 #undef _RESMAX_mod_max | |
495 #undef fade_in_i | |
496 #undef fade_out_i | |
497 #undef fade_rest_i | |
498 } | |
499 | |
500 | |
501 //int sndscale( | |
502 int sndscale_not_optimized( | |
503 /* rescales the sound (including pitch) */ | |
504 /* returns number of output samples produced */ | |
505 s16 * buffer, /* ring buffer */ | |
506 int snr_i, int snr_o, /* enlarge - spec */ | |
507 int chnr, /* # of channels */ | |
508 s16 * outbuff, /* output */ | |
509 int * out_prod, /* # of output-samples produced */ | |
510 int snr_proc, /* # of in-samples to process */ | |
511 /* must be manifold of channels */ | |
512 int initialize /* bool */ | |
513 ) | |
514 { | |
515 static s16 last_samp[10]; /* 10 channels maximum ;) */ | |
516 static double pos_d = 0.0; | |
517 | |
518 int snr; | |
519 int pos1, pos2; | |
520 s16 samp1, samp2; | |
521 int index1, index2; | |
522 int ch; | |
523 double ds; | |
524 double ratio1, ratio2, outd; | |
525 | |
526 | |
527 if ( initialize ){ | |
528 for( ch=0; ch<chnr; ch++ ){ | |
529 last_samp[ch] = 0; | |
530 } | |
531 pos_d = 0.0; | |
532 } | |
533 | |
534 ds = 1.0*(double)snr_i/(double)snr_o; | |
535 | |
536 /* fprintf(stderr,"ds=%f\n",ds); */ | |
537 /* fprintf(stderr,"pos_d =%f\n",pos_d);*/ | |
538 | |
539 /* produce proper amount of samples */ | |
540 for( snr=0 ; pos_d < snr_proc/chnr-1 ; pos_d+=ds ){ | |
541 | |
542 pos1 = (int)floor(pos_d); | |
543 pos2 = pos1+1; | |
544 ratio1 = 1-pos_d+floor(pos_d); | |
545 ratio2 = pos_d-floor(pos_d); | |
546 | |
547 for( ch=0; ch<chnr; ch++ ){ | |
548 | |
549 index1 = pos1*chnr+ch; | |
550 index2 = pos2*chnr+ch; | |
551 | |
552 samp1 = (pos_d<0) ? last_samp[ch] : buffer[index1] ; | |
553 samp2 = buffer[index2]; | |
554 | |
555 outd = (double)samp1 * ratio1 | |
556 + (double)samp2 * ratio2 ; | |
557 | |
558 outbuff[snr+ch] = outd+0.5; | |
559 | |
560 } | |
561 | |
562 snr+=chnr; | |
563 | |
564 } | |
565 | |
566 pos_d -= (double)(snr_proc/chnr); | |
567 | |
568 for( ch=0; ch<chnr; ch++ ){ | |
569 last_samp[ch] = buffer[snr_proc-chnr+ch]; | |
570 } | |
571 | |
572 *out_prod = snr; | |
573 | |
574 /* fprintf(stderr,"snr = %d\n",snr);*/ | |
575 | |
576 /* last_samp = buffer[snr_proc-1]; */ | |
577 | |
578 return( snr ); | |
579 } | |
580 | |
581 | |
582 | |
583 int sndscale( //optimized | |
584 //int sndscale2p_optimized( | |
585 /* rescales the sound (including pitch) */ | |
586 /* returns number of output samples produced */ | |
587 s16 * buffer, /* ring buffer */ | |
588 int snr_i, int snr_o, /* enlarge - spec snr_o must not exceed RESMAX */ | |
589 int chnr, /* # of channels */ | |
590 s16 * outbuff, /* output */ | |
591 int * out_prod, /* # of output-samples produced */ | |
592 int snr_proc, /* # of in-samples to process */ | |
593 /* must be manifold of channels */ | |
594 int initialize /* bool */ | |
595 ) | |
596 { | |
597 static s16 last_samp[10]; /* 10 channels maximum ;) */ | |
598 static int pos_rest; | |
599 | |
600 static int snr; | |
601 static int pos1, pos2; | |
602 static int ch; | |
603 static int ratio1_i; | |
604 static int ds_li, ds_li_c, ds_rest; | |
605 static int snr_proc_m_chnr; | |
606 | |
607 | |
608 if ( initialize ){ | |
609 for( ch=0; ch<chnr; ch++ ){ | |
610 last_samp[ch] = 0; | |
611 } | |
612 pos1 = 0; | |
613 } | |
614 | |
615 ds_li = snr_i/snr_o; | |
616 ds_li_c = ds_li*chnr; | |
617 ds_rest = snr_i%snr_o; | |
618 | |
619 snr_proc_m_chnr = snr_proc-chnr; | |
620 for( snr=0 ; pos1 < snr_proc_m_chnr ; pos1+=ds_li_c ){ | |
621 | |
622 pos2 = pos1+chnr; | |
623 ratio1_i = snr_o-pos_rest; | |
624 | |
625 if (pos1<0){ | |
626 for( ch=0; ch<chnr; ch++ ){ | |
627 outbuff[snr+ch] = (s16) | |
628 ( ( (int)last_samp[ch] * ratio1_i + | |
629 (int)buffer[pos2+ch] * pos_rest ) / snr_o ) ; | |
630 } | |
631 } else { | |
632 for( ch=0; ch<chnr; ch++ ){ | |
633 outbuff[snr+ch] = (s16) | |
634 ( ( (int)buffer[pos1+ch] * ratio1_i + | |
635 (int)buffer[pos2+ch] * pos_rest ) / snr_o ) ; | |
636 } | |
637 } | |
638 | |
639 snr+=chnr; | |
640 | |
641 pos_rest+=ds_rest; | |
642 if( pos_rest>=snr_o ){ pos_rest-=snr_o; pos1+=chnr; } | |
643 } | |
644 | |
645 pos1 -= snr_proc; | |
646 | |
647 for( ch=0; ch<chnr; ch++ ){ | |
648 last_samp[ch] = buffer[snr_proc-chnr+ch]; | |
649 } | |
650 | |
651 *out_prod = snr; | |
652 | |
653 return( snr ); | |
654 } | |
655 | |
656 | |
657 | |
658 int sndscale_job( //optimized | |
659 //int sndscale2p_optimized( | |
660 /* rescales the sound (including pitch) */ | |
661 /* returns number of output samples produced */ | |
662 s16 * buffer, /* ring buffer */ | |
663 int snr_i, int snr_o, /* enlarge - spec snr_o must not exceed RESMAX */ | |
664 int chnr, /* # of channels */ | |
665 s16 * outbuff, /* output */ | |
666 int * out_prod, /* # of output-samples produced */ | |
667 int snr_proc, /* # of in-samples to process */ | |
668 /* must be manifold of channels */ | |
669 int initialize, /* bool */ | |
670 ScaleJob * job | |
671 ) | |
672 { | |
673 /* static s16 * last_samp; | |
674 static int pos_rest; | |
675 | |
676 static int snr; | |
677 static int pos1, pos2; | |
678 static int ch; | |
679 static int ratio1_i; | |
680 static int ds_li, ds_li_c, ds_rest; | |
681 static int snr_proc_m_chnr;*/ | |
682 | |
683 #define last_samp job->last_samp | |
684 #define pos_rest job->pos_rest | |
685 #define snr job->snr | |
686 #define pos1 job->pos1 | |
687 #define pos2 job->pos2 | |
688 #define ch job->ch | |
689 #define ratio1_i job->ratio1_i | |
690 #define ds_li job->ds_li | |
691 #define ds_li_c job->ds_li_c | |
692 #define ds_rest job->ds_rest | |
693 #define snr_proc_m_chnr job->snr_proc_m_chnr | |
694 | |
695 if ( initialize ){ | |
696 for( ch=0; ch<chnr; ch++ ){ | |
697 last_samp[ch] = 0; | |
698 } | |
699 pos1 = 0; | |
700 } | |
701 | |
702 ds_li = snr_i/snr_o; | |
703 ds_li_c = ds_li*chnr; | |
704 ds_rest = snr_i%snr_o; | |
705 | |
706 snr_proc_m_chnr = snr_proc-chnr; | |
707 for( snr=0 ; pos1 < snr_proc_m_chnr ; pos1+=ds_li_c ){ | |
708 | |
709 pos2 = pos1+chnr; | |
710 ratio1_i = snr_o-pos_rest; | |
711 | |
712 if (pos1<0){ | |
713 for( ch=0; ch<chnr; ch++ ){ | |
714 outbuff[snr+ch] = (s16) | |
715 ( ( (int)last_samp[ch] * ratio1_i + | |
716 (int)buffer[pos2+ch] * pos_rest ) / snr_o ) ; | |
717 } | |
718 } else { | |
719 for( ch=0; ch<chnr; ch++ ){ | |
720 outbuff[snr+ch] = (s16) | |
721 ( ( (int)buffer[pos1+ch] * ratio1_i + | |
722 (int)buffer[pos2+ch] * pos_rest ) / snr_o ) ; | |
723 } | |
724 } | |
725 | |
726 snr+=chnr; | |
727 | |
728 pos_rest+=ds_rest; | |
729 if( pos_rest>=snr_o ){ pos_rest-=snr_o; pos1+=chnr; } | |
730 } | |
731 | |
732 pos1 -= snr_proc; | |
733 | |
734 for( ch=0; ch<chnr; ch++ ){ | |
735 last_samp[ch] = buffer[snr_proc-chnr+ch]; | |
736 } | |
737 | |
738 *out_prod = snr; | |
739 | |
740 return( snr ); | |
741 | |
742 #undef last_samp | |
743 #undef pos_rest | |
744 #undef snr | |
745 #undef pos1 | |
746 #undef pos2 | |
747 #undef ch | |
748 #undef ratio1_i | |
749 #undef ds_li | |
750 #undef ds_li_c | |
751 #undef ds_rest | |
752 #undef snr_proc_m_chnr | |
753 } | |
754 | |
755 | |
756 | |
757 int snd_pitch_speed( | |
758 /* input */ | |
759 s16 *buff_i, int channels, int snr_proc, | |
760 /* algorihm parameters */ | |
761 int initialize, double pitch, double speed, int fade_shift, | |
762 /* output */ | |
763 s16 * buff_o, int * snr_produced | |
764 ) | |
765 { | |
766 static s16 * ring_buff = (s16*)0; | |
767 static s16 * ring_buff_old = (s16*)0; | |
768 static s16 * buff_help = (s16*)0; | |
769 static int ring_size = 1; | |
770 static int ring_size_old = 0; | |
771 static int ring_pos_w = 0; | |
772 static int ring_pos_r = 0; | |
773 static int snr_scale_i; | |
774 static int snr_scale_o; | |
775 static int snr_stretch_i; | |
776 static int snr_stretch_o; | |
777 static int snr_proc_scale; | |
778 static int snr_proc_stretch; | |
779 static int is_init = 0; | |
780 static int dsnr; | |
781 static double speed_act = 0; | |
782 static double pitch_act = 0; | |
783 static double fade_shift_act = 0; | |
784 | |
785 int snr_prod; | |
786 double speed_eff; | |
787 double pitch_eff; | |
788 int scaling_first; | |
789 int snr_stretching_i_max; | |
790 int snr_stretching_o_max; | |
791 | |
792 // int channels = 2; | |
793 int init_me = 0; | |
794 | |
795 speed_eff = speed/pitch; | |
796 pitch_eff = pitch; | |
797 | |
798 scaling_first=0; | |
799 // if( pitch > 1.0 ) scaling_first=0; else scaling_first=1; | |
800 | |
801 if ( !is_init || initialize || speed!=speed_act || pitch!=pitch_act || | |
802 fade_shift!=fade_shift_act ){ | |
803 | |
804 if( !is_init || initialize ) init_me=1; else init_me=0; | |
805 | |
806 #ifdef DEBUG | |
807 fprintf(stderr,"snd_stretch_scale - init - pitch:%f, speed:%f\n",pitch,speed); | |
808 #endif | |
809 | |
810 speed_act = speed; | |
811 pitch_act = pitch; | |
812 fade_shift_act = fade_shift; | |
813 | |
814 // if (ring_buff!=0) free(ring_buff); | |
815 // if (buff_help!=0) free(buff_help); | |
816 | |
817 if (initialize != -1){ | |
818 | |
819 dsnr = fade_shift; | |
820 // dsnr = 1764; // 25Hz | |
821 // dsnr = 1536; // 30Hz | |
822 // dsnr = 1102; // 40Hz | |
823 // dsnr = 882; // 50Hz | |
824 | |
825 if( scaling_first ){ | |
826 snr_stretching_i_max = ceil((double)snr_proc/pitch_act); | |
827 snr_stretching_i_max += channels-1; | |
828 snr_stretching_i_max /= channels; | |
829 snr_stretching_i_max *= channels; | |
830 } else { | |
831 snr_stretching_i_max = (snr_proc+channels-1)/channels*channels; | |
832 } | |
833 | |
834 snr_stretching_o_max = | |
835 ceil((double)snr_stretching_i_max / speed_eff); | |
836 snr_stretching_o_max = | |
837 (snr_stretching_o_max+channels-1)/channels*channels; | |
838 | |
839 ring_size = snr_stretching_o_max /* for reading */ | |
840 + dsnr*channels /* for grabbing back */ | |
841 + dsnr*channels /* for readpos catching */ | |
842 /* up with writepos */ | |
843 + 2*dsnr*channels ; /* for 2 pre - echos */ | |
844 // ring_size = snr_stretching_o_max+3*dsnr*channels; | |
845 | |
846 if( ring_size <= ring_size_old ){ | |
847 ring_size = ring_size_old; | |
848 #ifdef DEBUG | |
849 fprintf(stderr," ring_size = %d \n",ring_size); | |
850 #endif | |
851 } else { | |
852 /* if (ring_buff!=0) free(ring_buff); | |
853 if (buff_help!=0) free(buff_help); | |
854 ring_buff = calloc( ring_size, sizeof(s16) ); | |
855 buff_help = calloc( 65536, sizeof(s16) ); | |
856 ring_pos_r = 0;*/ | |
857 if (buff_help!=0) free(buff_help); | |
858 ring_buff_old = ring_buff; | |
859 ring_buff = calloc( ring_size, sizeof(s16) ); | |
860 buff_help = calloc( 65536, sizeof(s16) ); | |
861 if (ring_buff_old!=0) ringcopy( ring_buff, ring_size, ring_pos_r, ring_pos_w, ring_buff_old, ring_size_old, ring_pos_r ); | |
862 if (ring_buff_old!=0) free(ring_buff_old); | |
863 // ring_pos_w=ringpos(ring_pos_w+ring_size_old,ring_size); | |
864 #ifdef DEBUG | |
865 fprintf(stderr," %d s16-samples reserved\n",ring_size); | |
866 #endif | |
867 } | |
868 | |
869 ring_pos_w = | |
870 ringpos( ring_pos_r + /* base */ | |
871 dsnr*channels /* for grabing back */ | |
872 // dsnr*2 /* for grabing back */ // why *2 | |
873 , ring_size ); | |
874 #ifdef DEBUG | |
875 fprintf(stderr," ring_pos_w = %d\n",ring_pos_w); | |
876 #endif | |
877 ring_pos_w = (ring_pos_w+(channels-1))/channels*channels; | |
878 | |
879 ring_size_old = ring_size; | |
880 | |
881 is_init = 1; | |
882 | |
883 } else { /* initialize == -1 */ | |
884 if (ring_buff!=0) free(ring_buff); | |
885 if (buff_help!=0) free(buff_help); | |
886 /* buffers are released -> leave the function */ | |
887 return 0; /* !!! sloppy */ | |
888 } | |
889 | |
890 } | |
891 | |
892 if ( fabs(speed_eff-1.0)>0.001 ){ /* | |
893 0.001 ?! | |
894 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
895 this should be examined to the limits | |
896 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
897 */ | |
898 snr_stretch_i = (double)dsnr/(1.0/speed_eff-1.0); | |
899 snr_stretch_o = fabs(snr_stretch_i + dsnr); | |
900 snr_stretch_i = abs(snr_stretch_i); | |
901 // fprintf(stderr,"snd_stretch_scale - snr_str_i=%d\n",snr_stretch_i); | |
902 // fprintf(stderr,"snd_stretch_scale - snr_str_o=%d\n",snr_stretch_o); | |
903 } else { | |
904 snr_stretch_i = 10; | |
905 snr_stretch_o = 10; | |
906 } | |
907 | |
908 if ( pitch_eff!=1.0 ){ | |
909 snr_scale_i = (double)dsnr/(1.0/pitch_eff-1.0); | |
910 snr_scale_o = fabs(snr_scale_i + dsnr); | |
911 snr_scale_i = abs(snr_scale_i); | |
912 if( 1 /* optimized version doesnt support all scaling */ ){ | |
913 if( snr_scale_o > 65536 ){ | |
914 snr_scale_i = (int)((double)snr_scale_i*((double)65536/(double)snr_scale_o)+0.5); | |
915 snr_scale_o = 65536; | |
916 // make sure that snr_o for sndscale wont exceed 2^16 | |
917 } | |
918 } | |
919 // fprintf(stderr,"snd_stretch_scale - snr_sc_i=%d\n",snr_stretch_i); | |
920 // fprintf(stderr,"snd_stretch_scale - snr_sc_o=%d\n",snr_stretch_o); | |
921 } else { | |
922 // fprintf(stderr,"snd_stretch_scale - snr_scale_i=snr_scale_o=10\n"); | |
923 snr_scale_i = 65536; | |
924 snr_scale_o = 65536; | |
925 } | |
926 | |
927 if( 0/*scaling_first*/ ){ | |
928 | |
929 snr_proc_scale = snr_proc; | |
930 sndscale ( buff_i, | |
931 snr_scale_i, snr_scale_o, channels, | |
932 buff_help, &snr_prod, snr_proc_scale, init_me ); | |
933 /* buff_o, &snr_prod, snr_proc_scale, 0 ); */ | |
934 | |
935 if( speed_eff!=1.0 ){ /* add echo only when really stretching */ | |
936 ringload_IIR_1_div_e_echo_i( ring_buff, ring_size, ring_pos_w, buff_help, snr_prod, dsnr*channels ); | |
937 } else { | |
938 ringload( ring_buff, ring_size, ring_pos_w, buff_help, snr_prod ); | |
939 } | |
940 ring_pos_w = ringpos( ring_pos_w+snr_prod, ring_size ); | |
941 | |
942 snr_proc_stretch = snr_prod; | |
943 sndstretch ( ring_buff, ring_size, ring_pos_r, | |
944 snr_stretch_i*channels, snr_stretch_o*channels, channels, | |
945 buff_o, &snr_prod, snr_proc_stretch, init_me ); | |
946 | |
947 ring_pos_r = ringpos( ring_pos_r+snr_prod, ring_size ); | |
948 | |
949 } else { | |
950 | |
951 // fprintf(stderr,"sndstretch: ring_pos_r = %d\n",ring_pos_r); | |
952 // fprintf(stderr,"sndstretch: ring_pos_w = %d\n\n",ring_pos_w); | |
953 snr_prod = snr_proc; | |
954 if( speed_eff!=1.0 ){ /* add echo only when really stretching */ | |
955 ringload_IIR_1_div_e_echo_i( ring_buff, ring_size, ring_pos_w, buff_i, snr_proc, dsnr*channels ); | |
956 } else { | |
957 ringload( ring_buff, ring_size, ring_pos_w, buff_i, snr_proc ); | |
958 } | |
959 ring_pos_w = ringpos( ring_pos_w+snr_proc, ring_size ); | |
960 | |
961 snr_proc_stretch = snr_proc; | |
962 sndstretch ( ring_buff, ring_size, ring_pos_r, | |
963 snr_stretch_i*channels, snr_stretch_o*channels, channels, | |
964 buff_help, &snr_prod, snr_proc_stretch, init_me ); | |
965 | |
966 ring_pos_r = ringpos( ring_pos_r+snr_prod, ring_size ); | |
967 | |
968 snr_proc_scale = snr_prod; | |
969 // fprintf(stderr,"snr_scale_i, snr_scale_o = %d,%d\n",snr_scale_i, snr_scale_o); | |
970 sndscale ( buff_help, | |
971 snr_scale_i, snr_scale_o, channels, | |
972 buff_o, &snr_prod, snr_proc_scale, init_me ); | |
973 | |
974 } | |
975 | |
976 *snr_produced = snr_prod; | |
977 | |
978 return snr_prod; | |
979 } | |
980 | |
981 | |
982 int snd_pitch_speed_job( | |
983 /* input */ | |
984 s16 *buff_i, int channels, int snr_proc, | |
985 /* algorihm parameters */ | |
986 int initialize, double pitch, double speed, int fade_shift, | |
987 /* output */ | |
988 s16 * buff_o, int * snr_produced, PitchSpeedJob * job, | |
989 int vol_corr | |
990 ) | |
991 { | |
992 /* static s16 * ring_buff = (s16*)0; | |
993 static s16 * ring_buff_old = (s16*)0; | |
994 static s16 * buff_help = (s16*)0; | |
995 static int ring_size = 1; | |
996 static int ring_size_old = 0; | |
997 static int ring_pos_w = 0; | |
998 static int ring_pos_r = 0; | |
999 static int snr_scale_i; | |
1000 static int snr_scale_o; | |
1001 static int snr_stretch_i; | |
1002 static int snr_stretch_o; | |
1003 static int snr_proc_scale; | |
1004 static int snr_proc_stretch; | |
1005 static int is_init = 0; | |
1006 static int dsnr; | |
1007 static double speed_act = 0; | |
1008 static double pitch_act = 0;*/ | |
1009 | |
1010 int snr_prod; | |
1011 double speed_eff; | |
1012 double pitch_eff; | |
1013 int scaling_first; | |
1014 int snr_stretching_i_max; | |
1015 int snr_stretching_o_max; | |
1016 | |
1017 // int channels = 2; | |
1018 int init_me = 0; | |
1019 | |
1020 #define ring_buff job->ring_buff | |
1021 #define ring_buff_old job->ring_buff_old | |
1022 #define buff_help job->buff_help | |
1023 #define ring_size job->ring_size | |
1024 #define ring_size_old job->ring_size_old | |
1025 #define ring_pos_w job->ring_pos_w | |
1026 #define ring_pos_r job->ring_pos_r | |
1027 #define snr_scale_i job->snr_scale_i | |
1028 #define snr_scale_o job->snr_scale_o | |
1029 #define snr_stretch_i job->snr_stretch_i | |
1030 #define snr_stretch_o job->snr_stretch_o | |
1031 #define snr_proc_scale job->snr_proc_scale | |
1032 #define snr_proc_stretch job->snr_proc_stretch | |
1033 #define is_init job->is_init | |
1034 #define dsnr job->dsnr | |
1035 #define speed_act job->speed_act | |
1036 #define pitch_act job->pitch_act | |
1037 #define fade_shift_act job->fade_shift_act | |
1038 | |
1039 speed_eff = speed/pitch; | |
1040 pitch_eff = pitch; | |
1041 | |
1042 scaling_first=0; | |
1043 // if( pitch > 1.0 ) scaling_first=0; else scaling_first=1; | |
1044 | |
1045 if ( !is_init || initialize || speed!=speed_act || pitch!=pitch_act || | |
1046 fade_shift != fade_shift_act ){ | |
1047 | |
1048 if( !is_init || initialize ) init_me=1; else init_me=0; | |
1049 | |
1050 #ifdef DEBUG | |
1051 fprintf(stderr,"snd_stretch_scale - init - pitch:%f, speed:%f\n",pitch,speed); | |
1052 #endif | |
1053 | |
1054 speed_act = speed; | |
1055 pitch_act = pitch; | |
1056 if ( fade_shift != fade_shift_act ){ | |
1057 fprintf(stderr,"changed fade_shift_act\n"); | |
1058 } | |
1059 fade_shift_act = fade_shift; | |
1060 | |
1061 // if (ring_buff!=0) free(ring_buff); | |
1062 // if (buff_help!=0) free(buff_help); | |
1063 | |
1064 if (initialize != -1){ | |
1065 | |
1066 dsnr = fade_shift; | |
1067 // dsnr = 1764; // 25Hz | |
1068 // dsnr = 1536; // 30Hz | |
1069 // dsnr = 1102; // 40Hz | |
1070 // dsnr = 882; // 50Hz | |
1071 | |
1072 if( scaling_first ){ | |
1073 snr_stretching_i_max = ceil((double)snr_proc/pitch_act); | |
1074 snr_stretching_i_max += channels-1; | |
1075 snr_stretching_i_max /= channels; | |
1076 snr_stretching_i_max *= channels; | |
1077 } else { | |
1078 snr_stretching_i_max = (snr_proc+channels-1)/channels*channels; | |
1079 } | |
1080 | |
1081 snr_stretching_o_max = | |
1082 ceil((double)snr_stretching_i_max / speed_eff); | |
1083 snr_stretching_o_max = | |
1084 (snr_stretching_o_max+channels-1)/channels*channels; | |
1085 | |
1086 ring_size = snr_stretching_o_max /* for reading */ | |
1087 + dsnr*channels /* for grabbing back */ | |
1088 + dsnr*channels /* for readpos catching */ | |
1089 /* up with writepos */ | |
1090 + 2*dsnr*channels ; /* for 2 pre - echos */ | |
1091 // ring_size = snr_stretching_o_max+3*dsnr*channels; | |
1092 | |
1093 if( ring_size <= ring_size_old ){ | |
1094 ring_size = ring_size_old; | |
1095 #ifdef DEBUG | |
1096 fprintf(stderr," ring_size = %d \n",ring_size); | |
1097 #endif | |
1098 } else { | |
1099 /* if (ring_buff!=0) free(ring_buff); | |
1100 if (buff_help!=0) free(buff_help); | |
1101 ring_buff = calloc( ring_size, sizeof(s16) ); | |
1102 buff_help = calloc( 65536, sizeof(s16) ); | |
1103 ring_pos_r = 0;*/ | |
1104 if (buff_help!=0) free(buff_help); | |
1105 ring_buff_old = ring_buff; | |
1106 ring_buff = calloc( ring_size, sizeof(s16) ); | |
1107 buff_help = calloc( 65536, sizeof(s16) ); | |
1108 if (ring_buff_old!=0) ringcopy( ring_buff, ring_size, ring_pos_r, ring_pos_w, ring_buff_old, ring_size_old, ring_pos_r ); | |
1109 if (ring_buff_old!=0) free(ring_buff_old); | |
1110 // ring_pos_w=ringpos(ring_pos_w+ring_size_old,ring_size); | |
1111 #ifdef DEBUG | |
1112 fprintf(stderr," %d s16-samples reserved\n",ring_size); | |
1113 #endif | |
1114 } | |
1115 | |
1116 ring_pos_w = | |
1117 ringpos( ring_pos_r + /* base */ | |
1118 dsnr*channels /* for grabing back */ | |
1119 // dsnr*2 /* for grabing back */ // why *2 | |
1120 , ring_size ); | |
1121 #ifdef DEBUG | |
1122 fprintf(stderr," ring_pos_w = %d\n",ring_pos_w); | |
1123 #endif | |
1124 ring_pos_w = (ring_pos_w+(channels-1))/channels*channels; | |
1125 | |
1126 ring_size_old = ring_size; | |
1127 | |
1128 is_init = 1; | |
1129 | |
1130 } else { /* initialize == -1 */ | |
1131 if (ring_buff!=0) free(ring_buff); | |
1132 if (buff_help!=0) free(buff_help); | |
1133 /* buffers are released -> leave the function */ | |
1134 return 0; /* !!! sloppy */ | |
1135 } | |
1136 | |
1137 } | |
1138 | |
1139 if ( fabs(speed_eff-1.0)>0.001 ){ /* | |
1140 0.001 ?! | |
1141 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
1142 this should be examined to the limits | |
1143 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
1144 */ | |
1145 snr_stretch_i = (double)dsnr/(1.0/speed_eff-1.0); | |
1146 snr_stretch_o = fabs(snr_stretch_i + dsnr); | |
1147 snr_stretch_i = abs(snr_stretch_i); | |
1148 // fprintf(stderr,"snd_stretch_scale - snr_str_i=%d\n",snr_stretch_i); | |
1149 // fprintf(stderr,"snd_stretch_scale - snr_str_o=%d\n",snr_stretch_o); | |
1150 } else { | |
1151 snr_stretch_i = 10; | |
1152 snr_stretch_o = 10; | |
1153 } | |
1154 | |
1155 if ( pitch_eff!=1.0 ){ | |
1156 snr_scale_i = (double)dsnr/(1.0/pitch_eff-1.0); | |
1157 snr_scale_o = fabs(snr_scale_i + dsnr); | |
1158 snr_scale_i = abs(snr_scale_i); | |
1159 if( 1 /* optimized version doesnt support all scaling */ ){ | |
1160 if( snr_scale_o > 65536 ){ | |
1161 snr_scale_i = (int)((double)snr_scale_i*((double)65536/(double)snr_scale_o)+0.5); | |
1162 snr_scale_o = 65536; | |
1163 // make sure that snr_o for sndscale wont exceed 2^16 | |
1164 } | |
1165 } | |
1166 // fprintf(stderr,"snd_stretch_scale - snr_sc_i=%d\n",snr_stretch_i); | |
1167 // fprintf(stderr,"snd_stretch_scale - snr_sc_o=%d\n",snr_stretch_o); | |
1168 } else { | |
1169 // fprintf(stderr,"snd_stretch_scale - snr_scale_i=snr_scale_o=10\n"); | |
1170 snr_scale_i = 65536; | |
1171 snr_scale_o = 65536; | |
1172 } | |
1173 | |
1174 if( 0/*scaling_first*/ ){ | |
1175 | |
1176 snr_proc_scale = snr_proc; | |
1177 sndscale_job ( buff_i, | |
1178 snr_scale_i, snr_scale_o, channels, | |
1179 buff_help, &snr_prod, snr_proc_scale, init_me, | |
1180 &(job->scale_job) ); | |
1181 /* buff_o, &snr_prod, snr_proc_scale, 0 ); */ | |
1182 | |
1183 if( speed_eff!=1.0 ){ /* add echo only when really stretching */ | |
1184 if( !vol_corr ){ | |
1185 ringload_IIR_1_div_e_echo_i( ring_buff, ring_size, ring_pos_w, buff_help, snr_prod, dsnr*channels ); | |
1186 } else { | |
1187 ringload_IIR_1_div_e_echo_i_vc( ring_buff, ring_size, ring_pos_w, buff_help, snr_prod, dsnr*channels ); | |
1188 } | |
1189 } else { | |
1190 ringload( ring_buff, ring_size, ring_pos_w, buff_help, snr_prod ); | |
1191 } | |
1192 ring_pos_w = ringpos( ring_pos_w+snr_prod, ring_size ); | |
1193 | |
1194 snr_proc_stretch = snr_prod; | |
1195 sndstretch_job ( ring_buff, ring_size, ring_pos_r, | |
1196 snr_stretch_i*channels, snr_stretch_o*channels, channels, | |
1197 buff_o, &snr_prod, snr_proc_stretch, init_me, | |
1198 &(job->stretch_job) | |
1199 ); | |
1200 | |
1201 ring_pos_r = ringpos( ring_pos_r+snr_prod, ring_size ); | |
1202 | |
1203 } else { | |
1204 | |
1205 // fprintf(stderr,"sndstretch: ring_pos_r = %d\n",ring_pos_r); | |
1206 // fprintf(stderr,"sndstretch: ring_pos_w = %d\n\n",ring_pos_w); | |
1207 snr_prod = snr_proc; | |
1208 if( speed_eff!=1.0 ){ /* add echo only when really stretching */ | |
1209 if( !vol_corr ){ | |
1210 ringload_IIR_1_div_e_echo_i( ring_buff, ring_size, ring_pos_w, buff_i, snr_proc, dsnr*channels ); | |
1211 }else{ | |
1212 ringload_IIR_1_div_e_echo_i_vc( ring_buff, ring_size, ring_pos_w, buff_i, snr_proc, dsnr*channels ); | |
1213 } | |
1214 } else { | |
1215 ringload( ring_buff, ring_size, ring_pos_w, buff_i, snr_proc ); | |
1216 } | |
1217 ring_pos_w = ringpos( ring_pos_w+snr_proc, ring_size ); | |
1218 | |
1219 snr_proc_stretch = snr_proc; | |
1220 sndstretch_job ( ring_buff, ring_size, ring_pos_r, | |
1221 snr_stretch_i*channels, snr_stretch_o*channels, channels, | |
1222 buff_help, &snr_prod, snr_proc_stretch, init_me, | |
1223 &(job->stretch_job) | |
1224 ); | |
1225 | |
1226 ring_pos_r = ringpos( ring_pos_r+snr_prod, ring_size ); | |
1227 | |
1228 snr_proc_scale = snr_prod; | |
1229 // fprintf(stderr,"snr_scale_i, snr_scale_o = %d,%d\n",snr_scale_i, snr_scale_o); | |
1230 sndscale_job ( buff_help, | |
1231 snr_scale_i, snr_scale_o, channels, | |
1232 buff_o, &snr_prod, snr_proc_scale, init_me, | |
1233 &(job->scale_job) | |
1234 ); | |
1235 | |
1236 } | |
1237 | |
1238 *snr_produced = snr_prod; | |
1239 | |
1240 return snr_prod; | |
1241 #undef ring_buff | |
1242 #undef ring_buff_old | |
1243 #undef buff_help | |
1244 #undef ring_size | |
1245 #undef ring_size_old | |
1246 #undef ring_pos_w | |
1247 #undef ring_pos_r | |
1248 #undef snr_scale_i | |
1249 #undef snr_scale_o | |
1250 #undef snr_stretch_i | |
1251 #undef snr_stretch_o | |
1252 #undef snr_proc_scale | |
1253 #undef snr_proc_stretch | |
1254 #undef is_init | |
1255 #undef dsnr | |
1256 #undef speed_act | |
1257 #undef pitch_act | |
1258 #undef fade_shift_act | |
1259 } | |
1260 | |
1261 | |
1262 int snd_stretch_scale(s16 *buff_i, s16 * buff_o, | |
1263 double pitch, double speed, int channels, | |
1264 int snr_proc, int * snr_produced, int initialize | |
1265 ) | |
1266 { | |
1267 return snd_pitch_speed( | |
1268 /* input */ | |
1269 buff_i, channels, snr_proc, | |
1270 /* algorihm parameters */ | |
1271 initialize, pitch, speed, 1764, | |
1272 /* output */ | |
1273 buff_o, snr_produced | |
1274 ); | |
1275 } | |
1276 | |
1277 | |
1278 int snd_stretch_scale_job(s16 *buff_i, s16 * buff_o, | |
1279 double pitch, double speed, int channels, | |
1280 int snr_proc, int * snr_produced, int initialize, | |
1281 PitchSpeedJob * job, int init_job) | |
1282 /* init_job should be set to one the first time the routine is called with | |
1283 the actual job (e.g. for creating a new job) */ | |
1284 { | |
1285 if(init_job){ | |
1286 InitPitchSpeedJob(job); | |
1287 } | |
1288 return snd_pitch_speed_job( | |
1289 /* input */ | |
1290 buff_i, channels, snr_proc, | |
1291 /* algorihm parameters */ | |
1292 initialize, pitch, speed, 1764 , | |
1293 /* output */ | |
1294 buff_o, snr_produced, | |
1295 /* job data */ | |
1296 job, 0 | |
1297 ); | |
1298 } |