1 /****************************************************************************
3 * ____ ___ ____ __ ______ ___ ____ ____/ / *
4 * / __ `__ \/ __ `/ |/ / __ `__ \/ __ \/ __ / *
5 * / / / / / / /_/ /> </ / / / / / /_/ / /_/ / *
6 * /_/ /_/ /_/\__,_/_/|_/_/ /_/ /_/\____/\__,_/ *
8 * Copyright (c) 2008, Mukunda Johnson (mukunda@maxmod.org) *
10 * Permission to use, copy, modify, and/or distribute this software for any *
11 * purpose with or without fee is hereby granted, provided that the above *
12 * copyright notice and this permission notice appear in all copies. *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF *
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR *
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES *
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN *
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
21 ****************************************************************************/
23 #define MAX_UNROLL_THRESHOLD 1024 // will unroll upto 1kb more of data (when fixing NDS samples)
24 #define GBA_MIN_LOOP_SIZE 512
35 extern int ignore_sflags;
37 void Sample_PadStart( Sample* samp, u32 count )
39 // Pad beginning of sample with zero
44 return; // nothing to do
45 if( samp->format & SAMPF_16BIT )
47 newdata16 = (u16*)malloc( (samp->sample_length+count)*2 );
48 for( x = 0; x < count; x++ )
50 for( x = 0; x < samp->sample_length; x++ )
51 newdata16[count+x]=((u16*)samp->data)[x];
53 samp->data = (void*)newdata16;
57 newdata8 = (u8*)malloc( (samp->sample_length+count) );
58 for( x = 0; x < count; x++ )
60 for( x = 0; x < samp->sample_length; x++ )
61 newdata8[count+x] = ((u8*)samp->data)[x];
63 samp->data = (void*)newdata8;
65 samp->loop_start += count;
66 samp->loop_end += count;
67 samp->sample_length += count;
70 void Sample_PadEnd( Sample* samp, u32 count )
72 // Pad end of sample with zero
77 return; // nothing to do
78 if( samp->format & SAMPF_16BIT )
80 newdata16 = malloc( (samp->sample_length+count)*2 );
81 for( x = 0; x < samp->sample_length; x++ )
82 newdata16[x]= ((u16*)samp->data)[x];
83 for( x = 0; x < count; x++ )
84 newdata16[samp->sample_length+x]=32768;
86 samp->data = (void*)newdata16;
90 newdata8 = malloc( (samp->sample_length+count) );
91 for( x = 0; x < samp->sample_length; x++ )
92 newdata8[x]= ((u8*)samp->data)[x];
93 for( x = 0; x < count; x++ )
94 newdata8[samp->sample_length+x]=128;
96 samp->data = (void*)newdata8;
98 samp->loop_end += count;
99 samp->sample_length += count;
102 void Unroll_Sample_Loop( Sample* samp, u32 count )
104 // unrolls sample loop (count) times
105 // loop end MUST equal sample length
111 looplen = samp->loop_end-samp->loop_start;
112 newlen = samp->sample_length + looplen*count;
113 if( samp->format & SAMPF_16BIT )
115 newdata16 = (u16*)malloc( newlen *2 );
116 for( x = 0; x < samp->sample_length; x++ )
117 newdata16[x] = ((u16*)samp->data)[x];
118 for( x = 0; x < looplen*count; x++ )
119 newdata16[samp->sample_length+x] = ((u16*)samp->data)[samp->loop_start+ (x%looplen)];
121 samp->data = (void*)newdata16;
125 newdata8 = (u8*)malloc( newlen );
126 for( x = 0; x < samp->sample_length; x++ )
127 newdata8[x] = ((u8*)samp->data)[x];
128 for( x = 0; x < looplen*count; x++ )
129 newdata8[samp->sample_length+x] = ((u8*)samp->data)[samp->loop_start+ (x%looplen)];
131 samp->data = (void*)newdata8;
133 samp->loop_end += looplen*count;
134 samp->sample_length += looplen*count;
137 void Unroll_BIDI_Sample( Sample* samp )
139 // sample length MUST equal sample loop end
140 // sample MUST have loop type 2 (BIDI)
147 looplen = samp->loop_end-samp->loop_start;
148 newlen = (samp->sample_length + looplen);
150 if( samp->format & SAMPF_16BIT )
152 newdata16 = malloc( newlen *2 );
153 for( x = 0; x < samp->sample_length; x++ )
154 newdata16[x] = ((u16*)samp->data)[x];
155 for( x = 0; x < looplen; x++ )
156 newdata16[x+samp->sample_length] = ((u16*)samp->data)[samp->loop_end-1-x];
158 samp->data = (void*)newdata16;
162 newdata8 = malloc( newlen );
163 for( x = 0; x < samp->sample_length; x++ )
164 newdata8[x] = ((u8*)samp->data)[x];
165 for( x = 0; x < looplen; x++ )
166 newdata8[x+samp->sample_length] = ((u8*)samp->data)[samp->loop_end-1-x];
168 samp->data = (void*)newdata8;
171 samp->sample_length += looplen;
172 samp->loop_end += looplen;
175 /* NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
176 The following resample routine was stolen from CHIBITRACKER (http://chibitracker.berlios.de), thanks reduz!
177 NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE */
179 void Resample( Sample* samp, u32 newsize )
184 u8 *src8 = (u8*)samp->data;
185 u16 *src16 = (u16*)samp->data;
187 int oldlength = samp->sample_length;
188 int lpoint = samp->loop_start;
191 bool bit16 = samp->format & SAMPF_16BIT;
197 dst16 = (u16*)malloc(newsize*2);
202 dst8 = (u8*)malloc(newsize);
206 double tscale = (double)oldlength / (double)newsize;
209 for( i = 0; i < newsize; i++ )
211 posf = (double)i * tscale;
212 int posi = (int)floor(posf);
214 double mu = posf - (double)posi;
215 double s0, s1, s2, s3;
216 double mu2, a0, a1, a2, a3, res;
218 // get previous, current, next, and after next samples
221 s0 = (posi-1) < 0 ? 0 : ((double)(src16[posi-1]));
222 s1 = ((double)(src16[posi ]));
223 s2 = (posi+1) >= oldlength ?
225 ((double)(src16[lpoint + (posi + 1 - oldlength)])) : 0) :
226 ((double)(src16[posi+1]));
227 s3 = (posi+1) >= oldlength ?
229 ((double)(src16[lpoint + (posi + 2 - oldlength)])) : 0) :
230 ((double)(src16[posi+2]));
234 s0 = (posi-1) < 0 ? 0 : ((double)(src8[posi-1]));
235 s1 = ((double)(src8[posi ]));
236 s2 = (posi+1) >= oldlength ?
238 ((double)(src8[lpoint + (posi + 1 - oldlength)])) : 0) :
239 ((double)(src8[posi+1]));
240 s3 = (posi+1) >= oldlength ?
242 ((double)(src8[lpoint + (posi + 2 - oldlength)])) : 0) :
243 ((double)(src8[posi+2]));
253 a0 = s3 - s2 - s0 + s1;
258 res = a0*mu*mu2 + a1*mu2 + a2*mu + a3;
259 int resi = ((int)floor(res+0.5));
263 if( resi < -32768 ) resi = -32768;
264 if( resi > 32767 ) resi = 32767;
265 dst16[i] = resi + 32768;
269 if( resi < -128 ) resi = -128;
270 if( resi > 127 ) resi = 127;
271 dst8[i] = resi + 128;
278 samp->data = (void*)dst16;
280 samp->data = (void*)dst8;
282 samp->sample_length = newsize;
283 samp->loop_end = newsize;
284 samp->loop_start = (int)(((double)samp->loop_start * (double)newsize+((double)oldlength/2))/(double)oldlength);
285 samp->frequency = (int)(((double)samp->frequency * (double)newsize+((double)oldlength/2))/(double)oldlength);
288 void Sample_8bit( Sample* samp )
290 if( samp->format & SAMPF_16BIT )
294 newdata = (u8*)malloc( samp->sample_length );
295 for( t = 0; t < samp->sample_length; t++ )
296 newdata[t] = ((u16*)samp->data)[t] / 256;
298 samp->data = newdata;
299 // samp->bit16=false;
300 samp->format &= ~SAMPF_16BIT;
304 void Sample_Sign( Sample* samp )
306 // sample must be unsigned
308 if( samp->format & SAMPF_16BIT )
310 for( x =0 ; x < samp->sample_length; x++ )
312 int a = (( (int) ((u16*)samp->data)[x] ) - 32768);
313 if(a < -32767) a = -32767; // clamp LOW to -32767 (leave space for interpolation error)
314 // if(a > 32765) a = 32765; // clamp HIGH to 32766
315 ((u16*)samp->data)[x] = (u16)a ;
320 for( x =0 ; x < samp->sample_length; x++ )
322 int a = (( (int) ((u8*)samp->data)[x] ) - 128);
323 if( a == -128 ) a = -127;
324 ((u8*)samp->data)[x] = (u8) a;
328 samp->format |= SAMPF_SIGNED;
331 void FixSample_GBA( Sample* samp )
333 // convert to 8-bit if neccesary
336 // delete data after loop_end if loop exists
337 if( samp->loop_type != 0 )
338 samp->sample_length = samp->loop_end;
341 if( samp->loop_type == 2 )
342 Unroll_BIDI_Sample( samp );
344 if( samp->loop_type )
346 if( samp->loop_end-samp->loop_start < GBA_MIN_LOOP_SIZE )
348 Unroll_Sample_Loop( samp, (GBA_MIN_LOOP_SIZE / (samp->loop_end-samp->loop_start))+1 );
353 int strcmpshit( char* str1, char* str2 )
357 while( str1[x] != 0 )
359 if( str1[x] == str2[f] )f++;
361 if( str2[f] == 0 ) return 1;
367 void FixSample_NDS( Sample* samp )
369 if( samp->sample_length == 0 )
371 // sample has no data
372 samp->loop_end=samp->loop_start=0;
375 // delete data after loop_end if loop exists
376 if( samp->loop_type != 0 )
377 samp->sample_length = samp->loop_end;
380 if( samp->loop_type == 2 )
381 Unroll_BIDI_Sample( samp );
384 if( samp->loop_type )
388 if( ((strcmpshit( samp->name, "%o" )) > 0) )
390 Unroll_Sample_Loop( samp, 1 );
391 samp->loop_start += (samp->loop_end-samp->loop_start) / 2;
398 if( ((strcmpshit( samp->name, "%c" )) > 0) )
400 samp->format |= SAMPF_COMP;
405 if( samp->loop_type )
407 int looplen = samp->loop_end-samp->loop_start;
408 if( !(samp->format & SAMPF_COMP) )
410 if( samp->format & SAMPF_16BIT )
414 int addition = (samp->loop_end - samp->loop_start);
415 if( addition > MAX_UNROLL_THRESHOLD )
416 Resample( samp, samp->sample_length +1 );
418 Unroll_Sample_Loop( samp, 1 );
438 addition = looplen*count;
439 if( addition > MAX_UNROLL_THRESHOLD )
440 Resample( samp, samp->sample_length + (4-(looplen & 3)) );
442 Unroll_Sample_Loop( samp, count );
449 int count=0, addition;
455 addition = looplen*count;
456 if( addition > MAX_UNROLL_THRESHOLD )
457 Resample( samp, samp->sample_length + (4-(looplen & 7)) );
459 Unroll_Sample_Loop( samp, count );
464 if( samp->loop_type )
467 if( !(samp->format & SAMPF_COMP) )
469 if( samp->format & SAMPF_16BIT ) {
470 padsize = ( (2 - (samp->loop_start & 1)) & 1 );
472 padsize = ( (4 - (samp->loop_start & 3)) & 3 );
477 padsize = ( (8 - (samp->loop_start & 7)) & 7 );
479 Sample_PadStart( samp, padsize );
482 // Pad end, only happens when loop is disabled
483 if( !(samp->format & SAMPF_COMP) )
485 if( samp->format & SAMPF_16BIT )
487 if( samp->sample_length & 1 )
489 Sample_PadEnd( samp, 2-(samp->sample_length&1) );
494 if( samp->sample_length & 3 )
496 Sample_PadEnd( samp, 4-(samp->sample_length&3) );
502 if( samp->sample_length & 7 )
504 Sample_PadEnd( samp, 8-(samp->sample_length&7) );
508 Sample_Sign( samp ); // DS hardware takes signed samples
510 if( samp->format & SAMPF_COMP )
512 // compress with IMA-ADPCM hunger owned
513 adpcm_compress_sample( samp );
521 void FixSample( Sample* samp )
523 // Clamp loop_start and loop_end (f.e. FR_TOWER.MOD)
524 samp->loop_start = CLAMP(samp->loop_start, 0, samp->sample_length);
525 samp->loop_end = CLAMP(samp->loop_end, 0, samp->sample_length);
527 if( target_system == SYSTEM_GBA )
528 FixSample_GBA( samp );
529 else if( target_system == SYSTEM_NDS )
530 FixSample_NDS( samp );