annotate demo/trim_spc.c @ 2:c3248c71ae74

begin convert program
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 06:21:33 -0700
parents e38dacceb958
children
rev   line source
rlm@0 1 /* Trims silence off beginning of SPC file.
rlm@0 2 Requires the accurate DSP; won't compile with the fast DSP.
rlm@0 3 Please note that the algorithm could be improved; this is just
rlm@0 4 a simple example showing the idea.
rlm@0 5
rlm@0 6 Usage: trim_spc [test.spc [trimmed.spc]]
rlm@0 7 */
rlm@0 8
rlm@0 9 #include "snes_spc/spc.h"
rlm@0 10
rlm@0 11 #include "demo_util.h" /* error(), load_file() */
rlm@0 12
rlm@0 13 /* Change to 1 to have it trim to next key on event rather than trim silence */
rlm@0 14 enum { use_kon_check = 0 };
rlm@0 15
rlm@0 16 /* True if all count samples from in are silent (or very close to it) */
rlm@0 17 int is_silent( short const* in, int count );
rlm@0 18
rlm@0 19 int main( int argc, char** argv )
rlm@0 20 {
rlm@0 21 /* Load file into memory */
rlm@0 22 long spc_size;
rlm@0 23 void* spc = load_file( (argc > 1) ? argv [1] : "test.spc", &spc_size );
rlm@0 24
rlm@0 25 /* Load into emulator */
rlm@0 26 SNES_SPC* snes_spc = spc_new();
rlm@0 27 if ( !snes_spc ) error( "Out of memory" );
rlm@0 28 error( spc_load_spc( snes_spc, spc, spc_size ) );
rlm@0 29
rlm@0 30 /* Expand SPC data so there's enough room for emulator to save to.
rlm@0 31 We simply overwrite the emulator data in the old SPC file rather
rlm@0 32 than creating new SPC data. This preserves the text tags from
rlm@0 33 the old file. */
rlm@0 34 if ( spc_size < spc_file_size )
rlm@0 35 {
rlm@0 36 spc_size = spc_file_size;
rlm@0 37 spc = realloc( spc, spc_size ); /* leaks memory if allocation fails */
rlm@0 38 if ( !spc ) error( "Out of memory" );
rlm@0 39 }
rlm@0 40
rlm@0 41 /* Keep saving SPC, then playing a little more. Once SPC becomes non-silent,
rlm@0 42 write the SPC data saved just before this. */
rlm@0 43 {
rlm@0 44 long samples_trimmed = 0;
rlm@0 45 while ( 1 )
rlm@0 46 {
rlm@0 47 #define BUF_SIZE 1024
rlm@0 48 short buf [BUF_SIZE];
rlm@0 49
rlm@0 50 if ( samples_trimmed > 10 * spc_sample_rate * 2 )
rlm@0 51 error( "Excess silence found" );
rlm@0 52
rlm@0 53 spc_clear_echo( snes_spc );
rlm@0 54 spc_save_spc( snes_spc, spc );
rlm@0 55
rlm@0 56 /* Fill buffer */
rlm@0 57 error( spc_play( snes_spc, BUF_SIZE, buf ) );
rlm@0 58 samples_trimmed += BUF_SIZE;
rlm@0 59
rlm@0 60 /* See if SPC became non-silent */
rlm@0 61 if ( use_kon_check ? spc_check_kon( snes_spc ) : !is_silent( buf, BUF_SIZE ) )
rlm@0 62 break;
rlm@0 63 }
rlm@0 64
rlm@0 65 printf( "Trimmed %.1f seconds\n", (double) samples_trimmed / spc_sample_rate / 2 );
rlm@0 66 }
rlm@0 67
rlm@0 68 spc_delete( snes_spc );
rlm@0 69 write_file( (argc > 2) ? argv [2] : "trimmed.spc", spc, spc_size );
rlm@0 70
rlm@0 71 return 0;
rlm@0 72 }
rlm@0 73
rlm@0 74 int is_silent( short const* in, int count )
rlm@0 75 {
rlm@0 76 unsigned const threshold = 0x10;
rlm@0 77 while ( count-- )
rlm@0 78 {
rlm@0 79 if ( (unsigned) (*in++ + threshold / 2) > threshold )
rlm@0 80 return 0;
rlm@0 81 }
rlm@0 82 return 1;
rlm@0 83 }