annotate Plugins/Input/console/Vgm_Emu.cpp @ 98:e42694a28331 trunk

[svn] More progress -- now loads as an audacious module. :)
author nenolod
date Tue, 01 Nov 2005 21:34:11 -0800
parents 252843aac42f
children 0b9507985f0d
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 // Game_Music_Emu 0.2.4. http://www.slack.net/~ant/libs/
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
3
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
4 #include "Vgm_Emu.h"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
5
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
6 #include <string.h>
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
7 #include <math.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 /* Copyright (C) 2003-2005 Shay Green. This module is free software; you
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
10 can redistribute it and/or modify it under the terms of the GNU Lesser
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
11 General Public License as published by the Free Software Foundation; either
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
12 version 2.1 of the License, or (at your option) any later version. This
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
13 module is distributed in the hope that it will be useful, but WITHOUT ANY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
16 more details. You should have received a copy of the GNU Lesser General
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
17 Public License along with this module; if not, write to the Free Software
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
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 #include BLARGG_SOURCE_BEGIN
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 vgm_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 Vgm_Emu::Vgm_Emu( double gain )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
25 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
26 data = NULL;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
27 pos = NULL;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
28 apu.volume( gain );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
29
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
30 // to do: decide on equalization parameters
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
31 set_equalizer( equalizer_t( -32, 8000, 66 ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
32 }
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 Vgm_Emu::~Vgm_Emu()
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
35 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
36 unload();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
37 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
38
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
39 void Vgm_Emu::unload()
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 delete [] data;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
42 data = NULL;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
43 pos = NULL;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
44 track_ended_ = false;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
45 }
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 const char** Vgm_Emu::voice_names() const
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
48 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
49 static const char* names [] = { "Square 1", "Square 2", "Square 3", "Noise" };
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
50 return names;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
51 }
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 void Vgm_Emu::set_voice( int i, Blip_Buffer* c, Blip_Buffer* l, Blip_Buffer* r )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
54 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
55 apu.osc_output( i, c, l, r );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
56 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
57
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
58 void Vgm_Emu::update_eq( blip_eq_t const& eq )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
59 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
60 apu.treble_eq( eq );
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 const int time_bits = 12;
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 inline sms_time_t Vgm_Emu::clocks_from_samples( int samples ) const
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 const long round_up = 1L << (time_bits - 1);
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
68 return (samples * time_factor + round_up) >> time_bits;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
69 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
70
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
71 static long get_le32( const BOOST::uint8_t b [4] )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
72 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
73 return b [3] * 0x1000000L + b [2] * 0x10000L + b [1] * 0x100L + b [0];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
74 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
75
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
76 blargg_err_t Vgm_Emu::load( const header_t& h, Emu_Reader& in )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
77 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
78 unload();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
79
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
80 // compatibility
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
81 if ( 0 != memcmp( h.tag, "Vgm ", 4 ) )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
82 return "Not a VGM file";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
83 if ( get_le32( h.vers ) > 0x0101 )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
84 return "Unsupported VGM format";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
85
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
86 // clock rate
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
87 long clock_rate = get_le32( h.psg_rate );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
88 if ( !clock_rate )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
89 return "Only PSG sound chip is supported";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
90 time_factor = (long) floor( clock_rate * ((1L << time_bits) /
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
91 (double) vgm_sample_rate) + 0.5 );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
92
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
93 // data
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
94 long data_size = in.remain();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
95 data = new byte [data_size + 3]; // allow pointer to go past end
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
96 if ( !data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
97 return "Out of memory";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
98 end = data + data_size;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
99 data [data_size] = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
100 data [data_size + 1] = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
101 data [data_size + 2] = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
102 blargg_err_t err = in.read( data, data_size );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
103 if ( err ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
104 unload();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
105 return err;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
106 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
107 long loop_offset = get_le32( h.loop_offset );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
108 loop_begin = end;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
109 loop_duration = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
110 if ( loop_offset )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
111 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
112 loop_duration = get_le32( h.loop_duration );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
113 if ( loop_duration )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
114 loop_begin = &data [loop_offset + 0x1c - sizeof (header_t)];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
115 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
116
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
117 voice_count_ = Sms_Apu::osc_count;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
118 track_count_ = 1;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
119
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
120 return setup_buffer( clock_rate );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
121 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
122
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
123 int Vgm_Emu::track_length( const byte** end_out, int* remain_out ) const
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
124 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
125 require( data ); // file must have been loaded
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
126
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
127 long time = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
128
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
129 if ( end_out || !loop_duration )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
130 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
131 const byte* p = data;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
132 while ( p < end )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
133 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
134 int cmd = *p++;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
135 switch ( cmd )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
136 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
137 case 0x4f:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
138 case 0x50:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
139 p += 1;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
140 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
141
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
142 case 0x61:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
143 if ( p + 1 < end ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
144 time += p [1] * 0x100L + p [0];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
145 p += 2;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
146 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
147 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
148
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
149 case 0x62:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
150 time += 735; // ntsc frame
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
151 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
152
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
153 case 0x63:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
154 time += 882; // pal frame
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
155 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
156
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
157 default:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
158 if ( (p [-1] & 0xf0) == 0x50 ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
159 p += 2;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
160 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
161 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
162 dprintf( "Bad command in VGM stream: %02X\n", (int) cmd );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
163 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
164
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
165 case 0x66:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
166 if ( end_out )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
167 *end_out = p;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
168 if ( remain_out )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
169 *remain_out = end - p;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
170 p = end;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
171 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
172 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
173 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
174 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
175
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
176 // i.e. ceil( exact_length + 0.5 )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
177 return loop_duration ? 0 :
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
178 (time + (vgm_sample_rate >> 1) + vgm_sample_rate - 1) / vgm_sample_rate;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
179 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
180
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
181 blargg_err_t Vgm_Emu::start_track( int )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
182 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
183 require( data ); // file must have been loaded
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
184
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
185 pos = data;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
186 loop_remain = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
187 delay = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
188 track_ended_ = false;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
189 apu.reset();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
190 starting_track();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
191 return blargg_success;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
192 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
193
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
194 blip_time_t Vgm_Emu::run( int msec, bool* added_stereo )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
195 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
196 require( pos ); // track must have been started
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
197
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
198 const int duration = vgm_sample_rate / 100 * msec / 10;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
199 int time = delay;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
200 while ( time < duration && pos < end )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
201 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
202 if ( !loop_remain && pos >= loop_begin )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
203 loop_remain = loop_duration;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
204
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
205 int cmd = *pos++;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
206 int delay = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
207 switch ( cmd )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
208 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
209 case 0x66:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
210 pos = end; // end
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
211 if ( loop_duration ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
212 pos = loop_begin;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
213 loop_remain = loop_duration;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
214 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
215 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
216
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
217 case 0x62:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
218 delay = 735; // ntsc frame
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
219 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
220
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
221 case 0x63:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
222 delay = 882; // pal frame
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
223 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
224
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
225 case 0x4f:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
226 if ( pos == end ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
227 check( false ); // missing data
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
228 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
229 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
230 apu.write_ggstereo( clocks_from_samples( time ), *pos++ );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
231 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
232
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
233 case 0x50:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
234 if ( pos == end ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
235 check( false ); // missing data
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
236 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
237 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
238 apu.write_data( clocks_from_samples( time ), *pos++ );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
239 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
240
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
241 case 0x61:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
242 if ( end - pos < 1 ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
243 check( false ); // missing data
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
244 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
245 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
246 delay = pos [1] * 0x100L + pos [0];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
247 pos += 2;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
248 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
249
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
250 default:
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
251 if ( (cmd & 0xf0) == 0x50 )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
252 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
253 if ( end - pos < 1 ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
254 check( false ); // missing data
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
255 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
256 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
257 pos += 2;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
258 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
259 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
260 dprintf( "Bad command in VGM stream: %02X\n", (int) cmd );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
261 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
262
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
263 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
264 time += delay;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
265 if ( loop_remain && (loop_remain -= delay) <= 0 )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
266 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
267 pos = loop_begin;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
268 loop_remain = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
269 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
270 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
271
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
272 blip_time_t end_time = clocks_from_samples( duration );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
273 if ( pos < end )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
274 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
275 delay = time - duration;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
276 if ( apu.end_frame( end_time ) && added_stereo )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
277 *added_stereo = true;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
278 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
279 else {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
280 delay = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
281 track_ended_ = true;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
282 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
283
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
284 return end_time;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
285 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
286