1 // Konami SCC sound chip emulator
2 
3 // Game_Music_Emu 0.6.0
4 #ifndef KSS_SCC_APU_H
5 #define KSS_SCC_APU_H
6 
7 #include "blargg_common.h"
8 #include "Blip_Buffer.h"
9 #include <string.h>
10 
11 class Scc_Apu {
12 public:
13 	// Set buffer to generate all sound into, or disable sound if NULL
14 	void output( Blip_Buffer* );
15 
16 	// Reset sound chip
17 	void reset();
18 
19 	// Write to register at specified time
20 	enum { reg_count = 0x90 };
21 	void write( blip_time_t time, int reg, int data );
22 
23 	// Run sound to specified time, end current time frame, then start a new
24 	// time frame at time 0. Time frames have no effect on emulation and each
25 	// can be whatever length is convenient.
26 	void end_frame( blip_time_t length );
27 
28 // Additional features
29 
30 	// Set sound output of specific oscillator to buffer, where index is
31 	// 0 to 4. If buffer is NULL, the specified oscillator is muted.
32 	enum { osc_count = 5 };
33 	void osc_output( int index, Blip_Buffer* );
34 
35 	// Set overall volume (default is 1.0)
36 	void volume( double );
37 
38 	// Set treble equalization (see documentation)
39 	void treble_eq( blip_eq_t const& );
40 
41 public:
42 	Scc_Apu();
43 private:
44 	enum { amp_range = 0x8000 };
45 	struct osc_t
46 	{
47 		int delay;
48 		int phase;
49 		int last_amp;
50 		Blip_Buffer* output;
51 	};
52 	osc_t oscs [osc_count];
53 	blip_time_t last_time;
54 	unsigned char regs [reg_count];
55 	Blip_Synth<blip_med_quality,1> synth;
56 
57 	void run_until( blip_time_t );
58 };
59 
volume(double v)60 inline void Scc_Apu::volume( double v ) { synth.volume( 0.43 / osc_count / amp_range * v ); }
61 
treble_eq(blip_eq_t const & eq)62 inline void Scc_Apu::treble_eq( blip_eq_t const& eq ) { synth.treble_eq( eq ); }
63 
osc_output(int index,Blip_Buffer * b)64 inline void Scc_Apu::osc_output( int index, Blip_Buffer* b )
65 {
66 	assert( (unsigned) index < osc_count );
67 	oscs [index].output = b;
68 }
69 
write(blip_time_t time,int addr,int data)70 inline void Scc_Apu::write( blip_time_t time, int addr, int data )
71 {
72 	assert( (unsigned) addr < reg_count );
73 	run_until( time );
74 	regs [addr] = data;
75 }
76 
end_frame(blip_time_t end_time)77 inline void Scc_Apu::end_frame( blip_time_t end_time )
78 {
79 	if ( end_time > last_time )
80 		run_until( end_time );
81 	last_time -= end_time;
82 	assert( last_time >= 0 );
83 }
84 
output(Blip_Buffer * buf)85 inline void Scc_Apu::output( Blip_Buffer* buf )
86 {
87 	for ( int i = 0; i < osc_count; i++ )
88 		oscs [i].output = buf;
89 }
90 
Scc_Apu()91 inline Scc_Apu::Scc_Apu()
92 {
93 	output( 0 );
94 }
95 
reset()96 inline void Scc_Apu::reset()
97 {
98 	last_time = 0;
99 
100 	for ( int i = 0; i < osc_count; i++ )
101 		memset( &oscs [i], 0, offsetof (osc_t,output) );
102 
103 	memset( regs, 0, sizeof regs );
104 }
105 
106 #endif
107