Repository navigation
[Yamaha TG33] New adaptation for TG33 voices #591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d1aeba6
f5b1125
9c774b5
b23284d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <count MSB> <count LSB> "LM 0012VE" <587 bytes> <checksum> 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(): | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| 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 | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| 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(): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we give internal-bank retrieval an explicit selection sequence? With the edit-buffer API used here, the host downloads a bank by sending Program Change followed by the current-voice request. It doesn't select Internal first, so if the TG33 is currently on a preset or card bank, the download can collect voices from that bank while presenting them as the internal bank. Edisyn's changePatch() looks like a useful guide: it sends the VOICE-button SysEx to leave Edit mode, waits, sends CC0=0 and CC32 for the desired bank, then Program Change, with another wait before requesting the voice. It might be worth implementing a numbered request path that includes those steps, with a mock test for the outgoing sequence. Simply adding a custom program-change hook wouldn't cover this host download path. Reference: https://github.com/eclab/edisyn/blob/master/edisyn/synth/yamahatg33/YamahaTG33.java |
||
| 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): | ||
|
christofmuc marked this conversation as resolved.
Resolved
|
||
| 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) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the bank download transaction exclusive on the MIDI output.
The paced loop releases the GIL, so another GIL-gated
sendBlockOfMessagesToSynthfor the same device can run between messages. For example, a user patch selection can send a program change or bank select during the bank download. The download sequence then runs against changed device state, and a stored slot can receive a voice from a different selection.The repository has no lock on this path. Serialize the bank download against other sends to the same MIDI output. Do not hold the GIL across the pauses, because that blocks all Python hooks for the full download. The exclusion must cover the whole transaction, not each message separately.