diff options
Diffstat (limited to 'lib/rbcodec/codecs/libgme/nes_namco_apu.c')
-rw-r--r-- | lib/rbcodec/codecs/libgme/nes_namco_apu.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/nes_namco_apu.c b/lib/rbcodec/codecs/libgme/nes_namco_apu.c new file mode 100644 index 0000000000..34df200bb6 --- /dev/null +++ b/lib/rbcodec/codecs/libgme/nes_namco_apu.c | |||
@@ -0,0 +1,138 @@ | |||
1 | // Nes_Snd_Emu 0.1.8. http://www.slack.net/~ant/ | ||
2 | |||
3 | #include "nes_namco_apu.h" | ||
4 | |||
5 | /* Copyright (C) 2003-2006 Shay Green. This module is free software; you | ||
6 | can redistribute it and/or modify it under the terms of the GNU Lesser | ||
7 | General Public License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. This | ||
9 | module is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
11 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
12 | details. You should have received a copy of the GNU Lesser General Public | ||
13 | License along with this module; if not, write to the Free Software Foundation, | ||
14 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ | ||
15 | |||
16 | #include "blargg_source.h" | ||
17 | |||
18 | void Namco_init( struct Nes_Namco_Apu* this ) | ||
19 | { | ||
20 | Synth_init( &this->synth ); | ||
21 | |||
22 | Namco_output( this, NULL ); | ||
23 | Namco_volume( this, (int)FP_ONE_VOLUME ); | ||
24 | Namco_reset( this ); | ||
25 | } | ||
26 | |||
27 | void Namco_reset( struct Nes_Namco_Apu* this ) | ||
28 | { | ||
29 | this->last_time = 0; | ||
30 | this->addr_reg = 0; | ||
31 | |||
32 | int i; | ||
33 | for ( i = 0; i < namco_reg_count; i++ ) | ||
34 | this->reg [i] = 0; | ||
35 | |||
36 | for ( i = 0; i < namco_osc_count; i++ ) | ||
37 | { | ||
38 | struct Namco_Osc* osc = &this->oscs [i]; | ||
39 | osc->delay = 0; | ||
40 | osc->last_amp = 0; | ||
41 | osc->wave_pos = 0; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | void Namco_output( struct Nes_Namco_Apu* this, struct Blip_Buffer* buf ) | ||
46 | { | ||
47 | int i; | ||
48 | for ( i = 0; i < namco_osc_count; i++ ) | ||
49 | Namco_osc_output( this, i, buf ); | ||
50 | } | ||
51 | |||
52 | void Namco_end_frame( struct Nes_Namco_Apu* this, blip_time_t time ) | ||
53 | { | ||
54 | if ( time > this->last_time ) | ||
55 | Namco_run_until( this, time ); | ||
56 | |||
57 | assert( this->last_time >= time ); | ||
58 | this->last_time -= time; | ||
59 | } | ||
60 | |||
61 | void Namco_run_until( struct Nes_Namco_Apu* this, blip_time_t nes_end_time ) | ||
62 | { | ||
63 | int active_oscs = (this->reg [0x7F] >> 4 & 7) + 1; | ||
64 | int i; | ||
65 | for ( i = namco_osc_count - active_oscs; i < namco_osc_count; i++ ) | ||
66 | { | ||
67 | struct Namco_Osc* osc = &this->oscs [i]; | ||
68 | struct Blip_Buffer* output = osc->output; | ||
69 | if ( !output ) | ||
70 | continue; | ||
71 | |||
72 | blip_resampled_time_t time = | ||
73 | Blip_resampled_time( output, this->last_time ) + osc->delay; | ||
74 | blip_resampled_time_t end_time = Blip_resampled_time( output, nes_end_time ); | ||
75 | osc->delay = 0; | ||
76 | if ( time < end_time ) | ||
77 | { | ||
78 | const uint8_t* osc_reg = &this->reg [i * 8 + 0x40]; | ||
79 | if ( !(osc_reg [4] & 0xE0) ) | ||
80 | continue; | ||
81 | |||
82 | int volume = osc_reg [7] & 15; | ||
83 | if ( !volume ) | ||
84 | continue; | ||
85 | |||
86 | int freq = (osc_reg [4] & 3) * 0x10000 + osc_reg [2] * 0x100L + osc_reg [0]; | ||
87 | if ( freq < 64 * active_oscs ) | ||
88 | continue; // prevent low frequencies from excessively delaying freq changes | ||
89 | |||
90 | int const master_clock_divider = 12; // NES time derived via divider of master clock | ||
91 | int const n106_divider = 45; // N106 then divides master clock by this | ||
92 | int const max_freq = 0x3FFFF; | ||
93 | int const lowest_freq_period = (max_freq + 1) * n106_divider / master_clock_divider; | ||
94 | // divide by 8 to avoid overflow | ||
95 | blip_resampled_time_t period = | ||
96 | Blip_resampled_duration( output, lowest_freq_period / 8 ) / freq * 8 * active_oscs; | ||
97 | |||
98 | int wave_size = 32 - (osc_reg [4] >> 2 & 7) * 4; | ||
99 | if ( !wave_size ) | ||
100 | continue; | ||
101 | |||
102 | int last_amp = osc->last_amp; | ||
103 | int wave_pos = osc->wave_pos; | ||
104 | |||
105 | Blip_set_modified( output ); | ||
106 | |||
107 | do | ||
108 | { | ||
109 | // read wave sample | ||
110 | int addr = wave_pos + osc_reg [6]; | ||
111 | int sample = this->reg [addr >> 1] >> (addr << 2 & 4); | ||
112 | wave_pos++; | ||
113 | sample = (sample & 15) * volume; | ||
114 | |||
115 | // output impulse if amplitude changed | ||
116 | int delta = sample - last_amp; | ||
117 | if ( delta ) | ||
118 | { | ||
119 | last_amp = sample; | ||
120 | Synth_offset_resampled( &this->synth, time, delta, output ); | ||
121 | } | ||
122 | |||
123 | // next sample | ||
124 | time += period; | ||
125 | if ( wave_pos >= wave_size ) | ||
126 | wave_pos = 0; | ||
127 | } | ||
128 | while ( time < end_time ); | ||
129 | |||
130 | osc->wave_pos = wave_pos; | ||
131 | osc->last_amp = last_amp; | ||
132 | } | ||
133 | osc->delay = time - end_time; | ||
134 | } | ||
135 | |||
136 | this->last_time = nes_end_time; | ||
137 | } | ||
138 | |||