Updated Game_Music_Emu to support Game Boy DMG sound in VGM files

CQTexperiment
Chris Moeller 2014-11-02 18:28:28 -08:00
parent 0290c1a3dd
commit 4acbb21f99
8 changed files with 3384 additions and 3300 deletions

View File

@ -161,6 +161,12 @@ void Gb_Apu::set_tempo( double t )
frame_period = t ? blip_time_t (frame_period / t) : blip_time_t(0); frame_period = t ? blip_time_t (frame_period / t) : blip_time_t(0);
} }
void Gb_Apu::set_hacks( unsigned int mask )
{
wave.set_volume_hack((mask & 1) != 0);
noise.set_volume_hack((mask & 2) == 0);
}
Gb_Apu::Gb_Apu() Gb_Apu::Gb_Apu()
{ {
wave.wave_ram = &regs [wave_ram - io_addr]; wave.wave_ram = &regs [wave_ram - io_addr];
@ -187,6 +193,7 @@ Gb_Apu::Gb_Apu()
set_tempo( 1.0 ); set_tempo( 1.0 );
volume_ = 1.0; volume_ = 1.0;
reset(); reset();
set_hacks(4);
} }
void Gb_Apu::run_until_( blip_time_t end_time ) void Gb_Apu::run_until_( blip_time_t end_time )

View File

@ -84,6 +84,11 @@ public:
// Loads state. You should call reset() BEFORE this. // Loads state. You should call reset() BEFORE this.
blargg_err_t load_state( gb_apu_state_t const& in ); blargg_err_t load_state( gb_apu_state_t const& in );
// Enable hacks (bitmask):
// 0x01 = Double wave channel volume
// 0x02 = Low noise channel volume (disable doubling it)
void set_hacks( unsigned int mask );
private: private:
// noncopyable // noncopyable
Gb_Apu( const Gb_Apu& ); Gb_Apu( const Gb_Apu& );

View File

