2021-05-15 19:18:16 +00:00
|
|
|
#include "sms.h"
|
2021-05-15 21:42:48 +00:00
|
|
|
#include "../engine.h"
|
2021-05-15 19:18:16 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2021-12-06 21:51:18 +00:00
|
|
|
void DivPlatformSMS::acquire(short* bufL, short* bufR, size_t start, size_t len) {
|
|
|
|
sn->sound_stream_update(bufL+start,len);
|
2021-12-06 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformSMS::acquireOne() {
|
2021-05-15 19:18:16 +00:00
|
|
|
short v;
|
|
|
|
sn->sound_stream_update(&v,1);
|
2021-12-06 10:21:42 +00:00
|
|
|
return v;
|
2021-05-15 19:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSMS::tick() {
|
2021-05-15 21:42:48 +00:00
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
chan[i].std.next();
|
2021-05-17 08:06:45 +00:00
|
|
|
if (chan[i].std.hadVol) {
|
|
|
|
chan[i].outVol=(chan[i].vol*chan[i].std.vol)>>4;
|
2021-05-17 20:36:14 +00:00
|
|
|
sn->write(0x90|(i<<5)|(15-(chan[i].outVol&15)));
|
2021-05-17 08:06:45 +00:00
|
|
|
}
|
2021-05-16 08:03:23 +00:00
|
|
|
if (chan[i].std.hadArp) {
|
2021-05-16 17:47:05 +00:00
|
|
|
if (chan[i].std.arpMode) {
|
2021-05-17 05:36:09 +00:00
|
|
|
chan[i].baseFreq=round(1712.0f/pow(2.0f,((float)(chan[i].std.arp)/12.0f)));
|
2021-05-16 17:47:05 +00:00
|
|
|
} else {
|
|
|
|
chan[i].baseFreq=round(1712.0f/pow(2.0f,((float)(chan[i].note+chan[i].std.arp-12)/12.0f)));
|
|
|
|
}
|
2021-05-16 08:03:23 +00:00
|
|
|
chan[i].freqChanged=true;
|
|
|
|
}
|
2021-05-17 06:51:14 +00:00
|
|
|
if (chan[i].std.hadDuty) {
|
|
|
|
snNoiseMode=(snNoiseMode&2)|(chan[i].std.duty&1);
|
|
|
|
if (chan[i].std.duty<2) {
|
|
|
|
chan[3].freqChanged=false;
|
|
|
|
}
|
|
|
|
updateSNMode=true;
|
|
|
|
}
|
2021-05-15 21:42:48 +00:00
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
for (int i=0; i<3; i++) {
|
|
|
|
if (chan[i].freqChanged) {
|
2021-05-16 08:03:23 +00:00
|
|
|
chan[i].freq=(chan[i].baseFreq*(ONE_SEMITONE-chan[i].pitch))/ONE_SEMITONE;
|
2021-05-17 01:10:52 +00:00
|
|
|
if (chan[i].note>0x5d) chan[i].freq=0x01;
|
2021-05-15 19:18:16 +00:00
|
|
|
sn->write(0x80|i<<5|(chan[i].freq&15));
|
|
|
|
sn->write(chan[i].freq>>4);
|
|
|
|
chan[i].freqChanged=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chan[3].freqChanged || updateSNMode) {
|
|
|
|
updateSNMode=false;
|
2021-05-16 08:03:23 +00:00
|
|
|
chan[3].freq=(chan[3].baseFreq*(ONE_SEMITONE-chan[3].pitch))/ONE_SEMITONE;
|
2021-05-17 01:10:52 +00:00
|
|
|
if (chan[3].note>0x5d) chan[3].freq=0x01;
|
2021-05-15 19:18:16 +00:00
|
|
|
chan[3].freqChanged=false;
|
|
|
|
if (snNoiseMode&2) { // take period from channel 3
|
|
|
|
if (snNoiseMode&1) {
|
|
|
|
sn->write(0xe7);
|
|
|
|
} else {
|
|
|
|
sn->write(0xe3);
|
|
|
|
}
|
|
|
|
sn->write(0xdf);
|
|
|
|
sn->write(0xc0|(chan[3].freq&15));
|
|
|
|
sn->write(chan[3].freq>>4);
|
|
|
|
} else { // 3 fixed values
|
2021-05-17 08:06:45 +00:00
|
|
|
unsigned char value;
|
|
|
|
if (chan[3].std.hadArp) {
|
|
|
|
if (chan[3].std.arpMode) {
|
|
|
|
value=chan[3].std.arp%12;
|
|
|
|
} else {
|
|
|
|
value=(chan[3].note+chan[3].std.arp)%12;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value=chan[3].note%12;
|
|
|
|
}
|
|
|
|
if (value<3) {
|
|
|
|
value=2-value;
|
|
|
|
sn->write(0xe0|value|((snNoiseMode&1)<<2));
|
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformSMS::dispatch(DivCommand c) {
|
|
|
|
switch (c.cmd) {
|
|
|
|
case DIV_CMD_NOTE_ON:
|
2021-05-16 08:03:23 +00:00
|
|
|
chan[c.chan].baseFreq=round(1712.0f/pow(2.0f,((float)c.value/12.0f)));
|
2021-05-15 19:18:16 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
|
|
|
chan[c.chan].active=true;
|
2021-05-17 20:36:14 +00:00
|
|
|
sn->write(0x90|c.chan<<5|(15-(chan[c.chan].vol&15)));
|
2021-05-17 01:49:54 +00:00
|
|
|
chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
|
2021-05-15 19:18:16 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
|
|
|
chan[c.chan].active=false;
|
2021-05-15 21:59:57 +00:00
|
|
|
sn->write(0x9f|c.chan<<5);
|
|
|
|
chan[c.chan].std.init(NULL);
|
2021-05-15 19:18:16 +00:00
|
|
|
break;
|
2021-05-15 21:42:48 +00:00
|
|
|
case DIV_CMD_INSTRUMENT:
|
|
|
|
chan[c.chan].ins=c.value;
|
2021-05-17 01:49:54 +00:00
|
|
|
//chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
|
2021-05-15 21:42:48 +00:00
|
|
|
break;
|
2021-05-15 19:18:16 +00:00
|
|
|
case DIV_CMD_VOLUME:
|
2021-05-17 06:51:14 +00:00
|
|
|
if (chan[c.chan].vol!=c.value) {
|
|
|
|
chan[c.chan].vol=c.value;
|
2021-05-17 08:06:45 +00:00
|
|
|
if (!chan[c.chan].std.hasVol) {
|
|
|
|
chan[c.chan].outVol=c.value;
|
|
|
|
}
|
2021-05-17 20:36:14 +00:00
|
|
|
sn->write(0x90|c.chan<<5|(15-(chan[c.chan].vol&15)));
|
2021-05-17 06:51:14 +00:00
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
break;
|
2021-05-17 08:06:45 +00:00
|
|
|
case DIV_CMD_GET_VOLUME:
|
|
|
|
if (chan[c.chan].std.hasVol) {
|
|
|
|
return chan[c.chan].vol;
|
|
|
|
}
|
|
|
|
return chan[c.chan].outVol;
|
|
|
|
break;
|
2021-05-15 21:42:48 +00:00
|
|
|
case DIV_CMD_PITCH:
|
|
|
|
chan[c.chan].pitch=c.value;
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
break;
|
2021-05-16 08:03:23 +00:00
|
|
|
case DIV_CMD_NOTE_PORTA: {
|
|
|
|
int destFreq=round(1712.0f/pow(2.0f,((float)c.value2/12.0f)));
|
|
|
|
bool return2=false;
|
|
|
|
if (destFreq>chan[c.chan].baseFreq) {
|
|
|
|
chan[c.chan].baseFreq+=c.value;
|
|
|
|
if (chan[c.chan].baseFreq>=destFreq) {
|
|
|
|
chan[c.chan].baseFreq=destFreq;
|
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chan[c.chan].baseFreq-=c.value;
|
|
|
|
if (chan[c.chan].baseFreq<=destFreq) {
|
|
|
|
chan[c.chan].baseFreq=destFreq;
|
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
if (return2) return 2;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
case DIV_CMD_STD_NOISE_MODE:
|
|
|
|
snNoiseMode=(c.value&1)|((c.value&16)>>3);
|
|
|
|
updateSNMode=true;
|
|
|
|
break;
|
2021-05-16 08:03:23 +00:00
|
|
|
case DIV_CMD_LEGATO:
|
|
|
|
chan[c.chan].baseFreq=round(1712.0f/pow(2.0f,((float)c.value/12.0f)));
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
|
|
|
break;
|
2021-05-17 06:51:14 +00:00
|
|
|
case DIV_CMD_PRE_PORTA:
|
|
|
|
chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
|
|
|
|
break;
|
2021-05-17 20:06:11 +00:00
|
|
|
case DIV_CMD_GET_VOLMAX:
|
|
|
|
return 15;
|
|
|
|
break;
|
2021-05-28 05:36:25 +00:00
|
|
|
case DIV_ALWAYS_SET_VOLUME:
|
|
|
|
return 0;
|
|
|
|
break;
|
2021-05-15 19:18:16 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-07 06:23:57 +00:00
|
|
|
bool DivPlatformSMS::keyOffAffectsArp() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-15 19:18:16 +00:00
|
|
|
int DivPlatformSMS::init(DivEngine* p, int channels, int sugRate) {
|
|
|
|
parent=p;
|
|
|
|
rate=223722;
|
2021-05-28 04:03:10 +00:00
|
|
|
sn=new sn76496_device(223722);
|
2021-05-15 19:18:16 +00:00
|
|
|
sn->device_start();
|
2021-05-15 21:59:57 +00:00
|
|
|
snNoiseMode=3;
|
2021-05-15 19:18:16 +00:00
|
|
|
updateSNMode=false;
|
|
|
|
return 4;
|
|
|
|
}
|