--- np2/sound/vermouth/midiout.c 2003/12/01 04:01:42 1.5 +++ np2/sound/vermouth/midiout.c 2006/12/16 12:11:07 1.16 @@ -2,9 +2,8 @@ #include "midiout.h" -#define MIDIOUT_VERSION 0x104 -#define MIDIOUT_VERSTRING "VERMOUTH 1.04" - +#define MIDIOUT_VERSION 0x116 +#define MIDIOUT_VERSTRING "VERMOUTH 1.16" static const char vermouthver[] = MIDIOUT_VERSTRING; @@ -16,16 +15,37 @@ static const int gaintbl[24+1] = // ---- voice -static void voice_volupdate(VOICE v) { +static void VERMOUTHCL voice_volupdate(VOICE v) { CHANNEL ch; int vol; ch = v->channel; +#if defined(VOLUME_ACURVE) + vol = ch->level * acurve[v->velocity]; + vol >>= 8; +#else vol = ch->level * v->velocity; vol >>= 7; +#endif vol *= v->sample->volume; vol >>= (21 - 16); +#if defined(PANPOT_REVA) + switch(v->flag & VOICE_MIXMASK) { + case VOICE_MIXNORMAL: + v->volleft = (vol * revacurve[v->panpot ^ 127]) >> 8; + v->volright = (vol * revacurve[v->panpot]) >> 8; + break; + + case VOICE_MIXCENTRE: + v->volleft = (vol * 155) >> 8; + break; + + default: + v->volleft = vol; + break; + } +#else v->volleft = vol; if ((v->flag & VOICE_MIXMASK) == VOICE_MIXNORMAL) { if (v->panpot < 64) { @@ -40,9 +60,10 @@ static void voice_volupdate(VOICE v) { v->volleft = vol; } } +#endif } -static INSTLAYER selectlayer(VOICE v, INSTRUMENT inst) { +static INSTLAYER VERMOUTHCL selectlayer(VOICE v, INSTRUMENT inst) { int layers; INSTLAYER layer; @@ -89,7 +110,7 @@ static INSTLAYER selectlayer(VOICE v, IN return(layersel); } -static void freq_update(VOICE v) { +static void VERMOUTHCL freq_update(VOICE v) { CHANNEL ch; float step; @@ -113,7 +134,8 @@ static void freq_update(VOICE v) { v->sampstep = (int)step; } -static void voice_on(MIDIHDL midi, CHANNEL ch, VOICE v, int key, int vel) { +static void VERMOUTHCL voice_on(MIDIHDL midi, CHANNEL ch, VOICE v, int key, + int vel) { INSTRUMENT inst; INSTLAYER layer; @@ -183,6 +205,21 @@ static void voice_on(MIDIHDL midi, CHANN else { panpot = layer->panpot; } +#if defined(PANPOT_REVA) + if (panpot == 64) { + v->flag = VOICE_MIXCENTRE; + } + else if (panpot < 3) { + v->flag = VOICE_MIXLEFT; + } + else if (panpot >= 126) { + v->flag = VOICE_MIXRIGHT; + } + else { + v->flag = VOICE_MIXNORMAL; + v->panpot = panpot; + } +#else if ((panpot >= 60) && (panpot < 68)) { v->flag = VOICE_MIXCENTRE; } @@ -196,6 +233,7 @@ static void voice_on(MIDIHDL midi, CHANN v->flag = VOICE_MIXNORMAL; v->panpot = panpot; } +#endif if (!layer->samprate) { v->flag |= VOICE_FIXPITCH; } @@ -218,7 +256,7 @@ static void voice_on(MIDIHDL midi, CHANN envelope_updates(v); } -static void voice_off(VOICE v) { +static void VERMOUTHCL voice_off(VOICE v) { voice_setphase(v, VOICE_OFF); if (v->sample->mode & MODE_ENVELOPE) { @@ -228,7 +266,7 @@ static void voice_off(VOICE v) { } } -static void allresetvoices(MIDIHDL midi) { +static void VERMOUTHCL allresetvoices(MIDIHDL midi) { VOICE v; VOICE vterm; @@ -244,7 +282,7 @@ static void allresetvoices(MIDIHDL midi) // ---- key -static void key_on(MIDIHDL midi, CHANNEL ch, int key, int vel) { +static void VERMOUTHCL key_on(MIDIHDL midi, CHANNEL ch, int key, int vel) { VOICE v; VOICE v1; @@ -294,7 +332,7 @@ static void key_on(MIDIHDL midi, CHANNEL } } -static void key_off(MIDIHDL midi, CHANNEL ch, int key) { +static void VERMOUTHCL key_off(MIDIHDL midi, CHANNEL ch, int key) { VOICE v; VOICE vterm; @@ -316,7 +354,8 @@ static void key_off(MIDIHDL midi, CHANNE } while(v < vterm); } -static void key_pressure(MIDIHDL midi, CHANNEL ch, int key, int vel) { +static void VERMOUTHCL key_pressure(MIDIHDL midi, CHANNEL ch, int key, + int vel) { VOICE v; VOICE vterm; @@ -338,12 +377,16 @@ static void key_pressure(MIDIHDL midi, C // ---- control -static void volumeupdate(MIDIHDL midi, CHANNEL ch) { +static void VERMOUTHCL volumeupdate(MIDIHDL midi, CHANNEL ch) { VOICE v; VOICE vterm; +#if defined(VOLUME_ACURVE) + ch->level = (midi->level * acurve[ch->volume] * ch->expression) >> 15; +#else ch->level = (midi->level * ch->volume * ch->expression) >> 14; +#endif v = midi->voice; vterm = v + VOICE_MAX; do { @@ -355,7 +398,7 @@ static void volumeupdate(MIDIHDL midi, C } while(v < vterm); } -static void pedaloff(MIDIHDL midi, CHANNEL ch) { +static void VERMOUTHCL pedaloff(MIDIHDL midi, CHANNEL ch) { VOICE v; VOICE vterm; @@ -370,7 +413,7 @@ static void pedaloff(MIDIHDL midi, CHANN } while(v < vterm); } -static void allsoundsoff(MIDIHDL midi, CHANNEL ch) { +static void VERMOUTHCL allsoundsoff(MIDIHDL midi, CHANNEL ch) { VOICE v; VOICE vterm; @@ -386,7 +429,7 @@ static void allsoundsoff(MIDIHDL midi, C } while(v < vterm); } -static void resetallcontrollers(CHANNEL ch) { +static void VERMOUTHCL resetallcontrollers(CHANNEL ch) { ch->flag &= CHANNEL_MASK; if (ch->flag == 9) { @@ -398,7 +441,7 @@ static void resetallcontrollers(CHANNEL ch->pitchfactor = 1.0; } -static void allnotesoff(MIDIHDL midi, CHANNEL ch) { +static void VERMOUTHCL allnotesoff(MIDIHDL midi, CHANNEL ch) { VOICE v; VOICE vterm; @@ -424,13 +467,21 @@ static void allnotesoff(MIDIHDL midi, CH } while(v < vterm); } -static void ctrlchange(MIDIHDL midi, CHANNEL ch, int ctrl, int val) { +static void VERMOUTHCL ctrlchange(MIDIHDL midi, CHANNEL ch, int ctrl, + int val) { val &= 0x7f; switch(ctrl & 0x7f) { +#if !defined(MIDI_GMONLY) case CTRL_PGBANK: +#if defined(ENABLE_GSRX) + if (!(ch->gsrx[2] & GSRX2_BANKSELECT)) { + break; + } +#endif ch->bank = val; break; +#endif case CTRL_DATA_M: // TRACEOUT(("data: %x %x %x", c->rpn_l, c->rpn_m, val)); @@ -480,7 +531,7 @@ static void ctrlchange(MIDIHDL midi, CHA case CTRL_RESETCTRL: resetallcontrollers(ch); - break; + /*FALLTHROUGH*/ case CTRL_NOTEOFF: allnotesoff(midi, ch); @@ -500,7 +551,7 @@ static void ctrlchange(MIDIHDL midi, CHA } } -static void progchange(MIDIHDL midi, CHANNEL ch, int val) { +static void VERMOUTHCL progchange(MIDIHDL midi, CHANNEL ch, int val) { #if !defined(MIDI_GMONLY) MIDIMOD module; @@ -535,7 +586,7 @@ static void progchange(MIDIHDL midi, CHA ch->program = val; } -static void chpressure(MIDIHDL midi, CHANNEL ch, int vel) { +static void VERMOUTHCL chpressure(MIDIHDL midi, CHANNEL ch, int vel) { VOICE v; VOICE vterm; @@ -553,7 +604,8 @@ static void chpressure(MIDIHDL midi, CHA } while(v < vterm); } -static void pitchbendor(MIDIHDL midi, CHANNEL ch, int val1, int val2) { +static void VERMOUTHCL pitchbendor(MIDIHDL midi, CHANNEL ch, int val1, + int val2) { VOICE v; VOICE vterm; @@ -582,7 +634,7 @@ static void pitchbendor(MIDIHDL midi, CH } } -static void allvolupdate(MIDIHDL midi) { +static void VERMOUTHCL allvolupdate(MIDIHDL midi) { int level; CHANNEL ch; @@ -610,23 +662,46 @@ static void allvolupdate(MIDIHDL midi) { } while(v < vterm); } -static void allresetmidi(MIDIHDL midi) { - +#if defined(ENABLE_GSRX) +static void VERMOUTHCL allresetmidi(MIDIHDL midi, BOOL gs) +#else +#define allresetmidi(m, g) _allresetmidi(m) +static void VERMOUTHCL _allresetmidi(MIDIHDL midi) +#endif +{ CHANNEL ch; CHANNEL chterm; UINT flag; + midi->master = 127; ch = midi->channel; chterm = ch + 16; ZeroMemory(ch, sizeof(_CHANNEL) * 16); flag = 0; do { ch->flag = flag++; - ch->panpot = 64; ch->pitchsens = 2; +#if !defined(MIDI_GMONLY) ch->bank = 0; +#endif + ch->panpot = 64; progchange(midi, ch, 0); resetallcontrollers(ch); +#if defined(ENABLE_GSRX) + ch->keyshift = 0x40; + ch->noterange[0] = 0x00; + ch->noterange[1] = 0x7f; + if (gs) { + ch->gsrx[0] = 0xff; + ch->gsrx[1] = 0xff; + ch->gsrx[2] = 0xff; + } + else { + ch->gsrx[0] = 0x7f; + ch->gsrx[1] = 0xff; + ch->gsrx[2] = 0x02; + } +#endif ch++; } while(ch < chterm); allresetvoices(midi); @@ -636,13 +711,14 @@ static void allresetmidi(MIDIHDL midi) { // ---- -UINT midiout_getver(char *string, int leng) { +VEXTERN UINT VEXPORT midiout_getver(char *string, int leng) { - milstr_ncpy(string, vermouthver, leng); + leng = min(leng, sizeof(vermouthver)); + CopyMemory(string, vermouthver, leng); return((MIDIOUT_VERSION << 8) | 0x00); } -MIDIHDL midiout_create(MIDIMOD module, UINT worksize) { +VEXTERN MIDIHDL VEXPORT midiout_create(MIDIMOD module, UINT worksize) { UINT size; MIDIHDL ret; @@ -657,28 +733,33 @@ MIDIHDL midiout_create(MIDIMOD module, U size += sizeof(_SAMPLE) * worksize; ret = (MIDIHDL)_MALLOC(size, "MIDIHDL"); if (ret) { + midimod_lock(module); ZeroMemory(ret, size); ret->samprate = module->samprate; ret->worksize = worksize; ret->module = module; - ret->master = 127; + // ret->master = 127; ret->bank0[0] = module->tone[0]; ret->bank0[1] = module->tone[1]; ret->sampbuf = (SINT32 *)(ret + 1); ret->resampbuf = (SAMPLE)(ret->sampbuf + worksize * 2); - allresetmidi(ret); + allresetmidi(ret, FALSE); } return(ret); } -void midiout_destroy(MIDIHDL hdl) { +VEXTERN void VEXPORT midiout_destroy(MIDIHDL hdl) { + + MIDIMOD module; if (hdl) { + module = hdl->module; _MFREE(hdl); + midimod_lock(module); } } -void midiout_shortmsg(MIDIHDL hdl, UINT32 msg) { +VEXTERN void VEXPORT midiout_shortmsg(MIDIHDL hdl, UINT32 msg) { UINT cmd; CHANNEL ch; @@ -731,19 +812,258 @@ void midiout_shortmsg(MIDIHDL hdl, UINT3 } } -void midiout_longmsg(MIDIHDL hdl, const BYTE *msg, UINT size) { +static void VERMOUTHCL longmsg_uni(MIDIHDL hdl, const UINT8 *msg, UINT size) { + + if ((size >= 6) && (msg[2] == 0x7f)) { + switch(msg[3]) { + case 0x04: + if ((msg[4] == 0x01) && (size >= 8)) { + hdl->master = msg[6] & 0x7f; + allvolupdate(hdl); + } + break; + } + } +} + +static void VERMOUTHCL longmsg_gm(MIDIHDL hdl, const UINT8 *msg, UINT size) { + + if ((size >= 6) && (msg[2] == 0x7f)) { + switch(msg[3]) { + case 0x09: + if (msg[4] == 0x01) { + allresetmidi(hdl, FALSE); // GM reset + break; + } +#if !defined(MIDI_GMONLY) + else if ((msg[4] == 0x02) || (msg[4] == 0x03)) { + allresetmidi(hdl, TRUE); // GM reset + break; + } +#endif + break; + } + } +} + +static void VERMOUTHCL rolandcmd4(MIDIHDL hdl, UINT addr, UINT8 data) { + + UINT part; + CHANNEL ch; +#if defined(ENABLE_GSRX) + UINT8 bit; +#endif + + addr = addr & 0x000fffff; + if (addr == 0x00004) { // Vol + hdl->master = data; + allvolupdate(hdl); + } + else if ((addr & (~0xff)) == 0x00100) { + const UINT pos = addr & 0xff; + if (pos < 0x30) { // Patch Name + } + else { + switch(addr & 0xff) { + case 0x30: // Reverb Macro + case 0x31: // Reverb Charactor + case 0x32: // Reverb Pre-LPF + case 0x33: // Reverb Level + case 0x34: // Reverb Time + case 0x35: // Reverb Delay FeedBack + case 0x37: // Reverb Predelay Time + case 0x38: // Chorus Macro + case 0x39: // Chorus Pre-LPF + case 0x3a: // Chorus Level + case 0x3b: // Chorus FeedBack + case 0x3c: // Chorus Delay + case 0x3d: // Chorus Rate + case 0x3e: // Chorus Depth + case 0x3f: // Chorus send level to reverb + case 0x40: // Chorus send level to delay + case 0x50: // Delay Macro + case 0x51: // Delay Time Pre-LPF + case 0x52: // Delay Time Center + case 0x53: // Delay Time Ratio Left + case 0x54: // Delay Time Ratio Right + case 0x55: // Delay Level Center + case 0x56: // Delay Level Left + case 0x57: // Delay Level Right + case 0x58: // Delay Level + case 0x59: // Delay Freeback + case 0x5a: // Delay sendlevel to Reverb + break; + } + } + } + else if ((addr & (~(0x0fff))) == 0x01000) { // GS CH + part = (addr >> 8) & 0x0f; + if (part == 0) { // part10 + part = 9; + } + else if (part < 10) { // part1-9 + part--; + } + ch = hdl->channel + part; + switch(addr & 0xff) { +#if !defined(MIDI_GMONLY) + case 0x00: // TONE NUMBER + ch->bank = data; + break; +#endif + + case 0x01: // PROGRAM NUMBER + progchange(hdl, ch, data); + break; + + case 0x02: // Rx.CHANNEL + TRACEOUT(("RxCHANNEL: %d", data)); + break; + +#if defined(ENABLE_GSRX) + case 0x03: // Rx.PITCHBEND + case 0x04: // Rx.CH PRESSURE + case 0x05: // Rx.PROGRAM CHANGE + case 0x06: // Rx.CONTROL CHANGE + case 0x07: // Rx.POLY PRESSURE + case 0x08: // Rx.NOTE MESSAGE + case 0x09: // Rx.PRN + case 0x0a: // Rx.NRPN + bit = 1 << ((addr - 0x03) & 7); + if (data == 0) { + ch->gsrx[0] = ch->gsrx[0] & (~bit); + } + else if (data == 1) { + ch->gsrx[0] = ch->gsrx[0] | bit; + } + break; + + case 0x0b: // Rx.MODULATION + case 0x0c: // Rx.VOLUME + case 0x0d: // Rx.PANPOT + case 0x0e: // Rx.EXPRESSION + case 0x0f: // Rx.HOLD1 + case 0x10: // Rx.PORTAMENTO + case 0x11: // Rx.SOSTENUTO + case 0x12: // Rx.SOFT + bit = 1 << ((addr - 0x0b) & 7); + if (data == 0) { + ch->gsrx[1] = ch->gsrx[1] & (~bit); + } + else if (data == 1) { + ch->gsrx[1] = ch->gsrx[1] | bit; + } + break; +#endif + case 0x15: // USE FOR RHYTHM PART + if (data == 0) { + ch->flag &= ~CHANNEL_RHYTHM; + TRACEOUT(("ch%d - tone", part + 1)); + } + else if ((data == 1) || (data == 2)) { + ch->flag |= CHANNEL_RHYTHM; + TRACEOUT(("ch%d - rhythm", part + 1)); + } + break; + +#if defined(ENABLE_GSRX) + case 0x16: // PITCH KEY SHIFT + if ((data >= 0x28) && (data <= 0x58)) { + ch->keyshift = data; + } + break; + + case 0x1d: // KEYBOARD RANGE LOW + ch->noterange[0] = data; + break; + + case 0x1e: // KEYBOARD RANGE HIGH + ch->noterange[1] = data; + break; + + case 0x23: // Rx.BANK SELECT + case 0x24: // Rx.BANK SELECT LSB + bit = 1 << ((addr - 0x23) & 7); + if (data == 0) { + ch->gsrx[2] = ch->gsrx[2] & (~bit); + } + else if (data == 1) { + ch->gsrx[2] = ch->gsrx[2] | bit; + } + break; +#endif + default: + TRACEOUT(("Roland GS - %.6x %.2x", addr, data)); + break; + } + } + else { + TRACEOUT(("Roland GS - %.6x %.2x", addr, data)); + } +} + +static void VERMOUTHCL longmsg_roland(MIDIHDL hdl, const UINT8 *msg, + UINT size) { + + UINT addr; + UINT8 data; + + if (size <= 10) { + return; + } + // GS data set + if ((msg[2] != 0x10) || (msg[3] != 0x42) || (msg[4] != 0x12)) { + return; + } + addr = (msg[5] << 16) + (msg[6] << 8) + msg[7]; + msg += 8; + size -= 10; + while(size) { + size--; + data = (*msg++) & 0x7f; + if ((addr & (~0x400000)) == 0x7f) { // GS reset + allresetmidi(hdl, TRUE); + TRACEOUT(("GS-Reset")); + } + else if ((addr & 0xfff00000) == 0x00400000) { + rolandcmd4(hdl, addr, data); + } +#if defined(ENABLE_PORTB) + else if ((addr & 0xfff00000) == 0x00500000) { + if (hdl->portb) { + rolandcmd4(hdl->portb, addr, data); + } + } +#endif // defined(ENABLE_PORTB) + addr++; + } +} + +VEXTERN void VEXPORT midiout_longmsg(MIDIHDL hdl, const UINT8 *msg, UINT size) { + + UINT id; if ((hdl == NULL) || (msg == NULL)) { return; } - if ((size > 5) && (msg[1] == 0x7e)) { // GM - if ((msg[2] == 0x7f) && (msg[3] == 0x09)) { - allresetmidi(hdl); // GM reset + if (size > 3) { // (msg[size - 1] == 0xf7) + id = msg[1]; + if (id == 0x7f) { // Universal realtime + longmsg_uni(hdl, msg, size); + } + else if (id == 0x7e) { // GM + longmsg_gm(hdl, msg, size); + } + else if (id == 0x41) { // Roland + longmsg_roland(hdl, msg, size); + } + else { + TRACEOUT(("long msg unknown id:%02x", id)); } } } -const SINT32 *midiout_get(MIDIHDL hdl, UINT *samples) { +VEXTERN const SINT32 * VEXPORT midiout_get(MIDIHDL hdl, UINT *samples) { UINT size; VOICE v; @@ -816,7 +1136,87 @@ moget_err: return(NULL); } -UINT midiout_get32(MIDIHDL hdl, SINT32 *pcm, UINT size) { +VEXTERN UINT VEXPORT midiout_get16(MIDIHDL hdl, SINT16 *pcm, UINT size) { + + UINT step; + VOICE v; + VOICE vterm; + SINT32 *buf; + SAMPLE src; + SAMPLE srcterm; + UINT cnt; + UINT pos; + UINT rem; + SINT32 l; + SINT32 r; + + if ((hdl != NULL) && (size)) { + do { + step = min(size, hdl->worksize); + size -= step; + buf = hdl->sampbuf; + ZeroMemory(buf, step * 2 * sizeof(SINT32)); + v = hdl->voice; + vterm = v + VOICE_MAX; + do { + if (v->phase != VOICE_FREE) { + cnt = step; + if (v->phase & VOICE_REL) { + voice_setfree(v); + if (cnt > REL_COUNT) { + cnt = REL_COUNT; + } + } + if (v->flag & VOICE_FIXPITCH) { + pos = v->samppos >> FREQ_SHIFT; + src = v->sample->data + pos; + rem = (v->sample->datasize >> FREQ_SHIFT) - pos; + if (cnt < rem) { + v->samppos += cnt << FREQ_SHIFT; + srcterm = src + cnt; + } + else { + voice_setfree(v); + srcterm = src + rem; + } + } + else { + src = hdl->resampbuf; + srcterm = v->resamp(v, src, src + cnt); + } + if (src != srcterm) { + v->mix(v, buf, src, srcterm); + } + } + v++; + } while(v < vterm); + do { + l = pcm[0]; + r = pcm[1]; + l += buf[0] >> (SAMP_SHIFT + 1); + r += buf[1] >> (SAMP_SHIFT + 1); + if (l < -32768) { + l = -32768; + } + else if (l > 32767) { + l = 32767; + } + if (r < -32768) { + r = -32768; + } + else if (r > 32767) { + r = 32767; + } + pcm[0] = l; + pcm[1] = r; + buf += 2; + pcm += 2; + } while(--step); + } while(size); + } + return(0);} + +VEXTERN UINT VEXPORT midiout_get32(MIDIHDL hdl, SINT32 *pcm, UINT size) { UINT step; VOICE v; @@ -879,7 +1279,7 @@ UINT midiout_get32(MIDIHDL hdl, SINT32 * return(0); } -void midiout_setgain(MIDIHDL hdl, int gain) { +VEXTERN void VEXPORT midiout_setgain(MIDIHDL hdl, int gain) { if (hdl) { if (gain < -16) { @@ -893,3 +1293,19 @@ void midiout_setgain(MIDIHDL hdl, int ga } } +VEXTERN void VEXPORT midiout_setmoduleid(MIDIHDL hdl, UINT8 moduleid) { + + if (hdl) { + hdl->moduleid = moduleid; + } +} + +VEXTERN void VEXPORT midiout_setportb(MIDIHDL hdl, MIDIHDL portb) { + +#if defined(ENABLE_PORTB) + if (hdl) { + hdl->portb = portb; + } +#endif // defined(ENABLE_PORTB) +} +