annotate demo/play_spc.c @ 8:a37863126396

reordered error messages
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 07:26:53 -0700
parents e38dacceb958
children
rev   line source
rlm@0 1 /* Records SPC into wave file. Uses dsp_filter to give more authentic sound.
rlm@0 2
rlm@0 3 Usage: play_spc [test.spc]
rlm@0 4 */
rlm@0 5
rlm@0 6 #include "snes_spc/spc.h"
rlm@0 7
rlm@0 8 #include "wave_writer.h"
rlm@0 9 #include "demo_util.h" /* error(), load_file() */
rlm@0 10
rlm@0 11 int main( int argc, char** argv )
rlm@0 12 {
rlm@0 13 /* Create emulator and filter */
rlm@0 14 SNES_SPC* snes_spc = spc_new();
rlm@0 15 SPC_Filter* filter = spc_filter_new();
rlm@0 16 if ( !snes_spc || !filter ) error( "Out of memory" );
rlm@0 17
rlm@0 18 /* Load SPC */
rlm@0 19 {
rlm@0 20 /* Load file into memory */
rlm@0 21 long spc_size;
rlm@0 22 void* spc = load_file( (argc > 1) ? argv [1] : "test.spc", &spc_size );
rlm@0 23
rlm@0 24 /* Load SPC data into emulator */
rlm@0 25 error( spc_load_spc( snes_spc, spc, spc_size ) );
rlm@0 26 free( spc ); /* emulator makes copy of data */
rlm@0 27
rlm@0 28 /* Most SPC files have garbage data in the echo buffer, so clear that */
rlm@0 29 spc_clear_echo( snes_spc );
rlm@0 30
rlm@0 31 /* Clear filter before playing */
rlm@0 32 spc_filter_clear( filter );
rlm@0 33 }
rlm@0 34
rlm@0 35 /* Record 20 seconds to wave file */
rlm@0 36 wave_open( spc_sample_rate, "out.wav" );
rlm@0 37 wave_enable_stereo();
rlm@0 38 while ( wave_sample_count() < 20 * spc_sample_rate * 2 )
rlm@0 39 {
rlm@0 40 /* Play into buffer */
rlm@0 41 #define BUF_SIZE 2048
rlm@0 42 short buf [BUF_SIZE];
rlm@0 43 error( spc_play( snes_spc, BUF_SIZE, buf ) );
rlm@0 44
rlm@0 45 /* Filter samples */
rlm@0 46 spc_filter_run( filter, buf, BUF_SIZE );
rlm@0 47
rlm@0 48 wave_write( buf, BUF_SIZE );
rlm@0 49 }
rlm@0 50
rlm@0 51 /* Cleanup */
rlm@0 52 spc_filter_delete( filter );
rlm@0 53 spc_delete( snes_spc );
rlm@0 54 wave_close();
rlm@0 55
rlm@0 56 return 0;
rlm@0 57 }