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