Try to send bank dump (does not work yet)

pull/905/head
probonopd 1 week ago
parent 32b3b57ca1
commit 507a336b02
  1. 48
      src/midichunker.cpp
  2. 37
      src/midichunker.h
  3. 27
      src/mididevice.cpp
  4. 1
      src/mididevice.h
  5. 116
      src/midikeyboard.cpp
  6. 14
      src/midikeyboard.h
  7. 43
      src/minidexed.cpp
  8. 1
      src/minidexed.h
  9. 3
      syslogserver.py

@ -0,0 +1,48 @@
//
// midichunker.cpp
//
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
// Copyright (C) 2022-25 The MiniDexed Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "midichunker.h"
#include <algorithm>
MIDISysExChunker::MIDISysExChunker(const uint8_t* data, size_t length, size_t chunkSize)
: m_data(data), m_length(length), m_chunkSize(chunkSize), m_offset(0) {}
bool MIDISysExChunker::hasNext() const {
return m_offset < m_length;
}
std::vector<uint8_t> MIDISysExChunker::next() {
if (!hasNext()) return {};
size_t remaining = m_length - m_offset;
size_t chunkLen = std::min(m_chunkSize, remaining);
// Only the last chunk should contain the final 0xF7
if (m_offset + chunkLen >= m_length && m_data[m_length-1] == 0xF7) {
chunkLen = m_length - m_offset;
} else if (m_offset + chunkLen > 0 && m_data[m_offset + chunkLen - 1] == 0xF7) {
chunkLen--;
}
std::vector<uint8_t> chunk(m_data + m_offset, m_data + m_offset + chunkLen);
m_offset += chunkLen;
return chunk;
}
void MIDISysExChunker::reset() {
m_offset = 0;
}

@ -0,0 +1,37 @@
//
// midichunker.h
//
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
// Copyright (C) 2022-25 The MiniDexed Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#pragma once
#include <cstddef>
#include <vector>
#include <cstdint>
class MIDISysExChunker {
public:
MIDISysExChunker(const uint8_t* data, size_t length, size_t chunkSize = 256);
bool hasNext() const;
std::vector<uint8_t> next();
void reset();
private:
const uint8_t* m_data;
size_t m_length;
size_t m_chunkSize;
size_t m_offset;
};

@ -30,7 +30,7 @@
#include "midi.h" #include "midi.h"
#include "userinterface.h" #include "userinterface.h"
LOGMODULE ("mididevice"); LOGMODULE("midikeyboard");
// MIDI "System" level (i.e. all TG) custom CC maps // MIDI "System" level (i.e. all TG) custom CC maps
// Note: Even if number of TGs is not 8, there are only 8 // Note: Even if number of TGs is not 8, there are only 8
@ -249,8 +249,7 @@ void CMIDIDevice::MIDIMessageHandler (const u8 *pMessage, size_t nLength, unsign
m_pSynthesizer->setMasterVolume(fMasterVolume); m_pSynthesizer->setMasterVolume(fMasterVolume);
} }
else else
{ { // Perform any MiniDexed level MIDI handling before specific Tone Generators
// Perform any MiniDexed level MIDI handling before specific Tone Generators
unsigned nPerfCh = m_pSynthesizer->GetPerformanceSelectChannel(); unsigned nPerfCh = m_pSynthesizer->GetPerformanceSelectChannel();
switch (ucType) switch (ucType)
{ {
@ -621,7 +620,7 @@ void CMIDIDevice::HandleSystemExclusive(const uint8_t* pMessage, const size_t nL
else if (nLength == 5 && pMessage[3] == 0x09) else if (nLength == 5 && pMessage[3] == 0x09)
{ {
LOGDBG("SysEx bank dump request: device %d", nTG); LOGDBG("SysEx bank dump request: device %d", nTG);
LOGDBG("Still to be implemented"); SendSystemExclusiveBank(nTG, m_DeviceName, nCable, nTG);
return; return;
} }
@ -747,12 +746,6 @@ void CMIDIDevice::HandleSystemExclusive(const uint8_t* pMessage, const size_t nL
break; break;
} }
} }
else if(sysex_return >= 500 && sysex_return < 600)
{
LOGDBG("SysEx send voice %u request",sysex_return-500);
SendSystemExclusiveVoice(sysex_return-500, m_DeviceName, nCable, nTG);
}
break;
} }
} }
@ -769,3 +762,17 @@ void CMIDIDevice::SendSystemExclusiveVoice(uint8_t nVoice, const std::string& de
LOGWARN("No device found in s_DeviceMap for name: %s", deviceName.c_str()); LOGWARN("No device found in s_DeviceMap for name: %s", deviceName.c_str());
} }
} }
void CMIDIDevice::SendSystemExclusiveBank(uint8_t nVoice, const std::string& deviceName, unsigned nCable, uint8_t nTG)
{
// Example: F0 43 20 09 F7
static uint8_t voicedump[4104]; // Use static buffer, correct size for DX7 bank dump
m_pSynthesizer->getSysExBankDump(voicedump, nTG);
TDeviceMap::const_iterator Iterator = s_DeviceMap.find(deviceName);
if (Iterator != s_DeviceMap.end()) {
Iterator->second->Send(voicedump, 4104, nCable);
LOGDBG("Send SYSEX bank dump %u to \"%s\"", nVoice, deviceName.c_str());
} else {
LOGWARN("No device found in s_DeviceMap for name: %s", deviceName.c_str());
}
}

