1043 lines
28 KiB
C
1043 lines
28 KiB
C
![]() |
|
||
|
typedef struct _opl3_slot opl3_slot;
|
||
|
typedef struct _opl3_channel opl3_channel;
|
||
|
typedef struct _opl3_chip opl3_chip;
|
||
|
|
||
|
struct _opl3_slot {
|
||
|
opl3_channel *channel;
|
||
|
opl3_chip *chip;
|
||
|
int16_t out;
|
||
|
int16_t fbmod;
|
||
|
int16_t *mod;
|
||
|
int16_t prout;
|
||
|
uint16_t eg_rout;
|
||
|
uint16_t eg_out;
|
||
|
// uint8_t eg_inc;
|
||
|
uint8_t eg_gen;
|
||
|
// uint8_t eg_rate;
|
||
|
uint8_t eg_ksl;
|
||
|
uint8_t *trem;
|
||
|
uint8_t reg_vib;
|
||
|
uint8_t reg_type;
|
||
|
uint8_t reg_ksr;
|
||
|
uint8_t reg_mult;
|
||
|
uint8_t reg_ksl;
|
||
|
uint8_t reg_tl;
|
||
|
uint8_t reg_ar;
|
||
|
uint8_t reg_dr;
|
||
|
uint8_t reg_sl;
|
||
|
uint8_t reg_rr;
|
||
|
uint8_t reg_wf;
|
||
|
uint8_t key;
|
||
|
uint8_t detrigger;
|
||
|
uint8_t retrigger;
|
||
|
uint32_t pg_reset;
|
||
|
uint32_t pg_phase;
|
||
|
uint16_t pg_phase_out;
|
||
|
uint8_t slot_num;
|
||
|
};
|
||
|
|
||
|
struct _opl3_channel {
|
||
|
opl3_slot *slots[2];
|
||
|
opl3_channel *pair;
|
||
|
opl3_chip *chip;
|
||
|
int16_t *out[4];
|
||
|
|
||
|
int32_t level[2];
|
||
|
|
||
|
uint8_t chtype;
|
||
|
uint16_t f_num;
|
||
|
uint8_t block;
|
||
|
uint8_t fb;
|
||
|
uint8_t con;
|
||
|
uint8_t alg;
|
||
|
uint8_t ksv;
|
||
|
uint8_t ch_num;
|
||
|
};
|
||
|
|
||
|
struct _opl3_chip {
|
||
|
opl3_channel *channel;
|
||
|
opl3_slot *slot;
|
||
|
uint16_t timer;
|
||
|
uint64_t eg_timer;
|
||
|
uint8_t eg_timerrem;
|
||
|
uint8_t eg_state;
|
||
|
uint8_t eg_add;
|
||
|
uint8_t nts;
|
||
|
uint8_t vibpos;
|
||
|
uint8_t vibshift;
|
||
|
uint8_t tremolo;
|
||
|
uint8_t tremolopos;
|
||
|
uint8_t tremoloshift;
|
||
|
uint8_t n_voices;
|
||
|
uint32_t noise;
|
||
|
int16_t zeromod;
|
||
|
int32_t mixbuff[4];
|
||
|
|
||
|
int32_t rateratio;
|
||
|
int32_t samplecnt;
|
||
|
int16_t oldsamples[2];
|
||
|
int16_t samples[2];
|
||
|
};
|
||
|
|
||
|
/* input: [0, 256), output: [0, 65536] */
|
||
|
#define OPL_SIN(x) ((int32_t)(sin((x) * M_PI / 512.0) * 65536.0))
|
||
|
|
||
|
#define RSM_FRAC 10
|
||
|
|
||
|
enum {
|
||
|
ch_2op = 0,
|
||
|
ch_4op = 1,
|
||
|
ch_4op2 = 2
|
||
|
};
|
||
|
|
||
|
// Tables
|
||
|
|
||
|
static const uint16_t logsinrom[256] = {
|
||
|
0x859, 0x6c3, 0x607, 0x58b, 0x52e, 0x4e4, 0x4a6, 0x471,
|
||
|
0x443, 0x41a, 0x3f5, 0x3d3, 0x3b5, 0x398, 0x37e, 0x365,
|
||
|
0x34e, 0x339, 0x324, 0x311, 0x2ff, 0x2ed, 0x2dc, 0x2cd,
|
||
|
0x2bd, 0x2af, 0x2a0, 0x293, 0x286, 0x279, 0x26d, 0x261,
|
||
|
0x256, 0x24b, 0x240, 0x236, 0x22c, 0x222, 0x218, 0x20f,
|
||
|
0x206, 0x1fd, 0x1f5, 0x1ec, 0x1e4, 0x1dc, 0x1d4, 0x1cd,
|
||
|
0x1c5, 0x1be, 0x1b7, 0x1b0, 0x1a9, 0x1a2, 0x19b, 0x195,
|
||
|
0x18f, 0x188, 0x182, 0x17c, 0x177, 0x171, 0x16b, 0x166,
|
||
|
0x160, 0x15b, 0x155, 0x150, 0x14b, 0x146, 0x141, 0x13c,
|
||
|
0x137, 0x133, 0x12e, 0x129, 0x125, 0x121, 0x11c, 0x118,
|
||
|
0x114, 0x10f, 0x10b, 0x107, 0x103, 0x0ff, 0x0fb, 0x0f8,
|
||
|
0x0f4, 0x0f0, 0x0ec, 0x0e9, 0x0e5, 0x0e2, 0x0de, 0x0db,
|
||
|
0x0d7, 0x0d4, 0x0d1, 0x0cd, 0x0ca, 0x0c7, 0x0c4, 0x0c1,
|
||
|
0x0be, 0x0bb, 0x0b8, 0x0b5, 0x0b2, 0x0af, 0x0ac, 0x0a9,
|
||
|
0x0a7, 0x0a4, 0x0a1, 0x09f, 0x09c, 0x099, 0x097, 0x094,
|
||
|
0x092, 0x08f, 0x08d, 0x08a, 0x088, 0x086, 0x083, 0x081,
|
||
|
0x07f, 0x07d, 0x07a, 0x078, 0x076, 0x074, 0x072, 0x070,
|
||
|
0x06e, 0x06c, 0x06a, 0x068, 0x066, 0x064, 0x062, 0x060,
|
||
|
0x05e, 0x05c, 0x05b, 0x059, 0x057, 0x055, 0x053, 0x052,
|
||
|
0x050, 0x04e, 0x04d, 0x04b, 0x04a, 0x048, 0x046, 0x045,
|
||
|
0x043, 0x042, 0x040, 0x03f, 0x03e, 0x03c, 0x03b, 0x039,
|
||
|
0x038, 0x037, 0x035, 0x034, 0x033, 0x031, 0x030, 0x02f,
|
||
|
0x02e, 0x02d, 0x02b, 0x02a, 0x029, 0x028, 0x027, 0x026,
|
||
|
0x025, 0x024, 0x023, 0x022, 0x021, 0x020, 0x01f, 0x01e,
|
||
|
0x01d, 0x01c, 0x01b, 0x01a, 0x019, 0x018, 0x017, 0x017,
|
||
|
0x016, 0x015, 0x014, 0x014, 0x013, 0x012, 0x011, 0x011,
|
||
|
0x010, 0x00f, 0x00f, 0x00e, 0x00d, 0x00d, 0x00c, 0x00c,
|
||
|
0x00b, 0x00a, 0x00a, 0x009, 0x009, 0x008, 0x008, 0x007,
|
||
|
0x007, 0x007, 0x006, 0x006, 0x005, 0x005, 0x005, 0x004,
|
||
|
0x004, 0x004, 0x003, 0x003, 0x003, 0x002, 0x002, 0x002,
|
||
|
0x002, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
|
||
|
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000
|
||
|
};
|
||
|
|
||
|
static const uint16_t exprom[256] = {
|
||
|
0x7fa, 0x7f5, 0x7ef, 0x7ea, 0x7e4, 0x7df, 0x7da, 0x7d4,
|
||
|
0x7cf, 0x7c9, 0x7c4, 0x7bf, 0x7b9, 0x7b4, 0x7ae, 0x7a9,
|
||
|
0x7a4, 0x79f, 0x799, 0x794, 0x78f, 0x78a, 0x784, 0x77f,
|
||
|
0x77a, 0x775, 0x770, 0x76a, 0x765, 0x760, 0x75b, 0x756,
|
||
|
0x751, 0x74c, 0x747, 0x742, 0x73d, 0x738, 0x733, 0x72e,
|
||
|
0x729, 0x724, 0x71f, 0x71a, 0x715, 0x710, 0x70b, 0x706,
|
||
|
0x702, 0x6fd, 0x6f8, 0x6f3, 0x6ee, 0x6e9, 0x6e5, 0x6e0,
|
||
|
0x6db, 0x6d6, 0x6d2, 0x6cd, 0x6c8, 0x6c4, 0x6bf, 0x6ba,
|
||
|
0x6b5, 0x6b1, 0x6ac, 0x6a8, 0x6a3, 0x69e, 0x69a, 0x695,
|
||
|
0x691, 0x68c, 0x688, 0x683, 0x67f, 0x67a, 0x676, 0x671,
|
||
|
0x66d, 0x668, 0x664, 0x65f, 0x65b, 0x657, 0x652, 0x64e,
|
||
|
0x649, 0x645, 0x641, 0x63c, 0x638, 0x634, 0x630, 0x62b,
|
||
|
0x627, 0x623, 0x61e, 0x61a, 0x616, 0x612, 0x60e, 0x609,
|
||
|
0x605, 0x601, 0x5fd, 0x5f9, 0x5f5, 0x5f0, 0x5ec, 0x5e8,
|
||
|
0x5e4, 0x5e0, 0x5dc, 0x5d8, 0x5d4, 0x5d0, 0x5cc, 0x5c8,
|
||
|
0x5c4, 0x5c0, 0x5bc, 0x5b8, 0x5b4, 0x5b0, 0x5ac, 0x5a8,
|
||
|
0x5a4, 0x5a0, 0x59c, 0x599, 0x595, 0x591, 0x58d, 0x589,
|
||
|
0x585, 0x581, 0x57e, 0x57a, 0x576, 0x572, 0x56f, 0x56b,
|
||
|
0x567, 0x563, 0x560, 0x55c, 0x558, 0x554, 0x551, 0x54d,
|
||
|
0x549, 0x546, 0x542, 0x53e, 0x53b, 0x537, 0x534, 0x530,
|
||
|
0x52c, 0x529, 0x525, 0x522, 0x51e, 0x51b, 0x517, 0x514,
|
||
|
0x510, 0x50c, 0x509, 0x506, 0x502, 0x4ff, 0x4fb, 0x4f8,
|
||
|
0x4f4, 0x4f1, 0x4ed, 0x4ea, 0x4e7, 0x4e3, 0x4e0, 0x4dc,
|
||
|
0x4d9, 0x4d6, 0x4d2, 0x4cf, 0x4cc, 0x4c8, 0x4c5, 0x4c2,
|
||
|
0x4be, 0x4bb, 0x4b8, 0x4b5, 0x4b1, 0x4ae, 0x4ab, 0x4a8,
|
||
|
0x4a4, 0x4a1, 0x49e, 0x49b, 0x498, 0x494, 0x491, 0x48e,
|
||
|
0x48b, 0x488, 0x485, 0x482, 0x47e, 0x47b, 0x478, 0x475,
|
||
|
0x472, 0x46f, 0x46c, 0x469, 0x466, 0x463, 0x460, 0x45d,
|
||
|
0x45a, 0x457, 0x454, 0x451, 0x44e, 0x44b, 0x448, 0x445,
|
||
|
0x442, 0x43f, 0x43c, 0x439, 0x436, 0x433, 0x430, 0x42d,
|
||
|
0x42a, 0x428, 0x425, 0x422, 0x41f, 0x41c, 0x419, 0x416,
|
||
|
0x414, 0x411, 0x40e, 0x40b, 0x408, 0x406, 0x403, 0x400
|
||
|
};
|
||
|
|
||
|
static const uint8_t mt[16] = {
|
||
|
1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30
|
||
|
};
|
||
|
|
||
|
static const uint8_t kslrom[16] = {
|
||
|
0, 32, 40, 45, 48, 51, 53, 55, 56, 58, 59, 60, 61, 62, 63, 64
|
||
|
};
|
||
|
|
||
|
static const uint8_t kslshift[4] = {
|
||
|
8, 1, 2, 0
|
||
|
};
|
||
|
|
||
|
static const uint8_t eg_incstep[4][4] = {
|
||
|
{ 0, 0, 0, 0 },
|
||
|
{ 1, 0, 0, 0 },
|
||
|
{ 1, 0, 1, 0 },
|
||
|
{ 1, 1, 1, 0 }
|
||
|
};
|
||
|
|
||
|
// Envelope generator
|
||
|
|
||
|
typedef int16_t(*envelope_sinfunc)(uint16_t phase, uint16_t envelope);
|
||
|
typedef void(*envelope_genfunc)(opl3_slot *slott);
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcExp(uint32_t level)
|
||
|
{
|
||
|
if (level > 0x1fff)
|
||
|
{
|
||
|
level = 0x1fff;
|
||
|
}
|
||
|
return (exprom[level & 0xff] << 1) >> (level >> 8);
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin0(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t out = 0;
|
||
|
uint16_t neg = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if (phase & 0x200)
|
||
|
{
|
||
|
neg = 0xffff;
|
||
|
}
|
||
|
if (phase & 0x100)
|
||
|
{
|
||
|
out = logsinrom[(phase & 0xff) ^ 0xff];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
out = logsinrom[phase & 0xff];
|
||
|
}
|
||
|
return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin1(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t out = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if (phase & 0x200)
|
||
|
{
|
||
|
out = 0x1000;
|
||
|
}
|
||
|
else if (phase & 0x100)
|
||
|
{
|
||
|
out = logsinrom[(phase & 0xff) ^ 0xff];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
out = logsinrom[phase & 0xff];
|
||
|
}
|
||
|
return OPL3_EnvelopeCalcExp(out + (envelope << 3));
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin2(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t out = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if (phase & 0x100)
|
||
|
{
|
||
|
out = logsinrom[(phase & 0xff) ^ 0xff];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
out = logsinrom[phase & 0xff];
|
||
|
}
|
||
|
return OPL3_EnvelopeCalcExp(out + (envelope << 3));
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin3(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t out = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if (phase & 0x100)
|
||
|
{
|
||
|
out = 0x1000;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
out = logsinrom[phase & 0xff];
|
||
|
}
|
||
|
return OPL3_EnvelopeCalcExp(out + (envelope << 3));
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin4(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t out = 0;
|
||
|
uint16_t neg = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if ((phase & 0x300) == 0x100)
|
||
|
{
|
||
|
neg = 0xffff;
|
||
|
}
|
||
|
if (phase & 0x200)
|
||
|
{
|
||
|
out = 0x1000;
|
||
|
}
|
||
|
else if (phase & 0x80)
|
||
|
{
|
||
|
out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
out = logsinrom[(phase << 1) & 0xff];
|
||
|
}
|
||
|
return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin5(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t out = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if (phase & 0x200)
|
||
|
{
|
||
|
out = 0x1000;
|
||
|
}
|
||
|
else if (phase & 0x80)
|
||
|
{
|
||
|
out = logsinrom[((phase ^ 0xff) << 1) & 0xff];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
out = logsinrom[(phase << 1) & 0xff];
|
||
|
}
|
||
|
return OPL3_EnvelopeCalcExp(out + (envelope << 3));
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin6(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t neg = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if (phase & 0x200)
|
||
|
{
|
||
|
neg = 0xffff;
|
||
|
}
|
||
|
return OPL3_EnvelopeCalcExp(envelope << 3) ^ neg;
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_EnvelopeCalcSin7(uint16_t phase, uint16_t envelope)
|
||
|
{
|
||
|
uint16_t out = 0;
|
||
|
uint16_t neg = 0;
|
||
|
phase &= 0x3ff;
|
||
|
if (phase & 0x200)
|
||
|
{
|
||
|
neg = 0xffff;
|
||
|
phase = (phase & 0x1ff) ^ 0x1ff;
|
||
|
}
|
||
|
out = phase << 3;
|
||
|
return OPL3_EnvelopeCalcExp(out + (envelope << 3)) ^ neg;
|
||
|
}
|
||
|
|
||
|
static const envelope_sinfunc envelope_sin[8] = {
|
||
|
OPL3_EnvelopeCalcSin0,
|
||
|
OPL3_EnvelopeCalcSin1,
|
||
|
OPL3_EnvelopeCalcSin2,
|
||
|
OPL3_EnvelopeCalcSin3,
|
||
|
OPL3_EnvelopeCalcSin4,
|
||
|
OPL3_EnvelopeCalcSin5,
|
||
|
OPL3_EnvelopeCalcSin6,
|
||
|
OPL3_EnvelopeCalcSin7
|
||
|
};
|
||
|
|
||
|
enum envelope_gen_num
|
||
|
{
|
||
|
envelope_gen_num_attack = 0,
|
||
|
envelope_gen_num_decay = 1,
|
||
|
envelope_gen_num_sustain = 2,
|
||
|
envelope_gen_num_release = 3
|
||
|
};
|
||
|
|
||
|
void OPL3_EnvelopeCalc(opl3_slot *slot)
|
||
|
{
|
||
|
uint8_t nonzero;
|
||
|
uint8_t rate;
|
||
|
uint8_t rate_hi;
|
||
|
uint8_t rate_lo;
|
||
|
uint8_t reg_rate = 0;
|
||
|
uint8_t ks;
|
||
|
uint8_t eg_shift, shift;
|
||
|
uint16_t eg_rout;
|
||
|
int16_t eg_inc;
|
||
|
uint8_t eg_off;
|
||
|
uint8_t reset = 0;
|
||
|
if(slot->retrigger) {
|
||
|
slot->eg_rout = 0x1ff;
|
||
|
}
|
||
|
slot->eg_out = slot->eg_rout + (slot->reg_tl << 2)
|
||
|
+ (slot->eg_ksl >> kslshift[slot->reg_ksl]) + *slot->trem;
|
||
|
if (slot->key && slot->eg_gen == envelope_gen_num_release)
|
||
|
{
|
||
|
reset = 1;
|
||
|
reg_rate = slot->reg_ar;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
switch (slot->eg_gen)
|
||
|
{
|
||
|
case envelope_gen_num_attack:
|
||
|
reg_rate = slot->reg_ar;
|
||
|
break;
|
||
|
case envelope_gen_num_decay:
|
||
|
reg_rate = slot->reg_dr;
|
||
|
break;
|
||
|
case envelope_gen_num_sustain:
|
||
|
if (!slot->reg_type)
|
||
|
{
|
||
|
reg_rate = slot->reg_rr;
|
||
|
}
|
||
|
break;
|
||
|
case envelope_gen_num_release:
|
||
|
reg_rate = slot->reg_rr;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
slot->pg_reset = reset;
|
||
|
ks = slot->channel->ksv >> ((slot->reg_ksr ^ 1) << 1);
|
||
|
nonzero = (reg_rate != 0);
|
||
|
rate = ks + (reg_rate << 2);
|
||
|
rate_hi = rate >> 2;
|
||
|
rate_lo = rate & 0x03;
|
||
|
if (rate_hi & 0x10)
|
||
|
{
|
||
|
rate_hi = 0x0f;
|
||
|
}
|
||
|
eg_shift = rate_hi + slot->chip->eg_add;
|
||
|
shift = 0;
|
||
|
if (nonzero)
|
||
|
{
|
||
|
if (rate_hi < 12)
|
||
|
{
|
||
|
if (slot->chip->eg_state)
|
||
|
{
|
||
|
switch (eg_shift)
|
||
|
{
|
||
|
case 12:
|
||
|
shift = 1;
|
||
|
break;
|
||
|
case 13:
|
||
|
shift = (rate_lo >> 1) & 0x01;
|
||
|
break;
|
||
|
case 14:
|
||
|
shift = rate_lo & 0x01;
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
shift = (rate_hi & 0x03) + eg_incstep[rate_lo][slot->chip->timer & 0x03];
|
||
|
if (shift & 0x04)
|
||
|
{
|
||
|
shift = 0x03;
|
||
|
}
|
||
|
if (!shift)
|
||
|
{
|
||
|
shift = slot->chip->eg_state;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
eg_rout = slot->eg_rout;
|
||
|
eg_inc = 0;
|
||
|
eg_off = 0;
|
||
|
/* Instant attack */
|
||
|
if (reset && rate_hi == 0x0f)
|
||
|
{
|
||
|
eg_rout = 0x00;
|
||
|
}
|
||
|
/* Envelope off */
|
||
|
if ((slot->eg_rout & 0x1f8) == 0x1f8)
|
||
|
{
|
||
|
eg_off = 1;
|
||
|
}
|
||
|
if (slot->eg_gen != envelope_gen_num_attack && !reset && eg_off)
|
||
|
{
|
||
|
eg_rout = 0x1ff;
|
||
|
}
|
||
|
switch (slot->eg_gen)
|
||
|
{
|
||
|
case envelope_gen_num_attack:
|
||
|
if (!(slot->eg_rout))
|
||
|
{
|
||
|
slot->eg_gen = envelope_gen_num_decay;
|
||
|
}
|
||
|
else if (slot->key && shift > 0 && rate_hi != 0x0f)
|
||
|
{
|
||
|
eg_inc = ~slot->eg_rout >> (4 - shift);
|
||
|
}
|
||
|
break;
|
||
|
case envelope_gen_num_decay:
|
||
|
if ((slot->eg_rout >> 4) == slot->reg_sl)
|
||
|
{
|
||
|
slot->eg_gen = envelope_gen_num_sustain;
|
||
|
}
|
||
|
else if (!eg_off && !reset && shift > 0)
|
||
|
{
|
||
|
eg_inc = 1 << (shift - 1);
|
||
|
}
|
||
|
break;
|
||
|
case envelope_gen_num_sustain:
|
||
|
case envelope_gen_num_release:
|
||
|
if (!eg_off && !reset && shift > 0)
|
||
|
{
|
||
|
eg_inc = 1 << (shift - 1);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
slot->eg_rout = (eg_rout + eg_inc) & 0x1ff;
|
||
|
/* Key off */
|
||
|
if (reset)
|
||
|
{
|
||
|
slot->eg_gen = envelope_gen_num_attack;
|
||
|
}
|
||
|
if (!(slot->key))
|
||
|
{
|
||
|
slot->eg_gen = envelope_gen_num_release;
|
||
|
}
|
||
|
slot->detrigger = 0;
|
||
|
slot->retrigger = 0;
|
||
|
}
|
||
|
|
||
|
void OPL3_EnvelopeUpdateKSL(opl3_slot *slot)
|
||
|
{
|
||
|
int16_t ksl = (kslrom[slot->channel->f_num >> 6] << 2)
|
||
|
- ((0x08 - slot->channel->block) << 5);
|
||
|
if (ksl < 0)
|
||
|
{
|
||
|
ksl = 0;
|
||
|
}
|
||
|
slot->eg_ksl = (uint8_t)ksl;
|
||
|
}
|
||
|
|
||
|
void OPL3_EnvelopeKeyOn(opl3_slot *slot)
|
||
|
{
|
||
|
slot->key = 0x01;
|
||
|
if(slot->detrigger) {
|
||
|
slot->eg_gen = envelope_gen_num_release;
|
||
|
// slot->eg_rout = 0x1ff;
|
||
|
// slot->eg_out = slot->eg_rout = 0x1ff;
|
||
|
slot->detrigger = 0;
|
||
|
}
|
||
|
slot->retrigger = 1;
|
||
|
}
|
||
|
|
||
|
void OPL3_EnvelopeKeyOff(opl3_slot *slot)
|
||
|
{
|
||
|
slot->key = 0x00;
|
||
|
slot->detrigger = 1;
|
||
|
slot->retrigger = 0;
|
||
|
}
|
||
|
|
||
|
void OPL3_PhaseGenerate(opl3_slot *slot)
|
||
|
{
|
||
|
opl3_chip *chip;
|
||
|
uint16_t f_num;
|
||
|
uint32_t basefreq;
|
||
|
uint16_t phase;
|
||
|
|
||
|
chip = slot->chip;
|
||
|
f_num = slot->channel->f_num;
|
||
|
if (slot->reg_vib)
|
||
|
{
|
||
|
int8_t range;
|
||
|
uint8_t vibpos;
|
||
|
|
||
|
range = (f_num >> 7) & 7;
|
||
|
vibpos = slot->chip->vibpos;
|
||
|
|
||
|
if (!(vibpos & 3))
|
||
|
{
|
||
|
range = 0;
|
||
|
}
|
||
|
else if (vibpos & 1)
|
||
|
{
|
||
|
range >>= 1;
|
||
|
}
|
||
|
range >>= slot->chip->vibshift;
|
||
|
|
||
|
if (vibpos & 4)
|
||
|
{
|
||
|
range = -range;
|
||
|
}
|
||
|
f_num += range;
|
||
|
}
|
||
|
basefreq = (f_num << slot->channel->block) >> 1;
|
||
|
phase = (uint16_t)(slot->pg_phase >> 9);
|
||
|
if (slot->pg_reset)
|
||
|
{
|
||
|
slot->pg_phase = 0;
|
||
|
}
|
||
|
slot->pg_phase += (basefreq * mt[slot->reg_mult]) >> 1;
|
||
|
slot->pg_phase_out = phase;
|
||
|
}
|
||
|
|
||
|
void OPL3_SlotGenerate(opl3_slot *slot)
|
||
|
{
|
||
|
slot->out = envelope_sin[slot->reg_wf](slot->pg_phase_out + *slot->mod, slot->eg_out);
|
||
|
}
|
||
|
|
||
|
void OPL3_SlotCalcFB(opl3_slot *slot)
|
||
|
{
|
||
|
if (slot->channel->fb != 0x00)
|
||
|
{
|
||
|
slot->fbmod = (slot->prout + slot->out) >> (0x09 - slot->channel->fb);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
slot->fbmod = 0;
|
||
|
}
|
||
|
slot->prout = slot->out;
|
||
|
}
|
||
|
|
||
|
void OPL3_ProcessSlot(opl3_slot *slot)
|
||
|
{
|
||
|
OPL3_SlotCalcFB(slot);
|
||
|
OPL3_EnvelopeCalc(slot);
|
||
|
OPL3_PhaseGenerate(slot);
|
||
|
OPL3_SlotGenerate(slot);
|
||
|
}
|
||
|
|
||
|
static int16_t OPL3_ClipSampleOld(int32_t sample)
|
||
|
{
|
||
|
if (sample > 32767)
|
||
|
{
|
||
|
sample = 32767;
|
||
|
}
|
||
|
else if (sample < -32768)
|
||
|
{
|
||
|
sample = -32768;
|
||
|
}
|
||
|
return (int16_t)sample;
|
||
|
}
|
||
|
|
||
|
int16_t OPL3_ClipSample(int32_t sample)
|
||
|
{
|
||
|
int32_t sign = (sample < 0) ? -1 : 1;
|
||
|
sample = (sample < 0) ? -sample : sample;
|
||
|
sample *= 5;
|
||
|
sample /= 8;
|
||
|
sample = sample > 32767 ? 32767 : sample;
|
||
|
sample *= sign;
|
||
|
return (int16_t)sample;
|
||
|
}
|
||
|
|
||
|
void OPL3_Generate(opl3_chip *chip, int16_t *buf)
|
||
|
{
|
||
|
opl3_channel *channel;
|
||
|
int16_t **out;
|
||
|
int32_t mix[2];
|
||
|
uint8_t ii;
|
||
|
int16_t accm;
|
||
|
uint8_t shift = 0;
|
||
|
|
||
|
buf[0] = OPL3_ClipSample(chip->mixbuff[0]);
|
||
|
buf[1] = OPL3_ClipSample(chip->mixbuff[1]);
|
||
|
|
||
|
for (ii = 0; ii < (chip->n_voices * 2); ii++)
|
||
|
{
|
||
|
OPL3_ProcessSlot(&chip->slot[ii]);
|
||
|
}
|
||
|
|
||
|
mix[0] = mix[1] = 0;
|
||
|
for (ii = 0; ii < chip->n_voices; ii++)
|
||
|
{
|
||
|
channel = &chip->channel[ii];
|
||
|
out = channel->out;
|
||
|
accm = *out[0] + *out[1] + *out[2] + *out[3];
|
||
|
mix[0] += (int16_t)((accm * channel->level[0]) >> 16);
|
||
|
mix[1] += (int16_t)((accm * channel->level[1]) >> 16);
|
||
|
}
|
||
|
chip->mixbuff[0] = mix[0];
|
||
|
chip->mixbuff[1] = mix[1];
|
||
|
|
||
|
if ((chip->timer & 0x3f) == 0x3f)
|
||
|
{
|
||
|
chip->tremolopos = (chip->tremolopos + 1) % 210;
|
||
|
}
|
||
|
if (chip->tremolopos < 105)
|
||
|
{
|
||
|
chip->tremolo = chip->tremolopos >> chip->tremoloshift;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
chip->tremolo = (210 - chip->tremolopos) >> chip->tremoloshift;
|
||
|
}
|
||
|
|
||
|
if ((chip->timer & 0x3ff) == 0x3ff)
|
||
|
{
|
||
|
chip->vibpos = (chip->vibpos + 1) & 7;
|
||
|
}
|
||
|
|
||
|
chip->timer++;
|
||
|
|
||
|
chip->eg_add = 0;
|
||
|
if (chip->eg_timer)
|
||
|
{
|
||
|
while (shift < 36 && ((chip->eg_timer >> shift) & 1) == 0)
|
||
|
{
|
||
|
shift++;
|
||
|
}
|
||
|
if (shift > 12)
|
||
|
{
|
||
|
chip->eg_add = 0;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
chip->eg_add = shift + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (chip->eg_timerrem || chip->eg_state)
|
||
|
{
|
||
|
if (chip->eg_timer == 0xfffffffff)
|
||
|
{
|
||
|
chip->eg_timer = 0;
|
||
|
chip->eg_timerrem = 1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
chip->eg_timer++;
|
||
|
chip->eg_timerrem = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
chip->eg_state ^= 1;
|
||
|
}
|
||
|
|
||
|
void OPL3_GenerateResampled(opl3_chip *chip, int16_t *buf)
|
||
|
{
|
||
|
while (chip->samplecnt >= chip->rateratio)
|
||
|
{
|
||
|
chip->oldsamples[0] = chip->samples[0];
|
||
|
chip->oldsamples[1] = chip->samples[1];
|
||
|
OPL3_Generate(chip, chip->samples);
|
||
|
chip->samplecnt -= chip->rateratio;
|
||
|
}
|
||
|
buf[0] = (int16_t)((chip->oldsamples[0] * (chip->rateratio - chip->samplecnt)
|
||
|
+ chip->samples[0] * chip->samplecnt) / chip->rateratio);
|
||
|
buf[1] = (int16_t)((chip->oldsamples[1] * (chip->rateratio - chip->samplecnt)
|
||
|
+ chip->samples[1] * chip->samplecnt) / chip->rateratio);
|
||
|
chip->samplecnt += 1 << RSM_FRAC;
|
||
|
}
|
||
|
|
||
|
// Operator
|
||
|
|
||
|
void OPL3_SlotFlags(opl3_slot *slot, uint8_t tremolo, uint8_t vibrato, uint8_t sustaining, uint8_t ksr) {
|
||
|
if (tremolo)
|
||
|
{
|
||
|
slot->trem = &slot->chip->tremolo;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
slot->trem = (uint8_t*)&slot->chip->zeromod;
|
||
|
}
|
||
|
slot->reg_vib = vibrato & 0x01;
|
||
|
slot->reg_type = sustaining & 0x01;
|
||
|
slot->reg_ksr = ksr & 0x01;
|
||
|
}
|
||
|
|
||
|
void OPL3_SlotMult(opl3_slot *slot, uint8_t mult) {
|
||
|
slot->reg_mult = mult & 0x0f;
|
||
|
}
|
||
|
|
||
|
void OPL3_SlotKSL(opl3_slot *slot, uint8_t ksl) {
|
||
|
slot->reg_ksl = ksl & 0x03;
|
||
|
OPL3_EnvelopeUpdateKSL(slot);
|
||
|
}
|
||
|
|
||
|
void OPL3_SlotLevel(opl3_slot *slot, uint8_t level) {
|
||
|
slot->reg_tl = level & 0x3f;
|
||
|
}
|
||
|
|
||
|
void OPL3_SlotADSR(opl3_slot *slot, uint8_t attack, uint8_t decay, uint8_t sustain, uint8_t release) {
|
||
|
slot->reg_ar = attack & 0x0f;
|
||
|
slot->reg_dr = decay & 0x0f;
|
||
|
slot->reg_sl = sustain & 0x0f;
|
||
|
if (slot->reg_sl == 0x0f)
|
||
|
{
|
||
|
slot->reg_sl = 0x1f;
|
||
|
}
|
||
|
slot->reg_rr = release & 0x0f;
|
||
|
if (slot->reg_rr == 0x00)
|
||
|
{
|
||
|
slot->reg_rr = 0x01;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OPL3_SlotWaveform(opl3_slot *slot, uint8_t waveform) {
|
||
|
slot->reg_wf = waveform & 0x07;
|
||
|
}
|
||
|
|
||
|
// Channel
|
||
|
|
||
|
static void OPL3_ChannelSetupAlg(opl3_channel *channel)
|
||
|
{
|
||
|
if (channel->alg & 0x08)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
if (channel->alg & 0x04)
|
||
|
{
|
||
|
channel->pair->out[0] = &channel->chip->zeromod;
|
||
|
channel->pair->out[1] = &channel->chip->zeromod;
|
||
|
channel->pair->out[2] = &channel->chip->zeromod;
|
||
|
channel->pair->out[3] = &channel->chip->zeromod;
|
||
|
switch (channel->alg & 0x03)
|
||
|
{
|
||
|
case 0x00:
|
||
|
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
|
||
|
channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
|
||
|
channel->slots[0]->mod = &channel->pair->slots[1]->out;
|
||
|
channel->slots[1]->mod = &channel->slots[0]->out;
|
||
|
channel->out[0] = &channel->slots[1]->out;
|
||
|
channel->out[1] = &channel->chip->zeromod;
|
||
|
channel->out[2] = &channel->chip->zeromod;
|
||
|
channel->out[3] = &channel->chip->zeromod;
|
||
|
break;
|
||
|
case 0x01:
|
||
|
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
|
||
|
channel->pair->slots[1]->mod = &channel->pair->slots[0]->out;
|
||
|
channel->slots[0]->mod = &channel->chip->zeromod;
|
||
|
channel->slots[1]->mod = &channel->slots[0]->out;
|
||
|
channel->out[0] = &channel->pair->slots[1]->out;
|
||
|
channel->out[1] = &channel->slots[1]->out;
|
||
|
channel->out[2] = &channel->chip->zeromod;
|
||
|
channel->out[3] = &channel->chip->zeromod;
|
||
|
break;
|
||
|
case 0x02:
|
||
|
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
|
||
|
channel->pair->slots[1]->mod = &channel->chip->zeromod;
|
||
|
channel->slots[0]->mod = &channel->pair->slots[1]->out;
|
||
|
channel->slots[1]->mod = &channel->slots[0]->out;
|
||
|
channel->out[0] = &channel->pair->slots[0]->out;
|
||
|
channel->out[1] = &channel->slots[1]->out;
|
||
|
channel->out[2] = &channel->chip->zeromod;
|
||
|
channel->out[3] = &channel->chip->zeromod;
|
||
|
break;
|
||
|
case 0x03:
|
||
|
channel->pair->slots[0]->mod = &channel->pair->slots[0]->fbmod;
|
||
|
channel->pair->slots[1]->mod = &channel->chip->zeromod;
|
||
|
channel->slots[0]->mod = &channel->pair->slots[1]->out;
|
||
|
channel->slots[1]->mod = &channel->chip->zeromod;
|
||
|
channel->out[0] = &channel->pair->slots[0]->out;
|
||
|
channel->out[1] = &channel->slots[0]->out;
|
||
|
channel->out[2] = &channel->slots[1]->out;
|
||
|
channel->out[3] = &channel->chip->zeromod;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
switch (channel->alg & 0x01)
|
||
|
{
|
||
|
case 0x00:
|
||
|
channel->slots[0]->mod = &channel->slots[0]->fbmod;
|
||
|
channel->slots[1]->mod = &channel->slots[0]->out;
|
||
|
channel->out[0] = &channel->slots[1]->out;
|
||
|
channel->out[1] = &channel->chip->zeromod;
|
||
|
channel->out[2] = &channel->chip->zeromod;
|
||
|
channel->out[3] = &channel->chip->zeromod;
|
||
|
break;
|
||
|
case 0x01:
|
||
|
channel->slots[0]->mod = &channel->slots[0]->fbmod;
|
||
|
channel->slots[1]->mod = &channel->chip->zeromod;
|
||
|
channel->out[0] = &channel->slots[0]->out;
|
||
|
channel->out[1] = &channel->slots[1]->out;
|
||
|
channel->out[2] = &channel->chip->zeromod;
|
||
|
channel->out[3] = &channel->chip->zeromod;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void OPL3_ChannelUpdateAlg(opl3_channel *channel)
|
||
|
{
|
||
|
channel->alg = channel->con;
|
||
|
if (channel->chtype == ch_4op)
|
||
|
{
|
||
|
channel->pair->alg = 0x04 | (channel->con << 1) | (channel->pair->con);
|
||
|
channel->alg = 0x08;
|
||
|
OPL3_ChannelSetupAlg(channel->pair);
|
||
|
}
|
||
|
else if (channel->chtype == ch_4op2)
|
||
|
{
|
||
|
channel->alg = 0x04 | (channel->pair->con << 1) | (channel->con);
|
||
|
channel->pair->alg = 0x08;
|
||
|
OPL3_ChannelSetupAlg(channel);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
OPL3_ChannelSetupAlg(channel);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OPL3_ChannelFreq(opl3_channel *channel, uint8_t block, uint16_t f_num) {
|
||
|
if (channel->chtype == ch_4op2)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
channel->f_num = f_num & 0x3ff;
|
||
|
channel->block = block & 0x07;
|
||
|
channel->ksv = (channel->block << 1)
|
||
|
| ((channel->f_num >> (0x09 - channel->chip->nts)) & 0x01);
|
||
|
OPL3_EnvelopeUpdateKSL(channel->slots[0]);
|
||
|
OPL3_EnvelopeUpdateKSL(channel->slots[1]);
|
||
|
if (channel->chtype == ch_4op)
|
||
|
{
|
||
|
channel->pair->f_num = channel->f_num;
|
||
|
channel->pair->block = channel->block;
|
||
|
channel->pair->ksv = channel->ksv;
|
||
|
OPL3_EnvelopeUpdateKSL(channel->pair->slots[0]);
|
||
|
OPL3_EnvelopeUpdateKSL(channel->pair->slots[1]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OPL3_ChannelOutput(opl3_channel *channel, uint8_t output, uint32_t level) {
|
||
|
channel->level[output & 1] = level;
|
||
|
}
|
||
|
|
||
|
void OPL3_Channel4Op(opl3_channel *channel, uint8_t op4) {
|
||
|
if (op4)
|
||
|
{
|
||
|
channel->chtype = ch_4op;
|
||
|
channel->pair->chtype = ch_4op2;
|
||
|
OPL3_ChannelUpdateAlg(channel);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
channel->chtype = ch_2op;
|
||
|
channel->pair->chtype = ch_2op;
|
||
|
OPL3_ChannelUpdateAlg(channel);
|
||
|
OPL3_ChannelUpdateAlg(channel->pair);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OPL3_ChannelFeedback(opl3_channel *channel, uint8_t feedback) {
|
||
|
channel->fb = feedback & 0x07;
|
||
|
}
|
||
|
|
||
|
void OPL3_ChannelAM(opl3_channel *channel, uint8_t am) {
|
||
|
channel->con = am & 0x01;
|
||
|
OPL3_ChannelUpdateAlg(channel);
|
||
|
}
|
||
|
|
||
|
static void OPL3_ChannelKeyOn(opl3_channel *channel)
|
||
|
{
|
||
|
if (channel->chtype == ch_4op)
|
||
|
{
|
||
|
OPL3_EnvelopeKeyOn(channel->slots[0]);
|
||
|
OPL3_EnvelopeKeyOn(channel->slots[1]);
|
||
|
OPL3_EnvelopeKeyOn(channel->pair->slots[0]);
|
||
|
OPL3_EnvelopeKeyOn(channel->pair->slots[1]);
|
||
|
}
|
||
|
else if (channel->chtype == ch_2op)
|
||
|
{
|
||
|
OPL3_EnvelopeKeyOn(channel->slots[0]);
|
||
|
OPL3_EnvelopeKeyOn(channel->slots[1]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void OPL3_ChannelKeyOff(opl3_channel *channel)
|
||
|
{
|
||
|
if (channel->chtype == ch_4op)
|
||
|
{
|
||
|
OPL3_EnvelopeKeyOff(channel->slots[0]);
|
||
|
OPL3_EnvelopeKeyOff(channel->slots[1]);
|
||
|
OPL3_EnvelopeKeyOff(channel->pair->slots[0]);
|
||
|
OPL3_EnvelopeKeyOff(channel->pair->slots[1]);
|
||
|
}
|
||
|
else if (channel->chtype == ch_2op)
|
||
|
{
|
||
|
OPL3_EnvelopeKeyOff(channel->slots[0]);
|
||
|
OPL3_EnvelopeKeyOff(channel->slots[1]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OPL3_Reset(opl3_chip *chip)
|
||
|
{
|
||
|
uint8_t slotnum;
|
||
|
uint8_t channum;
|
||
|
|
||
|
int32_t rateratio = chip->rateratio;
|
||
|
uint8_t n_voices = chip->n_voices;
|
||
|
opl3_slot *slot = chip->slot;
|
||
|
opl3_channel *channel = chip->channel;
|
||
|
|
||
|
memset(chip, 0, sizeof(opl3_chip) + (n_voices * (sizeof(opl3_slot) * 2 + sizeof(opl3_channel))));
|
||
|
|
||
|
chip->rateratio = rateratio;
|
||
|
chip->n_voices = n_voices;
|
||
|
chip->slot = slot;
|
||
|
chip->channel = channel;
|
||
|
|
||
|
for (slotnum = 0; slotnum < (chip->n_voices * 2); slotnum++)
|
||
|
{
|
||
|
slot = &chip->slot[slotnum];
|
||
|
slot->chip = chip;
|
||
|
slot->mod = &chip->zeromod;
|
||
|
slot->eg_rout = 0x1ff;
|
||
|
slot->eg_out = 0x1ff;
|
||
|
slot->eg_gen = envelope_gen_num_release;
|
||
|
slot->trem = (uint8_t*)&chip->zeromod;
|
||
|
slot->slot_num = slotnum;
|
||
|
}
|
||
|
for (channum = 0; channum < chip->n_voices; channum++)
|
||
|
{
|
||
|
channel = &chip->channel[channum];
|
||
|
channel->slots[0] = &chip->slot[channum * 2];
|
||
|
channel->slots[1] = &chip->slot[channum * 2 + 1];
|
||
|
chip->slot[channum * 2].channel = channel;
|
||
|
chip->slot[channum * 2 + 1].channel = channel;
|
||
|
channel->pair = &chip->channel[(channum & 1) ? (channum - 1) : (channum + 1)];
|
||
|
channel->chip = chip;
|
||
|
channel->out[0] = &chip->zeromod;
|
||
|
channel->out[1] = &chip->zeromod;
|
||
|
channel->out[2] = &chip->zeromod;
|
||
|
channel->out[3] = &chip->zeromod;
|
||
|
channel->chtype = ch_2op;
|
||
|
channel->level[0] = 0x10000;
|
||
|
channel->level[1] = 0x10000;
|
||
|
channel->ch_num = channum;
|
||
|
OPL3_ChannelSetupAlg(channel);
|
||
|
}
|
||
|
chip->tremoloshift = 4;
|
||
|
chip->vibshift = 1;
|
||
|
}
|
||
|
|
||
|
void OPL3_Rate(opl3_chip *chip, uint32_t samplerate) {
|
||
|
chip->rateratio = (samplerate << RSM_FRAC) / 49716;
|
||
|
}
|
||
|
|
||
|
void OPL3_ChipFlags(opl3_chip *chip, uint8_t nts, uint8_t vibrato, uint8_t deeptremolo, uint8_t deepvibrato) {
|
||
|
chip->nts = nts;
|
||
|
chip->tremoloshift = ((deeptremolo ^ 1) << 1) + 2;
|
||
|
chip->vibshift = deepvibrato ^ 1;
|
||
|
}
|
||
|
|
||
|
opl3_chip *OPL3_Alloc(uint32_t samplerate, uint8_t voices) {
|
||
|
opl3_chip *chip = malloc(sizeof(opl3_chip) + (voices * (sizeof(opl3_slot) * 2 + sizeof(opl3_channel))));
|
||
|
chip->channel = (opl3_channel*)(((uint8_t*)chip) + sizeof(opl3_chip));
|
||
|
chip->slot = (opl3_slot*)(((uint8_t*)chip->channel) + (voices * sizeof(opl3_channel)));
|
||
|
chip->n_voices = voices;
|
||
|
OPL3_Rate(chip, samplerate);
|
||
|
OPL3_Reset(chip);
|
||
|
return chip;
|
||
|
}
|
||
|
|
||
|
uint8_t OPL3_Playing(opl3_chip *chip) {
|
||
|
opl3_slot *slot;
|
||
|
for(int z = 0; z < (chip->n_voices * 2); z++) {
|
||
|
slot = &chip->slot[z];
|
||
|
if(slot->eg_out <= 0x100) {
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|