diff options
Diffstat (limited to 'apps/codecs/libgme/sms_fm_apu.c')
-rw-r--r-- | apps/codecs/libgme/sms_fm_apu.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/apps/codecs/libgme/sms_fm_apu.c b/apps/codecs/libgme/sms_fm_apu.c new file mode 100644 index 0000000000..b15fc56680 --- /dev/null +++ b/apps/codecs/libgme/sms_fm_apu.c | |||
@@ -0,0 +1,82 @@ | |||
1 | #include "sms_fm_apu.h" | ||
2 | |||
3 | #include "blargg_source.h" | ||
4 | |||
5 | void Fm_apu_create( struct Sms_Fm_Apu* this ) | ||
6 | { | ||
7 | Synth_init( &this->synth ); | ||
8 | Ym2413_init( &this->apu ); | ||
9 | } | ||
10 | |||
11 | blargg_err_t Fm_apu_init( struct Sms_Fm_Apu* this, double clock_rate, double sample_rate ) | ||
12 | { | ||
13 | this->period_ = (blip_time_t) (clock_rate / sample_rate + 0.5); | ||
14 | CHECK_ALLOC( !Ym2413_set_rate( &this->apu, sample_rate, clock_rate ) ); | ||
15 | |||
16 | Fm_apu_set_output( this, 0 ); | ||
17 | Fm_apu_volume( this, 1.0 ); | ||
18 | Fm_apu_reset( this ); | ||
19 | return 0; | ||
20 | } | ||
21 | |||
22 | void Fm_apu_reset( struct Sms_Fm_Apu* this ) | ||
23 | { | ||
24 | this->addr = 0; | ||
25 | this->next_time = 0; | ||
26 | this->last_amp = 0; | ||
27 | |||
28 | Ym2413_reset( &this->apu ); | ||
29 | } | ||
30 | |||
31 | void fm_run_until( struct Sms_Fm_Apu* this, blip_time_t end_time ) ICODE_ATTR; | ||
32 | void Fm_apu_write_data( struct Sms_Fm_Apu* this, blip_time_t time, int data ) | ||
33 | { | ||
34 | if ( time > this->next_time ) | ||
35 | fm_run_until( this, time ); | ||
36 | |||
37 | Ym2413_write( &this->apu, this->addr, data ); | ||
38 | } | ||
39 | |||
40 | void fm_run_until( struct Sms_Fm_Apu* this, blip_time_t end_time ) | ||
41 | { | ||
42 | assert( end_time > this->next_time ); | ||
43 | |||
44 | struct Blip_Buffer* const output = this->output_; | ||
45 | if ( !output ) | ||
46 | { | ||
47 | this->next_time = end_time; | ||
48 | return; | ||
49 | } | ||
50 | |||
51 | blip_time_t time = this->next_time; | ||
52 | struct Ym2413_Emu* emu = &this->apu; | ||
53 | do | ||
54 | { | ||
55 | short samples [2]; | ||
56 | Ym2413_run( emu, 1, samples ); | ||
57 | int amp = (samples [0] + samples [1]) >> 1; | ||
58 | |||
59 | int delta = amp - this->last_amp; | ||
60 | if ( delta ) | ||
61 | { | ||
62 | this->last_amp = amp; | ||
63 | Synth_offset_inline( &this->synth, time, delta, output ); | ||
64 | } | ||
65 | time += this->period_; | ||
66 | } | ||
67 | while ( time < end_time ); | ||
68 | |||
69 | this->next_time = time; | ||
70 | } | ||
71 | |||
72 | void Fm_apu_end_frame( struct Sms_Fm_Apu* this, blip_time_t time ) | ||
73 | { | ||
74 | if ( time > this->next_time ) | ||
75 | fm_run_until( this, time ); | ||
76 | |||
77 | this->next_time -= time; | ||
78 | assert( this->next_time >= 0 ); | ||
79 | |||
80 | if ( this->output_ ) | ||
81 | Blip_set_modified( this->output_ ); | ||
82 | } | ||