@ -56,6 +56,7 @@ public:
virtual void Send (const u8 *pMessage, size_t nLength, unsigned nCable = 0) {} virtual void Send (const u8 *pMessage, size_t nLength, unsigned nCable = 0) {}
// Change signature to specify device name // Change signature to specify device name
void SendSystemExclusiveVoice(uint8_t nVoice, const std::string& deviceName, unsigned nCable, uint8_t nTG); void SendSystemExclusiveVoice(uint8_t nVoice, const std::string& deviceName, unsigned nCable, uint8_t nTG);
void SendSystemExclusiveBank(uint8_t nVoice, const std::string& deviceName, unsigned nCable, uint8_t nTG);
const std::string& GetDeviceName() const { return m_DeviceName; } const std::string& GetDeviceName() const { return m_DeviceName; }
protected: protected:

@ -21,15 +21,24 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
#include "midikeyboard.h" #include "midikeyboard.h"
#include "midichunker.h"
#include <circle/devicenameservice.h> #include <circle/devicenameservice.h>
#include <circle/sched/scheduler.h>
#include <circle/timer.h>
#include <circle/logger.h>
#include <cstring> #include <cstring>
#include <assert.h> #include <assert.h>
#include <unistd.h>
#include <vector>
LOGMODULE("midikeyboard");
CMIDIKeyboard::CMIDIKeyboard (CMiniDexed *pSynthesizer, CConfig *pConfig, CUserInterface *pUI, unsigned nInstance) CMIDIKeyboard::CMIDIKeyboard (CMiniDexed *pSynthesizer, CConfig *pConfig, CUserInterface *pUI, unsigned nInstance)
: CMIDIDevice (pSynthesizer, pConfig, pUI), : CMIDIDevice (pSynthesizer, pConfig, pUI),
m_nSysExIdx (0), m_nSysExIdx (0),
m_nInstance (nInstance), m_nInstance (nInstance),
m_pMIDIDevice (0) m_pMIDIDevice (0),
m_HasQueuedSysEx(false)
{ {
m_DeviceName.Format ("umidi%u", nInstance+1); m_DeviceName.Format ("umidi%u", nInstance+1);
@ -42,6 +51,33 @@ CMIDIKeyboard::~CMIDIKeyboard (void)
void CMIDIKeyboard::Process (boolean bPlugAndPlayUpdated) void CMIDIKeyboard::Process (boolean bPlugAndPlayUpdated)
{ {
// Send any queued SysEx response in a safe context
if (m_HasQueuedSysEx && m_pMIDIDevice) {
// Pad to multiple of 4 bytes for USB MIDI event packets
size_t sysexLen = m_QueuedSysEx.size();
size_t paddedLen = (sysexLen + 3) & ~3; // round up to next multiple of 4
if (paddedLen > sysexLen) {
m_QueuedSysEx.resize(paddedLen, 0x00);
}
// Send in safe chunks to avoid USB lockup
static constexpr size_t kUSBMIDIMaxChunk = 256; // or 512 if your stack allows
size_t offset = 0;
// Only send one chunk per Process() call to avoid blocking or watchdog reset
if (offset < m_QueuedSysEx.size()) {
size_t chunk = std::min(kUSBMIDIMaxChunk, m_QueuedSysEx.size() - offset);
m_pMIDIDevice->SendEventPackets(m_QueuedSysEx.data() + offset, chunk);
offset += chunk;
// Save progress for next Process() call
if (offset < m_QueuedSysEx.size()) {
// Not done yet, keep queued SysEx and return
m_QueuedSysEx.erase(m_QueuedSysEx.begin(), m_QueuedSysEx.begin() + chunk);
return;
}
}
m_QueuedSysEx.clear();
m_HasQueuedSysEx = false;
}
while (!m_SendQueue.empty ()) while (!m_SendQueue.empty ())
{ {
TSendQueueEntry Entry = m_SendQueue.front (); TSendQueueEntry Entry = m_SendQueue.front ();
@ -73,16 +109,80 @@ void CMIDIKeyboard::Process (boolean bPlugAndPlayUpdated)
} }
} }
void CMIDIKeyboard::Send (const u8 *pMessage, size_t nLength, unsigned nCable) // Helper: Convert SysEx to USB MIDI event packets
std::vector<std::vector<uint8_t>> SysExToUSBMIDIPackets(const uint8_t* data, size_t length, unsigned cable)
{ {
TSendQueueEntry Entry; LOGNOTE("SysExToUSBMIDIPackets: length=%u, cable=%u", (unsigned)length, cable);
Entry.pMessage = new u8[nLength]; std::vector<std::vector<uint8_t>> packets;
Entry.nLength = nLength; size_t idx = 0;
Entry.nCable = nCable; while (idx < length) {
size_t remaining = length - idx;
uint8_t cin;
uint8_t packet[4] = {0};
packet[0] = (uint8_t)(cable << 4); // Upper nibble: cable number, lower: CIN
if (remaining >= 3) {
if (idx == 0) {
cin = 0x4; // SysEx Start or continue
} else {
cin = 0x4; // SysEx continue
}
packet[0] |= cin;
packet[1] = data[idx];
packet[2] = data[idx+1];
packet[3] = data[idx+2];
LOGNOTE(" Packet: [%02X %02X %02X %02X] (idx=%u)", packet[0], packet[1], packet[2], packet[3], (unsigned)idx);
idx += 3;
} else if (remaining == 2) {
cin = 0x6; // SysEx ends with 2 bytes
packet[0] |= cin;
packet[1] = data[idx];
packet[2] = data[idx+1];
packet[3] = 0;
LOGNOTE(" Packet: [%02X %02X %02X %02X] (last 2 bytes)", packet[0], packet[1], packet[2], packet[3]);
idx += 2;
} else if (remaining == 1) {
cin = 0x5; // SysEx ends with 1 byte
packet[0] |= cin;
packet[1] = data[idx];
packet[2] = 0;
packet[3] = 0;
LOGNOTE(" Packet: [%02X %02X %02X %02X] (last 1 byte)", packet[0], packet[1], packet[2], packet[3]);
idx += 1;
}
packets.push_back({packet[0], packet[1], packet[2], packet[3]});
}
LOGNOTE("SysExToUSBMIDIPackets: total packets=%u", (unsigned)packets.size());
return packets;
}
memcpy (Entry.pMessage, pMessage, nLength); void CMIDIKeyboard::Send(const u8 *pMessage, size_t nLength, unsigned nCable)
{
// NOTE: For USB MIDI, we do NOT use MIDISysExChunker for SysEx sending.
// The chunker splits SysEx into arbitrary chunks for traditional MIDI (e.g., serial/DIN),
// but USB MIDI requires SysEx to be split into 4-byte USB MIDI event packets with specific CIN headers.
// Therefore, for USB MIDI, we packetize SysEx according to the USB MIDI spec and send with SendEventPackets().
// See: https://www.usb.org/sites/default/files/midi10.pdf (USB MIDI 1.0 spec)
// This is why the chunker is bypassed for USB MIDI SysEx sending.
m_SendQueue.push (Entry); // Check for valid SysEx
if (nLength >= 2 && pMessage[0] == 0xF0 && pMessage[nLength-1] == 0xF7 && m_pMIDIDevice) {
// Convert to USB MIDI event packets and send directly
auto packets = SysExToUSBMIDIPackets(pMessage, nLength, nCable);
std::vector<uint8_t> flat;
for (const auto& pkt : packets) {
flat.insert(flat.end(), pkt.begin(), pkt.end());
}
m_QueuedSysEx = flat;
m_HasQueuedSysEx = true;
return;
}
// Not a SysEx, send as-is
TSendQueueEntry Entry;
Entry.pMessage = new u8[nLength];
Entry.nLength = nLength;
Entry.nCable = nCable;
memcpy(Entry.pMessage, pMessage, nLength);
m_SendQueue.push(Entry);
} }
// Most packets will be passed straight onto the main MIDI message handler // Most packets will be passed straight onto the main MIDI message handler

@ -30,6 +30,7 @@
#include <circle/string.h> #include <circle/string.h>
#include <circle/types.h> #include <circle/types.h>
#include <queue> #include <queue>
#include <vector>
#define USB_SYSEX_BUFFER_SIZE (MAX_DX7_SYSEX_LENGTH+128) // Allow a bit spare to handle unexpected SysEx messages #define USB_SYSEX_BUFFER_SIZE (MAX_DX7_SYSEX_LENGTH+128) // Allow a bit spare to handle unexpected SysEx messages
@ -45,6 +46,14 @@ public:
void Send (const u8 *pMessage, size_t nLength, unsigned nCable = 0) override; void Send (const u8 *pMessage, size_t nLength, unsigned nCable = 0) override;
void QueueSysExResponse(const uint8_t* data, size_t len) {
m_QueuedSysEx.assign(data, data + len);
m_HasQueuedSysEx = true;
}
bool HasQueuedSysExResponse() const { return m_HasQueuedSysEx; }
void ClearQueuedSysExResponse() { m_QueuedSysEx.clear(); m_HasQueuedSysEx = false; }
const std::vector<uint8_t>& GetQueuedSysExResponse() const { return m_QueuedSysEx; }
private: private:
static void MIDIPacketHandler (unsigned nCable, u8 *pPacket, unsigned nLength, unsigned nDevice, void *pParam); static void MIDIPacketHandler (unsigned nCable, u8 *pPacket, unsigned nLength, unsigned nDevice, void *pParam);
static void DeviceRemovedHandler (CDevice *pDevice, void *pContext); static void DeviceRemovedHandler (CDevice *pDevice, void *pContext);
@ -68,6 +77,11 @@ private:
CUSBMIDIDevice * volatile m_pMIDIDevice; CUSBMIDIDevice * volatile m_pMIDIDevice;
std::queue<TSendQueueEntry> m_SendQueue; std::queue<TSendQueueEntry> m_SendQueue;
std::vector<uint8_t> m_QueuedSysEx;
bool m_HasQueuedSysEx = false;
}; };
std::vector<std::vector<uint8_t>> SysExToUSBMIDIPackets(const uint8_t* data, size_t length, unsigned cable);
#endif #endif

