diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/libgme/vgm_emu.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/libgme/vgm_emu.c')
-rw-r--r-- | lib/rbcodec/codecs/libgme/vgm_emu.c | 858 |
1 files changed, 858 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/vgm_emu.c b/lib/rbcodec/codecs/libgme/vgm_emu.c new file mode 100644 index 0000000000..4b8953ccf4 --- /dev/null +++ b/lib/rbcodec/codecs/libgme/vgm_emu.c | |||
@@ -0,0 +1,858 @@ | |||
1 | // Game_Music_Emu 0.5.5. http://www.slack.net/~ant/ | ||
2 | |||
3 | #include "vgm_emu.h" | ||
4 | |||
5 | #include "blargg_endian.h" | ||
6 | #include <string.h> | ||
7 | #include <math.h> | ||
8 | |||
9 | /* Copyright (C) 2003-2006 Shay Green. This module is free software; you | ||
10 | can redistribute it and/or modify it under the terms of the GNU Lesser | ||
11 | General Public License as published by the Free Software Foundation; either | ||
12 | version 2.1 of the License, or (at your option) any later version. This | ||
13 | module is distributed in the hope that it will be useful, but WITHOUT ANY | ||
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
16 | details. You should have received a copy of the GNU Lesser General Public | ||
17 | License along with this module; if not, write to the Free Software Foundation, | ||
18 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ | ||
19 | |||
20 | #include "blargg_source.h" | ||
21 | |||
22 | const char* const gme_wrong_file_type = "Wrong file type for this emulator"; | ||
23 | |||
24 | int const fm_gain = 3; // FM emulators are internally quieter to avoid 16-bit overflow | ||
25 | |||
26 | // VGM commands (Spec v1.50) | ||
27 | enum { | ||
28 | cmd_gg_stereo = 0x4F, | ||
29 | cmd_psg = 0x50, | ||
30 | cmd_ym2413 = 0x51, | ||
31 | cmd_ym2612_port0 = 0x52, | ||
32 | cmd_ym2612_port1 = 0x53, | ||
33 | cmd_ym2151 = 0x54, | ||
34 | cmd_delay = 0x61, | ||
35 | cmd_delay_735 = 0x62, | ||
36 | cmd_delay_882 = 0x63, | ||
37 | cmd_byte_delay = 0x64, | ||
38 | cmd_end = 0x66, | ||
39 | cmd_data_block = 0x67, | ||
40 | cmd_short_delay = 0x70, | ||
41 | cmd_pcm_delay = 0x80, | ||
42 | cmd_pcm_seek = 0xE0, | ||
43 | |||
44 | pcm_block_type = 0x00, | ||
45 | ym2612_dac_port = 0x2A, | ||
46 | ym2612_dac_pan_port = 0xB6 | ||
47 | }; | ||
48 | |||
49 | static void clear_track_vars( struct Vgm_Emu* this ) | ||
50 | { | ||
51 | this->current_track = -1; | ||
52 | track_stop( &this->track_filter ); | ||
53 | } | ||
54 | |||
55 | int play_frame( struct Vgm_Emu* this, blip_time_t blip_time, int sample_count, sample_t* buf ); | ||
56 | static int play_frame_( void* data, blip_time_t blip_time, int sample_count, short int* buf ) | ||
57 | { | ||
58 | return play_frame( (struct Vgm_Emu*) data, blip_time, sample_count, buf ); | ||
59 | } | ||
60 | |||
61 | void Vgm_init( struct Vgm_Emu* this ) | ||
62 | { | ||
63 | this->sample_rate = 0; | ||
64 | this->mute_mask_ = 0; | ||
65 | this->tempo = (int)(FP_ONE_TEMPO); | ||
66 | |||
67 | // defaults | ||
68 | this->tfilter = *track_get_setup( &this->track_filter ); | ||
69 | this->tfilter.max_initial = 2; | ||
70 | this->tfilter.lookahead = 1; | ||
71 | this->track_filter.silence_ignored_ = false; | ||
72 | |||
73 | // Disable oversampling by default | ||
74 | this->disable_oversampling = true; | ||
75 | this->psg_rate = 0; | ||
76 | |||
77 | Sms_apu_init( &this->psg ); | ||
78 | Synth_init( &this->pcm ); | ||
79 | |||
80 | Buffer_init( &this->stereo_buf ); | ||
81 | Blip_init( &this->blip_buf ); | ||
82 | |||
83 | // Init fm chips | ||
84 | Ym2413_init( &this->ym2413 ); | ||
85 | Ym2612_init( &this->ym2612 ); | ||
86 | |||
87 | // Init resampler | ||
88 | Resampler_init( &this->resampler ); | ||
89 | Resampler_set_callback( &this->resampler, play_frame_, this ); | ||
90 | |||
91 | // Set sound gain, a value too high | ||
92 | // will cause saturation | ||
93 | Sound_set_gain(this, (int)(FP_ONE_GAIN*0.7)); | ||
94 | |||
95 | // Unload | ||
96 | this->voice_count = 0; | ||
97 | this->voice_types = 0; | ||
98 | clear_track_vars( this ); | ||
99 | } | ||
100 | |||
101 | // Track info | ||
102 | |||
103 | static byte const* skip_gd3_str( byte const* in, byte const* end ) | ||
104 | { | ||
105 | while ( end - in >= 2 ) | ||
106 | { | ||
107 | in += 2; | ||
108 | if ( !(in [-2] | in [-1]) ) | ||
109 | break; | ||
110 | } | ||
111 | return in; | ||
112 | } | ||
113 | |||
114 | static byte const* get_gd3_str( byte const* in, byte const* end, char* field ) | ||
115 | { | ||
116 | byte const* mid = skip_gd3_str( in, end ); | ||
117 | int i, len = (mid - in) / 2 - 1; | ||
118 | if ( field && len > 0 ) | ||
119 | { | ||
120 | len = min( len, (int) gme_max_field ); | ||
121 | field [len] = 0; | ||
122 | for ( i = 0; i < len; i++ ) | ||
123 | field [i] = (in [i * 2 + 1] ? '?' : in [i * 2]); // TODO: convert to utf-8 | ||
124 | } | ||
125 | return mid; | ||
126 | } | ||
127 | |||
128 | static byte const* get_gd3_pair( byte const* in, byte const* end, char* field ) | ||
129 | { | ||
130 | return skip_gd3_str( get_gd3_str( in, end, field ), end ); | ||
131 | } | ||
132 | |||
133 | static void parse_gd3( byte const* in, byte const* end, struct track_info_t* out ) | ||
134 | { | ||
135 | in = get_gd3_pair( in, end, out->song ); | ||
136 | in = get_gd3_pair( in, end, out->game ); | ||
137 | in = get_gd3_pair( in, end, NULL ); // Skip system | ||
138 | in = get_gd3_pair( in, end, out->author ); | ||
139 | } | ||
140 | |||
141 | int const gd3_header_size = 12; | ||
142 | |||
143 | static int check_gd3_header( byte const* h, int remain ) | ||
144 | { | ||
145 | if ( remain < gd3_header_size ) return 0; | ||
146 | if ( memcmp( h, "Gd3 ", 4 ) ) return 0; | ||
147 | if ( get_le32( h + 4 ) >= 0x200 ) return 0; | ||
148 | |||
149 | int gd3_size = get_le32( h + 8 ); | ||
150 | if ( gd3_size > remain - gd3_header_size ) | ||
151 | gd3_size = remain - gd3_header_size; | ||
152 | return gd3_size; | ||
153 | } | ||
154 | |||
155 | static byte const* gd3_data( struct Vgm_Emu* this, int* size ) | ||
156 | { | ||
157 | if ( size ) | ||
158 | *size = 0; | ||
159 | |||
160 | int gd3_offset = get_le32( header( this )->gd3_offset ) - 0x2C; | ||
161 | if ( gd3_offset < 0 ) | ||
162 | return 0; | ||
163 | |||
164 | byte const* gd3 = this->file_begin + header_size + gd3_offset; | ||
165 | int gd3_size = check_gd3_header( gd3, this->file_end - gd3 ); | ||
166 | if ( !gd3_size ) | ||
167 | return 0; | ||
168 | |||
169 | if ( size ) | ||
170 | *size = gd3_size + gd3_header_size; | ||
171 | |||
172 | return gd3; | ||
173 | } | ||
174 | |||
175 | static void get_vgm_length( struct header_t const* h, struct track_info_t* out ) | ||
176 | { | ||
177 | int length = get_le32( h->track_duration ) * 10 / 441; | ||
178 | if ( length > 0 ) | ||
179 | { | ||
180 | int loop = get_le32( h->loop_duration ); | ||
181 | if ( loop > 0 && get_le32( h->loop_offset ) ) | ||
182 | { | ||
183 | out->loop_length = loop * 10 / 441; | ||
184 | out->intro_length = length - out->loop_length; | ||
185 | } | ||
186 | else | ||
187 | { | ||
188 | out->length = length; // 1000 / 44100 (VGM files used 44100 as timebase) | ||
189 | out->intro_length = length; // make it clear that track is no longer than length | ||
190 | out->loop_length = 0; | ||
191 | } | ||
192 | } | ||
193 | } | ||
194 | |||
195 | static blargg_err_t track_info( struct Vgm_Emu* this, struct track_info_t* out ) | ||
196 | { | ||
197 | memset(out, 0, sizeof *out); | ||
198 | get_vgm_length( header( this ), out ); | ||
199 | |||
200 | int size; | ||
201 | byte const* gd3 = gd3_data( this, &size ); | ||
202 | if ( gd3 ) | ||
203 | parse_gd3( gd3 + gd3_header_size, gd3 + size, out ); | ||
204 | |||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | static blargg_err_t check_vgm_header( struct header_t* h ) | ||
209 | { | ||
210 | if ( memcmp( h->tag, "Vgm ", 4 ) ) | ||
211 | return gme_wrong_file_type; | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | static void set_voice( struct Vgm_Emu* this, int i, struct Blip_Buffer* c, struct Blip_Buffer* l, struct Blip_Buffer* r ) | ||
216 | { | ||
217 | if ( i < sms_osc_count ) { | ||
218 | Sms_apu_set_output( &this->psg, i, c, l, r ); | ||
219 | } | ||
220 | } | ||
221 | |||
222 | blargg_err_t setup_fm( struct Vgm_Emu* this ); | ||
223 | blargg_err_t Vgm_load_mem( struct Vgm_Emu* this, byte const* new_data, long new_size, bool parse_info ) | ||
224 | { | ||
225 | // Unload | ||
226 | this->voice_count = 0; | ||
227 | clear_track_vars( this ); | ||
228 | |||
229 | // Clear info | ||
230 | memset( &this->info, 0, sizeof this->info ); | ||
231 | |||
232 | assert( offsetof (struct header_t,unused2 [8]) == header_size ); | ||
233 | |||
234 | if ( new_size <= header_size ) | ||
235 | return gme_wrong_file_type; | ||
236 | |||
237 | // Reset data pointers | ||
238 | this->file_begin = new_data; | ||
239 | this->file_end = new_data + new_size; | ||
240 | |||
241 | struct header_t* h = (struct header_t*) new_data; | ||
242 | RETURN_ERR( check_vgm_header( h ) ); | ||
243 | check( get_le32( h.version ) <= 0x150 ); | ||
244 | |||
245 | // If this was VGZ file gd3 parse info | ||
246 | if ( parse_info ) { | ||
247 | track_info( this, &this->info ); | ||
248 | |||
249 | // If file was trimmed add an | ||
250 | // incomplete token to the game tag | ||
251 | if ( get_le32( h->data_size ) > (unsigned) new_size ) { | ||
252 | *((char *) this->file_end) = cmd_end; | ||
253 | strcat(this->info.game, "(Trimmed VGZ file)" ); | ||
254 | } | ||
255 | } | ||
256 | |||
257 | // Get loop | ||
258 | this->loop_begin = this->file_end; | ||
259 | |||
260 | // If file was trimmed don't loop | ||
261 | if ( get_le32( h->loop_offset ) && get_le32( h->data_size ) <= (unsigned) new_size ) | ||
262 | this->loop_begin = &new_data [get_le32( h->loop_offset ) + offsetof (struct header_t,loop_offset)]; | ||
263 | |||
264 | // PSG rate | ||
265 | this->psg_rate = get_le32( h->psg_rate ); | ||
266 | if ( !this->psg_rate ) | ||
267 | this->psg_rate = 3579545; | ||
268 | |||
269 | Blip_set_clock_rate( &this->blip_buf, this->psg_rate ); | ||
270 | |||
271 | // Disable FM | ||
272 | this->fm_rate = 0; | ||
273 | Ym2612_enable( &this->ym2612, false ); | ||
274 | Ym2413_enable( &this->ym2413, false ); | ||
275 | |||
276 | this->voice_count = sms_osc_count; | ||
277 | static int const types [8] = { | ||
278 | wave_type+1, wave_type+2, wave_type+3, noise_type+1, | ||
279 | 0, 0, 0, 0 | ||
280 | }; | ||
281 | this->voice_types = types; | ||
282 | |||
283 | RETURN_ERR( setup_fm( this ) ); | ||
284 | |||
285 | // do after FM in case output buffer is changed | ||
286 | // setup buffer | ||
287 | this->clock_rate_ = this->psg_rate; | ||
288 | Buffer_clock_rate( &this->stereo_buf, this->psg_rate ); | ||
289 | RETURN_ERR( Buffer_set_channel_count( &this->stereo_buf, this->voice_count, this->voice_types ) ); | ||
290 | this->buf_changed_count = Buffer_channels_changed_count( &this->stereo_buf ); | ||
291 | |||
292 | // Post load | ||
293 | Sound_set_tempo( this, this->tempo ); | ||
294 | Sound_mute_voices( this, this->mute_mask_ ); | ||
295 | |||
296 | // so we can start playback | ||
297 | this->current_track = 0; | ||
298 | return 0; | ||
299 | } | ||
300 | |||
301 | void update_fm_rates( struct Vgm_Emu* this, int* ym2413_rate, int* ym2612_rate ); | ||
302 | static blargg_err_t init_fm( struct Vgm_Emu* this, int* rate ) | ||
303 | { | ||
304 | int ym2612_rate = get_le32( header( this )->ym2612_rate ); | ||
305 | int ym2413_rate = get_le32( header( this )->ym2413_rate ); | ||
306 | if ( ym2413_rate && get_le32( header( this )->version ) < 0x110 ) | ||
307 | update_fm_rates( this, &ym2413_rate, &ym2612_rate ); | ||
308 | |||
309 | if ( ym2612_rate ) | ||
310 | { | ||
311 | if ( !*rate ) | ||
312 | *rate = ym2612_rate / 144; | ||
313 | RETURN_ERR( Ym2612_set_rate( &this->ym2612, *rate, ym2612_rate ) ); | ||
314 | Ym2612_enable( &this->ym2612, true ); | ||
315 | } | ||
316 | else if ( ym2413_rate ) | ||
317 | { | ||
318 | if ( !*rate ) | ||
319 | *rate = ym2413_rate / 72; | ||
320 | int result = Ym2413_set_rate( &this->ym2413, *rate, ym2413_rate ); | ||
321 | if ( result == 2 ) | ||
322 | return "YM2413 FM sound not supported"; | ||
323 | CHECK_ALLOC( !result ); | ||
324 | Ym2413_enable( &this->ym2413, true ); | ||
325 | } | ||
326 | |||
327 | this->fm_rate = *rate; | ||
328 | |||
329 | return 0; | ||
330 | } | ||
331 | |||
332 | blargg_err_t setup_fm( struct Vgm_Emu* this ) | ||
333 | { | ||
334 | int fm_rate = 0; | ||
335 | if ( !this->disable_oversampling ) | ||
336 | fm_rate = (this->sample_rate * 3) / 2; // oversample factor = 1.5 | ||
337 | RETURN_ERR( init_fm( this, &fm_rate ) ); | ||
338 | |||
339 | if ( uses_fm( this ) ) | ||
340 | { | ||
341 | this->voice_count = 8; | ||
342 | RETURN_ERR( Resampler_setup( &this->resampler, fm_rate, fm_gain, this->sample_rate, this->gain ) ); | ||
343 | RETURN_ERR( Resampler_reset( &this->resampler, Blip_length( &this->blip_buf ) * this->sample_rate / 1000 ) ); | ||
344 | Sms_apu_volume( &this->psg, ((this->gain/5)-(this->gain*5)/1000) * fm_gain ); | ||
345 | } | ||
346 | else | ||
347 | { | ||
348 | Sms_apu_volume( &this->psg, this->gain ); | ||
349 | } | ||
350 | |||
351 | return 0; | ||
352 | } | ||
353 | |||
354 | // Emulation | ||
355 | |||
356 | blip_time_t run( struct Vgm_Emu* this, vgm_time_t end_time ); | ||
357 | static blip_time_t run_psg( struct Vgm_Emu* this, int msec ) | ||
358 | { | ||
359 | blip_time_t t = run( this, msec * this->vgm_rate / 1000 ); | ||
360 | Sms_apu_end_frame( &this->psg, t ); | ||
361 | return t; | ||
362 | } | ||
363 | |||
364 | static void check_end( struct Vgm_Emu* this ) | ||
365 | { | ||
366 | if( this->pos >= this->file_end ) | ||
367 | track_set_end( &this->track_filter ); | ||
368 | } | ||
369 | |||
370 | static blargg_err_t run_clocks( struct Vgm_Emu* this, blip_time_t* time_io, int msec ) | ||
371 | { | ||
372 | check_end( this ); | ||
373 | *time_io = run_psg( this, msec ); | ||
374 | return 0; | ||
375 | } | ||
376 | |||
377 | blargg_err_t play_( void *emu, int count, sample_t out [] ) | ||
378 | { | ||
379 | struct Vgm_Emu* this = (struct Vgm_Emu*) emu; | ||
380 | |||
381 | if ( !uses_fm( this ) ) { | ||
382 | int remain = count; | ||
383 | while ( remain ) | ||
384 | { | ||
385 | Buffer_disable_immediate_removal( &this->stereo_buf ); | ||
386 | remain -= Buffer_read_samples( &this->stereo_buf, &out [count - remain], remain ); | ||
387 | if ( remain ) | ||
388 | { | ||
389 | if ( this->buf_changed_count != Buffer_channels_changed_count( &this->stereo_buf ) ) | ||
390 | { | ||
391 | this->buf_changed_count = Buffer_channels_changed_count( &this->stereo_buf ); | ||
392 | |||
393 | // Remute voices | ||
394 | Sound_mute_voices( this, this->mute_mask_ ); | ||
395 | } | ||
396 | int msec = Buffer_length( &this->stereo_buf ); | ||
397 | blip_time_t clocks_emulated = msec * this->clock_rate_ / 1000 - 100; | ||
398 | RETURN_ERR( run_clocks( this, &clocks_emulated, msec ) ); | ||
399 | assert( clocks_emulated ); | ||
400 | Buffer_end_frame( &this->stereo_buf, clocks_emulated ); | ||
401 | } | ||
402 | } | ||
403 | |||
404 | return 0; | ||
405 | } | ||
406 | |||
407 | Resampler_play( &this->resampler, count, out, &this->blip_buf ); | ||
408 | return 0; | ||
409 | } | ||
410 | |||
411 | // Vgm_Emu_impl | ||
412 | |||
413 | static inline int command_len( int command ) | ||
414 | { | ||
415 | static byte const lens [0x10] = { | ||
416 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F | ||
417 | 1,1,1,2,2,3,1,1,1,1,3,3,4,4,5,5 | ||
418 | }; | ||
419 | int len = lens [command >> 4]; | ||
420 | check( len != 1 ); | ||
421 | return len; | ||
422 | } | ||
423 | |||
424 | static inline fm_time_t to_fm_time( struct Vgm_Emu* this, vgm_time_t t ) | ||
425 | { | ||
426 | return (t * this->fm_time_factor + this->fm_time_offset) >> fm_time_bits; | ||
427 | } | ||
428 | |||
429 | static inline blip_time_t to_psg_time( struct Vgm_Emu* this, vgm_time_t t ) | ||
430 | { | ||
431 | return (t * this->blip_time_factor) >> blip_time_bits; | ||
432 | } | ||
433 | |||
434 | static void write_pcm( struct Vgm_Emu* this, vgm_time_t vgm_time, int amp ) | ||
435 | { | ||
436 | check( amp >= 0 ); | ||
437 | blip_time_t blip_time = to_psg_time( this, vgm_time ); | ||
438 | int old = this->dac_amp; | ||
439 | int delta = amp - old; | ||
440 | this->dac_amp = amp; | ||
441 | Blip_set_modified( &this->blip_buf ); | ||
442 | if ( old >= 0 ) // first write is ignored, to avoid click | ||
443 | Synth_offset_inline( &this->pcm, blip_time, delta, &this->blip_buf ); | ||
444 | else | ||
445 | this->dac_amp |= this->dac_disabled; | ||
446 | } | ||
447 | |||
448 | blip_time_t run( struct Vgm_Emu* this, vgm_time_t end_time ) | ||
449 | { | ||
450 | vgm_time_t vgm_time = this->vgm_time; | ||
451 | byte const* pos = this->pos; | ||
452 | /* if ( pos > this->file_end ) | ||
453 | { | ||
454 | warning( "Stream lacked end event" ); | ||
455 | } */ | ||
456 | |||
457 | while ( vgm_time < end_time && pos < this->file_end ) | ||
458 | { | ||
459 | // TODO: be sure there are enough bytes left in stream for particular command | ||
460 | // so we don't read past end | ||
461 | switch ( *pos++ ) | ||
462 | { | ||
463 | case cmd_end: | ||
464 | pos = this->loop_begin; // if not looped, loop_begin == data_end | ||
465 | break; | ||
466 | |||
467 | case cmd_delay_735: | ||
468 | vgm_time += 735; | ||
469 | break; | ||
470 | |||
471 | case cmd_delay_882: | ||
472 | vgm_time += 882; | ||
473 | break; | ||
474 | |||
475 | case cmd_gg_stereo: | ||
476 | Sms_apu_write_ggstereo( &this->psg, to_psg_time( this, vgm_time ), *pos++ ); | ||
477 | break; | ||
478 | |||
479 | case cmd_psg: | ||
480 | Sms_apu_write_data( &this->psg, to_psg_time( this, vgm_time ), *pos++ ); | ||
481 | break; | ||
482 | |||
483 | case cmd_delay: | ||
484 | vgm_time += pos [1] * 0x100 + pos [0]; | ||
485 | pos += 2; | ||
486 | break; | ||
487 | |||
488 | case cmd_byte_delay: | ||
489 | vgm_time += *pos++; | ||
490 | break; | ||
491 | |||
492 | case cmd_ym2413: | ||
493 | if ( Ym2413_run_until( &this->ym2413, to_fm_time( this, vgm_time ) ) ) | ||
494 | Ym2413_write( &this->ym2413, pos [0], pos [1] ); | ||
495 | pos += 2; | ||
496 | break; | ||
497 | |||
498 | case cmd_ym2612_port0: | ||
499 | if ( pos [0] == ym2612_dac_port ) | ||
500 | { | ||
501 | write_pcm( this, vgm_time, pos [1] ); | ||
502 | } | ||
503 | else if ( Ym2612_run_until( &this->ym2612, to_fm_time( this, vgm_time ) ) ) | ||
504 | { | ||
505 | if ( pos [0] == 0x2B ) | ||
506 | { | ||
507 | this->dac_disabled = (pos [1] >> 7 & 1) - 1; | ||
508 | this->dac_amp |= this->dac_disabled; | ||
509 | } | ||
510 | Ym2612_write0( &this->ym2612, pos [0], pos [1] ); | ||
511 | } | ||
512 | pos += 2; | ||
513 | break; | ||
514 | |||
515 | case cmd_ym2612_port1: | ||
516 | if ( Ym2612_run_until( &this->ym2612, to_fm_time( this, vgm_time ) ) ) | ||
517 | Ym2612_write1( &this->ym2612, pos [0], pos [1] ); | ||
518 | pos += 2; | ||
519 | break; | ||
520 | |||
521 | case cmd_data_block: { | ||
522 | check( *pos == cmd_end ); | ||
523 | int type = pos [1]; | ||
524 | int size = get_le32( pos + 2 ); | ||
525 | pos += 6; | ||
526 | if ( type == pcm_block_type ) | ||
527 | this->pcm_data = pos; | ||
528 | pos += size; | ||
529 | break; | ||
530 | } | ||
531 | |||
532 | case cmd_pcm_seek: | ||
533 | this->pcm_pos = this->pcm_data + pos [3] * 0x1000000 + pos [2] * 0x10000 + | ||
534 | pos [1] * 0x100 + pos [0]; | ||
535 | pos += 4; | ||
536 | break; | ||
537 | |||
538 | default: { | ||
539 | int cmd = pos [-1]; | ||
540 | switch ( cmd & 0xF0 ) | ||
541 | { | ||
542 | case cmd_pcm_delay: | ||
543 | write_pcm( this, vgm_time, *this->pcm_pos++ ); | ||
544 | vgm_time += cmd & 0x0F; | ||
545 | break; | ||
546 | |||
547 | case cmd_short_delay: | ||
548 | vgm_time += (cmd & 0x0F) + 1; | ||
549 | break; | ||
550 | |||
551 | case 0x50: | ||
552 | pos += 2; | ||
553 | break; | ||
554 | |||
555 | default: | ||
556 | pos += command_len( cmd ) - 1; | ||
557 | /* warning( "Unknown stream event" ); */ | ||
558 | } | ||
559 | } | ||
560 | } | ||
561 | } | ||
562 | vgm_time -= end_time; | ||
563 | this->pos = pos; | ||
564 | this->vgm_time = vgm_time; | ||
565 | |||
566 | return to_psg_time( this, end_time ); | ||
567 | } | ||
568 | |||
569 | int play_frame( struct Vgm_Emu* this, blip_time_t blip_time, int sample_count, blip_sample_t out [] ) | ||
570 | { | ||
571 | check_end( this); | ||
572 | |||
573 | // to do: timing is working mostly by luck | ||
574 | int min_pairs = (unsigned) sample_count / 2; | ||
575 | int vgm_time = (min_pairs << fm_time_bits) / this->fm_time_factor - 1; | ||
576 | assert( to_fm_time( this, vgm_time ) <= min_pairs ); | ||
577 | int pairs; | ||
578 | while ( (pairs = to_fm_time( this, vgm_time )) < min_pairs ) | ||
579 | vgm_time++; | ||
580 | //debug_printf( "pairs: %d, min_pairs: %d\n", pairs, min_pairs ); | ||
581 | |||
582 | if ( Ym2612_enabled( &this->ym2612 ) ) | ||
583 | { | ||
584 | Ym2612_begin_frame( &this->ym2612, out ); | ||
585 | memset( out, 0, pairs * stereo * sizeof *out ); | ||
586 | } | ||
587 | else if ( Ym2413_enabled( &this->ym2413 ) ) | ||
588 | { | ||
589 | Ym2413_begin_frame( &this->ym2413, out ); | ||
590 | } | ||
591 | |||
592 | run( this, vgm_time ); | ||
593 | Ym2612_run_until( &this->ym2612, pairs ); | ||
594 | Ym2413_run_until( &this->ym2413, pairs ); | ||
595 | |||
596 | this->fm_time_offset = (vgm_time * this->fm_time_factor + this->fm_time_offset) - (pairs << fm_time_bits); | ||
597 | |||
598 | Sms_apu_end_frame( &this->psg, blip_time ); | ||
599 | |||
600 | return pairs * stereo; | ||
601 | } | ||
602 | |||
603 | // Update pre-1.10 header FM rates by scanning commands | ||
604 | void update_fm_rates( struct Vgm_Emu* this, int* ym2413_rate, int* ym2612_rate ) | ||
605 | { | ||
606 | byte const* p = this->file_begin + 0x40; | ||
607 | while ( p < this->file_end ) | ||
608 | { | ||
609 | switch ( *p ) | ||
610 | { | ||
611 | case cmd_end: | ||
612 | return; | ||
613 | |||
614 | case cmd_psg: | ||
615 | case cmd_byte_delay: | ||
616 | p += 2; | ||
617 | break; | ||
618 | |||
619 | case cmd_delay: | ||
620 | p += 3; | ||
621 | break; | ||
622 | |||
623 | case cmd_data_block: | ||
624 | p += 7 + get_le32( p + 3 ); | ||
625 | break; | ||
626 | |||
627 | case cmd_ym2413: | ||
628 | *ym2612_rate = 0; | ||
629 | return; | ||
630 | |||
631 | case cmd_ym2612_port0: | ||
632 | case cmd_ym2612_port1: | ||
633 | *ym2612_rate = *ym2413_rate; | ||
634 | *ym2413_rate = 0; | ||
635 | return; | ||
636 | |||
637 | case cmd_ym2151: | ||
638 | *ym2413_rate = 0; | ||
639 | *ym2612_rate = 0; | ||
640 | return; | ||
641 | |||
642 | default: | ||
643 | p += command_len( *p ); | ||
644 | } | ||
645 | } | ||
646 | } | ||
647 | |||
648 | |||
649 | // Music Emu | ||
650 | |||
651 | blargg_err_t Vgm_set_sample_rate( struct Vgm_Emu* this, int rate ) | ||
652 | { | ||
653 | require( !this->sample_rate ); // sample rate can't be changed once set | ||
654 | RETURN_ERR( Blip_set_sample_rate( &this->blip_buf, rate, 1000 / 30 ) ); | ||
655 | RETURN_ERR( Buffer_set_sample_rate( &this->stereo_buf, rate, 1000 / 20 ) ); | ||
656 | |||
657 | // Set bass frequency | ||
658 | Buffer_bass_freq( &this->stereo_buf, 80 ); | ||
659 | |||
660 | this->sample_rate = rate; | ||
661 | RETURN_ERR( track_init( &this->track_filter, this ) ); | ||
662 | this->tfilter.max_silence = 6 * stereo * this->sample_rate; | ||
663 | return 0; | ||
664 | } | ||
665 | |||
666 | void Sound_mute_voice( struct Vgm_Emu* this, int index, bool mute ) | ||
667 | { | ||
668 | require( (unsigned) index < (unsigned) this->voice_count ); | ||
669 | int bit = 1 << index; | ||
670 | int mask = this->mute_mask_ | bit; | ||
671 | if ( !mute ) | ||
672 | mask ^= bit; | ||
673 | Sound_mute_voices( this, mask ); | ||
674 | } | ||
675 | |||
676 | void Sound_mute_voices( struct Vgm_Emu* this, int mask ) | ||
677 | { | ||
678 | require( this->sample_rate ); // sample rate must be set first | ||
679 | this->mute_mask_ = mask; | ||
680 | |||
681 | int i; | ||
682 | for ( i = this->voice_count; i--; ) | ||
683 | { | ||
684 | if ( mask & (1 << i) ) | ||
685 | { | ||
686 | set_voice( this, i, 0, 0, 0 ); | ||
687 | } | ||
688 | else | ||
689 | { | ||
690 | struct channel_t ch = Buffer_channel( &this->stereo_buf, i ); | ||
691 | assert( (ch.center && ch.left && ch.right) || | ||
692 | (!ch.center && !ch.left && !ch.right) ); // all or nothing | ||
693 | set_voice( this, i, ch.center, ch.left, ch.right ); | ||
694 | } | ||
695 | } | ||
696 | |||
697 | // TODO: what was this for? | ||
698 | //core.pcm.output( &core.blip_buf ); | ||
699 | |||
700 | // TODO: silence PCM if FM isn't used? | ||
701 | if ( uses_fm( this ) ) | ||
702 | { | ||
703 | for ( i = sms_osc_count; --i >= 0; ) | ||
704 | Sms_apu_set_output( &this->psg, i, ( mask & 0x80 ) ? 0 : &this->blip_buf, NULL, NULL ); | ||
705 | if ( Ym2612_enabled( &this->ym2612 ) ) | ||
706 | { | ||
707 | Synth_volume( &this->pcm, (mask & 0x40) ? 0 : (int)((long long)(0.1115*FP_ONE_VOLUME) / 256 * fm_gain * this->gain / FP_ONE_VOLUME) ); | ||
708 | Ym2612_mute_voices( &this->ym2612, mask ); | ||
709 | } | ||
710 | |||
711 | if ( Ym2413_enabled( &this->ym2413 ) ) | ||
712 | { | ||
713 | int m = mask & 0x3F; | ||
714 | if ( mask & 0x20 ) | ||
715 | m |= 0x01E0; // channels 5-8 | ||
716 | if ( mask & 0x40 ) | ||
717 | m |= 0x3E00; | ||
718 | Ym2413_mute_voices( &this->ym2413, m ); | ||
719 | } | ||
720 | } | ||
721 | } | ||
722 | |||
723 | void Sound_set_tempo( struct Vgm_Emu* this, int t ) | ||
724 | { | ||
725 | require( this->sample_rate ); // sample rate must be set first | ||
726 | int const min = (int)(FP_ONE_TEMPO*0.02); | ||
727 | int const max = (int)(FP_ONE_TEMPO*4.00); | ||
728 | if ( t < min ) t = min; | ||
729 | if ( t > max ) t = max; | ||
730 | this->tempo = t; | ||
731 | |||
732 | if ( this->file_begin ) | ||
733 | { | ||
734 | this->vgm_rate = (long) ((44100LL * t) / FP_ONE_TEMPO); | ||
735 | this->blip_time_factor = (int) (((1LL << blip_time_bits) * Blip_clock_rate( &this->blip_buf )) / this->vgm_rate); | ||
736 | //debug_printf( "blip_time_factor: %ld\n", blip_time_factor ); | ||
737 | //debug_printf( "vgm_rate: %ld\n", vgm_rate ); | ||
738 | // TODO: remove? calculates vgm_rate more accurately (above differs at most by one Hz only) | ||
739 | //blip_time_factor = (long) floor( double (1L << blip_time_bits) * psg_rate / 44100 / t + 0.5 ); | ||
740 | //vgm_rate = (long) floor( double (1L << blip_time_bits) * psg_rate / blip_time_factor + 0.5 ); | ||
741 | |||
742 | this->fm_time_factor = 2 + (int) ((this->fm_rate * (1LL << fm_time_bits)) / this->vgm_rate); | ||
743 | } | ||
744 | } | ||
745 | |||
746 | blargg_err_t Vgm_start_track( struct Vgm_Emu* this ) | ||
747 | { | ||
748 | clear_track_vars( this ); | ||
749 | |||
750 | Sms_apu_reset( &this->psg, get_le16( header( this )->noise_feedback ), header( this )->noise_width ); | ||
751 | |||
752 | this->dac_disabled = -1; | ||
753 | this->pos = this->file_begin + header_size; | ||
754 | this->pcm_data = this->pos; | ||
755 | this->pcm_pos = this->pos; | ||
756 | this->dac_amp = -1; | ||
757 | this->vgm_time = 0; | ||
758 | if ( get_le32( header( this )->version ) >= 0x150 ) | ||
759 | { | ||
760 | int data_offset = get_le32( header( this )->data_offset ); | ||
761 | check( data_offset ); | ||
762 | if ( data_offset ) | ||
763 | this->pos += data_offset + offsetof (struct header_t,data_offset) - 0x40; | ||
764 | } | ||
765 | |||
766 | if ( uses_fm( this ) ) | ||
767 | { | ||
768 | if ( Ym2413_enabled( &this->ym2413 ) ) | ||
769 | Ym2413_reset( &this->ym2413 ); | ||
770 | |||
771 | if ( Ym2612_enabled( &this->ym2612 ) ) | ||
772 | Ym2612_reset( &this->ym2612 ); | ||
773 | |||
774 | Blip_clear( &this->blip_buf ); | ||
775 | Resampler_clear( &this->resampler ); | ||
776 | } | ||
777 | |||
778 | this->fm_time_offset = 0; | ||
779 | |||
780 | Buffer_clear( &this->stereo_buf ); | ||
781 | |||
782 | // convert filter times to samples | ||
783 | struct setup_t s = this->tfilter; | ||
784 | s.max_initial *= this->sample_rate * stereo; | ||
785 | #ifdef GME_DISABLE_SILENCE_LOOKAHEAD | ||
786 | s.lookahead = 1; | ||
787 | #endif | ||
788 | track_setup( &this->track_filter, &s ); | ||
789 | |||
790 | return track_start( &this->track_filter ); | ||
791 | } | ||
792 | |||
793 | // Tell/Seek | ||
794 | |||
795 | static int msec_to_samples( int msec, int sample_rate ) | ||
796 | { | ||
797 | int sec = msec / 1000; | ||
798 | msec -= sec * 1000; | ||
799 | return (sec * sample_rate + msec * sample_rate / 1000) * stereo; | ||
800 | } | ||
801 | |||
802 | int Track_tell( struct Vgm_Emu* this ) | ||
803 | { | ||
804 | int rate = this->sample_rate * stereo; | ||
805 | int sec = track_sample_count( &this->track_filter ) / rate; | ||
806 | return sec * 1000 + (track_sample_count( &this->track_filter ) - sec * rate) * 1000 / rate; | ||
807 | } | ||
808 | |||
809 | blargg_err_t Track_seek( struct Vgm_Emu* this, int msec ) | ||
810 | { | ||
811 | int time = msec_to_samples( msec, this->sample_rate ); | ||
812 | if ( time < track_sample_count( &this->track_filter ) ) | ||
813 | RETURN_ERR( Vgm_start_track( this ) ); | ||
814 | return Track_skip( this, time - track_sample_count( &this->track_filter ) ); | ||
815 | } | ||
816 | |||
817 | blargg_err_t Track_skip( struct Vgm_Emu* this, int count ) | ||
818 | { | ||
819 | require( this->current_track >= 0 ); // start_track() must have been called already | ||
820 | return track_skip( &this->track_filter, count ); | ||
821 | } | ||
822 | |||
823 | blargg_err_t skip_( void* emu, int count ) | ||
824 | { | ||
825 | struct Vgm_Emu* this = (struct Vgm_Emu*) emu; | ||
826 | |||
827 | // for long skip, mute sound | ||
828 | const int threshold = 32768; | ||
829 | if ( count > threshold ) | ||
830 | { | ||
831 | int saved_mute = this->mute_mask_; | ||
832 | Sound_mute_voices( this, ~0 ); | ||
833 | |||
834 | int n = count - threshold/2; | ||
835 | n &= ~(2048-1); // round to multiple of 2048 | ||
836 | count -= n; | ||
837 | RETURN_ERR( skippy_( &this->track_filter, n ) ); | ||
838 | |||
839 | Sound_mute_voices( this, saved_mute ); | ||
840 | } | ||
841 | |||
842 | return skippy_( &this->track_filter, count ); | ||
843 | } | ||
844 | |||
845 | // Fading | ||
846 | |||
847 | void Track_set_fade( struct Vgm_Emu* this, int start_msec, int length_msec ) | ||
848 | { | ||
849 | track_set_fade( &this->track_filter, msec_to_samples( start_msec, this->sample_rate ), | ||
850 | length_msec * this->sample_rate / (1000 / stereo) ); | ||
851 | } | ||
852 | |||
853 | blargg_err_t Vgm_play( struct Vgm_Emu* this, int out_count, sample_t* out ) | ||
854 | { | ||
855 | require( this->current_track >= 0 ); | ||
856 | require( out_count % stereo == 0 ); | ||
857 | return track_play( &this->track_filter, out_count, out ); | ||
858 | } | ||