@ -571,6 +571,7 @@ void Gb_Noise::run( blip_time_t time, blip_time_t end_time )
Blip_Synth_Fast const* const synth = fast_synth; // cache Blip_Synth_Fast const* const synth = fast_synth; // cache
// Output amplitude transitions // Output amplitude transitions
if (volume_hack) vol <<= 1;
int delta = -vol; int delta = -vol;
do do
{ {
@ -603,11 +604,11 @@ void Gb_Wave::run( blip_time_t time, blip_time_t end_time )
#if GB_APU_NO_AGB #if GB_APU_NO_AGB
static byte const shifts [4] = { 4+4, 0+4, 1+4, 2+4 }; static byte const shifts [4] = { 4+4, 0+4, 1+4, 2+4 };
int const volume_idx = regs [2] >> 5 & 3; int const volume_idx = regs [2] >> 5 & 3;
int const volume_shift = shifts [volume_idx]; int const volume_shift = shifts [volume_idx] - (volume_hack ? 1 : 0);
int const volume_mul = 1; int const volume_mul = 1;
#else #else
static byte const volumes [8] = { 0, 4, 2, 1, 3, 3, 3, 3 }; static byte const volumes [8] = { 0, 4, 2, 1, 3, 3, 3, 3 };
int const volume_shift = 2 + 4; int const volume_shift = 2 + 4 - (volume_hack ? 1 : 0);
int const volume_idx = regs [2] >> 5 & (agb_mask | 3); // 2 bits on DMG/CGB, 3 on AGB int const volume_idx = regs [2] >> 5 & (agb_mask | 3); // 2 bits on DMG/CGB, 3 on AGB
int const volume_mul = volumes [volume_idx]; int const volume_mul = volumes [volume_idx];
#endif #endif

View File

@ -115,6 +115,8 @@ public:
int divider; // noise has more complex frequency divider setup int divider; // noise has more complex frequency divider setup
bool volume_hack;
void run( blip_time_t, blip_time_t ); void run( blip_time_t, blip_time_t );
void write_register( int frame_phase, int reg, int old_data, int data ); void write_register( int frame_phase, int reg, int old_data, int data );
@ -123,7 +125,11 @@ public:
divider = 0; divider = 0;
Gb_Env::reset(); Gb_Env::reset();
delay = 4 * clk_mul; // TODO: remove? delay = 4 * clk_mul; // TODO: remove?
volume_hack = true;
} }
void set_volume_hack( bool enable );
private: private:
enum { period2_mask = 0x1FFFF }; enum { period2_mask = 0x1FFFF };
@ -132,10 +138,14 @@ private:
unsigned lfsr_mask() const { return (regs [3] & 0x08) ? ~0x4040 : ~0x4000; } unsigned lfsr_mask() const { return (regs [3] & 0x08) ? ~0x4040 : ~0x4000; }
}; };
inline void Gb_Noise::set_volume_hack( bool enable ) { volume_hack = enable; }
class Gb_Wave : public Gb_Osc { class Gb_Wave : public Gb_Osc {
public: public:
int sample_buf; // last wave RAM byte read (hardware has this as well) int sample_buf; // last wave RAM byte read (hardware has this as well)
bool volume_hack;
void write_register( int frame_phase, int reg, int old_data, int data ); void write_register( int frame_phase, int reg, int old_data, int data );
void run( blip_time_t, blip_time_t ); void run( blip_time_t, blip_time_t );
@ -147,6 +157,7 @@ public:
{ {
sample_buf = 0; sample_buf = 0;
Gb_Osc::reset(); Gb_Osc::reset();
volume_hack = false;
} }
private: private:
@ -166,12 +177,16 @@ private:
void corrupt_wave(); void corrupt_wave();
void set_volume_hack( bool enable );
BOOST::uint8_t* wave_bank() const { return &wave_ram [(~regs [0] & bank40_mask) >> 2 & agb_mask]; } BOOST::uint8_t* wave_bank() const { return &wave_ram [(~regs [0] & bank40_mask) >> 2 & agb_mask]; }
// Wave index that would be accessed, or -1 if no access would occur // Wave index that would be accessed, or -1 if no access would occur
int access( int addr ) const; int access( int addr ) const;
}; };
inline void Gb_Wave::set_volume_hack( bool enable ) { volume_hack = enable; }
inline int Gb_Wave::read( int addr ) const inline int Gb_Wave::read( int addr ) const
{ {
int index = access( addr ); int index = access( addr );

View File

@ -76,6 +76,7 @@ enum {
cmd_rf5c68 = 0xB0, cmd_rf5c68 = 0xB0,
cmd_rf5c164 = 0xB1, cmd_rf5c164 = 0xB1,
cmd_pwm = 0xB2, cmd_pwm = 0xB2,
cmd_gbdmg_write = 0xB3,
cmd_okim6258_write = 0xB7, cmd_okim6258_write = 0xB7,
cmd_okim6295_write = 0xB8, cmd_okim6295_write = 0xB8,
cmd_huc6280_write = 0xB9, cmd_huc6280_write = 0xB9,
@ -712,6 +713,10 @@ void Vgm_Core::chip_reg_write(unsigned Sample, byte ChipType, byte ChipID, byte
ay[ChipID].write_data( to_ay_time( Sample ), Data ); ay[ChipID].write_data( to_ay_time( Sample ), Data );
break; break;
case 0x13:
gbdmg[ChipID].write_register( to_gbdmg_time( Sample ), 0xFF10 + Offset, Data );
break;
case 0x17: case 0x17:
if ( run_okim6258( ChipID, to_fm_time( Sample ) ) ) if ( run_okim6258( ChipID, to_fm_time( Sample ) ) )
okim6258[ChipID].write( Offset, Data ); okim6258[ChipID].write( Offset, Data );
@ -759,6 +764,8 @@ void Vgm_Core::set_tempo( double t )
(1 << blip_time_bits) / vgm_rate * stereo_buf[1].center()->clock_rate() + 0.5); (1 << blip_time_bits) / vgm_rate * stereo_buf[1].center()->clock_rate() + 0.5);
blip_huc6280_time_factor = (int) ((double) blip_huc6280_time_factor = (int) ((double)
(1 << blip_time_bits) / vgm_rate * stereo_buf[2].center()->clock_rate() + 0.5); (1 << blip_time_bits) / vgm_rate * stereo_buf[2].center()->clock_rate() + 0.5);
blip_gbdmg_time_factor = (int)((double)
(1 << blip_time_bits) / vgm_rate * stereo_buf[3].center()->clock_rate() + 0.5);
//dprintf( "blip_time_factor: %ld\n", blip_time_factor ); //dprintf( "blip_time_factor: %ld\n", blip_time_factor );
//dprintf( "vgm_rate: %ld\n", vgm_rate ); //dprintf( "vgm_rate: %ld\n", vgm_rate );
// TODO: remove? calculates vgm_rate more accurately (above differs at most by one Hz only) // TODO: remove? calculates vgm_rate more accurately (above differs at most by one Hz only)
@ -884,6 +891,14 @@ blargg_err_t Vgm_Core::load_mem_( byte const data [], int size )
huc6280_rate = 3579545; huc6280_rate = 3579545;
stereo_buf[2].clock_rate( huc6280_rate * 2 ); stereo_buf[2].clock_rate( huc6280_rate * 2 );
int gbdmg_rate = get_le32( h.gbdmg_rate ) & 0xBFFFFFFF;
if ( !gbdmg_rate )
gbdmg_rate = Gb_Apu::clock_rate;
stereo_buf[3].clock_rate( gbdmg_rate );
const int gbdmg_hacks = 3;
gbdmg[0].set_hacks( gbdmg_hacks );
gbdmg[1].set_hacks( gbdmg_hacks );
// Disable FM // Disable FM
fm_rate = 0; fm_rate = 0;
ymz280b.enable( false ); ymz280b.enable( false );
@ -1376,6 +1391,8 @@ void Vgm_Core::start_track()
ay[1].reset(); ay[1].reset();
huc6280[0].reset(); huc6280[0].reset();
huc6280[1].reset(); huc6280[1].reset();
gbdmg[0].reset();
gbdmg[1].reset();
blip_buf[0] = stereo_buf[0].center(); blip_buf[0] = stereo_buf[0].center();
blip_buf[1] = blip_buf[0]; blip_buf[1] = blip_buf[0];
@ -1490,6 +1507,7 @@ void Vgm_Core::start_track()
stereo_buf[0].clear(); stereo_buf[0].clear();
stereo_buf[1].clear(); stereo_buf[1].clear();
stereo_buf[2].clear(); stereo_buf[2].clear();
stereo_buf[3].clear();
} }
for ( unsigned i = 0; i < DacCtrlUsed; i++ ) for ( unsigned i = 0; i < DacCtrlUsed; i++ )
@ -1510,6 +1528,7 @@ void Vgm_Core::start_track()
fm_time_offset = 0; fm_time_offset = 0;
ay_time_offset = 0; ay_time_offset = 0;
huc6280_time_offset = 0; huc6280_time_offset = 0;
gbdmg_time_offset = 0;
dac_control_recursion = 0; dac_control_recursion = 0;
} }
@ -1534,6 +1553,11 @@ inline blip_time_t Vgm_Core::to_huc6280_time( vgm_time_t t ) const
return (t * blip_huc6280_time_factor) >> blip_time_bits; return (t * blip_huc6280_time_factor) >> blip_time_bits;
} }
inline blip_time_t Vgm_Core::to_gbdmg_time( vgm_time_t t ) const
{
return (t * blip_gbdmg_time_factor) >> blip_time_bits;
}
void Vgm_Core::write_pcm( vgm_time_t vgm_time, int chip, int amp ) void Vgm_Core::write_pcm( vgm_time_t vgm_time, int chip, int amp )
{ {
chip = !!chip; chip = !!chip;
@ -1798,6 +1822,12 @@ blip_time_t Vgm_Core::run( vgm_time_t end_time )
pos += 2; pos += 2;
break; break;
case cmd_gbdmg_write:
ChipID = (pos [0] & 0x80) ? 1 : 0;
chip_reg_write( vgm_time, 0x13, ChipID, 0x00, pos [0] & 0x7F, pos [1] );
pos += 2;
break;
case cmd_k051649_write: case cmd_k051649_write:
chip_reg_write( vgm_time, 0x19, 0x00, pos [0] & 0x7F, pos [1], pos [2] ); chip_reg_write( vgm_time, 0x19, 0x00, pos [0] & 0x7F, pos [1], pos [2] );
pos += 3; pos += 3;
@ -2265,6 +2295,12 @@ int Vgm_Core::play_frame( blip_time_t blip_time, int sample_count, blip_sample_t
huc6280[0].end_frame( huc6280_end_time ); huc6280[0].end_frame( huc6280_end_time );
huc6280[1].end_frame( huc6280_end_time ); huc6280[1].end_frame( huc6280_end_time );
gbdmg_time_offset = (vgm_time * blip_gbdmg_time_factor + gbdmg_time_offset) - (pairs << blip_time_bits);
blip_time_t gbdmg_end_time = to_gbdmg_time( vgm_time );
gbdmg[0].end_frame( gbdmg_end_time );
gbdmg[1].end_frame( gbdmg_end_time );
memset( DacCtrlTime, 0, sizeof(DacCtrlTime) ); memset( DacCtrlTime, 0, sizeof(DacCtrlTime) );
return pairs * stereo; return pairs * stereo;

View File

@ -26,6 +26,7 @@
#include "Qsound_Apu.h" #include "Qsound_Apu.h"
#include "Ym2203_Emu.h" #include "Ym2203_Emu.h"
#include "Ay_Apu.h" #include "Ay_Apu.h"
#include "Gb_Apu.h"
#include "Hes_Apu.h" #include "Hes_Apu.h"
#include "Sms_Apu.h" #include "Sms_Apu.h"
#include "Multi_Buffer.h" #include "Multi_Buffer.h"
@ -154,7 +155,8 @@ public:
k051649.enabled() || k053260.enabled() || k054539.enabled() || ym2203[0].enabled() || ym3812[0].enabled() || ymf262[0].enabled() || k051649.enabled() || k053260.enabled() || k054539.enabled() || ym2203[0].enabled() || ym3812[0].enabled() || ymf262[0].enabled() ||
ymz280b.enabled() || ym2610[0].enabled() || ym2608[0].enabled() || qsound[0].enabled() || ymz280b.enabled() || ym2610[0].enabled() || ym2608[0].enabled() || qsound[0].enabled() ||
(header().ay8910_rate[0] | header().ay8910_rate[1] | header().ay8910_rate[2] | header().ay8910_rate[3]) || (header().ay8910_rate[0] | header().ay8910_rate[1] | header().ay8910_rate[2] | header().ay8910_rate[3]) ||
(header().huc6280_rate[0] | header().huc6280_rate[1] | header().huc6280_rate[2] | header().huc6280_rate[3]); } (header().huc6280_rate[0] | header().huc6280_rate[1] | header().huc6280_rate[2] | header().huc6280_rate[3]) ||
(header().gbdmg_rate[0] | header().gbdmg_rate[1] | header().gbdmg_rate[2] | header().gbdmg_rate[3]); }
// Adjusts music tempo, where 1.0 is normal. Can be changed while playing. // Adjusts music tempo, where 1.0 is normal. Can be changed while playing.
// Loading a file resets tempo to 1.0. // Loading a file resets tempo to 1.0.
@ -175,8 +177,8 @@ public:
// True if all of file data has been played // True if all of file data has been played
bool track_ended() const { return pos >= file_end(); } bool track_ended() const { return pos >= file_end(); }
// 0 for PSG and YM2612 DAC, 1 for AY, 2 for HuC6280 // 0 for PSG and YM2612 DAC, 1 for AY, 2 for HuC6280, 3 for GB DMG
Stereo_Buffer stereo_buf[3]; Stereo_Buffer stereo_buf[4];
// PCM sound is always generated here // PCM sound is always generated here
Blip_Buffer * blip_buf[2]; Blip_Buffer * blip_buf[2];
@ -185,6 +187,7 @@ public:
Sms_Apu psg[2]; Sms_Apu psg[2];
Ay_Apu ay[2]; Ay_Apu ay[2];
Hes_Apu huc6280[2]; Hes_Apu huc6280[2];
Gb_Apu gbdmg[2];
// PCM synth, for setting volume and EQ // PCM synth, for setting volume and EQ
Blip_Synth_Fast pcm; Blip_Synth_Fast pcm;
@ -272,6 +275,10 @@ private:
int huc6280_time_offset; int huc6280_time_offset;
blip_time_t to_huc6280_time( vgm_time_t ) const; blip_time_t to_huc6280_time( vgm_time_t ) const;
int blip_gbdmg_time_factor;
int gbdmg_time_offset;
blip_time_t to_gbdmg_time( vgm_time_t ) const;
// Current time and position in log // Current time and position in log
vgm_time_t vgm_time; vgm_time_t vgm_time;
byte const* pos; byte const* pos;

View File

@ -319,6 +319,7 @@ blargg_err_t Vgm_Emu::set_sample_rate_( int sample_rate )
RETURN_ERR( core.stereo_buf[0].set_sample_rate( sample_rate, 1000 / 30 ) ); RETURN_ERR( core.stereo_buf[0].set_sample_rate( sample_rate, 1000 / 30 ) );
RETURN_ERR( core.stereo_buf[1].set_sample_rate( sample_rate, 1000 / 30 ) ); RETURN_ERR( core.stereo_buf[1].set_sample_rate( sample_rate, 1000 / 30 ) );
RETURN_ERR( core.stereo_buf[2].set_sample_rate( sample_rate, 1000 / 30 ) ); RETURN_ERR( core.stereo_buf[2].set_sample_rate( sample_rate, 1000 / 30 ) );
RETURN_ERR( core.stereo_buf[3].set_sample_rate( sample_rate, 1000 / 30 ) );
core.set_sample_rate(sample_rate); core.set_sample_rate(sample_rate);
return Classic_Emu::set_sample_rate_( sample_rate ); return Classic_Emu::set_sample_rate_( sample_rate );
} }
@ -331,6 +332,8 @@ void Vgm_Emu::update_eq( blip_eq_t const& eq )
core.ay[1].treble_eq( eq ); core.ay[1].treble_eq( eq );
core.huc6280[0].treble_eq( eq ); core.huc6280[0].treble_eq( eq );
core.huc6280[1].treble_eq( eq ); core.huc6280[1].treble_eq( eq );
core.gbdmg[0].treble_eq( eq );
core.gbdmg[1].treble_eq( eq );
core.pcm.treble_eq( eq ); core.pcm.treble_eq( eq );
} }
@ -366,6 +369,14 @@ void Vgm_Emu::mute_voices_( int mask )
Blip_Buffer * right = ( mask & j ) ? 0 : core.stereo_buf[2].right(); Blip_Buffer * right = ( mask & j ) ? 0 : core.stereo_buf[2].right();
core.huc6280[0].set_output( i, center, left, right ); core.huc6280[0].set_output( i, center, left, right );
core.huc6280[1].set_output( i, center, left, right ); core.huc6280[1].set_output( i, center, left, right );
}
for (unsigned i = 0, j = 1; i < core.gbdmg[0].osc_count; i++, j <<= 1)
{
Blip_Buffer * center = (mask & j) ? 0 : core.stereo_buf[3].center();
Blip_Buffer * left = (mask & j) ? 0 : core.stereo_buf[3].left();
Blip_Buffer * right = (mask & j) ? 0 : core.stereo_buf[3].right();
core.gbdmg[0].set_output(i, center, left, right);
core.gbdmg[1].set_output(i, center, left, right);
} }
if (core.ym2612[0].enabled()) if (core.ym2612[0].enabled())
{ {
@ -441,6 +452,8 @@ blargg_err_t Vgm_Emu::load_mem_( byte const data [], int size )
core.ay[1].volume( 0.135 * fm_gain * gain() ); core.ay[1].volume( 0.135 * fm_gain * gain() );
core.huc6280[0].volume( 0.135 * fm_gain * gain() ); core.huc6280[0].volume( 0.135 * fm_gain * gain() );
core.huc6280[1].volume( 0.135 * fm_gain * gain() ); core.huc6280[1].volume( 0.135 * fm_gain * gain() );
core.gbdmg[0].volume( 0.135 * fm_gain * gain() );
core.gbdmg[1].volume( 0.135 * fm_gain * gain() );
} }
else else
{ {
@ -518,8 +531,8 @@ blargg_err_t Vgm_Emu::play_( int count, sample_t out [] )
if ( !core.uses_fm() ) if ( !core.uses_fm() )
return Classic_Emu::play_( count, out ); return Classic_Emu::play_( count, out );
Stereo_Buffer * secondaries[] = { &core.stereo_buf[1], &core.stereo_buf[2] }; Stereo_Buffer * secondaries[] = { &core.stereo_buf[1], &core.stereo_buf[2], &core.stereo_buf[3] };
resampler.dual_play( count, out, core.stereo_buf[0], secondaries, 2 ); resampler.dual_play( count, out, core.stereo_buf[0], secondaries, 3 );
return blargg_ok; return blargg_ok;
} }