@ -1868,6 +1868,49 @@ void CMiniDexed::getSysExVoiceDump(uint8_t* dest, uint8_t nTG)
dest[162] = 0xF7; // SysEx end dest[162] = 0xF7; // SysEx end
} }
void CMiniDexed::getSysExBankDump(uint8_t* dest, uint8_t nTG)
{
// DX7 Bulk Dump: 32 voices
// Header: F0 43 00 09 20 00
// Data: 4096 bytes (32 voices x 128 bytes packed)
// Checksum: 1 byte (2's complement of sum of 4096 data bytes, masked to 7 bits)
// Footer: F7
// Total: 4104 bytes
constexpr size_t kVoices = 32;
constexpr size_t kPackedVoiceSize = 128;
constexpr size_t kBulkDataSize = kVoices * kPackedVoiceSize; // 4096
constexpr size_t kHeaderSize = 6;
constexpr size_t kTotalSize = kHeaderSize + kBulkDataSize + 2; // +checksum +F7 = 4104
// Header (Yamaha DX7 standard)
dest[0] = 0xF0; // SysEx start
dest[1] = 0x43; // Yamaha ID
dest[2] = 0x00; // Sub-status (0), device/channel (0)
dest[3] = 0x09; // Format number (9 = 32 voices)
dest[4] = 0x20; // Byte count MSB (4096 = 0x1000, MSB=0x20)
dest[5] = 0x00; // Byte count LSB
// Fill packed voice data
uint8_t* pData = dest + kHeaderSize;
uint8_t checksum = 0;
for (size_t v = 0; v < kVoices; ++v) {
uint8_t packedVoice[kPackedVoiceSize];
m_SysExFileLoader.GetVoice(m_nVoiceBankID[nTG], v, packedVoice);
for (size_t b = 0; b < kPackedVoiceSize; ++b) {
pData[v * kPackedVoiceSize + b] = packedVoice[b];
checksum += packedVoice[b];
}
}
// Checksum: 2's complement, masked to 7 bits
checksum = (~checksum + 1) & 0x7F;
dest[kHeaderSize + kBulkDataSize] = checksum;
// Footer
dest[kHeaderSize + kBulkDataSize + 1] = 0xF7;
}
void CMiniDexed::setOPMask(uint8_t uchOPMask, uint8_t nTG) void CMiniDexed::setOPMask(uint8_t uchOPMask, uint8_t nTG)
{ {
m_uchOPMask[nTG] = uchOPMask; m_uchOPMask[nTG] = uchOPMask;

@ -124,6 +124,7 @@ public:
void loadVoiceParameters(const uint8_t* data, uint8_t nTG); void loadVoiceParameters(const uint8_t* data, uint8_t nTG);
void setVoiceDataElement(uint8_t data, uint8_t number, uint8_t nTG); void setVoiceDataElement(uint8_t data, uint8_t number, uint8_t nTG);
void getSysExVoiceDump(uint8_t* dest, uint8_t nTG); void getSysExVoiceDump(uint8_t* dest, uint8_t nTG);
void getSysExBankDump(uint8_t* dest, uint8_t nTG);
void setOPMask(uint8_t uchOPMask, uint8_t nTG); void setOPMask(uint8_t uchOPMask, uint8_t nTG);
void setModController (unsigned controller, unsigned parameter, uint8_t value, uint8_t nTG); void setModController (unsigned controller, unsigned parameter, uint8_t value, uint8_t nTG);

@ -34,6 +34,9 @@ class SyslogServer:
def handle_message(self, data): def handle_message(self, data):
message = data[2:].decode('utf-8').strip() message = data[2:].decode('utf-8').strip()
if "Time exceeded (0)" in message:
return
if self.start_time is None: if self.start_time is None:
self.start_time = time.time() self.start_time = time.time()
relative_time = "0:00:00.000" relative_time = "0:00:00.000"

Loading…
Cancel
Save