-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRefaceDX.cpp
427 lines (380 loc) · 14.8 KB
/
RefaceDX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Copyright (c) 2020 Christof Ruch. All rights reserved.
Dual licensed: Distributed under Affero GPL license by default, an MIT license is available for purchase
*/
#include "RefaceDX.h"
#include "RefaceDXPatch.h"
#include "MidiHelpers.h"
#include "Sysex.h"
#include "MidiController.h"
#include <boost/format.hpp>
namespace midikraft {
std::vector<Range<size_t>> kRefaceDXBlankOutZones = {
{ 0, 10 }, // 10 Characters for the name, the first bytes of the common block so it's really at the start of the data
};
juce::MidiMessage RefaceDX::buildRequest(uint8 addressHigh, uint8 addressMid, uint8 addressLow) const
{
return MidiHelpers::sysexMessage(
{ 0x43 /* Yamaha */, (uint8)(0x20 /* dump request */ | deviceID_), 0x7f /* Group high */, 0x1c /* Group low */, 0x05 /* Model */, addressHigh, addressMid, addressLow }
);
}
juce::MidiMessage RefaceDX::buildParameterChange(uint8 addressHigh, uint8 addressMid, uint8 addressLow, uint8 value)
{
return MidiHelpers::sysexMessage(
{ 0x43 /* Yamaha */, (uint8)(0x10 /* parameter change */ | deviceID_), 0x7f /* Group high */, 0x1c /* Group low */, 0x05 /* Model */, addressHigh, addressMid, addressLow, value }
);
}
std::vector<MidiMessage> RefaceDX::requestStreamElement(int elemNo, StreamType streamType) const
{
switch (streamType) {
case StreamLoadCapability::StreamType::BANK_DUMP:
// Need to start at program 0
return { MidiMessage::programChange(channel().toOneBasedInt(), elemNo), buildRequest(0x0e, 0x0f, 0x00 /* Address of bulk header */) };
case StreamLoadCapability::StreamType::EDIT_BUFFER_DUMP:
// Just download the current program
return { buildRequest(0x0e, 0x0f, 0x00 /* Address of bulk header */) };
default:
jassertfalse;
return {};
}
}
int RefaceDX::numberOfStreamMessagesExpected(StreamType streamType) const
{
switch (streamType)
{
case StreamLoadCapability::StreamType::EDIT_BUFFER_DUMP:
return 7;
case StreamLoadCapability::StreamType::BANK_DUMP:
return 32 + 32 + 32 + 32 * 4;
default:
return 0;
}
}
bool RefaceDX::isMessagePartOfStream(MidiMessage const &message, StreamType streamType) const
{
ignoreUnused(streamType); // Both stream types consist of the same message types
if (isOwnSysex(message)) {
TDataBlock block;
if (dataBlockFromDump(message, block)) {
return (block.addressHigh == 0x0e && block.addressMid == 0x0f && block.addressLow == 0x00) // Bulk header
|| (block.addressHigh == 0x0f && block.addressMid == 0x0f && block.addressLow == 0x00) // Bulk footer
|| (block.addressHigh == 0x30 && block.addressMid == 0x00 && block.addressLow == 0x00) // Common voice data
|| (block.addressHigh == 0x31 && block.addressLow == 0x00); // Operator data
}
}
return false;
}
bool RefaceDX::isStreamComplete(std::vector<MidiMessage> const &messages, StreamType streamType) const {
int headers, footers, common, operators;
headers = footers = common = operators = 0;
for (auto message : messages) {
TDataBlock block;
if (dataBlockFromDump(message, block)) {
if (block.addressHigh == 0x0e && block.addressMid == 0x0f && block.addressLow == 0x00) headers++;
if (block.addressHigh == 0x0f && block.addressMid == 0x0f && block.addressLow == 0x00) footers++;
if (block.addressHigh == 0x30 && block.addressMid == 0x00 && block.addressLow == 0x00) common++;
if (block.addressHigh == 0x31 && block.addressLow == 0x00) operators++;
}
else {
jassert(false);
}
}
switch (streamType) {
case StreamLoadCapability::StreamType::BANK_DUMP:
return headers == 32 && footers == 32 && common == 32 && operators == (32 * 4);
case StreamLoadCapability::StreamType::EDIT_BUFFER_DUMP:
return headers == 1 && footers == 1 && common == 1 && operators == (1 * 4);
default:
jassertfalse;
return false;
}
}
bool RefaceDX::shouldStreamAdvance(std::vector<MidiMessage> const &messages, StreamType streamType) const
{
int headers, footers;
headers = footers = 0;
for (auto message : messages) {
TDataBlock block;
if (dataBlockFromDump(message, block)) {
if (block.addressHigh == 0x0e && block.addressMid == 0x0f && block.addressLow == 0x00) headers++;
if (block.addressHigh == 0x0f && block.addressMid == 0x0f && block.addressLow == 0x00) footers++;
}
else {
jassert(false);
}
}
return (headers == footers) && (streamType == StreamLoadCapability::StreamType::BANK_DUMP); // The Edit buffer dump would be finished, and does not need to advance
}
void RefaceDX::changeOutputChannel(MidiController *controller, MidiChannel newChannel, std::function<void()> finished)
{
// Transmit Channel has the address 0, 0, 0
controller->getMidiOutput(midiOutput())->sendMessageNow(
buildParameterChange(0, 0, 0, newChannel.isValid() ? ((uint8)newChannel.toZeroBasedInt()) : 0x7f)
);
transmitChannel_ = newChannel;
finished();
}
MidiChannel RefaceDX::getOutputChannel() const
{
return transmitChannel_;
}
bool RefaceDX::hasLocalControl() const
{
return true;
}
void RefaceDX::setLocalControl(MidiController *controller, bool localControlOn)
{
// Local control has the address 0, 0, 6
controller->getMidiOutput(midiOutput())->sendMessageNow(buildParameterChange(0, 0, 6, localControlOn ? 1 : 0));
localControl_ = localControlOn;
}
bool RefaceDX::getLocalControl() const
{
return localControl_;
}
bool RefaceDX::canChangeInputChannel() const
{
return true;
}
void RefaceDX::changeInputChannel(MidiController *controller, MidiChannel channel, std::function<void()> finished)
{
// Receive channel has the address 0, 0, 1
controller->getMidiOutput(midiOutput())->sendMessageNow(
buildParameterChange(0, 0, 1, channel.isValid() ? ((uint8)channel.toZeroBasedInt()) : 0x10 /* OMNI. We could dispute if we should rather set MIDI Control to Off */)
);
setCurrentChannelZeroBased(midiInput(), midiOutput(), channel.toZeroBasedInt());
finished();
}
MidiChannel RefaceDX::getInputChannel() const
{
return channel();
}
bool RefaceDX::hasMidiControl() const
{
return true;
}
bool RefaceDX::isMidiControlOn() const
{
return midiControlOn_;
}
void RefaceDX::setMidiControl(MidiController *controller, bool isOn)
{
controller->getMidiOutput(midiOutput())->sendMessageNow(
buildParameterChange(0, 0, 0x0e, isOn ? 1 : 0)
);
midiControlOn_ = isOn;
}
bool RefaceDX::dataBlockFromDump(const MidiMessage &message, TDataBlock &outBlock) const {
if (isOwnSysex(message)) {
// Check the number of bytes
uint16 dataLength = ((uint16)message.getSysExData()[4]) << 7 | message.getSysExData()[5];
if (message.getSysExDataSize() == dataLength + 7) {
// Looks good up to here, now extract the data block, but don't copy the checksum (hence - 1)
outBlock.data.clear();
int sum = 0;
for (int i = 0; i < dataLength - 1; i++) {
uint8 dataByte = message.getSysExData()[7 + i];
switch (i) {
case 0: outBlock.addressHigh = dataByte; break;
case 1: outBlock.addressMid = dataByte; break;
case 2: outBlock.addressLow = dataByte; break;
default:
outBlock.data.push_back(dataByte);
}
sum -= dataByte;
}
// Strangely, the Model ID 0x05 is included in the checksum. Manually add in back in here, as we did not get it out
uint8 checkSum = ((sum - 0x05) & 0x7f);
uint8 expected = message.getSysExData()[7 + dataLength - 1];
if (checkSum == expected) {
return true;
}
else {
// Checksum error
return false;
}
}
}
return false;
}
MidiMessage RefaceDX::buildDataBlockMessage(TDataBlock const &block) const {
std::vector<uint8> bulkDump(
{ 0x43 /* Yamaha */, (uint8)(0x00 /* bulk dump */ | deviceID_), 0x7f /* Group high */, 0x1c /* Group low */, 0, 0, 0x05 /* Model */,
block.addressHigh, block.addressMid, block.addressLow }
);
std::copy(block.data.begin(), block.data.end(), std::back_inserter(bulkDump));
// We need a checksum
int sum = 0;
std::for_each(bulkDump.begin() + 6, bulkDump.end(), [&](uint8 byte) { sum -= byte; });
bulkDump.push_back(sum & 0x7f);
// We need to record the number of bytes in this message
uint16 dataBytes = ((uint16)bulkDump.size()) - 7;
bulkDump[4] = (uint8)(dataBytes >> 7);
bulkDump[5] = dataBytes & 0x7f;
return MidiHelpers::sysexMessage(bulkDump);
}
std::string RefaceDX::getName() const
{
return "Yamaha Reface DX";
}
bool RefaceDX::isOwnSysex(MidiMessage const &message) const
{
if (message.isSysEx()) {
if (message.getSysExDataSize() > 6) {
// Strangely asymmetric with the message we send to the Reface
return message.getSysExData()[0] == 0x43 /* Yamaha */
&& message.getSysExData()[2] == 0x7f /* Group high */
&& message.getSysExData()[3] == 0x1c /* Group low */
&& message.getSysExData()[6] == 0x05 /* Model */;
}
}
return false;
}
Synth::PatchData RefaceDX::filterVoiceRelevantData(std::shared_ptr<DataFile> unfilteredData) const
{
return Patch::blankOut(kRefaceDXBlankOutZones, unfilteredData->data());
}
std::shared_ptr<DataFile> RefaceDX::patchFromPatchData(const Synth::PatchData &data, MidiProgramNumber place) const
{
auto newPatch = std::make_shared<RefaceDXPatch>(data, place);
return newPatch;
}
std::vector<juce::MidiMessage> RefaceDX::patchToSysex(std::shared_ptr<DataFile> patch) const
{
std::vector<juce::MidiMessage> result;
result.push_back(buildDataBlockMessage(TDataBlock(0x0e, 0x0f, 0x00, std::vector<uint8>().begin(), 0)));
result.push_back(buildDataBlockMessage(TDataBlock(0x30, 0x00, 0x00, patch->data().begin(), 38)));
for (size_t op = 0; op < 4; op++) {
result.push_back(buildDataBlockMessage(TDataBlock(0x31, (uint8)op, 0x00, patch->data().begin() + 38 + op * 28, 28)));
}
result.push_back(buildDataBlockMessage(TDataBlock(0x0f, 0x0f, 0x00, std::vector<uint8>().begin(), 0)));
return result;
}
std::vector<juce::MidiMessage> RefaceDX::deviceDetect(int channel)
{
ignoreUnused(channel);
// Instead of using the official device ID request, we will just send a dump request for the system settings, in which
// we will find both receiving and sending channel (which, excellently, can be different on the Reface).
return { buildRequest(0x00, 0x00, 0x00 /* Address system settings */) };
}
MidiChannel RefaceDX::channelIfValidDeviceResponse(const MidiMessage &message)
{
if (message.isSysEx()) {
TDataBlock block;
if (dataBlockFromDump(message, block)) {
// We expect the system data dump here
if ((block.data.size() == 32) && block.addressHigh == 0x00 && block.addressMid == 0x00 && block.addressLow == 0x00) {
uint8 transmitChannel = block.data[0]; // Transmit channel of Reface DX
if (transmitChannel != 0x7f) {
// It is not off
transmitChannel_ = MidiChannel::fromZeroBase(transmitChannel);
}
localControl_ = block.data[6] == 1;
midiControlOn_ = block.data[0x0e] == 1;
uint8 midiChannel = block.data[1]; // Receiving channel of Reface DX
if (midiChannel == 0x10) {
// That's omni, not good for your setup, so let's log a warning
SimpleLogger::instance()->postMessage("Warning: Your RefaceDX is set to receive MIDI omni, so it will react on all channels");
return MidiChannel::omniChannel();
}
return MidiChannel::fromZeroBase(midiChannel);
}
}
}
return MidiChannel::invalidChannel();
}
TPatchVector RefaceDX::loadPatchesFromStream(std::vector<MidiMessage> const &sysexMessages) const
{
// Patches loaded from a list of MidiMessages... First we need to find complete "voices"
std::vector<RefaceDXPatch::TVoiceData> voiceData;
bool patchActive = false;
int count = 0;
for (const auto& message : sysexMessages) {
TDataBlock block;
if (dataBlockFromDump(message, block)) {
if (block.addressHigh == 0x0e && block.addressMid == 0x0f && block.addressLow == 0x00) {
// Bulk header
if (patchActive) {
Logger::getCurrentLogger()->writeToLog("Warning: Parsing RefaceDX sysex - got bulk header before footer of previous bulk block, ignored");
}
else {
// Create a new but empty voice data entry
voiceData.push_back(RefaceDXPatch::TVoiceData());
voiceData.back().count = count++;
}
patchActive = true;
}
else if (block.addressHigh == 0x0f && block.addressMid == 0x0f && block.addressLow == 0x00) {
// Bulk footer
if (!patchActive) {
Logger::getCurrentLogger()->writeToLog("Warning: Parsing RefaceDX sysex - got bulk footer before header of bulk block, ignored");
}
patchActive = false;
}
else if (patchActive) {
if (block.addressHigh == 0x30 && block.addressMid == 0x00 && block.addressLow == 0x00) {
// Common Voice data
if (voiceData.back().common.size() == 0) {
voiceData.back().common = block.data;
}
else {
Logger::getCurrentLogger()->writeToLog("Warning: Parsing RefaceDX sysex - got second common voice block within bulk block, ignored");
}
}
else if (block.addressHigh == 0x31 && block.addressLow == 0x00) {
uint8 operatorIndex = block.addressMid;
if (operatorIndex >= 0 && operatorIndex < 4) {
if (voiceData.back().op[operatorIndex].size() == 0) {
voiceData.back().op[operatorIndex] = block.data;
}
else {
Logger::getCurrentLogger()->writeToLog("Warning: Parsing RefaceDX sysex - got additional operator block within bulk block, ignored");
}
}
}
else {
Logger::getCurrentLogger()->writeToLog("Warning: Parsing RefaceDX sysex - got invalid block within bulk block, ignored");
}
}
else {
//TODO - this could be a system block
Logger::getCurrentLogger()->writeToLog("Warning: Parsing RefaceDX sysex - got block outside of bulk block, ignored");
}
}
}
// We now might have or not a list of valid VoiceData packages, which we can wrap into patch classes
TPatchVector result;
for (auto voice : voiceData) {
std::vector<uint8> aggregated;
std::copy(voice.common.begin(), voice.common.end(), std::back_inserter(aggregated));
for (int op = 0; op < 4; op++) std::copy(voice.op[op].begin(), voice.op[op].end(), std::back_inserter(aggregated));
result.push_back(std::make_shared<RefaceDXPatch>(aggregated, MidiProgramNumber::fromZeroBase(voice.count)));
}
return result;
}
std::vector<juce::MidiMessage> RefaceDX::dataFileToMessages(std::shared_ptr<DataFile> dataFile, std::shared_ptr<SendTarget> target) const
{
ignoreUnused(target);
return patchToSysex(dataFile);
}
int RefaceDX::numberOfBanks() const
{
// Yes, I know, it has 4 banks of 8 patches each. But I refuse.
return 1;
}
int RefaceDX::numberOfPatches() const
{
return 32;
}
std::string RefaceDX::friendlyProgramName(MidiProgramNumber programNo) const
{
int bank = programNo.toZeroBased() / 8;
int patch = programNo.toZeroBased() % 8;
return (boost::format("Bank%d-%d") % bank % patch).str();
}
std::string RefaceDX::friendlyBankName(MidiBankNumber bankNo) const
{
ignoreUnused(bankNo);
return "Banks 1-4";
}
}