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