summaryrefslogtreecommitdiff
path: root/apps/codecs/libgme/vgm_emu.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libgme/vgm_emu.c')
-rw-r--r--apps/codecs/libgme/vgm_emu.c1053
1 files changed, 1053 insertions, 0 deletions
diff --git a/apps/codecs/libgme/vgm_emu.c b/apps/codecs/libgme/vgm_emu.c
new file mode 100644
index 0000000000..7fed4ef6d1
--- /dev/null
+++ b/apps/codecs/libgme/vgm_emu.c
@@ -0,0 +1,1053 @@
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
10can redistribute it and/or modify it under the terms of the GNU Lesser
11General Public License as published by the Free Software Foundation; either
12version 2.1 of the License, or (at your option) any later version. This
13module is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16details. You should have received a copy of the GNU Lesser General Public
17License along with this module; if not, write to the Free Software Foundation,
18Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
19
20#include "blargg_source.h"
21
22const char* const gme_wrong_file_type = "Wrong file type for this emulator";
23
24double const fm_gain = 3.0; // FM emulators are internally quieter to avoid 16-bit overflow
25double const rolloff = 0.990;
26double const oversample_factor = 1.5;
27
28int const silence_max = 6; // seconds
29int const silence_threshold = 0x10;
30long const fade_block_size = 512;
31int const fade_shift = 8; // fade ends with gain at 1.0 / (1 << fade_shift)
32
33// VGM commands (Spec v1.50)
34enum {
35 cmd_gg_stereo = 0x4F,
36 cmd_psg = 0x50,
37 cmd_ym2413 = 0x51,
38 cmd_ym2612_port0 = 0x52,
39 cmd_ym2612_port1 = 0x53,
40 cmd_ym2151 = 0x54,
41 cmd_delay = 0x61,
42 cmd_delay_735 = 0x62,
43 cmd_delay_882 = 0x63,
44 cmd_byte_delay = 0x64,
45 cmd_end = 0x66,
46 cmd_data_block = 0x67,
47 cmd_short_delay = 0x70,
48 cmd_pcm_delay = 0x80,
49 cmd_pcm_seek = 0xE0,
50
51 pcm_block_type = 0x00,
52 ym2612_dac_port = 0x2A,
53 ym2612_dac_pan_port = 0xB6
54};
55
56void clear_track_vars( struct Vgm_Emu* this )
57{
58 this->out_time = 0;
59 this->emu_time = 0;
60 this->emu_track_ended_ = true;
61 this->track_ended = true;
62 this->fade_start = INT_MAX / 2 + 1;
63 this->fade_step = 1;
64 this->silence_time = 0;
65 this->silence_count = 0;
66 this->buf_remain = 0;
67}
68
69int play_frame( struct Vgm_Emu* this, blip_time_t blip_time, int sample_count, sample_t* buf );
70static int play_frame_( void* data, blip_time_t blip_time, int sample_count, short int* buf )
71{
72 return play_frame( (struct Vgm_Emu*) data, blip_time, sample_count, buf );
73}
74
75void Vgm_init( struct Vgm_Emu* this )
76{
77 this->sample_rate = 0;
78 this->mute_mask_ = 0;
79 this->tempo = 1.0;
80
81 // defaults
82 this->max_initial_silence = 2;
83 this->silence_lookahead = 1; // tracks should already be trimmed
84 this->ignore_silence = false;
85
86 // Disable oversampling by default
87 this->disable_oversampling = true;
88 this->psg_rate = 0;
89
90 Sms_apu_init( &this->psg );
91 Synth_init( &this->pcm );
92
93 Buffer_init( &this->buf );
94 Buffer_init( &this->stereo_buf );
95 this->blip_buf = &this->stereo_buf.bufs [0];
96
97 // Init fm chips
98 Ym2413_init( &this->ym2413 );
99 Ym2612_init( &this->ym2612 );
100
101 // Init resampler
102 Resampler_init( &this->resampler );
103 Resampler_set_callback( &this->resampler, play_frame_, this );
104
105 // Set sound gain, a value too high
106 // will cause saturation
107 Sound_set_gain(this, 1.0);
108
109 // Unload
110 this->voice_count = 0;
111 clear_track_vars( this );
112}
113
114// Track info
115
116static byte const* skip_gd3_str( byte const* in, byte const* end )
117{
118 while ( end - in >= 2 )
119 {
120 in += 2;
121 if ( !(in [-2] | in [-1]) )
122 break;
123 }
124 return in;
125}
126
127static byte const* get_gd3_str( byte const* in, byte const* end, char* field )
128{
129 byte const* mid = skip_gd3_str( in, end );
130 int i, len = (mid - in) / 2 - 1;
131 if ( field && len > 0 )
132 {
133 len = min( len, (int) gme_max_field );
134 field [len] = 0;
135 for ( i = 0; i < len; i++ )
136 field [i] = (in [i * 2 + 1] ? '?' : in [i * 2]); // TODO: convert to utf-8
137 }
138 return mid;
139}
140
141static byte const* get_gd3_pair( byte const* in, byte const* end, char* field )
142{
143 return skip_gd3_str( get_gd3_str( in, end, field ), end );
144}
145
146static void parse_gd3( byte const* in, byte const* end, struct track_info_t* out )
147{
148 in = get_gd3_pair( in, end, out->song );
149 in = get_gd3_pair( in, end, out->game );
150 in = get_gd3_pair( in, end, NULL ); // Skip system
151 in = get_gd3_pair( in, end, out->author );
152}
153
154int const gd3_header_size = 12;
155
156static long check_gd3_header( byte const* h, long remain )
157{
158 if ( remain < gd3_header_size ) return 0;
159 if ( memcmp( h, "Gd3 ", 4 ) ) return 0;
160 if ( get_le32( h + 4 ) >= 0x200 ) return 0;
161
162 long gd3_size = get_le32( h + 8 );
163 if ( gd3_size > remain - gd3_header_size )
164 gd3_size = remain - gd3_header_size;
165 return gd3_size;
166}
167
168byte const* gd3_data( struct Vgm_Emu* this, int* size )
169{
170 if ( size )
171 *size = 0;
172
173 long gd3_offset = get_le32( header( this )->gd3_offset ) - 0x2C;
174 if ( gd3_offset < 0 )
175 return 0;
176
177 byte const* gd3 = this->file_begin + header_size + gd3_offset;
178 long gd3_size = check_gd3_header( gd3, this->file_end - gd3 );
179 if ( !gd3_size )
180 return 0;
181
182 if ( size )
183 *size = gd3_size + gd3_header_size;
184
185 return gd3;
186}
187
188static void get_vgm_length( struct header_t const* h, struct track_info_t* out )
189{
190 long length = get_le32( h->track_duration ) * 10 / 441;
191 if ( length > 0 )
192 {
193 long loop = get_le32( h->loop_duration );
194 if ( loop > 0 && get_le32( h->loop_offset ) )
195 {
196 out->loop_length = loop * 10 / 441;
197 out->intro_length = length - out->loop_length;
198 }
199 else
200 {
201 out->length = length; // 1000 / 44100 (VGM files used 44100 as timebase)
202 out->intro_length = length; // make it clear that track is no longer than length
203 out->loop_length = 0;
204 }
205 }
206}
207
208blargg_err_t track_info( struct Vgm_Emu* this, struct track_info_t* out )
209{
210 memset(out, 0, sizeof out);
211 get_vgm_length( header( this ), out );
212
213 int size;
214 byte const* gd3 = gd3_data( this, &size );
215 if ( gd3 )
216 parse_gd3( gd3 + gd3_header_size, gd3 + size, out );
217
218 return 0;
219}
220
221static blargg_err_t check_vgm_header( struct header_t* h )
222{
223 if ( memcmp( h->tag, "Vgm ", 4 ) )
224 return gme_wrong_file_type;
225 return 0;
226}
227
228void set_voice( struct Vgm_Emu* this, int i, struct Blip_Buffer* c, struct Blip_Buffer* l, struct Blip_Buffer* r )
229{
230 if ( i < sms_osc_count ) {
231 int j;
232 for ( j = sms_osc_count; --j >= 0; )
233 Sms_apu_set_output( &this->psg, j, c, l, r );
234 }
235}
236
237blargg_err_t setup_fm( struct Vgm_Emu* this );
238blargg_err_t Vgm_load_mem( struct Vgm_Emu* this, byte const* new_data, long new_size, bool parse_info )
239{
240 // Unload
241 this->voice_count = 0;
242 clear_track_vars( this );
243
244 // Clear info
245 memset( &this->info, 0, sizeof this->info );
246
247 assert( offsetof (struct header_t,unused2 [8]) == header_size );
248
249 if ( new_size <= header_size )
250 return gme_wrong_file_type;
251
252 // Reset data pointers
253 this->file_begin = new_data;
254 this->file_end = new_data + new_size;
255
256 struct header_t* h = (struct header_t*) new_data;
257 RETURN_ERR( check_vgm_header( h ) );
258 check( get_le32( h.version ) <= 0x150 );
259
260 // If this was VGZ file gd3 parse info
261 if ( parse_info ) {
262 track_info( this, &this->info );
263
264 // If file was trimmed add an
265 // incomplete token to the game tag
266 if ( get_le32( h->data_size ) > (unsigned) new_size ) {
267 *((char *) this->file_end) = cmd_end;
268 strcat(this->info.game, "(Trimmed VGZ file)" );
269 }
270 }
271
272 // Get loop
273 this->loop_begin = this->file_end;
274
275 // If file was trimmed don't loop
276 if ( get_le32( h->loop_offset ) && get_le32( h->data_size ) <= (unsigned) new_size )
277 this->loop_begin = &new_data [get_le32( h->loop_offset ) + offsetof (struct header_t,loop_offset)];
278
279 // PSG rate
280 this->psg_rate = get_le32( h->psg_rate );
281 if ( !this->psg_rate )
282 this->psg_rate = 3579545;
283
284 Buffer_clock_rate( &this->stereo_buf, this->psg_rate );
285
286 // Disable FM
287 this->fm_rate = 0;
288 Ym2612_enable( &this->ym2612, false );
289 Ym2413_enable( &this->ym2413, false );
290
291 Sound_set_tempo( this, 1 );
292
293 this->voice_count = sms_osc_count;
294
295 RETURN_ERR( setup_fm( this ) );
296
297 // do after FM in case output buffer is changed
298 // setup buffer
299 this->clock_rate_ = this->psg_rate;
300 Buffer_clock_rate( &this->buf, this->psg_rate );
301
302 // Setup bass
303 this->buf_changed_count = Buffer_channels_changed_count( &this->buf );
304
305 // Post load
306 Sound_set_tempo( this, this->tempo );
307 Sound_mute_voices( this, this->mute_mask_ );
308
309 return 0;
310}
311
312void update_fm_rates( struct Vgm_Emu* this, int* ym2413_rate, int* ym2612_rate );
313blargg_err_t init_fm( struct Vgm_Emu* this, double* rate )
314{
315 int ym2612_rate = get_le32( header( this )->ym2612_rate );
316 int ym2413_rate = get_le32( header( this )->ym2413_rate );
317 if ( ym2413_rate && get_le32( header( this )->version ) < 0x110 )
318 update_fm_rates( this, &ym2413_rate, &ym2612_rate );
319
320 if ( ym2612_rate )
321 {
322 if ( !*rate )
323 *rate = ym2612_rate / 144.0;
324 RETURN_ERR( Ym2612_set_rate( &this->ym2612, *rate, ym2612_rate ) );
325 Ym2612_enable( &this->ym2612, true );
326 }
327 else if ( ym2413_rate )
328 {
329 if ( !*rate )
330 *rate = ym2413_rate / 72.0;
331 int result = Ym2413_set_rate( &this->ym2413, *rate, ym2413_rate );
332 if ( result == 2 )
333 return "YM2413 FM sound not supported";
334 CHECK_ALLOC( !result );
335 Ym2413_enable( &this->ym2413, true );
336 }
337
338 this->fm_rate = *rate;
339
340 return 0;
341}
342
343blargg_err_t setup_fm( struct Vgm_Emu* this )
344{
345 double fm_rate = 0.0;
346 if ( !this->disable_oversampling )
347 this->fm_rate = this->sample_rate * oversample_factor;
348 RETURN_ERR( init_fm( this, &fm_rate ) );
349
350 if ( uses_fm( this ) )
351 {
352 this->voice_count = 8;
353 RETURN_ERR( Resampler_setup( &this->resampler, fm_rate / this->sample_rate, rolloff, fm_gain * this->gain ) );
354 RETURN_ERR( Resampler_reset( &this->resampler, Buffer_length( &this->stereo_buf ) * this->sample_rate / 1000 ) );
355 Sms_apu_volume( &this->psg, 0.195 * fm_gain * this->gain );
356 }
357 else
358 {
359 Sms_apu_volume( &this->psg, this->gain );
360 }
361
362 return 0;
363}
364
365// Emulation
366
367blip_time_t run( struct Vgm_Emu* this, vgm_time_t end_time );
368blargg_err_t run_clocks( struct Vgm_Emu* this, blip_time_t* time_io, int msec )
369{
370 *time_io = run( this, msec * this->vgm_rate / 1000 );
371 Sms_apu_end_frame( &this->psg, *time_io );
372 return 0;
373}
374
375
376
377blargg_err_t play_( struct Vgm_Emu* this, long count, sample_t* out )
378{
379 if ( !uses_fm( this ) ) {
380 long remain = count;
381 while ( remain )
382 {
383 remain -= Buffer_read_samples( &this->buf, &out [count - remain], remain );
384 if ( remain )
385 {
386 if ( this->buf_changed_count != Buffer_channels_changed_count( &this->buf ) )
387 {
388 this->buf_changed_count = Buffer_channels_changed_count( &this->buf );
389
390 // Remute voices
391 Sound_mute_voices( this, this->mute_mask_ );
392 }
393 int msec = Buffer_length( &this->buf );
394 blip_time_t clocks_emulated = (blargg_long) msec * this->clock_rate_ / 1000 - 100;
395 RETURN_ERR( run_clocks( this, &clocks_emulated, msec ) );
396 assert( clocks_emulated );
397 Buffer_end_frame( &this->buf, clocks_emulated );
398 }
399 }
400
401 return 0;
402 }
403
404 Resampler_play( &this->resampler, count, out, &this->stereo_buf );
405 return 0;
406}
407
408// Vgm_Emu_impl
409
410inline int command_len( int command )
411{
412 static byte const lens [0x10] ICONST_ATTR = {
413 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
414 1,1,1,2,2,3,1,1,1,1,3,3,4,4,5,5
415 };
416 int len = lens [command >> 4];
417 check( len != 1 );
418 return len;
419}
420
421inline fm_time_t to_fm_time( struct Vgm_Emu* this, vgm_time_t t )
422{
423 return (t * this->fm_time_factor + this->fm_time_offset) >> fm_time_bits;
424}
425
426inline blip_time_t to_psg_time( struct Vgm_Emu* this, vgm_time_t t )
427{
428 return (t * this->blip_time_factor) >> blip_time_bits;
429}
430
431void write_pcm( struct Vgm_Emu* this, vgm_time_t vgm_time, int amp )
432{
433 if ( this->blip_buf )
434 {
435 check( amp >= 0 );
436 blip_time_t blip_time = to_psg_time( this, vgm_time );
437 int old = this->dac_amp;
438 int delta = amp - old;
439 this->dac_amp = amp;
440 Blip_set_modified( this->blip_buf );
441 if ( old >= 0 ) // first write is ignored, to avoid click
442 Synth_offset_inline( &this->pcm, blip_time, delta, this->blip_buf );
443 else
444 this->dac_amp |= this->dac_disabled;
445 }
446}
447
448blip_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 this->emu_track_ended_ = true;
455 /* if ( pos > data_end )
456 warning( "Stream lacked end event" ); */
457 }
458
459 while ( vgm_time < end_time && pos < this->file_end )
460 {
461 // TODO: be sure there are enough bytes left in stream for particular command
462 // so we don't read past end
463 switch ( *pos++ )
464 {
465 case cmd_end:
466 pos = this->loop_begin; // if not looped, loop_begin == data_end
467 break;
468
469 case cmd_delay_735:
470 vgm_time += 735;
471 break;
472
473 case cmd_delay_882:
474 vgm_time += 882;
475 break;
476
477 case cmd_gg_stereo:
478 Sms_apu_write_ggstereo( &this->psg, to_psg_time( this, vgm_time ), *pos++ );
479 break;
480
481 case cmd_psg:
482 Sms_apu_write_data( &this->psg, to_psg_time( this, vgm_time ), *pos++ );
483 break;
484
485 case cmd_delay:
486 vgm_time += pos [1] * 0x100 + pos [0];
487 pos += 2;
488 break;
489
490 case cmd_byte_delay:
491 vgm_time += *pos++;
492 break;
493
494 case cmd_ym2413:
495 if ( Ym2413_run_until( &this->ym2413, to_fm_time( this, vgm_time ) ) )
496 Ym2413_write( &this->ym2413, pos [0], pos [1] );
497 pos += 2;
498 break;
499
500 case cmd_ym2612_port0:
501 if ( pos [0] == ym2612_dac_port )
502 {
503 write_pcm( this, vgm_time, pos [1] );
504 }
505 else if ( Ym2612_run_until( &this->ym2612, to_fm_time( this, vgm_time ) ) )
506 {
507 if ( pos [0] == 0x2B )
508 {
509 this->dac_disabled = (pos [1] >> 7 & 1) - 1;
510 this->dac_amp |= this->dac_disabled;
511 }
512 Ym2612_write0( &this->ym2612, pos [0], pos [1] );
513 }
514 pos += 2;
515 break;
516
517 case cmd_ym2612_port1:
518 if ( Ym2612_run_until( &this->ym2612, to_fm_time( this, vgm_time ) ) )
519 {
520 if ( pos [0] == ym2612_dac_pan_port )
521 {
522 struct Blip_Buffer* blip_buf = NULL;
523 switch ( pos [1] >> 6 )
524 {
525 case 0: blip_buf = NULL; break;
526 case 1: blip_buf = &this->stereo_buf.bufs [2]; break;
527 case 2: blip_buf = &this->stereo_buf.bufs [1]; break;
528 case 3: blip_buf = &this->stereo_buf.bufs [0]; break;
529 }
530 this->blip_buf = blip_buf;
531 }
532
533 Ym2612_write1( &this->ym2612, pos [0], pos [1] );
534 }
535 pos += 2;
536 break;
537
538 case cmd_data_block: {
539 check( *pos == cmd_end );
540 int type = pos [1];
541 long size = get_le32( pos + 2 );
542 pos += 6;
543 if ( type == pcm_block_type )
544 this->pcm_data = pos;
545 pos += size;
546 break;
547 }
548
549 case cmd_pcm_seek:
550 this->pcm_pos = this->pcm_data + pos [3] * 0x1000000 + pos [2] * 0x10000 +
551 pos [1] * 0x100 + pos [0];
552 pos += 4;
553 break;
554
555 default: {
556 int cmd = pos [-1];
557 switch ( cmd & 0xF0 )
558 {
559 case cmd_pcm_delay:
560 write_pcm( this, vgm_time, *this->pcm_pos++ );
561 vgm_time += cmd & 0x0F;
562 break;
563
564 case cmd_short_delay:
565 vgm_time += (cmd & 0x0F) + 1;
566 break;
567
568 case 0x50:
569 pos += 2;
570 break;
571
572 default:
573 pos += command_len( cmd ) - 1;
574 /* warning( "Unknown stream event" ); */
575 }
576 }
577 }
578 }
579 vgm_time -= end_time;
580 this->pos = pos;
581 this->vgm_time = vgm_time;
582
583 return to_psg_time( this, end_time );
584}
585
586int play_frame( struct Vgm_Emu* this, blip_time_t blip_time, int sample_count, blip_sample_t out [] )
587{
588 // to do: timing is working mostly by luck
589 int min_pairs = (unsigned) sample_count / 2;
590 int vgm_time = (min_pairs << fm_time_bits) / this->fm_time_factor - 1;
591 assert( to_fm_time( this, vgm_time ) <= min_pairs );
592 int pairs;
593 while ( (pairs = to_fm_time( this, vgm_time )) < min_pairs )
594 vgm_time++;
595 //debug_printf( "pairs: %d, min_pairs: %d\n", pairs, min_pairs );
596
597 if ( Ym2612_enabled( &this->ym2612 ) )
598 {
599 Ym2612_begin_frame( &this->ym2612, out );
600 memset( out, 0, pairs * stereo * sizeof *out );
601 }
602 else if ( Ym2413_enabled( &this->ym2413 ) )
603 {
604 Ym2413_begin_frame( &this->ym2413, out );
605 }
606
607 run( this, vgm_time );
608 Ym2612_run_until( &this->ym2612, pairs );
609 Ym2413_run_until( &this->ym2413, pairs );
610
611 this->fm_time_offset = (vgm_time * this->fm_time_factor + this->fm_time_offset) - (pairs << fm_time_bits);
612
613 Sms_apu_end_frame( &this->psg, blip_time );
614
615 return pairs * stereo;
616}
617
618// Update pre-1.10 header FM rates by scanning commands
619void update_fm_rates( struct Vgm_Emu* this, int* ym2413_rate, int* ym2612_rate )
620{
621 byte const* p = this->file_begin + 0x40;
622 while ( p < this->file_end )
623 {
624 switch ( *p )
625 {
626 case cmd_end:
627 return;
628
629 case cmd_psg:
630 case cmd_byte_delay:
631 p += 2;
632 break;
633
634 case cmd_delay:
635 p += 3;
636 break;
637
638 case cmd_data_block:
639 p += 7 + get_le32( p + 3 );
640 break;
641
642 case cmd_ym2413:
643 *ym2612_rate = 0;
644 return;
645
646 case cmd_ym2612_port0:
647 case cmd_ym2612_port1:
648 *ym2612_rate = *ym2413_rate;
649 *ym2413_rate = 0;
650 return;
651
652 case cmd_ym2151:
653 *ym2413_rate = 0;
654 *ym2612_rate = 0;
655 return;
656
657 default:
658 p += command_len( *p );
659 }
660 }
661}
662
663
664// Music Emu
665
666blargg_err_t Vgm_set_sample_rate( struct Vgm_Emu* this, long rate )
667{
668 require( !this->sample_rate ); // sample rate can't be changed once set
669 RETURN_ERR( Buffer_set_sample_rate( &this->stereo_buf, rate, 1000 / 30 ) );
670 RETURN_ERR( Buffer_set_sample_rate( &this->buf, rate, 1000 / 20 ) );
671
672 // Set bass frequency
673 Buffer_bass_freq( &this->buf, 80 );
674
675 this->sample_rate = rate;
676 return 0;
677}
678
679void Sound_mute_voice( struct Vgm_Emu* this, int index, bool mute )
680{
681 require( (unsigned) index < (unsigned) this->voice_count );
682 int bit = 1 << index;
683 int mask = this->mute_mask_ | bit;
684 if ( !mute )
685 mask ^= bit;
686 Sound_mute_voices( this, mask );
687}
688
689void Sound_mute_voices( struct Vgm_Emu* this, int mask )
690{
691 require( this->sample_rate ); // sample rate must be set first
692 this->mute_mask_ = mask;
693
694 int i;
695 for ( i = this->voice_count; i--; )
696 {
697 if ( mask & (1 << i) )
698 {
699 set_voice( this, i, 0, 0, 0 );
700 }
701 else
702 {
703 struct channel_t ch = Buffer_channel( &this->buf );
704 assert( (ch.center && ch.left && ch.right) ||
705 (!ch.center && !ch.left && !ch.right) ); // all or nothing
706 set_voice( this, i, ch.center, ch.left, ch.right );
707 }
708 }
709
710 // TODO: what was this for?
711 //core.pcm.output( &core.blip_buf );
712
713 // TODO: silence PCM if FM isn't used?
714 if ( uses_fm( this ) )
715 {
716 for ( i = sms_osc_count; --i >= 0; )
717 Sms_apu_set_output( &this->psg, i, ( mask & 0x80 ) ? 0 : &this->stereo_buf.bufs [0], NULL, NULL );
718 if ( Ym2612_enabled( &this->ym2612 ) )
719 {
720 Synth_volume( &this->pcm, (mask & 0x40) ? 0.0 : 0.1115 / 256 * fm_gain * this->gain );
721 Ym2612_mute_voices( &this->ym2612, mask );
722 }
723
724 if ( Ym2413_enabled( &this->ym2413 ) )
725 {
726 int m = mask & 0x3F;
727 if ( mask & 0x20 )
728 m |= 0x01E0; // channels 5-8
729 if ( mask & 0x40 )
730 m |= 0x3E00;
731 Ym2413_mute_voices( &this->ym2413, m );
732 }
733 }
734}
735
736void Sound_set_tempo( struct Vgm_Emu* this, double t )
737{
738 require( this->sample_rate ); // sample rate must be set first
739 double const min = 0.02;
740 double const max = 4.00;
741 if ( t < min ) t = min;
742 if ( t > max ) t = max;
743 this->tempo = t;
744
745 if ( this->file_begin )
746 {
747 this->vgm_rate = (long) (44100 * t + 0.5);
748 this->blip_time_factor = (int) ((double)
749 (1 << blip_time_bits) / this->vgm_rate * Blip_clock_rate( &this->stereo_buf.bufs [0] ) + 0.5);
750 //debug_printf( "blip_time_factor: %ld\n", blip_time_factor );
751 //debug_printf( "vgm_rate: %ld\n", vgm_rate );
752 // TODO: remove? calculates vgm_rate more accurately (above differs at most by one Hz only)
753 //blip_time_factor = (long) floor( double (1L << blip_time_bits) * psg_rate / 44100 / t + 0.5 );
754 //vgm_rate = (long) floor( double (1L << blip_time_bits) * psg_rate / blip_time_factor + 0.5 );
755
756 this->fm_time_factor = 2 + (int) (this->fm_rate * (1 << fm_time_bits) / this->vgm_rate + 0.5);
757 }
758}
759
760void fill_buf( struct Vgm_Emu *this );
761blargg_err_t Vgm_start_track( struct Vgm_Emu* this )
762{
763 clear_track_vars( this );
764
765 Sms_apu_reset( &this->psg, get_le16( header( this )->noise_feedback ), header( this )->noise_width );
766
767 this->blip_buf = &this->stereo_buf.bufs [0];
768
769 this->dac_disabled = -1;
770 this->pos = this->file_begin + header_size;
771 this->pcm_data = this->pos;
772 this->pcm_pos = this->pos;
773 this->dac_amp = -1;
774 this->vgm_time = 0;
775 if ( get_le32( header( this )->version ) >= 0x150 )
776 {
777 long data_offset = get_le32( header( this )->data_offset );
778 check( data_offset );
779 if ( data_offset )
780 this->pos += data_offset + offsetof (struct header_t,data_offset) - 0x40;
781 }
782
783 if ( uses_fm( this ) )
784 {
785 if ( Ym2413_enabled( &this->ym2413 ) )
786 Ym2413_reset( &this->ym2413 );
787
788 if ( Ym2612_enabled( &this->ym2612 ) )
789 Ym2612_reset( &this->ym2612 );
790
791 Buffer_clear( &this->stereo_buf );
792 Resampler_clear( &this->resampler );
793 }
794
795 this->fm_time_offset = 0;
796
797 Buffer_clear( &this->buf );
798
799 this->emu_track_ended_ = false;
800 this->track_ended = false;
801
802 if ( !this->ignore_silence )
803 {
804 // play until non-silence or end of track
805 long end;
806 for ( end = this->max_initial_silence * stereo * this->sample_rate; this->emu_time < end; )
807 {
808 fill_buf( this );
809 if ( this->buf_remain | (int) this->emu_track_ended_ )
810 break;
811 }
812
813 this->emu_time = this->buf_remain;
814 this->out_time = 0;
815 this->silence_time = 0;
816 this->silence_count = 0;
817 }
818 /* return track_ended() ? warning() : 0; */
819 return 0;
820}
821
822// Tell/Seek
823
824blargg_long msec_to_samples( blargg_long msec, long sample_rate )
825{
826 blargg_long sec = msec / 1000;
827 msec -= sec * 1000;
828 return (sec * sample_rate + msec * sample_rate / 1000) * stereo;
829}
830
831long Track_tell( struct Vgm_Emu* this )
832{
833 blargg_long rate = this->sample_rate * stereo;
834 blargg_long sec = this->out_time / rate;
835 return sec * 1000 + (this->out_time - sec * rate) * 1000 / rate;
836}
837
838blargg_err_t Track_seek( struct Vgm_Emu* this, long msec )
839{
840 blargg_long time = msec_to_samples( msec, this->sample_rate );
841 if ( time < this->out_time )
842 RETURN_ERR( Vgm_start_track( this ) );
843 return Track_skip( this, time - this->out_time );
844}
845
846blargg_err_t skip_( struct Vgm_Emu* this, long count );
847blargg_err_t Track_skip( struct Vgm_Emu* this, long count )
848{
849 this->out_time += count;
850
851 // remove from silence and buf first
852 {
853 long n = min( count, this->silence_count );
854 this->silence_count -= n;
855 count -= n;
856
857 n = min( count, this->buf_remain );
858 this->buf_remain -= n;
859 count -= n;
860 }
861
862 if ( count && !this->emu_track_ended_ )
863 {
864 this->emu_time += count;
865 if ( skip_( this, count ) )
866 this->emu_track_ended_ = true;
867 }
868
869 if ( !(this->silence_count | this->buf_remain) ) // caught up to emulator, so update track ended
870 this->track_ended |= this->emu_track_ended_;
871
872 return 0;
873}
874
875blargg_err_t skip_( struct Vgm_Emu* this, long count )
876{
877 // for long skip, mute sound
878 const long threshold = 30000;
879 if ( count > threshold )
880 {
881 int saved_mute = this->mute_mask_;
882 Sound_mute_voices( this, ~0 );
883
884 while ( count > threshold / 2 && !this->emu_track_ended_ )
885 {
886 RETURN_ERR( play_( this, buf_size, this->buf_ ) );
887 count -= buf_size;
888 }
889
890 Sound_mute_voices( this, saved_mute );
891 }
892
893 while ( count && !this->emu_track_ended_ )
894 {
895 long n = buf_size;
896 if ( n > count )
897 n = count;
898 count -= n;
899 RETURN_ERR( play_( this, n, this->buf_ ) );
900 }
901 return 0;
902}
903
904// Fading
905
906void Track_set_fade( struct Vgm_Emu* this, long start_msec, long length_msec )
907{
908 this->fade_step = this->sample_rate * length_msec / (fade_block_size * fade_shift * 1000 / stereo);
909 this->fade_start = msec_to_samples( start_msec, this->sample_rate );
910}
911
912// unit / pow( 2.0, (double) x / step )
913static int int_log( blargg_long x, int step, int unit )
914{
915 int shift = x / step;
916 int fraction = (x - shift * step) * unit / step;
917 return ((unit - fraction) + (fraction >> 1)) >> shift;
918}
919
920void handle_fade( struct Vgm_Emu* this, long out_count, sample_t* out )
921{
922 int i;
923 for ( i = 0; i < out_count; i += fade_block_size )
924 {
925 int const shift = 14;
926 int const unit = 1 << shift;
927 int gain = int_log( (this->out_time + i - this->fade_start) / fade_block_size,
928 this->fade_step, unit );
929 if ( gain < (unit >> fade_shift) )
930 this->track_ended = this->emu_track_ended_ = true;
931
932 sample_t* io = &out [i];
933 int count;
934 for ( count = min( fade_block_size, out_count - i ); count; --count )
935 {
936 *io = (sample_t) ((*io * gain) >> shift);
937 ++io;
938 }
939 }
940}
941
942// Silence detection
943
944void emu_play( struct Vgm_Emu* this, long count, sample_t* out )
945{
946 this->emu_time += count;
947 if ( !this->emu_track_ended_ ) {
948 if ( play_( this, count, out ) )
949 this->emu_track_ended_ = true;
950 }
951 else
952 memset( out, 0, count * sizeof *out );
953}
954
955// number of consecutive silent samples at end
956static long count_silence( sample_t* begin, long size )
957{
958 sample_t first = *begin;
959 *begin = silence_threshold; // sentinel
960 sample_t* p = begin + size;
961 while ( (unsigned) (*--p + silence_threshold / 2) <= (unsigned) silence_threshold ) { }
962 *begin = first;
963 return size - (p - begin);
964}
965
966// fill internal buffer and check it for silence
967void fill_buf( struct Vgm_Emu* this )
968{
969 assert( !this->buf_remain );
970 if ( !this->emu_track_ended_ )
971 {
972 emu_play( this, buf_size, this->buf_ );
973 long silence = count_silence( this->buf_, buf_size );
974 if ( silence < buf_size )
975 {
976 this->silence_time = this->emu_time - silence;
977 this->buf_remain = buf_size;
978 return;
979 }
980 }
981 this->silence_count += buf_size;
982}
983
984blargg_err_t Vgm_play( struct Vgm_Emu* this, long out_count, sample_t* out )
985{
986 if ( this->track_ended )
987 {
988 memset( out, 0, out_count * sizeof *out );
989 }
990 else
991 {
992 require( out_count % stereo == 0 );
993
994 assert( this->emu_time >= this->out_time );
995
996 // prints nifty graph of how far ahead we are when searching for silence
997 //debug_printf( "%*s \n", int ((emu_time - out_time) * 7 / sample_rate()), "*" );
998
999 long pos = 0;
1000 if ( this->silence_count )
1001 {
1002 // during a run of silence, run emulator at >=2x speed so it gets ahead
1003 long ahead_time = this->silence_lookahead * (this->out_time + out_count - this->silence_time) + this->silence_time;
1004 while ( this->emu_time < ahead_time && !(this->buf_remain | this->emu_track_ended_) )
1005 fill_buf( this );
1006
1007 // fill with silence
1008 pos = min( this->silence_count, out_count );
1009 memset( out, 0, pos * sizeof *out );
1010 this->silence_count -= pos;
1011
1012 if ( this->emu_time - this->silence_time > silence_max * stereo * this->sample_rate )
1013 {
1014 this->track_ended = this->emu_track_ended_ = true;
1015 this->silence_count = 0;
1016 this->buf_remain = 0;
1017 }
1018 }
1019
1020 if ( this->buf_remain )
1021 {
1022 // empty silence buf
1023 long n = min( this->buf_remain, out_count - pos );
1024 memcpy( &out [pos], this->buf_ + (buf_size - this->buf_remain), n * sizeof *out );
1025 this->buf_remain -= n;
1026 pos += n;
1027 }
1028
1029 // generate remaining samples normally
1030 long remain = out_count - pos;
1031 if ( remain )
1032 {
1033 emu_play( this, remain, out + pos );
1034 this->track_ended |= this->emu_track_ended_;
1035
1036 if ( !this->ignore_silence || this->out_time > this->fade_start )
1037 {
1038 // check end for a new run of silence
1039 long silence = count_silence( out + pos, remain );
1040 if ( silence < remain )
1041 this->silence_time = this->emu_time - silence;
1042
1043 if ( this->emu_time - this->silence_time >= buf_size )
1044 fill_buf( this ); // cause silence detection on next play()
1045 }
1046 }
1047
1048 if ( this->out_time > this->fade_start )
1049 handle_fade( this, out_count, out );
1050 }
1051 this->out_time += out_count;
1052 return 0;
1053}