Debian Codegraph @

src/gui/sysEx.cpp

raw
1 #include "gui.h"
2 #include "../ta-log.h"
3
4 // table taken from https://nornand.hatenablog.com/entry/2020/11/21/201911
5 // Yamaha why didn't you just use 0-127 as it should be?
6 const unsigned char tlTable[100]={
7 127, 122, 118, 114, 110, 107, 104, 102, 100, 98, 96, 94, 92, 90, 88, 86, 85, 84, 82, 81,
8 // desde aquĆ­ la tabla consiste de valores que bajan de 1 en 1
9 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58,
10 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36,
11 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14,
12 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
13 };
14
15 bool FurnaceGUI::parseSysEx(unsigned char* data, size_t len) {
16 SafeReader reader(data,len);
17
18 if (!midiMap.yamahaFMResponse) return true;
19
20 try {
21 unsigned char isSysEx=reader.readC();
22 if (isSysEx!=0xf0) {
23 logW("but this isn't a SysEx! (%x)",isSysEx);
24 return false;
25 }
26
27 unsigned char vendor=reader.readC();
28 if (vendor!=0x43) {
29 logV("not Yamaha. skipping.");
30 return true;
31 }
32
33 unsigned char channel=reader.readC();
34 logV("channel: %d",channel);
35 if (channel>15) {
36 logV("not a valid one");
37 return true;
38 }
39
40 unsigned char msgType=reader.readC();
41 unsigned char lenH=reader.readC();
42 unsigned char lenL=reader.readC();
43 unsigned short msgLen=(lenH<<7)|lenL;
44 logV("message length: %d",msgLen);
45 std::vector<DivInstrument*> instruments;
46
47 switch (msgType) {
48 case 0x03: { // VCED - voice data
49 logD("reading VCED...");
50 DivInstrument* ins=new DivInstrument;
51 ins->type=DIV_INS_FM;
52 for (int i=0; i<4; i++) {
53 DivInstrumentFM::Operator& op=ins->fm.op[i];
54 op.ar=reader.readC();
55 op.dr=reader.readC();
56 op.d2r=reader.readC();
57 op.rr=reader.readC();
58 op.sl=15-reader.readC();
59 reader.readC(); // LS - ignore
60 op.rs=reader.readC();
61 reader.readC(); // EBS - ignore
62 op.am=reader.readC();
63 op.kvs=(reader.readC()>2)?1:0;
64 op.tl=3+((99-reader.readC())*124)/99;
65 unsigned char freq=reader.readC();
66 logV("OP%d freq: %d",i,freq);
67 op.mult=freq>>2;
68 op.dt2=freq&3;
69 op.dt=reader.readC();
70 }
71
72 ins->fm.alg=reader.readC();
73 ins->fm.fb=reader.readC();
74 reader.readC(); // LFO speed - ignore
75 reader.readC(); // LFO delay - ignore
76 reader.readC(); // PMD
77 reader.readC(); // AMD
78 reader.readC(); // LFO sync
79 reader.readC(); // LFO shape
80 ins->fm.fms=reader.readC();
81 ins->fm.ams=reader.readC();
82 reader.readC(); // transpose
83
84 reader.readC(); // poly/mono
85 reader.readC(); // pitch bend range
86 reader.readC(); // porta mode
87 reader.readC(); // porta time
88 reader.readC(); // FC volume
89 reader.readC(); // sustain
90 reader.readC(); // portamento
91 reader.readC(); // chorus
92 reader.readC(); // mod wheel pitch
93 reader.readC(); // mod wheel amp
94 reader.readC(); // breath pitch
95 reader.readC(); // breath amp
96 reader.readC(); // breath pitch bias
97 reader.readC(); // breath EG bias
98
99 ins->name=reader.readString(10);
100
101 for (int i=0; i<7; i++) { // reserved (except the last one, which we don't support yet)
102 reader.readC();
103 }
104
105 // TX81Z-specific data
106 if (hasACED) {
107 hasACED=false;
108 ins->type=DIV_INS_OPZ;
109
110 for (int i=0; i<4; i++) {
111 DivInstrumentFM::Operator& op=ins->fm.op[i];
112 op.egt=acedData[(5*i)+0];
113 if (op.egt) {
114 op.dt=acedData[(5*i)+1];
115 }
116 op.dvb=acedData[(5*i)+2];
117 op.ws=acedData[(5*i)+3];
118 op.ksl=acedData[(5*i)+4];
119 op.dam=acedData[20];
120 }
121 }
122
123 instruments.push_back(ins);
124 break;
125 }
126 case 0x7e: { // ACED - TX81Z extended data
127 logD("reading ACED...");
128 String acedMagic=reader.readString(10);
129 if (acedMagic!="LM 8976AE") {
130 logD("not TX81Z ACED data");
131 break;
132 }
133 reader.read(acedData,23);
134 hasACED=true;
135 break;
136 }
137 case 0x04: { // VMEM - voice bank
138 logD("reading VMEM...");
139 for (int i=0; i<32; i++) {
140 DivInstrument* ins=new DivInstrument;
141 ins->type=DIV_INS_FM;
142 for (int i=0; i<4; i++) {
143 DivInstrumentFM::Operator& op=ins->fm.op[i];
144 op.ar=reader.readC();
145 op.dr=reader.readC();
146 op.d2r=reader.readC();
147 op.rr=reader.readC();
148 op.sl=15-reader.readC();
149 reader.readC(); // LS - ignore
150 op.am=(reader.readC()&0x40)?1:0;
151 op.tl=tlTable[reader.readC()%100];
152 unsigned char freq=reader.readC();
153 logV("OP%d freq: %d",i,freq);
154 op.mult=freq>>2;
155 op.dt2=freq&3;
156 unsigned char rsDt=reader.readC();
157 op.rs=rsDt>>3;
158 op.dt=rsDt&7;
159 }
160
161 unsigned char algFb=reader.readC();
162 ins->fm.alg=algFb&7;
163 ins->fm.fb=(algFb>>3)&7;
164 reader.readC(); // LFO speed - ignore
165 reader.readC(); // LFO delay - ignore
166 reader.readC(); // PMD
167 reader.readC(); // AMD
168 unsigned char fmsAmsShape=reader.readC();
169 ins->fm.fms=(fmsAmsShape>>4)&7;
170 ins->fm.ams=(fmsAmsShape>>2)&3;
171 for (int i=0; i<11; i++) { // skip 11 bytes
172 reader.readC();
173 }
174
175 ins->name=reader.readString(10);
176
177 for (int i=0; i<6; i++) { // reserved
178 reader.readC();
179 }
180
181 // TX81Z-specific data
182 for (int i=0; i<4; i++) {
183 DivInstrumentFM::Operator& op=ins->fm.op[i];
184 unsigned char egShiftFixedFixRange=reader.readC();
185 unsigned char wsFine=reader.readC();
186 op.egt=(egShiftFixedFixRange&8)?1:0;
187 if (op.egt) {
188 op.dt=egShiftFixedFixRange&7;
189 }
190 op.dvb=wsFine&15;
191 op.ws=wsFine>>4;
192 op.ksl=egShiftFixedFixRange>>4;
193 }
194 unsigned char rev=reader.readC();
195 for (int i=0; i<4; i++) {
196 ins->fm.op[i].dam=rev;
197 }
198
199 // skip more bytes
200 for (int i=0; i<46; i++) {
201 reader.readC();
202 }
203
204 instruments.push_back(ins);
205 }
206 break;
207 }
208 }
209
210 if (!reader.seek(6+msgLen,SEEK_SET)) {
211 logW("couldn't seek for checksum! (pre)");
212 for (DivInstrument* i: instruments) delete i;
213 return false;
214 }
215 unsigned char checkSum=reader.readC();
216 unsigned char localCheckSum=0xff;
217 if (!reader.seek(6,SEEK_SET)) {
218 logW("couldn't seek for checksum! (post)");
219 for (DivInstrument* i: instruments) delete i;
220 return false;
221 }
222 for (unsigned short i=0; i<msgLen; i++) {
223 localCheckSum+=reader.readC();
224 }
225 localCheckSum=(localCheckSum&0x7f)^0x7f;
226 logD("checksums: %.2x %.2x",checkSum,localCheckSum);
227
228 if (checkSum!=localCheckSum) {
229 logW("checksum invalid!");
230 return false;
231 }
232
233 for (DivInstrument* i: instruments) {
234 logI("got instrument from MIDI: %s",i->name);
235 e->addInstrumentPtr(i);
236 curIns=e->song.insLen-1;
237 }
238 } catch (EndOfFileException e) {
239 logW("end of data already?");
240 return false;
241 }
242 return true;
243 }