prepare for MIDI velocity mapping

This commit is contained in:
tildearrow 2023-12-13 20:44:32 -05:00
parent bdef72666b
commit 69998e2c7e
4 changed files with 21 additions and 2 deletions

View file

@ -48,7 +48,9 @@ enum DivDispatchCmds {
DIV_CMD_ENV_RELEASE,
DIV_CMD_INSTRUMENT, // (ins, force)
DIV_CMD_VOLUME, // (vol)
// TODO: think of possibly moving this
DIV_CMD_GET_VOLUME, // () -> vol
// TODO: move. shouldn't be a command.
DIV_CMD_GET_VOLMAX, // () -> volMax
DIV_CMD_NOTE_PORTA, // (target, speed) -> 2 if target reached
DIV_CMD_PITCH, // (pitch)
@ -589,6 +591,14 @@ class DivDispatch {
*/
virtual bool isVolGlobal();
/**
* map MIDI velocity (from 0 to 127) to chip volume.
* @param ch the chip channel. -1 means N/A.
* @param vel input velocity.
* @return output volume.
*/
virtual int mapVelocity(int ch, unsigned char vel);
/**
* get the lowest note in a portamento.
* @param ch the channel in question.

View file

@ -190,7 +190,7 @@ struct DivNoteEvent {
channel(-1),
ins(0),
note(0),
volume(0),
volume(-1),
on(false),
nop(true),
pad1(false),

View file

@ -102,6 +102,11 @@ bool DivDispatch::isVolGlobal() {
return false;
}
int DivDispatch::mapVelocity(int ch, unsigned char vel) {
const int volMax=MAX(1,dispatch(DivCommand(DIV_CMD_GET_VOLMAX,MAX(ch,0))));
return (vel*volMax)/127;
}
int DivDispatch::getPortaFloor(int ch) {
return 0x00;
}

View file

@ -1372,7 +1372,11 @@ bool DivEngine::nextTick(bool noAccum, bool inhibitLowLat) {
}
if (note.on) {
dispatchCmd(DivCommand(DIV_CMD_INSTRUMENT,note.channel,note.ins,1));
dispatchCmd(DivCommand(DIV_CMD_VOLUME,note.channel,(note.volume*(chan[note.channel].volMax>>8))/127));
if (note.volume>=0) {
int mappedVol=disCont[dispatchOfChan[note.channel]].dispatch->mapVelocity(note.channel,note.volume);
logV("dispatching volume (%d -> %d)",note.volume,mappedVol);
dispatchCmd(DivCommand(DIV_CMD_VOLUME,note.channel,mappedVol));
}
dispatchCmd(DivCommand(DIV_CMD_NOTE_ON,note.channel,note.note));
keyHit[note.channel]=true;
chan[note.channel].releasing=false;