annotate Plugins/Input/console/demo_panning.cpp @ 364:afdba4a1ebdf trunk

[svn] Implement VGM playback.
author chainsaw
date Sat, 31 Dec 2005 13:14:35 -0800
parents 252843aac42f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
1
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
2 // Use Panning_Buffer to add left/right panning while playing "test.vgm" using Vgm_Emu,
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
3 // and record to "out.wav".
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
4
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
5 #include "Vgm_Emu.h"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
6 #include "Wave_Writer.hpp"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
7 #include "Panning_Buffer.h"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
8
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
9 #include <stdlib.h>
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
10 #include <stdio.h>
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
11
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
12 static void exit_if_error( const char* str )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
13 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
14 if ( str ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
15 fprintf( stderr, "Error: %s\n", str );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
16 exit( EXIT_FAILURE );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
17 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
18 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
19
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
20 int main()
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
21 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
22 const long sample_rate = 44100;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
23
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
24 // Create panning buffer with 1/30 second length
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
25 Panning_Buffer buf;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
26 exit_if_error( buf.sample_rate( sample_rate, 1000 / 30 ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
27
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
28 // Prepare emulator with output set to panning buffer
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
29 Vgm_Emu* emu = new Vgm_Emu;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
30 if ( !emu )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
31 exit_if_error( "Out of memory" );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
32 exit_if_error( emu->init( &buf ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
33
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
34 // Load file
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
35 Emu_Std_Reader reader;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
36 exit_if_error( reader.open( "test.vgm" ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
37 Vgm_Emu::header_t header;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
38 exit_if_error( reader.read( &header, sizeof header ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
39 exit_if_error( emu->load( header, reader ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
40
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
41 // Configure panning buffer
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
42 buf.set_pan( 0, 1.40, 0.60 ); // pulse 1 - left
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
43 buf.set_pan( 1, 1.00, 1.00 ); // pulse 2 - center
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
44 buf.set_pan( 2, 0.40, 1.60 ); // pulse 3 - right
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
45 buf.set_pan( 3, 1.00,-1.00 ); // noise - "surround" (phase-inverted left/right)
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
46
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
47 // Record first track for several seconds
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
48 exit_if_error( emu->start_track( 0 ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
49 Wave_Writer wave( sample_rate, "out.wav" );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
50 wave.stereo( true );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
51 while ( wave.sample_count() < 2 * sample_rate * 10 )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
52 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
53 const long buf_size = 1234; // can be any size
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
54 Music_Emu::sample_t buf [buf_size];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
55
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
56 // fill buffer
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
57 exit_if_error( emu->play( buf_size, buf ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
58
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
59 // write to sound file
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
60 wave.write( buf, buf_size );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
61 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
62
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
63 delete emu;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
64
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
65 return 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
66 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
67