diff demo/play_spc.c @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/play_spc.c	Fri Oct 21 05:53:11 2011 -0700
     1.3 @@ -0,0 +1,57 @@
     1.4 +/* Records SPC into wave file. Uses dsp_filter to give more authentic sound.
     1.5 +
     1.6 +Usage: play_spc [test.spc]
     1.7 +*/
     1.8 +
     1.9 +#include "snes_spc/spc.h"
    1.10 +
    1.11 +#include "wave_writer.h"
    1.12 +#include "demo_util.h" /* error(), load_file() */
    1.13 +
    1.14 +int main( int argc, char** argv )
    1.15 +{
    1.16 +	/* Create emulator and filter */
    1.17 +	SNES_SPC* snes_spc = spc_new();
    1.18 +	SPC_Filter* filter = spc_filter_new();
    1.19 +	if ( !snes_spc || !filter ) error( "Out of memory" );
    1.20 +	
    1.21 +	/* Load SPC */
    1.22 +	{
    1.23 +		/* Load file into memory */
    1.24 +		long spc_size;
    1.25 +		void* spc = load_file( (argc > 1) ? argv [1] : "test.spc", &spc_size );
    1.26 +		
    1.27 +		/* Load SPC data into emulator */
    1.28 +		error( spc_load_spc( snes_spc, spc, spc_size ) );
    1.29 +		free( spc ); /* emulator makes copy of data */
    1.30 +		
    1.31 +		/* Most SPC files have garbage data in the echo buffer, so clear that */
    1.32 +		spc_clear_echo( snes_spc );
    1.33 +		
    1.34 +		/* Clear filter before playing */
    1.35 +		spc_filter_clear( filter );
    1.36 +	}
    1.37 +	
    1.38 +	/* Record 20 seconds to wave file */
    1.39 +	wave_open( spc_sample_rate, "out.wav" );
    1.40 +	wave_enable_stereo();
    1.41 +	while ( wave_sample_count() < 20 * spc_sample_rate * 2 )
    1.42 +	{
    1.43 +		/* Play into buffer */
    1.44 +		#define BUF_SIZE 2048
    1.45 +		short buf [BUF_SIZE];
    1.46 +		error( spc_play( snes_spc, BUF_SIZE, buf ) );
    1.47 +		
    1.48 +		/* Filter samples */
    1.49 +		spc_filter_run( filter, buf, BUF_SIZE );
    1.50 +		
    1.51 +		wave_write( buf, BUF_SIZE );
    1.52 +	}
    1.53 +	
    1.54 +	/* Cleanup */
    1.55 +	spc_filter_delete( filter );
    1.56 +	spc_delete( snes_spc );
    1.57 +	wave_close();
    1.58 +	
    1.59 +	return 0;
    1.60 +}