1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/
#include "hes_emu.h"
#include "blargg_endian.h"
#include "blargg_source.h"
/* Copyright (C) 2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
int const timer_mask = 0x04;
int const vdp_mask = 0x02;
int const i_flag_mask = 0x04;
int const unmapped = 0xFF;
long const period_60hz = 262 * 455L; // scanlines * clocks per scanline
int const stereo = 2; // number of channels for stereo
int const silence_max = 6; // seconds
int const silence_threshold = 0x10;
long const fade_block_size = 512;
int const fade_shift = 8; // fade ends with gain at 1.0 / (1 << fade_shift)
const char gme_wrong_file_type [] ICONST_ATTR = "Wrong file type for this emulator";
void clear_track_vars( struct Hes_Emu* this )
{
this->current_track_ = -1;
this->out_time = 0;
this->emu_time = 0;
this->emu_track_ended_ = true;
this->track_ended = true;
this->fade_start = (blargg_long)(LONG_MAX / 2 + 1);
this->fade_step = 1;
this->silence_time = 0;
this->silence_count = 0;
this->buf_remain = 0;
}
void Hes_init( struct Hes_Emu* this )
{
this->sample_rate_ = 0;
this->mute_mask_ = 0;
this->tempo_ = (int)(FP_ONE_TEMPO);
// defaults
this->max_initial_silence = 2;
this->ignore_silence = false;
// Unload
this->voice_count_ = 0;
clear_track_vars( this );
this->timer.raw_load = 0;
this->silence_lookahead = 6;
Sound_set_gain( this, (int)(FP_ONE_GAIN*1.11) );
Rom_init( &this->rom, 0x2000 );
Apu_init( &this->apu );
Adpcm_init( &this->adpcm );
Cpu_init( &this->cpu );
/* Set default track count */
this->track_count = 255;
}
static blargg_err_t check_hes_header( void const* header )
{
if ( memcmp( header, "HESM", 4 ) )
return gme_wrong_file_type;
return 0;
}
// Setup
blargg_err_t Hes_load( struct Hes_Emu* this, void* data, long size )
{
// Unload
this->voice_count_ = 0;
clear_track_vars( this );
assert( offsetof (struct header_t,unused [4]) == header_size );
RETURN_ERR( Rom_load( &this->rom, data, size, header_size, &this->header, unmapped ) );
RETURN_ERR( check_hes_header( this->header.tag ) );
/* if ( header_.vers != 0 )
warning( "Unknown file version" );
if ( memcmp( header_.data_tag, "DATA", 4 ) )
warning( "Data header missing" );
if ( memcmp( header_.unused, "\0\0\0\0", 4 ) )
warning( "Unknown header data" ); */
// File spec supports multiple blocks, but I haven't found any, and
// many files have bad sizes in the only block, so it's simpler to
// just try to load the damn data as best as possible.
long addr = get_le32( this->header.addr );
/* long rom_size = get_le32( this->header.size ); */
long const rom_max = 0x100000;
if ( addr & ~(rom_max - 1) )
{
/* warning( "Invalid address" ); */
addr &= rom_max - 1;
}
/* if ( (unsigned long) (addr + size) > (unsigned long) rom_max )
warning( "Invalid size" );
if ( rom_size != rom.file_size() )
{
if ( size <= rom.file_size() - 4 && !memcmp( rom.begin() + size, "DATA", 4 ) )
warning( "Multiple DATA not supported" );
else if ( size < rom.file_size() )
warning( "Extra file data" );
else
warning( "Missing file data" );
} */
Rom_set_addr( &this->rom, addr );
this->voice_count_ = osc_count + adpcm_osc_count;
Apu_volume( &this->apu, (double)(this->gain_)/FP_ONE_GAIN );
Adpcm_volume( &this->adpcm, (double)(this->gain_)/FP_ONE_GAIN );
// Setup buffer
this->clock_rate_ = 7159091;
Buffer_clock_rate( &this->stereo_buf, 7159091 );
this->buf_changed_count = Buffer_channels_changed_count( &this->stereo_buf );
Sound_set_tempo( this, this->tempo_ );
Sound_mute_voices( this, this->mute_mask_ );
// Reset track count
this->track_count = 255;
this->m3u.size = 0;
return 0;
}
// Emulation
void recalc_timer_load( struct Hes_Emu* this ) ICODE_ATTR;
void recalc_timer_load( struct Hes_Emu* this )
{
this->timer.load = this->timer.raw_load * this->timer_base + 1;
}
// Hardware
void irq_changed( struct Hes_Emu* this ) ICODE_ATTR;
void run_until( struct Hes_Emu* this, hes_time_t present ) ICODE_ATTR;
void Cpu_write_vdp( struct Hes_Emu* this, int addr, int data )
{
switch ( addr )
{
case 0:
this->vdp.latch = data & 0x1F;
break;
case 2:
if ( this->vdp.latch == 5 )
{
/* if ( data & 0x04 )
warning( "Scanline interrupt unsupported" ); */
run_until( this, Cpu_time( &this->cpu ) );
this->vdp.control = data;
irq_changed( this );
}
else
{
dprintf( "VDP not supported: $%02X <- $%02X\n", this->vdp.latch, data );
}
break;
case 3:
dprintf( "VDP MSB not supported: $%02X <- $%02X\n", this->vdp.latch, data );
break;
}
}
int Cpu_done( struct Hes_Emu* this )
{
check( time() >= end_time() ||
(!(r.status & i_flag_mask) && time() >= irq_time()) );
if ( !(this->cpu.r.status & i_flag_mask) )
{
hes_time_t present = Cpu_time( &this->cpu );
if ( this->irq.timer <= present && !(this->irq.disables & timer_mask) )
{
this->timer.fired = true;
this->irq.timer = (hes_time_t)future_hes_time;
irq_changed( this ); // overkill, but not worth writing custom code
#if defined (GME_FRAME_HOOK_DEFINED)
{
unsigned const threshold = period_60hz / 30;
unsigned long elapsed = present - last_frame_hook;
if ( elapsed - period_60hz + threshold / 2 < threshold )
{
last_frame_hook = present;
GME_FRAME_HOOK( this );
}
}
#endif
return 0x0A;
}
if ( this->irq.vdp <= present && !(this->irq.disables & vdp_mask) )
{
// work around for bugs with music not acknowledging VDP
//run_until( present );
//irq.vdp = future_hes_time;
//irq_changed();
#if defined(GME_FRAME_HOOK_DEFINED)
last_frame_hook = present;
GME_FRAME_HOOK( this );
#endif
return 0x08;
}
}
return 0;
}
void Emu_cpu_write( struct Hes_Emu* this, hes_addr_t addr, int data )
{
hes_time_t time = Cpu_time( &this->cpu );
if ( (unsigned) (addr - start_addr) <= end_addr - start_addr )
{
GME_APU_HOOK( this, addr - apu.start_addr, data );
// avoid going way past end when a long block xfer is writing to I/O space
hes_time_t t = min( time, this->cpu.end_time + 8 );
Apu_write_data( &this->apu, t, addr, data );
return;
}
if ( (unsigned) (addr - io_addr) < io_size )
{
hes_time_t t = min( time, this->cpu.end_time + 6 );
Adpcm_write_data( &this->adpcm, t, addr, data );
return;
}
switch ( addr )
{
case 0x0000:
case 0x0002:
case 0x0003:
Cpu_write_vdp( this, addr, data );
return;
case 0x0C00: {
run_until( this, time );
this->timer.raw_load = (data & 0x7F) + 1;
recalc_timer_load( this );
this->timer.count = this->timer.load;
break;
}
case 0x0C01:
data &= 1;
if ( this->timer.enabled == data )
return;
run_until( this, time );
this->timer.enabled = data;
if ( data )
this->timer.count = this->timer.load;
break;
case 0x1402:
run_until( this, time );
this->irq.disables = data;
// flag questionable values
if ( (data & 0xF8) && (data & 0xF8) != 0xF8 ) {
dprintf( "Int mask: $%02X\n", data );
}
break;
case 0x1403:
run_until( this, time );
if ( this->timer.enabled )
this->timer.count = this->timer.load;
this->timer.fired = false;
break;
#ifndef NDEBUG
case 0x1000: // I/O port
case 0x0402: // palette
case 0x0403:
case 0x0404:
case 0x0405:
return;
default:
dprintf( "unmapped write $%04X <- $%02X\n", addr, data );
return;
#endif
}
irq_changed( this );
}
int Emu_cpu_read( struct Hes_Emu* this, hes_addr_t addr )
{
hes_time_t time = Cpu_time( &this->cpu );
addr &= page_size - 1;
switch ( addr )
{
case 0x0000:
if ( this->irq.vdp > time )
return 0;
this->irq.vdp = (hes_time_t)future_hes_time;
run_until( this, time );
irq_changed( this );
return 0x20;
case 0x0002:
case 0x0003:
dprintf( "VDP read not supported: %d\n", addr );
return 0;
case 0x0C01:
//return timer.enabled; // TODO: remove?
case 0x0C00:
run_until( this, time );
dprintf( "Timer count read\n" );
return (unsigned) (this->timer.count - 1) / this->timer_base;
case 0x1402:
return this->irq.disables;
case 0x1403:
{
int status = 0;
if ( this->irq.timer <= time ) status |= timer_mask;
if ( this->irq.vdp <= time ) status |= vdp_mask;
return status;
}
case 0x180A:
case 0x180B:
case 0x180C:
case 0x180D:
return Adpcm_read_data( &this->adpcm, time, addr );
#ifndef NDEBUG
case 0x1000: // I/O port
// case 0x180C: // CD-ROM
// case 0x180D:
break;
default:
dprintf( "unmapped read $%04X\n", addr );
#endif
}
return unmapped;
}
// see hes_cpu_io.h for core read/write functions
// Emulation
void run_until( struct Hes_Emu* this, hes_time_t present )
{
while ( this->vdp.next_vbl < present )
this->vdp.next_vbl += this->play_period;
hes_time_t elapsed = present - this->timer.last_time;
if ( elapsed > 0 )
{
if ( this->timer.enabled )
{
this->timer.count -= elapsed;
if ( this->timer.count <= 0 )
this->timer.count += this->timer.load;
}
this->timer.last_time = present;
}
}
void irq_changed( struct Hes_Emu* this )
{
hes_time_t present = Cpu_time( &this->cpu );
if ( this->irq.timer > present )
{
this->irq.timer = (hes_time_t)future_hes_time;
if ( this->timer.enabled && !this->timer.fired )
this->irq.timer = present + this->timer.count;
}
if ( this->irq.vdp > present )
{
this->irq.vdp = (hes_time_t)future_hes_time;
if ( this->vdp.control & 0x08 )
this->irq.vdp = this->vdp.next_vbl;
}
hes_time_t time = (hes_time_t)future_hes_time;
if ( !(this->irq.disables & timer_mask) ) time = this->irq.timer;
if ( !(this->irq.disables & vdp_mask) ) time = min( time, this->irq.vdp );
// Set cpu irq time
this->cpu.state->time += Cpu_update_end_time( &this->cpu, this->cpu.r.status,
this->cpu.end_time, (this->cpu.irq_time = time) );
}
static void adjust_time( blargg_long* time, hes_time_t delta ) ICODE_ATTR;
static void adjust_time( blargg_long* time, hes_time_t delta )
{
if ( *time < (blargg_long)future_hes_time )
{
*time -= delta;
if ( *time < 0 )
*time = 0;
}
}
blargg_err_t run_clocks( struct Hes_Emu* this, blip_time_t* duration_ ) ICODE_ATTR;
blargg_err_t run_clocks( struct Hes_Emu* this, blip_time_t* duration_ )
{
blip_time_t duration = *duration_; // cache
Cpu_run( this, duration );
/* warning( "Emulation error (illegal instruction)" ); */
check( time() >= duration );
//check( time() - duration < 20 ); // Txx instruction could cause going way over
run_until( this, duration );
// end time frame
this->timer.last_time -= duration;
this->vdp.next_vbl -= duration;
#if defined (GME_FRAME_HOOK_DEFINED)
last_frame_hook -= *duration;
#endif
// End cpu frame
this->cpu.state_.base -= duration;
if ( this->cpu.irq_time < (hes_time_t)future_hes_time ) this->cpu.irq_time -= duration;
if ( this->cpu.end_time < (hes_time_t)future_hes_time ) this->cpu.end_time -= duration;
adjust_time( &this->irq.timer, duration );
adjust_time( &this->irq.vdp, duration );
Apu_end_frame( &this->apu, duration );
Adpcm_end_frame( &this->adpcm, duration );
return 0;
}
blargg_err_t play_( struct Hes_Emu* this, long count, sample_t* out ) ICODE_ATTR;
blargg_err_t play_( struct Hes_Emu* this, long count, sample_t* out )
{
long remain = count;
while ( remain )
{
remain -= Buffer_read_samples( &this->stereo_buf, &out [count - remain], remain );
if ( remain )
{
if ( this->buf_changed_count != Buffer_channels_changed_count( &this->stereo_buf ) )
{
this->buf_changed_count = Buffer_channels_changed_count( &this->stereo_buf );
// Remute voices
Sound_mute_voices( this, this->mute_mask_ );
}
int msec = Buffer_length( &this->stereo_buf );
blip_time_t clocks_emulated = (blargg_long) msec * this->clock_rate_ / 1000;
RETURN_ERR( run_clocks( this, &clocks_emulated ) );
assert( clocks_emulated );
Buffer_end_frame( &this->stereo_buf, clocks_emulated );
}
}
return 0;
}
// Music emu
blargg_err_t Hes_set_sample_rate( struct Hes_Emu* this, long rate )
{
require( !this->sample_rate_ ); // sample rate can't be changed once set
Buffer_init( &this->stereo_buf );
RETURN_ERR( Buffer_set_sample_rate( &this->stereo_buf, rate, 1000 / 20 ) );
// Set bass frequency
Buffer_bass_freq( &this->stereo_buf, 60 );
this->sample_rate_ = rate;
return 0;
}
void Sound_mute_voice( struct Hes_Emu* this, int index, bool mute )
{
require( (unsigned) index < (unsigned) this->voice_count_ );
int bit = 1 << index;
int mask = this->mute_mask_ | bit;
if ( !mute )
mask ^= bit;
Sound_mute_voices( this, mask );
}
void Sound_mute_voices( struct Hes_Emu* this, int mask )
{
require( this->sample_rate_ ); // sample rate must be set first
this->mute_mask_ = mask;
// Set adpcm voice
struct channel_t ch = Buffer_channel( &this->stereo_buf );
if ( mask & (1 << this->voice_count_ ) )
Adpcm_set_output( &this->adpcm, 0, 0, 0, 0 );
else
Adpcm_set_output( &this->adpcm, 0, ch.center, ch.left, ch.right );
// Set apu voices
int i = this->voice_count_ - 1;
for ( ; i--; )
{
if ( mask & (1 << i) )
{
Apu_osc_output( &this->apu, i, 0, 0, 0 );
}
else
{
assert( (ch.center && ch.left && ch.right) ||
(!ch.center && !ch.left && !ch.right) ); // all or nothing
Apu_osc_output( &this->apu, i, ch.center, ch.left, ch.right );
}
}
}
void Sound_set_tempo( struct Hes_Emu* this, int t )
{
require( this->sample_rate_ ); // sample rate must be set first
int const min = (int)(FP_ONE_TEMPO*0.02);
int const max = (int)(FP_ONE_TEMPO*4.00);
if ( t < min ) t = min;
if ( t > max ) t = max;
this->play_period = (hes_time_t) ((period_60hz*FP_ONE_TEMPO) / t);
this->timer_base = (int) ((1024*FP_ONE_TEMPO) / t);
recalc_timer_load( this );
this->tempo_ = t;
}
void fill_buf( struct Hes_Emu* this ) ICODE_ATTR;
blargg_err_t Hes_start_track( struct Hes_Emu* this, int track )
{
clear_track_vars( this );
// Remap track if playlist available
if ( this->m3u.size > 0 ) {
struct entry_t* e = &this->m3u.entries[track];
track = e->track;
}
this->current_track_ = track;
Buffer_clear( &this->stereo_buf );
memset( this->cpu.ram, 0, sizeof this->cpu.ram ); // some HES music relies on zero fill
memset( this->sgx, 0, sizeof this->sgx );
Apu_reset( &this->apu );
Adpcm_reset( &this->adpcm );
Cpu_reset( &this->cpu );
unsigned i;
for ( i = 0; i < sizeof this->header.banks; i++ )
Cpu_set_mmr( this, i, this->header.banks [i] );
Cpu_set_mmr( this, page_count, 0xFF ); // unmapped beyond end of address space
this->irq.disables = timer_mask | vdp_mask;
this->irq.timer = (hes_time_t)future_hes_time;
this->irq.vdp = (hes_time_t)future_hes_time;
this->timer.enabled = false;
this->timer.raw_load= 0x80;
this->timer.count = this->timer.load;
this->timer.fired = false;
this->timer.last_time = 0;
this->vdp.latch = 0;
this->vdp.control = 0;
this->vdp.next_vbl = 0;
this->cpu.ram [0x1FF] = (idle_addr - 1) >> 8;
this->cpu.ram [0x1FE] = (idle_addr - 1) & 0xFF;
this->cpu.r.sp = 0xFD;
this->cpu.r.pc = get_le16( this->header.init_addr );
this->cpu.r.a = track;
recalc_timer_load( this );
this->last_frame_hook = 0;
this->emu_track_ended_ = false;
this->track_ended = false;
if ( !this->ignore_silence )
{
// play until non-silence or end of track
long end;
for ( end =this-> max_initial_silence * stereo * this->sample_rate_; this->emu_time < end; )
{
fill_buf( this );
if ( this->buf_remain | (int) this->emu_track_ended_ )
break;
}
this->emu_time = this->buf_remain;
this->out_time = 0;
this->silence_time = 0;
this->silence_count = 0;
}
/* return track_ended() ? warning() : 0; */
return 0;
}
// Tell/Seek
blargg_long msec_to_samples( blargg_long msec, long sample_rate )
{
blargg_long sec = msec / 1000;
msec -= sec * 1000;
return (sec * sample_rate + msec * sample_rate / 1000) * stereo;
}
long Track_tell( struct Hes_Emu* this )
{
blargg_long rate = this->sample_rate_ * stereo;
blargg_long sec = this->out_time / rate;
return sec * 1000 + (this->out_time - sec * rate) * 1000 / rate;
}
blargg_err_t Track_seek( struct Hes_Emu* this, long msec )
{
blargg_long time = msec_to_samples( msec, this->sample_rate_ );
if ( time < this->out_time )
RETURN_ERR( Hes_start_track( this, this->current_track_ ) );
return Track_skip( this, time - this->out_time );
}
blargg_err_t skip_( struct Hes_Emu* this, long count ) ICODE_ATTR;
blargg_err_t skip_( struct Hes_Emu* this, long count )
{
// for long skip, mute sound
const long threshold = 30000;
if ( count > threshold )
{
int saved_mute = this->mute_mask_;
Sound_mute_voices( this, ~0 );
while ( count > threshold / 2 && !this->emu_track_ended_ )
{
RETURN_ERR( play_( this, buf_size, this->buf ) );
count -= buf_size;
}
Sound_mute_voices( this, saved_mute );
}
while ( count && !this->emu_track_ended_ )
{
long n = buf_size;
if ( n > count )
n = count;
count -= n;
RETURN_ERR( play_( this, n, this->buf ) );
}
return 0;
}
blargg_err_t Track_skip( struct Hes_Emu* this, long count )
{
require( this->current_track_ >= 0 ); // start_track() must have been called already
this->out_time += count;
// remove from silence and buf first
{
long n = min( count, this->silence_count );
this->silence_count -= n;
count -= n;
n = min( count, this->buf_remain );
this->buf_remain -= n;
count -= n;
}
if ( count && !this->emu_track_ended_ )
{
this->emu_time += count;
// End track if error
if ( skip_( this, count ) )
this->emu_track_ended_ = true;
}
if ( !(this->silence_count | this->buf_remain) ) // caught up to emulator, so update track ended
this->track_ended |= this->emu_track_ended_;
return 0;
}
// Fading
void Track_set_fade( struct Hes_Emu* this, long start_msec, long length_msec )
{
this->fade_step = this->sample_rate_ * length_msec / (fade_block_size * fade_shift * 1000 / stereo);
this->fade_start = msec_to_samples( start_msec, this->sample_rate_ );
}
// unit / pow( 2.0, (double) x / step )
static int int_log( blargg_long x, int step, int unit ) ICODE_ATTR;
static int int_log( blargg_long x, int step, int unit )
{
int shift = x / step;
int fraction = (x - shift * step) * unit / step;
return ((unit - fraction) + (fraction >> 1)) >> shift;
}
void handle_fade( struct Hes_Emu* this, long out_count, sample_t* out ) ICODE_ATTR;
void handle_fade( struct Hes_Emu* this, long out_count, sample_t* out )
{
int i;
for ( i = 0; i < out_count; i += fade_block_size )
{
int const shift = 14;
int const unit = 1 << shift;
int gain = int_log( (this->out_time + i - this->fade_start) / fade_block_size,
this->fade_step, unit );
if ( gain < (unit >> fade_shift) )
this->track_ended = this->emu_track_ended_ = true;
sample_t* io = &out [i];
int count;
for ( count = min( fade_block_size, out_count - i ); count; --count )
{
*io = (sample_t) ((*io * gain) >> shift);
++io;
}
}
}
// Silence detection
void emu_play( struct Hes_Emu* this, long count, sample_t* out ) ICODE_ATTR;
void emu_play( struct Hes_Emu* this, long count, sample_t* out )
{
check( current_track_ >= 0 );
this->emu_time += count;
if ( this->current_track_ >= 0 && !this->emu_track_ended_ ) {
// End track if error
if ( play_( this, count, out ) )
this->emu_track_ended_ = true;
}
else
memset( out, 0, count * sizeof *out );
}
// number of consecutive silent samples at end
static long count_silence( sample_t* begin, long size ) ICODE_ATTR;
static long count_silence( sample_t* begin, long size )
{
sample_t first = *begin;
*begin = silence_threshold; // sentinel
sample_t* p = begin + size;
while ( (unsigned) (*--p + silence_threshold / 2) <= (unsigned) silence_threshold ) { }
*begin = first;
return size - (p - begin);
}
// fill internal buffer and check it for silence
void fill_buf( struct Hes_Emu* this )
{
assert( !this->buf_remain );
if ( !this->emu_track_ended_ )
{
emu_play( this, buf_size, this->buf );
long silence = count_silence( this->buf, buf_size );
if ( silence < buf_size )
{
this->silence_time = this->emu_time - silence;
this->buf_remain = buf_size;
return;
}
}
this->silence_count += buf_size;
}
blargg_err_t Hes_play( struct Hes_Emu* this, long out_count, sample_t* out )
{
if ( this->track_ended )
{
memset( out, 0, out_count * sizeof *out );
}
else
{
require( this->current_track_ >= 0 );
require( out_count % stereo == 0 );
assert( this->emu_time >= this->out_time );
// prints nifty graph of how far ahead we are when searching for silence
//dprintf( "%*s \n", int ((emu_time - out_time) * 7 / sample_rate()), "*" );
long pos = 0;
if ( this->silence_count )
{
// during a run of silence, run emulator at >=2x speed so it gets ahead
long ahead_time = this->silence_lookahead * (this->out_time + out_count - this->silence_time) + this->silence_time;
while ( this->emu_time < ahead_time && !(this->buf_remain | this->emu_track_ended_) )
fill_buf( this );
// fill with silence
pos = min( this->silence_count, out_count );
memset( out, 0, pos * sizeof *out );
this->silence_count -= pos;
if ( this->emu_time - this->silence_time > silence_max * stereo * this->sample_rate_ )
{
this->track_ended = this->emu_track_ended_ = true;
this->silence_count = 0;
this->buf_remain = 0;
}
}
if ( this->buf_remain )
{
// empty silence buf
long n = min( this->buf_remain, out_count - pos );
memcpy( &out [pos], this->buf + (buf_size - this->buf_remain), n * sizeof *out );
this->buf_remain -= n;
pos += n;
}
// generate remaining samples normally
long remain = out_count - pos;
if ( remain )
{
emu_play( this, remain, out + pos );
this->track_ended |= this->emu_track_ended_;
if ( !this->ignore_silence || this->out_time > this->fade_start )
{
// check end for a new run of silence
long silence = count_silence( out + pos, remain );
if ( silence < remain )
this->silence_time = this->emu_time - silence;
if ( this->emu_time - this->silence_time >= buf_size )
fill_buf( this ); // cause silence detection on next play()
}
}
if ( this->out_time > this->fade_start )
handle_fade( this, out_count, out );
}
this->out_time += out_count;
return 0;
}
|