update for more compact event commands

This commit is contained in:
wbcbz7 2024-04-07 00:02:09 +07:00
parent 6a6a43cac1
commit a681a2c53b
6 changed files with 142 additions and 106 deletions

View file

@ -47,10 +47,8 @@ enum {
OPM_STREAM_BACKREF = 0xE0, // E0..EF - word backrefpos (12 bit), byte frames OPM_STREAM_BACKREF = 0xE0, // E0..EF - word backrefpos (12 bit), byte frames
// setter commands // setter commands
OPM_SET_VOLUME = 0x00, // 00..7F - set volume (total level) for operator, end of frame OPM_SET_OPERATOR = 0x00, // 00..7F - set operator parameters
OPM_SET_MULT_WAVE_TL = 0x80, // 80..9F - set mult/waveform/total level/ksr for operator OPM_SET_FREQ_FB_VOL = 0x80, // 80..BF - set frequency, feedback and total level
OPM_SET_ADSR = 0xA0, // A0..AF - set attack/sustain/decay/release for operator
OPM_SET_FREQ_FB = 0xB0, // B0..BF - set frequency + feedback
// control register set // control register set
OPM_CTRL_KEY_PERC = 0x00, // 00..7F - set key on/off for percussion, end of frame OPM_CTRL_KEY_PERC = 0x00, // 00..7F - set key on/off for percussion, end of frame
@ -63,21 +61,20 @@ enum {
OPM_SET_VOLUME_END_OF_FRAME = (1 << 7), OPM_SET_VOLUME_END_OF_FRAME = (1 << 7),
OPM_CMD80_SET_MULT = (1 << 0), OPM_CMD00_SET_MULT = (1 << 0),
OPM_CMD80_SET_TL = (1 << 1), OPM_CMD00_SET_TL = (1 << 1),
OPM_CMD80_SET_WAVEFORM = (1 << 2), OPM_CMD00_SET_AD = (1 << 2),
OPM_CMD80_SELECT_OPERATOR = (1 << 3), OPM_CMD00_SET_SR = (1 << 3),
OPM_CMD80_END_OF_FRAME = (1 << 4), OPM_CMD00_SET_WAVEFORM = (1 << 4),
OPM_CMD00_SELECT_OPERATOR = (1 << 5),
OPM_CMD00_END_OF_FRAME = (1 << 6),
OPM_CMDA0_SET_AD = (1 << 0), OPM_CMD80_SET_KEYBLOCK = (1 << 0),
OPM_CMDA0_SET_SR = (1 << 1), OPM_CMD80_SET_FREQ = (1 << 1),
OPM_CMDA0_SELECT_OPERATOR = (1 << 2), OPM_CMD80_SET_FEEDBACK = (1 << 2),
OPM_CMDA0_END_OF_FRAME = (1 << 3), OPM_CMD80_SET_TL0 = (1 << 3),
OPM_CMD80_SET_TL1 = (1 << 4),
OPM_CMDB0_SET_FEEDBACK = (1 << 0), OPM_CMD80_END_OF_FRAME = (1 << 5),
OPM_CMDB0_SET_FREQ_LOW = (1 << 1),
OPM_CMDB0_SET_FREQ_HIGH = (1 << 2),
OPM_CMDB0_END_OF_FRAME = (1 << 3),
OPM_CTRL_SET_REG01 = (1 << 0), OPM_CTRL_SET_REG01 = (1 << 0),
OPM_CTRL_SET_REG08 = (1 << 1), OPM_CTRL_SET_REG08 = (1 << 1),

View file

@ -109,10 +109,24 @@ int opmplay_load_module(opmplay_context_t* ctx, opmplay_io_t* io) {
return OPMPLAY_ERR_OK; return OPMPLAY_ERR_OK;
} }
void opmplay_pop_stack(opmplay_channel_context_t* chctx) {
opmplay_channel_stack_t* st = chctx->stack + (--chctx->stack_pos);
chctx->stream.ptr = st->ptr;
chctx->stream.samples_to_play = st->frames_to_play;
}
void opmplay_push_stack(opmplay_channel_context_t* chctx) {
opmplay_channel_stack_t* st = chctx->stack + chctx->stack_pos;
st->ptr = chctx->stream.ptr;
st->frames_to_play = chctx->stream.samples_to_play;
chctx->stack_pos++;
}
int opmplay_loop(opmplay_context_t* ctx) { int opmplay_loop(opmplay_context_t* ctx) {
// channel streams // channel streams
for (int ch = 0; ch < OPMPLAY_MAX_CHANNLES; ch++) { for (int ch = 0; ch < OPMPLAY_MAX_CHANNLES; ch++) {
// init stack // init stack
ctx->channels[ch].stack_pos = 0;
ctx->channels[ch].stream.samples_to_play = -1; ctx->channels[ch].stream.samples_to_play = -1;
ctx->channels[ch].stream.ptr = ctx->channels[ch].stream.loop; ctx->channels[ch].stream.ptr = ctx->channels[ch].stream.loop;
ctx->channels[ch].stream.delay = ctx->channels[ch].stream.reload = 1; ctx->channels[ch].stream.delay = ctx->channels[ch].stream.reload = 1;
@ -244,34 +258,29 @@ int opmplay_tick(opmplay_context_t* ctx, opl3_chip* opl3) {
if (--chctx->stream.delay == 0) { if (--chctx->stream.delay == 0) {
while (isRun) { while (isRun) {
// get streams // get streams
if ((*(data) & 0xE0) == OPM_SET_MULT_WAVE_TL) { if ((*(data) & 0x80) == OPM_SET_OPERATOR) {
int mask = *data; int mask = *data;
int op = (mask & OPM_CMD80_SELECT_OPERATOR ? 3 : 0) + opmplay_opregs_channel_offset[ch]; int op = (mask & OPM_CMD00_SELECT_OPERATOR ? 3 : 0) + opmplay_opregs_channel_offset[ch];
data++; data++;
if (mask & OPM_CMD80_SET_MULT) OPL3_WriteRegBuffered(opl3, 0x20+op, *data++); if (mask & OPM_CMD00_SET_MULT) OPL3_WriteRegBuffered(opl3, 0x20 + op, *data++);
if (mask & OPM_CMD80_SET_TL) OPL3_WriteRegBuffered(opl3, 0x40+op, *data++); if (mask & OPM_CMD00_SET_TL) OPL3_WriteRegBuffered(opl3, 0x40 + op, *data++);
if (mask & OPM_CMD80_SET_WAVEFORM) OPL3_WriteRegBuffered(opl3, 0xE0+op, *data++); if (mask & OPM_CMD00_SET_AD) OPL3_WriteRegBuffered(opl3, 0x60 + op, *data++);
if (mask & OPM_CMD00_SET_SR) OPL3_WriteRegBuffered(opl3, 0x80 + op, *data++);
if (mask & OPM_CMD00_SET_WAVEFORM) OPL3_WriteRegBuffered(opl3, 0xE0 + op, *data++);
if (mask & OPM_CMD00_END_OF_FRAME) isRun = false;
continue;
}
if ((*(data) & 0xC0) == OPM_SET_FREQ_FB_VOL) {
int mask = *data;
data++;
if (mask & OPM_CMD80_SET_KEYBLOCK) { chctx->block = *data; OPL3_WriteRegBuffered(opl3, 0xB0 + ch, *data++); };
if (mask & OPM_CMD80_SET_FREQ) OPL3_WriteRegBuffered(opl3, 0xA0 + ch, *data++);
if (mask & OPM_CMD80_SET_FEEDBACK) OPL3_WriteRegBuffered(opl3, 0xC0 + ch, *data++);
if (mask & OPM_CMD80_SET_TL0) OPL3_WriteRegBuffered(opl3, 0x40 + opmplay_opregs_channel_offset[ch], *data++);
if (mask & OPM_CMD80_SET_TL1) OPL3_WriteRegBuffered(opl3, 0x43 + opmplay_opregs_channel_offset[ch], *data++);
if (mask & OPM_CMD80_END_OF_FRAME) isRun = false; if (mask & OPM_CMD80_END_OF_FRAME) isRun = false;
continue; continue;
} }
if ((*(data) & 0xF0) == OPM_SET_ADSR) {
int mask = *data;
int op = (mask & OPM_CMDA0_SELECT_OPERATOR ? 3 : 0) + opmplay_opregs_channel_offset[ch];
data++;
if (mask & OPM_CMDA0_SET_AD) OPL3_WriteRegBuffered(opl3, 0x60 + op, *data++);
if (mask & OPM_CMDA0_SET_SR) OPL3_WriteRegBuffered(opl3, 0x80 + op, *data++);
if (mask & OPM_CMDA0_END_OF_FRAME) isRun = false;
continue;
}
if ((*(data) & 0xF0) == OPM_SET_FREQ_FB) {
int mask = *data;
data++;
if (mask & OPM_CMDB0_SET_FEEDBACK) OPL3_WriteRegBuffered(opl3, 0xC0 + ch, *data++);
if (mask & OPM_CMDB0_SET_FREQ_LOW) OPL3_WriteRegBuffered(opl3, 0xA0 + ch, *data++);
if (mask & OPM_CMDB0_SET_FREQ_HIGH) { chctx->block = *data; OPL3_WriteRegBuffered(opl3, 0xB0 + ch, *data++); };
if (mask & OPM_CMDB0_END_OF_FRAME) isRun = false;
continue;
}
if ((*data & 0xFC) == OPM_KEY_TRIGGER) { if ((*data & 0xFC) == OPM_KEY_TRIGGER) {
if (*data & OPM_KEY_ON) chctx->block |= (1 << 5); else chctx->block &= ~(1 << 5); if (*data & OPM_KEY_ON) chctx->block |= (1 << 5); else chctx->block &= ~(1 << 5);
OPL3_WriteRegBuffered(opl3, 0xB0 + ch, chctx->block); OPL3_WriteRegBuffered(opl3, 0xB0 + ch, chctx->block);
@ -279,6 +288,16 @@ int opmplay_tick(opmplay_context_t* ctx, opl3_chip* opl3) {
data++; data++;
continue; continue;
} }
if ((*data & 0xF0) == OPM_STREAM_BACKREF) {
// back reference, nested call :)
int distance = ((*(data + 0) & 0x0F) << 8) | (*(data + 1));
int frames_to_play = *(data + 2);
chctx->stream.ptr = data + 3;
opmplay_push_stack(chctx);
data -= distance;
chctx->stream.samples_to_play = frames_to_play;
continue;
}
// check for common stuff // check for common stuff
switch (*data) { switch (*data) {
@ -317,10 +336,21 @@ int opmplay_tick(opmplay_context_t* ctx, opl3_chip* opl3) {
} }
} }
chctx->stream.delay = chctx->stream.reload; chctx->stream.delay = chctx->stream.reload;
chctx->stream.ptr = data;
// decrement samples to play counter
if (--chctx->stream.samples_to_play == 0) {
// pop context from the stack
do opmplay_pop_stack(chctx); while (--chctx->stream.samples_to_play == 0);
}
else {
// save data pointer
chctx->stream.ptr = data;
}
} }
} }
ctx->pos.frame++;
return rtn; return rtn;
} }

View file

@ -27,6 +27,7 @@ extern "C" {
// general enums // general enums
enum { enum {
OPMPLAY_MAX_CHANNLES = 9+1, OPMPLAY_MAX_CHANNLES = 9+1,
OPMPLAY_MAX_STACK_DEPTH = 4,
}; };
// return error codes // return error codes
@ -68,9 +69,15 @@ struct opmplay_io_t {
uint32_t(*seek)(opmplay_io_t* io, uint32_t offset); // returns 0 if success uint32_t(*seek)(opmplay_io_t* io, uint32_t offset); // returns 0 if success
}; };
struct opmplay_channel_stack_t {
uint8_t* ptr;
uint32_t frames_to_play;
};
struct opmplay_channel_context_t { struct opmplay_channel_context_t {
// TODO: internal registers (skipped atm) // stack
uint8_t block; // used to track key on/off changes opmplay_channel_stack_t stack[OPMPLAY_MAX_STACK_DEPTH];
uint32_t stack_pos;
// stream data // stream data
struct { struct {
@ -82,6 +89,9 @@ struct opmplay_channel_context_t {
uint8_t* ptr; uint8_t* ptr;
uint8_t* loop; // if active uint8_t* loop; // if active
} stream; } stream;
// internal registers
uint8_t block; // used to track key on/off changes
}; };
struct opmplay_context_t { struct opmplay_context_t {

View file

@ -31,15 +31,14 @@ struct opm_control_track_t {
enum { enum {
OPM_MULT = (1 << 0), OPM_MULT = (1 << 0),
OPM_KSL_TL = (1 << 1), OPM_KSL_TL = (1 << 1),
OPM_WAVEFORM = (1 << 2), OPM_AD = (1 << 2),
OPM_AD = (1 << 3), OPM_SR = (1 << 3),
OPM_SR = (1 << 4), OPM_WAVEFORM = (1 << 4),
OPM_OP1_SHIFT = 5, OPM_OP1_SHIFT = 5,
OPM_FEEDBACK = (1 << 10), OPM_BLOCK = (1 << 10),
OPM_FNUM = (1 << 11), OPM_FNUM = (1 << 11),
OPM_FEEDBACK = (1 << 12),
OPM_BLOCK = (1 << 12),
OPM_KEY = (1 << 13), OPM_KEY = (1 << 13),
OPM_KEYPERC = (1 << 14), OPM_KEYPERC = (1 << 14),
@ -48,6 +47,10 @@ enum {
OPM_REG_105 = (1 << 17), OPM_REG_105 = (1 << 17),
OPM_REG_104 = (1 << 18), OPM_REG_104 = (1 << 18),
OPM_REG_BD = (1 << 19), OPM_REG_BD = (1 << 19),
// opm_serialize_channel_stream() tweaks
OPM_TL0 = (1 << 13),
OPM_TL1 = (1 << 14),
}; };
struct opm_frame_record { struct opm_frame_record {

View file

@ -47,10 +47,8 @@ enum {
OPM_STREAM_BACKREF = 0xE0, // E0..EF - word backrefpos (12 bit), byte frames OPM_STREAM_BACKREF = 0xE0, // E0..EF - word backrefpos (12 bit), byte frames
// setter commands // setter commands
OPM_SET_VOLUME = 0x00, // 00..7F - set volume (total level) for operator, end of frame OPM_SET_OPERATOR = 0x00, // 00..7F - set operator parameters
OPM_SET_MULT_WAVE_TL = 0x80, // 80..9F - set mult/waveform/total level/ksr for operator OPM_SET_FREQ_FB_VOL = 0x80, // 80..BF - set frequency, feedback and total level
OPM_SET_ADSR = 0xA0, // A0..AF - set attack/sustain/decay/release for operator
OPM_SET_FREQ_FB = 0xB0, // B0..BF - set frequency + feedback
// control register set // control register set
OPM_CTRL_KEY_PERC = 0x00, // 00..7F - set key on/off for percussion, end of frame OPM_CTRL_KEY_PERC = 0x00, // 00..7F - set key on/off for percussion, end of frame
@ -63,26 +61,25 @@ enum {
OPM_SET_VOLUME_END_OF_FRAME = (1 << 7), OPM_SET_VOLUME_END_OF_FRAME = (1 << 7),
OPM_CMD80_SET_MULT = (1 << 0), OPM_CMD00_SET_MULT = (1 << 0),
OPM_CMD80_SET_TL = (1 << 1), OPM_CMD00_SET_TL = (1 << 1),
OPM_CMD80_SET_WAVEFORM = (1 << 2), OPM_CMD00_SET_AD = (1 << 2),
OPM_CMD80_SELECT_OPERATOR = (1 << 3), OPM_CMD00_SET_SR = (1 << 3),
OPM_CMD80_END_OF_FRAME = (1 << 4), OPM_CMD00_SET_WAVEFORM = (1 << 4),
OPM_CMD00_SELECT_OPERATOR = (1 << 5),
OPM_CMD00_END_OF_FRAME = (1 << 6),
OPM_CMDA0_SET_AD = (1 << 0), OPM_CMD80_SET_KEYBLOCK = (1 << 0),
OPM_CMDA0_SET_SR = (1 << 1), OPM_CMD80_SET_FREQ = (1 << 1),
OPM_CMDA0_SELECT_OPERATOR = (1 << 2), OPM_CMD80_SET_FEEDBACK = (1 << 2),
OPM_CMDA0_END_OF_FRAME = (1 << 3), OPM_CMD80_SET_TL0 = (1 << 3),
OPM_CMD80_SET_TL1 = (1 << 4),
OPM_CMDB0_SET_FEEDBACK = (1 << 0), OPM_CMD80_END_OF_FRAME = (1 << 5),
OPM_CMDB0_SET_FREQ_LOW = (1 << 1),
OPM_CMDB0_SET_FREQ_HIGH = (1 << 2),
OPM_CMDB0_END_OF_FRAME = (1 << 3),
OPM_CTRL_SET_REG01 = (1 << 0), OPM_CTRL_SET_REG01 = (1 << 0),
OPM_CTRL_SET_REG08 = (1 << 1), OPM_CTRL_SET_REG08 = (1 << 1),
OPM_CTRL_SET_REG105 = (1 << 2), OPM_CTRL_SET_REG105 = (1 << 2),
OPM_CTRL_SET_RE104 = (1 << 3), OPM_CTRL_SET_REG104 = (1 << 3),
OPM_CTRL_SET_REGBD = (1 << 4), OPM_CTRL_SET_REGBD = (1 << 4),
}; };

View file

@ -599,25 +599,29 @@ int opm_serialize_channel_stream(opm_convert_context_t* ctx, int ch) {
auto& e = s.records[i]; auto& e = s.records[i];
struct { struct {
int op[2]; int op[2];
int adsr[2]; int tl_relocated;
int freq_fb; int freq_fb;
bool key; bool key_only;
int last; int last;
} mask; } mask;
// generate masks // generate masks
mask.last = 0; int bit = 0; mask.last = 0; mask.tl_relocated = 0; int bit = 0;
for (int op = 0; op < 2; op++) { for (int op = 0; op < 2; op++) {
mask.op[op] = ((e.flags >> (OPM_OP1_SHIFT * op)) & (OPM_MULT | OPM_KSL_TL | OPM_WAVEFORM)); mask.op[op] = ((e.flags >> (OPM_OP1_SHIFT * op)) & (OPM_MULT | OPM_KSL_TL | OPM_AD | OPM_SR | OPM_WAVEFORM));
mask.last |= ((mask.op[op] ? 1 : 0) << bit++); // if only TL is updated, move it to the freq/feedback command
mask.adsr[op] = ((e.flags >> (OPM_OP1_SHIFT * op)) & (OPM_AD | OPM_SR)); if (mask.op[op] == OPM_KSL_TL) {
mask.last |= ((mask.adsr[op] ? 1 : 0) << bit++); mask.op[op] = 0;
mask.tl_relocated |= (OPM_TL0 << op);
}
mask.last |= ((mask.op[op] ? 1 : 0) << op);
} }
mask.freq_fb = (e.flags & (OPM_FNUM | OPM_BLOCK | OPM_FEEDBACK)); mask.freq_fb = (e.flags & (OPM_BLOCK | OPM_FNUM | OPM_FEEDBACK)) | mask.tl_relocated;
mask.last |= ((mask.freq_fb ? 16 : 0) << 0); mask.last |= ((mask.freq_fb ? 4 : 0) << 0);
mask.key = (e.flags & (OPM_FNUM | OPM_BLOCK | OPM_FEEDBACK | OPM_KEY)) == (OPM_BLOCK | OPM_KEY); mask.key_only = ((e.flags & (OPM_BLOCK | OPM_FNUM | OPM_FEEDBACK | OPM_KEY)) | (mask.tl_relocated)) == (OPM_BLOCK | OPM_KEY);
mask.last |= ((mask.key ? 32 : 0) << 0); mask.last |= ((mask.key_only ? 8 : 0) << 0);
if (i < s.records.size() - 1) mask.last |= 64; // prevent non-last events from emitting eof token if (i < s.records.size() - 1)
mask.last |= (1 << 31); // prevent non-last events from emitting eof token
// TODO: do it less dirty :) // TODO: do it less dirty :)
if (mask.last == 0) { if (mask.last == 0) {
@ -626,41 +630,34 @@ int opm_serialize_channel_stream(opm_convert_context_t* ctx, int ch) {
else { else {
// output tokens // output tokens
for (int op = 0; op < 2; op++) { for (int op = 0; op < 2; op++) {
// mult/tl/waveform // per-operator params
if (mask.last & 1) { if (mask.last & 1) {
s.rawdata.push_back( s.rawdata.push_back(
OPM_SET_MULT_WAVE_TL | (mask.op[op] >> 0) | (OPM_CMD80_SELECT_OPERATOR * op) | OPM_SET_OPERATOR | (mask.op[op] >> 0) | (OPM_CMD00_SELECT_OPERATOR * op) |
(mask.last == 1 ? OPM_CMD80_END_OF_FRAME : 0) (mask.last == 1 ? OPM_CMD00_END_OF_FRAME : 0)
); );
if (mask.op[op] & OPM_MULT) s.rawdata.push_back(e.op[op].mult); if (mask.op[op] & OPM_MULT) s.rawdata.push_back(e.op[op].mult);
if (mask.op[op] & OPM_KSL_TL) s.rawdata.push_back(e.op[op].ksl_tl); if (mask.op[op] & OPM_KSL_TL) s.rawdata.push_back(e.op[op].ksl_tl);
if (mask.op[op] & OPM_WAVEFORM) s.rawdata.push_back(e.op[op].waveform); if (mask.op[op] & OPM_AD) s.rawdata.push_back(e.op[op].ad);
} if (mask.op[op] & OPM_SR) s.rawdata.push_back(e.op[op].sr);
mask.last >>= 1; if (mask.op[op] & OPM_WAVEFORM) s.rawdata.push_back(e.op[op].waveform);
// ad/sr
if (mask.last & 1) {
s.rawdata.push_back(
OPM_SET_ADSR | (mask.adsr[op] >> 3) | (OPM_CMDA0_SELECT_OPERATOR * op) |
(mask.last == 1 ? OPM_CMDA0_END_OF_FRAME : 0)
);
if (mask.adsr[op] & OPM_AD) s.rawdata.push_back(e.op[op].ad);
if (mask.adsr[op] & OPM_SR) s.rawdata.push_back(e.op[op].sr);
} }
mask.last >>= 1; mask.last >>= 1;
} }
// freq/feedback // freq/feedback
if ((mask.last & 1) && !mask.key) { if ((mask.last & 1) && !mask.key_only) {
s.rawdata.push_back( s.rawdata.push_back(
OPM_SET_FREQ_FB | (mask.freq_fb >> 10) | OPM_SET_FREQ_FB_VOL | (mask.freq_fb >> 10) |
(mask.last == 1 ? OPM_CMDB0_END_OF_FRAME : 0) (mask.last == 1 ? OPM_CMD80_END_OF_FRAME : 0)
); );
if (mask.freq_fb & OPM_FEEDBACK) s.rawdata.push_back(e.feedback); if (mask.freq_fb & OPM_BLOCK) s.rawdata.push_back(e.block);
if (mask.freq_fb & OPM_FNUM) s.rawdata.push_back(e.fnum); if (mask.freq_fb & OPM_FNUM) s.rawdata.push_back(e.fnum);
if (mask.freq_fb & OPM_BLOCK) s.rawdata.push_back(e.block); if (mask.freq_fb & OPM_FEEDBACK) s.rawdata.push_back(e.feedback);
if (mask.freq_fb & OPM_TL0) s.rawdata.push_back(e.op[0].ksl_tl);
if (mask.freq_fb & OPM_TL1) s.rawdata.push_back(e.op[1].ksl_tl);
} }
mask.last >>= 1; mask.last >>= 1;
if (mask.key) { if (mask.key_only) {
s.rawdata.push_back( s.rawdata.push_back(
OPM_KEY_TRIGGER | OPM_KEY_TRIGGER |
((e.block >> 5) & 1) | ((e.block >> 5) & 1) |
@ -669,7 +666,8 @@ int opm_serialize_channel_stream(opm_convert_context_t* ctx, int ch) {
} }
mask.last >>= 1; mask.last >>= 1;
} }
} else s.rawdata.push_back(OPM_STREAM_END_FRAME); } else
s.rawdata.push_back(OPM_STREAM_END_FRAME);
} }
return 0; return 0;
} }
@ -693,6 +691,7 @@ int opm_concat_streams(opm_convert_context_t* ctx) {
// copy raw register data // copy raw register data
ctx->oplchan_out[ch].insert(ctx->oplchan_out[ch].end(), f.rawdata.begin(), f.rawdata.end()); ctx->oplchan_out[ch].insert(ctx->oplchan_out[ch].end(), f.rawdata.begin(), f.rawdata.end());
} }
ctx->oplchan_out[ch].push_back(OPM_STREAM_END);
} }
return 0; return 0;