2013-09-28 03:24:23 +00:00
|
|
|
// Game_Music_Emu $vers. http://www.slack.net/~ant/
|
|
|
|
|
|
|
|
#include "Ym2413_Emu.h"
|
2015-11-27 10:02:41 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "../vgmplay/chips/emu2413.h"
|
|
|
|
}
|
2013-09-28 03:24:23 +00:00
|
|
|
|
|
|
|
Ym2413_Emu::Ym2413_Emu() { opll = 0; }
|
|
|
|
|
|
|
|
Ym2413_Emu::~Ym2413_Emu()
|
|
|
|
{
|
2015-11-27 10:02:41 +00:00
|
|
|
if ( opll ) OPLL_delete( (OPLL *) opll );
|
2013-09-28 03:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Ym2413_Emu::set_rate( int sample_rate, int clock_rate )
|
|
|
|
{
|
|
|
|
if ( opll )
|
|
|
|
{
|
2015-11-27 10:02:41 +00:00
|
|
|
OPLL_delete( (OPLL *) opll );
|
2013-09-28 03:24:23 +00:00
|
|
|
opll = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-27 10:02:41 +00:00
|
|
|
opll = OPLL_new( clock_rate, sample_rate );
|
2013-09-28 03:24:23 +00:00
|
|
|
if ( !opll )
|
|
|
|
return 1;
|
2015-11-27 10:02:41 +00:00
|
|
|
|
|
|
|
OPLL_SetChipMode( (OPLL *) opll, 0 );
|
2013-09-28 03:24:23 +00:00
|
|
|
|
|
|
|
reset();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ym2413_Emu::reset()
|
|
|
|
{
|
2015-11-27 10:02:41 +00:00
|
|
|
OPLL_reset( (OPLL *) opll );
|
|
|
|
OPLL_SetMuteMask( (OPLL *) opll, 0 );
|
2013-09-28 03:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ym2413_Emu::write( int addr, int data )
|
|
|
|
{
|
2015-11-27 10:02:41 +00:00
|
|
|
OPLL_writeIO( (OPLL *) opll, 0, addr );
|
|
|
|
OPLL_writeIO( (OPLL *) opll, 1, data );
|
2013-09-28 03:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ym2413_Emu::mute_voices( int mask )
|
|
|
|
{
|
2015-11-27 10:02:41 +00:00
|
|
|
OPLL_SetMuteMask( (OPLL *) opll, mask );
|
2013-09-28 03:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ym2413_Emu::run( int pair_count, sample_t* out )
|
|
|
|
{
|
2015-11-27 10:02:41 +00:00
|
|
|
e_int32 bufMO[ 1024 ];
|
|
|
|
e_int32 bufRO[ 1024 ];
|
|
|
|
e_int32 * buffers[2] = { bufMO, bufRO };
|
2013-09-28 03:24:23 +00:00
|
|
|
|
|
|
|
while (pair_count > 0)
|
|
|
|
{
|
|
|
|
int todo = pair_count;
|
|
|
|
if (todo > 1024) todo = 1024;
|
2015-11-27 10:02:41 +00:00
|
|
|
OPLL_calc_stereo( (OPLL *) opll, buffers, todo, -1 );
|
2013-09-28 03:24:23 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < todo; i++)
|
|
|
|
{
|
|
|
|
int output_l, output_r;
|
|
|
|
int output = bufMO [i];
|
|
|
|
output += bufRO [i];
|
|
|
|
output *= 3;
|
|
|
|
output_l = output + out [0];
|
|
|
|
output_r = output + out [1];
|
|
|
|
if ( (short)output_l != output_l ) output_l = 0x7FFF ^ ( output_l >> 31 );
|
|
|
|
if ( (short)output_r != output_r ) output_r = 0x7FFF ^ ( output_r >> 31 );
|
|
|
|
out [0] = output_l;
|
|
|
|
out [1] = output_r;
|
|
|
|
out += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
pair_count -= todo;
|
|
|
|
}
|
|
|
|
}
|