diff --git a/The-Orm/PatchView.cpp b/The-Orm/PatchView.cpp index bdb95ad7..7b89a4bd 100644 --- a/The-Orm/PatchView.cpp +++ b/The-Orm/PatchView.cpp @@ -460,7 +460,7 @@ void PatchView::downloadBanksFromSynth(std::shared_ptr synth, void PatchView::sendBankToSynth(std::shared_ptr bankToSend, bool ignoreDirty, std::function finishedHandler) { - if (!bankToSend) return; + if (!bankToSend || !bankToSend->isWritable()) return; if (bankToSend->hasEmptySlots()) { AlertWindow::showMessageBox(juce::AlertWindow::AlertIconType::WarningIcon, "Bank has empty slots", "Fill all slots with patches before sending this bank to the synth. No patches have been sent."); diff --git a/The-Orm/SynthBankPanel.cpp b/The-Orm/SynthBankPanel.cpp index cd22d91e..3eb3912a 100644 --- a/The-Orm/SynthBankPanel.cpp +++ b/The-Orm/SynthBankPanel.cpp @@ -196,13 +196,13 @@ void SynthBankPanel::refresh() { { auto activeBank = std::static_pointer_cast(synthBank_); auto timeAgo = (juce::Time::getCurrentTime() - activeBank->lastSynced()).getApproximateDescription(); - std::string romText; + std::string readOnlyText; if (!synthBank_->isWritable()) { - romText = " [ROM]"; + readOnlyText = " [Read-only]"; } bankNameAndDate_.setText(fmt::format("'{}'{} ({} ago)" , midikraft::SynthBank::friendlyBankName(synthBank_->synth(), synthBank_->bankNumber()) - , romText + , readOnlyText , timeAgo.toStdString()) , dontSendNotification); } diff --git a/adaptations/CMakeLists.txt b/adaptations/CMakeLists.txt index f2a473a9..0089915f 100644 --- a/adaptations/CMakeLists.txt +++ b/adaptations/CMakeLists.txt @@ -35,7 +35,7 @@ set(adaptation_files "Sequential_Fourm.py" "Sequential Pro 3.py" "Sequential Prophet 5 Rev4.py" "Sequential Prophet 6.py" "Sequential Prophet X.py" "Sequential_Take_5.py" "Sequential_Trigon6.py" "Studiologic Sledge.py" "Waldorf Blofeld.py" "Waldorf_Kyra.py" "Waldorf_MicroWave.py" "Waldorf_M.py" "Waldorf_Pulse.py" - "YamahaDX7.py" "YamahaDX7II.py" "Yamaha_TX7.py" "Yamaha_FS1R.py" "YamahaRefaceDX.py" "YamahaRefaceCP.py" "Yamaha_TX81Z.py" "Yamaha-YC-Series.py" + "YamahaDX7.py" "YamahaDX7II.py" "Yamaha_TX7.py" "Yamaha_FS1R.py" "YamahaRefaceDX.py" "YamahaRefaceCP.py" "Yamaha_TX81Z.py" "Yamaha-YC-Series.py" "Yamaha_TG33.py" "Zoom_MS.py" ) diff --git a/adaptations/GenericAdaptation.cpp b/adaptations/GenericAdaptation.cpp index ccdaf19b..5f9acc33 100644 --- a/adaptations/GenericAdaptation.cpp +++ b/adaptations/GenericAdaptation.cpp @@ -31,6 +31,7 @@ #ifdef _MSC_VER #pragma warning ( pop ) #endif +#include #include #include #include @@ -90,6 +91,7 @@ namespace knobkraft { * kOnPatchSelected = "onPatchSelected", * kOnPatchSent = "onPatchSent", * kMessageTimings = "messageTimings", + * kMessageSendDelays = "messageSendDelays", * kExpectsUploadReply = "expectsUploadReply", * kIsPartOfUploadReply = "isPartOfUploadReply"; @@ -134,6 +136,7 @@ namespace knobkraft { kGetClearText, kGetStoredTags, kMessageTimings, + kMessageSendDelays, kOnPatchSelected, kOnPatchSent, kExpectsUploadReply, @@ -608,6 +611,41 @@ namespace knobkraft { void GenericAdaptation::sendBlockOfMessagesToSynth(juce::MidiDeviceInfo const& midiOutput, std::vector const& buffer) { py::gil_scoped_acquire acquire; + std::vector messageDelays; + if (!buffer.empty() && pythonModuleHasFunction(kMessageSendDelays)) { + try { + // The optional hook receives the flattened block and can return the + // delay before each message in milliseconds, or None for normal pacing. + auto messages = midiMessagesToVector(buffer); + auto result = callMethod(kMessageSendDelays, messages); + if (!result.is_none()) { + auto delays = result.cast>(); + if (delays.size() != buffer.size() + || std::any_of(delays.begin(), delays.end(), [](int delay) { return delay < 0; })) { + throw std::invalid_argument("Expected one non-negative delay per MIDI message"); + } + messageDelays = std::move(delays); + } + } + catch (py::error_already_set& ex) { + logAdaptationError(kMessageSendDelays, ex); + } + catch (std::exception& ex) { + logAdaptationError(kMessageSendDelays, ex); + } + } + if (!messageDelays.empty()) { + py::gil_scoped_release release; + auto output = midikraft::MidiController::instance()->getMidiOutput(midiOutput); + for (size_t i = 0; i < buffer.size(); ++i) { + if (messageDelays[i] > 0) { + juce::Thread::sleep(messageDelays[i]); + } + output->sendMessageNow(buffer[i]); + } + return; + } + int delay = 0; bool handled = false; if (pythonModuleHasFunction(kMessageTimings)) { diff --git a/adaptations/Yamaha_TG33.py b/adaptations/Yamaha_TG33.py new file mode 100644 index 00000000..fd6f718a --- /dev/null +++ b/adaptations/Yamaha_TG33.py @@ -0,0 +1,179 @@ +# +# Yamaha TG33 adaptation, contributed to KnobKraft Orm under its dual licensing +# (Affero GPL by default, an MIT license is available for purchase from Christof Ruch). +# +# Voices, through the edit buffer. The SysEx follows Sean Luke's Edisyn TG33/SY22/SY35 editor +# (https://github.com/eclab/edisyn, edisyn/synth/yamahatg33, Apache 2.0): +# - the current voice is requested with F0 43 2n 7E "LM 0012VE" F7, n the device number; +# - it comes back as one message, F0 43 0n 7E "LM 0012VE" <587 bytes> F7, +# 605 bytes in all; the voice name is 8 ASCII characters at byte 28; +# - the checksum covers "LM 0012VE" and the voice data; +# - sent back in the same form, it replaces the voice being edited. +# The TG33 has no request for a stored voice. As Edisyn's changePatch() does, a voice of the internal bank +# is fetched by leaving Edit mode (the VOICE button, F0 43 1n 26 07 06 F7), selecting the bank with CC 0 = 0 +# then CC 32 = 0 (the TG33 ignores bank selects in Edit mode, and needs both), a program change, then the +# current-voice request; Edisyn pauses a second after the button and after the program change. +# The count bytes give the length of "LM 0012VE" and the voice data, 597 (04 55), as in Yamaha's manual +# (MIDI data format) and Edisyn's emitter; Edisyn's initial voice says 04 5D, so a voice is read whatever its +# count says and sent with 04 55. +# The SY22 and SY35 use another format ("PK 2203AE", 592 bytes) and are not covered here. +# +import hashlib +from typing import List + +import testing + +_header = [0x4C, 0x4D, 0x20, 0x20, 0x30, 0x30, 0x31, 0x32, 0x56, 0x45] # "LM 0012VE" +_voice_length = 605 +_name_start = 28 +_name_length = 8 +_count = [(_voice_length - 8) >> 7, (_voice_length - 8) & 0x7F] # 597: 04 55 +_voice_button = 0x06 +_internal_bank = 0 +_selection_pause_ms = 1000 + + +def name(): + return "Yamaha TG33" + + +def createDeviceDetectMessage(channel): + return createEditBufferRequest(channel) + + +def needsChannelSpecificDetection(): + return True + + +def messageTimings(): + return { + # Orm starts the reply timer before sending the selection sequence. + "replyTimeoutMs": 8000, + # Fallback for Orm builds without messageSendDelays(). + "generalMessageDelay": _selection_pause_ms, + "deviceDetectWaitMilliseconds": 300, + } + + +def channelIfValidDeviceResponse(message): + if isEditBufferDump(message): + return message[2] & 0x0F + return -1 + + +def createEditBufferRequest(channel): + return [0xF0, 0x43, 0x20 | (channel & 0x0F), 0x7E] + _header + [0xF7] + + +def isEditBufferDump(message): + return (len(message) == _voice_length + and message[0] == 0xF0 + and message[1] == 0x43 + and message[2] & 0xF0 == 0x00 + and message[3] == 0x7E + and message[6:16] == _header + and message[-1] == 0xF7 + and checksum(message[6:-2]) == message[-2]) + + +def createProgramDumpRequest(channel, patchNo): + c = channel & 0x0F + return ([0xF0, 0x43, 0x10 | c, 0x26, 0x07, _voice_button, 0xF7] + + [0xB0 | c, 0, 0] + [0xB0 | c, 32, _internal_bank] + + [0xC0 | c, patchNo % numberOfPatchesPerBank()] + + createEditBufferRequest(channel)) + + +def messageSendDelays(messages): + if len(messages) == 30: + channel = messages[2] & 0x0F + patch_number = messages[14] + if messages == createProgramDumpRequest(channel, patch_number): + # Wait after VOICE and Program Change, as Edisyn does. + return [0, _selection_pause_ms, 0, 0, _selection_pause_ms] + # Detection and edit-buffer writes need no selection delay. + if isEditBufferDump(messages) or (len(messages) == 15 and messages == createEditBufferRequest(messages[2] & 0x0F)): + return [0] + return None + + +def isSingleProgramDump(message): + # The voice comes back as the current voice: it holds no program number. + return isEditBufferDump(message) + + +def convertToProgramDump(channel, message, program_number): + # Orm uses this hook to enable numbered reads and to export individual voices. + # Bank descriptors prevent the resulting edit-buffer SysEx from being used for bank writes. + return convertToEditBuffer(channel, message) + + +def numberOfBanks(): + return 1 + + +def numberOfPatchesPerBank(): + return 64 + + +def bankDescriptors(): + # The internal bank can be read, but this adaptation only sends to the edit buffer. + return [{"bank": _internal_bank, "name": "Internal", "size": numberOfPatchesPerBank(), "isROM": True}] + + +def convertToEditBuffer(channel, message): + if isEditBufferDump(message): + return message[0:2] + [channel & 0x0F] + message[3:4] + _count + message[6:] + raise Exception("This is not a Yamaha TG33 voice") + + +def nameFromDump(message): + if isEditBufferDump(message): + return ''.join(chr(x) if 32 <= x < 127 else ' ' for x in message[_name_start:_name_start + _name_length]) + return "Invalid" + + +def renamePatch(message, new_name): + if isEditBufferDump(message): + name = [ord(c) if 32 <= ord(c) < 127 else 0x20 for c in new_name[:_name_length].ljust(_name_length)] + result = message[:_name_start] + name + message[_name_start + _name_length:] + result[-2] = checksum(result[6:-2]) + return result + raise Exception("Can only rename Yamaha TG33 voices") + + +def calculateFingerprint(message): + # The voice data without its name: the same sound whatever its name, device number or count bytes. + data = message[16:-2] + data[_name_start - 16:_name_start - 16 + _name_length] = [0] * _name_length + return hashlib.md5(bytearray(data)).hexdigest() + + +def checksum(data): + return -sum(data) & 0x7F + + +def make_test_data(): + def edit_buffers(data: testing.TestData) -> List[testing.ProgramTestData]: + # Edisyn's initial TG33 voice (edisyn/synth/yamahatg33/YamahaTG33.init, Apache 2.0) + voice = data.all_messages[0] + assert checksum(voice[6:-2]) == voice[-2] + assert createEditBufferRequest(2) == [0xF0, 0x43, 0x22, 0x7E] + _header + [0xF7] + assert channelIfValidDeviceResponse(convertToEditBuffer(5, voice)) == 5 + damaged = voice.copy() + damaged[100] ^= 0x01 + assert not isEditBufferDump(damaged), "a voice damaged in transfer is refused" + sent = convertToEditBuffer(5, voice) + assert sent[4:6] == [0x04, 0x55], "a voice is sent with the count Yamaha's manual gives" + assert isEditBufferDump(sent) + assert createProgramDumpRequest(2, 9) == ([0xF0, 0x43, 0x12, 0x26, 0x07, 0x06, 0xF7, 0xB2, 0, 0, 0xB2, 32, 0, 0xC2, 9] + + createEditBufferRequest(2)), "VOICE button, bank, program, request" + fingerprint = calculateFingerprint(voice) + assert calculateFingerprint(sent) == fingerprint, "the device number and count change no sound" + assert calculateFingerprint(renamePatch(voice, "Other")) == fingerprint, "nor does its name" + changed = voice.copy() + changed[100] ^= 0x01 + assert calculateFingerprint(changed) != fingerprint + yield testing.ProgramTestData(message=voice, name="Untitled", rename_name="Vector 1") + + return testing.TestData(sysex=R"testData/Yamaha_TG33/edisyn-init-voice.syx", edit_buffer_generator=edit_buffers) diff --git a/adaptations/testData/Yamaha_TG33/edisyn-init-voice.syx b/adaptations/testData/Yamaha_TG33/edisyn-init-voice.syx new file mode 100644 index 00000000..d911878d Binary files /dev/null and b/adaptations/testData/Yamaha_TG33/edisyn-init-voice.syx differ diff --git a/adaptations/test_Yamaha_TG33.py b/adaptations/test_Yamaha_TG33.py new file mode 100644 index 00000000..7c56832d --- /dev/null +++ b/adaptations/test_Yamaha_TG33.py @@ -0,0 +1,67 @@ +from pathlib import Path + +import knobkraft +import Yamaha_TG33 as tg33 +from testing.librarian import Librarian +from testing.mock_midi import MockMidiController + +VOICE = knobkraft.load_sysex(str(Path(__file__).resolve().parent / "testData" / "Yamaha_TG33" / "edisyn-init-voice.syx"))[0] + + +class TG33: + """A TG33 as Edisyn describes it: in Edit mode it ignores bank selects, the VOICE button leaves Edit mode, + the bank is CC 0 then CC 32, and a request returns the current voice.""" + + def __init__(self, channel, banks): + self.channel, self.banks = channel, banks + self.editing, self.bank, self.msb, self.program = True, 2, None, 0 + + def respond(self, message): + c = self.channel + if message == [0xF0, 0x43, 0x10 | c, 0x26, 0x07, 0x06, 0xF7]: + self.editing = False + elif message[:2] == [0xB0 | c, 0] and not self.editing: + self.msb = message[2] + elif message[:2] == [0xB0 | c, 32] and not self.editing and self.msb == 0: + self.bank = message[2] + elif message[0] == 0xC0 | c: + self.program = message[1] + elif message == tg33.createEditBufferRequest(c): + return [self.banks[self.bank][self.program]] + else: + raise AssertionError(f"unexpected {knobkraft.syxToString(message)}") + return [] + + +def test_a_bank_download_selects_the_internal_bank_first(): + internal = [tg33.renamePatch(VOICE, f"INT {i}") for i in range(64)] + preset = [tg33.renamePatch(VOICE, f"PRE {i}") for i in range(64)] + midi = MockMidiController(TG33(3, {0: internal, 2: preset})) + downloaded = [] + Librarian().start_downloading_all_patches(midi, 3, tg33, 0, downloaded.extend) + midi.drain() + assert [tg33.nameFromDump(v) for v in downloaded] == [f"INT {i}".ljust(8) for i in range(64)] + assert midi.sent_messages[:5] == [[0xF0, 0x43, 0x13, 0x26, 0x07, 0x06, 0xF7], [0xB3, 0, 0], [0xB3, 32, 0], [0xC3, 0], + tg33.createEditBufferRequest(3)] + + +def test_program_requests_pause_only_after_voice_and_program_change(): + timing = tg33.messageTimings() + for channel in (0, 3, 15): + for patch_number in (0, 9, 63): + request = tg33.createProgramDumpRequest(channel, patch_number) + delays = tg33.messageSendDelays(request) + assert delays == [0, 1000, 0, 0, 1000] + assert sum(delays) < timing["replyTimeoutMs"] + # The fallback sleeps before all five messages on older hosts. + assert 5 * timing["generalMessageDelay"] < timing["replyTimeoutMs"] + assert tg33.messageSendDelays(tg33.createEditBufferRequest(3)) == [0] + assert tg33.messageSendDelays(tg33.convertToEditBuffer(3, VOICE)) == [0] + assert tg33.messageSendDelays([]) is None + + +def test_internal_bank_is_read_only_but_voices_can_be_sent_to_the_edit_buffer(): + assert tg33.bankDescriptors() == [{"bank": 0, "name": "Internal", "size": 64, "isROM": True}] + sent = tg33.convertToEditBuffer(3, VOICE) + assert tg33.channelIfValidDeviceResponse(sent) == 3 + assert tg33.nameFromDump(sent) == tg33.nameFromDump(VOICE)