diff options
Diffstat (limited to 'apps/codecs/libgme/nes_fme7_apu.h')
-rw-r--r-- | apps/codecs/libgme/nes_fme7_apu.h | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/apps/codecs/libgme/nes_fme7_apu.h b/apps/codecs/libgme/nes_fme7_apu.h new file mode 100644 index 0000000000..47b93e5cfe --- /dev/null +++ b/apps/codecs/libgme/nes_fme7_apu.h | |||
@@ -0,0 +1,90 @@ | |||
1 | // Sunsoft FME-7 sound emulator | ||
2 | |||
3 | // Game_Music_Emu 0.5.5 | ||
4 | #ifndef NES_FME7_APU_H | ||
5 | #define NES_FME7_APU_H | ||
6 | |||
7 | #include "blargg_common.h" | ||
8 | #include "blip_buffer.h" | ||
9 | |||
10 | enum { fme7_reg_count = 14 }; | ||
11 | |||
12 | // Mask and addresses of registers | ||
13 | enum { fme7_addr_mask = 0xE000 }; | ||
14 | enum { fme7_data_addr = 0xE000 }; | ||
15 | enum { fme7_latch_addr = 0xC000 }; | ||
16 | enum { fme7_osc_count = 3 }; | ||
17 | |||
18 | enum { amp_range = 192 }; // can be any value; this gives best error/quality tradeoff | ||
19 | |||
20 | struct osc_t { | ||
21 | struct Blip_Buffer* output; | ||
22 | int last_amp; | ||
23 | }; | ||
24 | |||
25 | // static unsigned char const amp_table [16]; | ||
26 | |||
27 | struct Nes_Fme7_Apu { | ||
28 | // fme7 apu state | ||
29 | uint8_t regs [fme7_reg_count]; | ||
30 | uint8_t phases [3]; // 0 or 1 | ||
31 | uint8_t latch; | ||
32 | uint16_t delays [3]; // a, b, c | ||
33 | |||
34 | struct osc_t oscs [fme7_osc_count]; | ||
35 | blip_time_t last_time; | ||
36 | |||
37 | struct Blip_Synth synth; | ||
38 | }; | ||
39 | |||
40 | // See Nes_Apu.h for reference | ||
41 | void Fme7_init( struct Nes_Fme7_Apu* this ); | ||
42 | void Fme7_reset( struct Nes_Fme7_Apu* this ); | ||
43 | |||
44 | static inline void Fme7_volume( struct Nes_Fme7_Apu* this, double v ) | ||
45 | { | ||
46 | Synth_volume( &this->synth, 0.38 / amp_range * v ); // to do: fine-tune | ||
47 | } | ||
48 | |||
49 | static inline void Fme7_osc_output( struct Nes_Fme7_Apu* this, int i, struct Blip_Buffer* buf ) | ||
50 | { | ||
51 | assert( (unsigned) i < fme7_osc_count ); | ||
52 | this->oscs [i].output = buf; | ||
53 | } | ||
54 | |||
55 | static inline void Fme7_output( struct Nes_Fme7_Apu* this, struct Blip_Buffer* buf ) | ||
56 | { | ||
57 | int i; | ||
58 | for ( i = 0; i < fme7_osc_count; i++ ) | ||
59 | Fme7_osc_output( this, i, buf ); | ||
60 | } | ||
61 | |||
62 | // (addr & addr_mask) == latch_addr | ||
63 | static inline void Fme7_write_latch( struct Nes_Fme7_Apu* this, int data ) { this->latch = data; } | ||
64 | |||
65 | // (addr & addr_mask) == data_addr | ||
66 | void Fme7_run_until( struct Nes_Fme7_Apu* this, blip_time_t end_time ) ICODE_ATTR; | ||
67 | static inline void Fme7_write_data( struct Nes_Fme7_Apu* this, blip_time_t time, int data ) | ||
68 | { | ||
69 | if ( (unsigned) this->latch >= fme7_reg_count ) | ||
70 | { | ||
71 | #ifdef debug_printf | ||
72 | debug_printf( "FME7 write to %02X (past end of sound registers)\n", (int) latch ); | ||
73 | #endif | ||
74 | return; | ||
75 | } | ||
76 | |||
77 | Fme7_run_until( this, time ); | ||
78 | this->regs [this->latch] = data; | ||
79 | } | ||
80 | |||
81 | static inline void Fme7_end_frame( struct Nes_Fme7_Apu* this, blip_time_t time ) | ||
82 | { | ||
83 | if ( time > this->last_time ) | ||
84 | Fme7_run_until( this, time ); | ||
85 | |||
86 | assert( this->last_time >= time ); | ||
87 | this->last_time -= time; | ||
88 | } | ||
89 | |||
90 | #endif | ||