PageRenderTime 47ms CodeModel.GetById 15ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/project/jni/sdl_mixer/timidity/resample.c

https://github.com/aichunyu/FFPlayer
C | 748 lines | 596 code | 85 blank | 67 comment | 105 complexity | c8344a064085081f163f5ea9ad3ea092 MD5 | raw file
  1/*
  2
  3    TiMidity -- Experimental MIDI to WAVE converter
  4    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 19
 20    resample.c
 21*/
 22
 23#include <math.h>
 24#include <stdio.h>
 25#include <stdlib.h>
 26
 27#include "config.h"
 28#include "common.h"
 29#include "instrum.h"
 30#include "playmidi.h"
 31#include "output.h"
 32#include "ctrlmode.h"
 33#include "tables.h"
 34#include "resample.h"
 35
 36#ifdef LINEAR_INTERPOLATION
 37# if defined(LOOKUP_HACK) && defined(LOOKUP_INTERPOLATION)
 38#   define RESAMPLATION \
 39       v1=src[ofs>>FRACTION_BITS];\
 40       v2=src[(ofs>>FRACTION_BITS)+1];\
 41       *dest++ = (resample_t)(v1 + (iplookup[(((v2-v1)<<5) & 0x03FE0) | \
 42           ((ofs & FRACTION_MASK) >> (FRACTION_BITS-5))]));
 43# else
 44#   define RESAMPLATION \
 45      v1=src[ofs>>FRACTION_BITS];\
 46      v2=src[(ofs>>FRACTION_BITS)+1];\
 47      *dest++ = (resample_t)(v1 + (((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 48# endif
 49#  define INTERPVARS sample_t v1, v2
 50#else
 51/* Earplugs recommended for maximum listening enjoyment */
 52#  define RESAMPLATION *dest++ = src[ofs>>FRACTION_BITS];
 53#  define INTERPVARS
 54#endif
 55
 56#define FINALINTERP if (ofs == le) *dest++=src[ofs>>FRACTION_BITS];
 57/* So it isn't interpolation. At least it's final. */
 58
 59extern resample_t *resample_buffer;
 60
 61/*************** resampling with fixed increment *****************/
 62
 63static resample_t *rs_plain(int v, int32 *countptr)
 64{
 65
 66  /* Play sample until end, then free the voice. */
 67
 68  INTERPVARS;
 69  Voice 
 70    *vp=&voice[v];
 71  resample_t 
 72    *dest=resample_buffer;
 73  sample_t 
 74    *src=vp->sample->data;
 75  int32 
 76    ofs=vp->sample_offset,
 77    incr=vp->sample_increment,
 78    le=vp->sample->data_length,
 79    count=*countptr;
 80
 81#ifdef PRECALC_LOOPS
 82  int32 i, j;
 83
 84  if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
 85
 86  /* Precalc how many times we should go through the loop.
 87     NOTE: Assumes that incr > 0 and that ofs <= le */
 88  i = (le - ofs) / incr + 1;
 89
 90  if (i > count)
 91    {
 92      i = count;
 93      count = 0;
 94    } 
 95  else count -= i;
 96
 97  for(j = 0; j < i; j++)
 98    {
 99      RESAMPLATION;
100      ofs += incr;
101    }
102
103  if (ofs >= le) 
104    {
105      FINALINTERP;
106      vp->status=VOICE_FREE;
107      ctl->note(v);
108      *countptr-=count+1;
109    }
110
111#else /* PRECALC_LOOPS */
112    while (count--)
113    {
114      RESAMPLATION;
115      ofs += incr;
116      if (ofs >= le)
117	{
118	  FINALINTERP;
119	  vp->status=VOICE_FREE;
120 	  ctl->note(v);
121	  *countptr-=count+1;
122	  break;
123	}
124    }
125#endif /* PRECALC_LOOPS */
126  
127  vp->sample_offset=ofs; /* Update offset */
128  return resample_buffer;
129}
130
131static resample_t *rs_loop(Voice *vp, int32 count)
132{
133
134  /* Play sample until end-of-loop, skip back and continue. */
135
136  INTERPVARS;
137  int32 
138    ofs=vp->sample_offset, 
139    incr=vp->sample_increment,
140    le=vp->sample->loop_end, 
141    ll=le - vp->sample->loop_start;
142  resample_t
143    *dest=resample_buffer;
144  sample_t
145    *src=vp->sample->data;
146
147#ifdef PRECALC_LOOPS
148  int32 i;
149 
150  if (ofs < 0 || le < 0) return resample_buffer;
151
152  while (count) 
153    {
154      if (ofs >= le)
155	/* NOTE: Assumes that ll > incr and that incr > 0. */
156	ofs -= ll;
157      /* Precalc how many times we should go through the loop */
158      i = (le - ofs) / incr + 1;
159      if (i > count) 
160	{
161	  i = count;
162	  count = 0;
163	} 
164      else count -= i;
165      if (i > 0)
166      while (i--) 
167	{
168	  RESAMPLATION;
169	  ofs += incr;
170	}
171    }
172#else
173  while (count--)
174    {
175      RESAMPLATION;
176      ofs += incr;
177      if (ofs>=le)
178	ofs -= ll; /* Hopefully the loop is longer than an increment. */
179    }
180#endif
181
182  vp->sample_offset=ofs; /* Update offset */
183  return resample_buffer;
184}
185
186static resample_t *rs_bidir(Voice *vp, int32 count)
187{
188  INTERPVARS;
189  int32 
190    ofs=vp->sample_offset,
191    incr=vp->sample_increment,
192    le=vp->sample->loop_end,
193    ls=vp->sample->loop_start;
194  resample_t 
195    *dest=resample_buffer; 
196  sample_t 
197    *src=vp->sample->data;
198
199#ifdef PRECALC_LOOPS
200  int32
201    le2 = le<<1, 
202    ls2 = ls<<1,
203    i;
204  /* Play normally until inside the loop region */
205
206  if (ofs <= ls) 
207    {
208      /* NOTE: Assumes that incr > 0, which is NOT always the case
209	 when doing bidirectional looping.  I have yet to see a case
210	 where both ofs <= ls AND incr < 0, however. */
211      i = (ls - ofs) / incr + 1;
212      if (i > count) 
213	{
214	  i = count;
215	  count = 0;
216	} 
217      else count -= i;
218      while (i--) 
219	{
220	  RESAMPLATION;
221	  ofs += incr;
222	}
223    }
224
225  /* Then do the bidirectional looping */
226  
227  while(count) 
228    {
229      /* Precalc how many times we should go through the loop */
230      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
231      if (i > count) 
232	{
233	  i = count;
234	  count = 0;
235	} 
236      else count -= i;
237      while (i--) 
238	{
239	  RESAMPLATION;
240	  ofs += incr;
241	}
242      if (ofs>=le) 
243	{
244	  /* fold the overshoot back in */
245	  ofs = le2 - ofs;
246	  incr *= -1;
247	} 
248      else if (ofs <= ls) 
249	{
250	  ofs = ls2 - ofs;
251	  incr *= -1;
252	}
253    }
254
255#else /* PRECALC_LOOPS */
256  /* Play normally until inside the loop region */
257
258  if (ofs < ls)
259    {
260      while (count--)
261	{
262	  RESAMPLATION;
263	  ofs += incr;
264	  if (ofs>=ls)
265	    break;
266	}
267    }
268
269  /* Then do the bidirectional looping */
270
271  if (count>0)
272    while (count--)
273      {
274	RESAMPLATION;
275	ofs += incr;
276	if (ofs>=le)
277	  {
278	    /* fold the overshoot back in */
279	    ofs = le - (ofs - le);
280	    incr = -incr;
281	  }
282	else if (ofs <= ls)
283	  {
284	    ofs = ls + (ls - ofs);
285	    incr = -incr;
286	  }
287      }  
288#endif /* PRECALC_LOOPS */
289  vp->sample_increment=incr;
290  vp->sample_offset=ofs; /* Update offset */
291  return resample_buffer;
292}
293
294/*********************** vibrato versions ***************************/
295
296/* We only need to compute one half of the vibrato sine cycle */
297static int vib_phase_to_inc_ptr(int phase)
298{
299  if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
300    return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
301  else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
302    return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
303  else
304    return phase-VIBRATO_SAMPLE_INCREMENTS/2;
305}
306
307static int32 update_vibrato(Voice *vp, int sign)
308{
309  int32 depth;
310  int phase, pb;
311  double a;
312
313  if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
314    vp->vibrato_phase=0;
315  phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
316  
317  if (vp->vibrato_sample_increment[phase])
318    {
319      if (sign)
320	return -vp->vibrato_sample_increment[phase];
321      else
322	return vp->vibrato_sample_increment[phase];
323    }
324
325  /* Need to compute this sample increment. */
326    
327  depth=vp->sample->vibrato_depth<<7;
328
329  if (vp->vibrato_sweep)
330    {
331      /* Need to update sweep */
332      vp->vibrato_sweep_position += vp->vibrato_sweep;
333      if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
334	vp->vibrato_sweep=0;
335      else
336	{
337	  /* Adjust depth */
338	  depth *= vp->vibrato_sweep_position;
339	  depth >>= SWEEP_SHIFT;
340	}
341    }
342
343  a = FSCALE(((double)(vp->sample->sample_rate) *
344	      (double)(vp->frequency)) /
345	     ((double)(vp->sample->root_freq) *
346	      (double)(play_mode->rate)),
347	     FRACTION_BITS);
348
349  pb=(int)((sine(vp->vibrato_phase * 
350		 (SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
351	    * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
352
353  if (pb<0)
354    {
355      pb=-pb;
356      a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
357    }
358  else
359    a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
360  
361  /* If the sweep's over, we can store the newly computed sample_increment */
362  if (!vp->vibrato_sweep)
363    vp->vibrato_sample_increment[phase]=(int32) a;
364
365  if (sign)
366    a = -a; /* need to preserve the loop direction */
367
368  return (int32) a;
369}
370
371static resample_t *rs_vib_plain(int v, int32 *countptr)
372{
373
374  /* Play sample until end, then free the voice. */
375
376  INTERPVARS;
377  Voice *vp=&voice[v];
378  resample_t 
379    *dest=resample_buffer; 
380  sample_t 
381    *src=vp->sample->data;
382  int32 
383    le=vp->sample->data_length,
384    ofs=vp->sample_offset, 
385    incr=vp->sample_increment, 
386    count=*countptr;
387  int 
388    cc=vp->vibrato_control_counter;
389
390  /* This has never been tested */
391
392  if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
393
394  while (count--)
395    {
396      if (!cc--)
397	{
398	  cc=vp->vibrato_control_ratio;
399	  incr=update_vibrato(vp, 0);
400	}
401      RESAMPLATION;
402      ofs += incr;
403      if (ofs >= le)
404	{
405	  FINALINTERP;
406	  vp->status=VOICE_FREE;
407 	  ctl->note(v);
408	  *countptr-=count+1;
409	  break;
410	}
411    }
412  
413  vp->vibrato_control_counter=cc;
414  vp->sample_increment=incr;
415  vp->sample_offset=ofs; /* Update offset */
416  return resample_buffer;
417}
418
419static resample_t *rs_vib_loop(Voice *vp, int32 count)
420{
421
422  /* Play sample until end-of-loop, skip back and continue. */
423  
424  INTERPVARS;
425  int32 
426    ofs=vp->sample_offset, 
427    incr=vp->sample_increment, 
428    le=vp->sample->loop_end,
429    ll=le - vp->sample->loop_start;
430  resample_t 
431    *dest=resample_buffer; 
432  sample_t 
433    *src=vp->sample->data;
434  int 
435    cc=vp->vibrato_control_counter;
436
437#ifdef PRECALC_LOOPS
438  int32 i;
439  int
440    vibflag=0;
441
442  while (count) 
443    {
444      /* Hopefully the loop is longer than an increment */
445      if(ofs >= le)
446	ofs -= ll;
447      /* Precalc how many times to go through the loop, taking
448	 the vibrato control ratio into account this time. */
449      i = (le - ofs) / incr + 1;
450      if(i > count) i = count;
451      if(i > cc)
452	{
453	  i = cc;
454	  vibflag = 1;
455	} 
456      else cc -= i;
457      count -= i;
458      while(i--) 
459	{
460	  RESAMPLATION;
461	  ofs += incr;
462	}
463      if(vibflag) 
464	{
465	  cc = vp->vibrato_control_ratio;
466	  incr = update_vibrato(vp, 0);
467	  vibflag = 0;
468	}
469    }
470
471#else /* PRECALC_LOOPS */
472  while (count--)
473    {
474      if (!cc--)
475	{
476	  cc=vp->vibrato_control_ratio;
477	  incr=update_vibrato(vp, 0);
478	}
479      RESAMPLATION;
480      ofs += incr;
481      if (ofs>=le)
482	ofs -= ll; /* Hopefully the loop is longer than an increment. */
483    }
484#endif /* PRECALC_LOOPS */
485
486  vp->vibrato_control_counter=cc;
487  vp->sample_increment=incr;
488  vp->sample_offset=ofs; /* Update offset */
489  return resample_buffer;
490}
491
492static resample_t *rs_vib_bidir(Voice *vp, int32 count)
493{
494  INTERPVARS;
495  int32 
496    ofs=vp->sample_offset, 
497    incr=vp->sample_increment,
498    le=vp->sample->loop_end, 
499    ls=vp->sample->loop_start;
500  resample_t 
501    *dest=resample_buffer; 
502  sample_t 
503    *src=vp->sample->data;
504  int 
505    cc=vp->vibrato_control_counter;
506
507#ifdef PRECALC_LOOPS
508  int32
509    le2=le<<1,
510    ls2=ls<<1,
511    i;
512  int
513    vibflag = 0;
514
515  /* Play normally until inside the loop region */
516  while (count && (ofs <= ls)) 
517    {
518      i = (ls - ofs) / incr + 1;
519      if (i > count) i = count;
520      if (i > cc) 
521	{
522	  i = cc;
523	  vibflag = 1;
524	} 
525      else cc -= i;
526      count -= i;
527      while (i--) 
528	{
529	  RESAMPLATION;
530	  ofs += incr;
531	}
532      if (vibflag) 
533	{
534	  cc = vp->vibrato_control_ratio;
535	  incr = update_vibrato(vp, 0);
536	  vibflag = 0;
537	}
538    }
539  
540  /* Then do the bidirectional looping */
541
542  while (count) 
543    {
544      /* Precalc how many times we should go through the loop */
545      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
546      if(i > count) i = count;
547      if(i > cc) 
548	{
549	  i = cc;
550	  vibflag = 1;
551	} 
552      else cc -= i;
553      count -= i;
554      while (i--) 
555	{
556	  RESAMPLATION;
557	  ofs += incr;
558	}
559      if (vibflag) 
560	{
561	  cc = vp->vibrato_control_ratio;
562	  incr = update_vibrato(vp, (incr < 0));
563	  vibflag = 0;
564	}
565      if (ofs >= le) 
566	{
567	  /* fold the overshoot back in */
568	  ofs = le2 - ofs;
569	  incr *= -1;
570	} 
571      else if (ofs <= ls) 
572	{
573	  ofs = ls2 - ofs;
574	  incr *= -1;
575	}
576    }
577
578#else /* PRECALC_LOOPS */
579  /* Play normally until inside the loop region */
580
581  if (ofs < ls)
582    {
583      while (count--)
584	{
585	  if (!cc--)
586	    {
587	      cc=vp->vibrato_control_ratio;
588	      incr=update_vibrato(vp, 0);
589	    }
590	  RESAMPLATION;
591	  ofs += incr;
592	  if (ofs>=ls)
593	    break;
594	}
595    }
596
597  /* Then do the bidirectional looping */
598
599  if (count>0)
600    while (count--)
601      {
602	if (!cc--)
603	  {
604	    cc=vp->vibrato_control_ratio;
605	    incr=update_vibrato(vp, (incr < 0));
606	  }
607	RESAMPLATION;
608	ofs += incr;
609	if (ofs>=le)
610	  {
611	    /* fold the overshoot back in */
612	    ofs = le - (ofs - le);
613	    incr = -incr;
614	  }
615	else if (ofs <= ls)
616	  {
617	    ofs = ls + (ls - ofs);
618	    incr = -incr;
619	  }
620      }
621#endif /* PRECALC_LOOPS */
622
623  vp->vibrato_control_counter=cc;
624  vp->sample_increment=incr;
625  vp->sample_offset=ofs; /* Update offset */
626  return resample_buffer;
627}
628
629resample_t *resample_voice(int v, int32 *countptr)
630{
631  int32 ofs;
632  uint8 modes;
633  Voice *vp=&voice[v];
634  
635  if (!(vp->sample->sample_rate))
636    {
637      /* Pre-resampled data -- just update the offset and check if
638         we're out of data. */
639      ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
640						 FRACTION_BITS here... */
641      if (*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
642	{
643	  /* Note finished. Free the voice. */
644	  vp->status = VOICE_FREE;
645	  ctl->note(v);
646	  
647	  /* Let the caller know how much data we had left */
648	  *countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
649	}
650      else
651	vp->sample_offset += *countptr << FRACTION_BITS;
652      
653      return (resample_t *)vp->sample->data+ofs;
654    }
655
656  /* Need to resample. Use the proper function. */
657  modes=vp->sample->modes;
658
659  if (vp->vibrato_control_ratio)
660    {
661      if ((modes & MODES_LOOPING) &&
662	  ((modes & MODES_ENVELOPE) ||
663	   (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
664	{
665	  if (modes & MODES_PINGPONG)
666	    return rs_vib_bidir(vp, *countptr);
667	  else
668	    return rs_vib_loop(vp, *countptr);
669	}
670      else
671	return rs_vib_plain(v, countptr);
672    }
673  else
674    {
675      if ((modes & MODES_LOOPING) &&
676	  ((modes & MODES_ENVELOPE) ||
677	   (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
678	{
679	  if (modes & MODES_PINGPONG)
680	    return rs_bidir(vp, *countptr);
681	  else
682	    return rs_loop(vp, *countptr);
683	}
684      else
685	return rs_plain(v, countptr);
686    }
687}
688
689void pre_resample(Sample * sp)
690{
691  double a, xdiff;
692  int32 incr, ofs, newlen, count;
693  int16 *src = (int16 *) sp->data;
694  resample_t *newdata, *dest;
695  int16 v1, v2, v3, v4, *vptr;
696  static const char note_name[12][3] =
697  {
698    "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
699  };
700
701  ctl->cmsg(CMSG_INFO, VERB_NOISY, " * pre-resampling for note %d (%s%d)",
702	    sp->note_to_use,
703	    note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
704
705  a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
706    ((double) (sp->root_freq) * play_mode->rate);
707  if (a <= 0) return;
708  newlen = (int32)(sp->data_length / a);
709  if (newlen < 0 || (newlen >> FRACTION_BITS) > MAX_SAMPLE_SIZE) return;
710  dest = newdata = safe_malloc(newlen >> (FRACTION_BITS - 1));
711
712  count = (newlen >> FRACTION_BITS) - 1;
713  ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
714
715  if (--count)
716    *dest++ = src[0];
717
718  /* Since we're pre-processing and this doesn't have to be done in
719     real-time, we go ahead and do the full sliding cubic interpolation. */
720  while (--count)
721    {
722      vptr = src + (ofs >> FRACTION_BITS);
723      v1 = (vptr == src) ? *vptr : *(vptr - 1);
724      v2 = *vptr;
725      v3 = *(vptr + 1);
726      v4 = *(vptr + 2);
727      xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS);
728      *dest++ = (int16)(v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
729      xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4))));
730      ofs += incr;
731    }
732
733  if (ofs & FRACTION_MASK)
734    {
735      v1 = src[ofs >> FRACTION_BITS];
736      v2 = src[(ofs >> FRACTION_BITS) + 1];
737      *dest++ = (resample_t)(v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
738    }
739  else
740    *dest++ = src[ofs >> FRACTION_BITS];
741
742  sp->data_length = newlen;
743  sp->loop_start = (int32)(sp->loop_start / a);
744  sp->loop_end = (int32)(sp->loop_end / a);
745  free(sp->data);
746  sp->data = (sample_t *) newdata;
747  sp->sample_rate = 0;
748}