Skip to content

Commit

Permalink
mt32 sysex lcd message
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffaello committed Oct 16, 2023
1 parent b4321de commit 2f7d639
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 65 deletions.
3 changes: 3 additions & 0 deletions sdl2-hyper-sonic-drivers/examples/mt32-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#include <fmt/color.h>
#include <map>

#include <HyperSonicDrivers/utils/ILogger.hpp>

using namespace HyperSonicDrivers;

int main(int argc, char* argv[])
{
using audio::mixer::eChannelGroup;

utils::ILogger::instance->setLevelAll(utils::ILogger::eLevel::Trace);
spdlog::warn("MT32 requires 2 ROMs files not included in this example");
spdlog::warn("the ROM filenames assumed to be fund in the current working directory are:");
spdlog::warn("MT32_CONTROL.ROM --- MT32_PCM.ROM");
Expand Down
70 changes: 5 additions & 65 deletions sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/devices/MT32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,6 @@

namespace HyperSonicDrivers::devices
{
// test
//static audio::midi::MIDIEvent MT32SysEx(uint32_t addr, const uint8_t* data, uint32_t dataSize)
//{
// //typedef uint8_t byte;
// //uint32_t dataSize = 0;
// //uint8_t* data = nullptr;
// //uint32_t addr = 0x1FC000;
// static const uint8_t header[] = { 0x41, 0x10, 0x16, 0x12 };

// uint8_t* msg = new uint8_t[sizeof(header) + 4 + dataSize];
// memcpy(msg, header, sizeof(header));
// uint8_t* dst = msg + sizeof(header);
// const uint8_t* src = dst;

// *dst++ = (addr >> 14) & 0x7F;
// *dst++ = (addr >> 7) & 0x7F;
// *dst++ = addr & 0x7F;

// while (dataSize) {
// *dst++ = *data++;
// --dataSize;
// }

// uint8_t checkSum = 0;
// while (src < dst)
// checkSum -= *src++;

// *dst++ = checkSum & 0x7F;

// dataSize = dst - msg;
// audio::midi::MIDIEvent e;
// for (int i = 0; i < sizeof(header) + 4 + dataSize; i++)
// e.data.push_back(msg[i]);

// delete[] msg;

// return e;
//}


MT32::MT32(
const std::shared_ptr<audio::IMixer>& mixer,
const std::filesystem::path& control_rom_file,
Expand All @@ -52,31 +12,6 @@ namespace HyperSonicDrivers::devices
{
m_mt32 = std::make_shared<hardware::mt32::MT32>(control_rom_file, pcm_rom_file, mixer);
m_hardware = m_mt32.get();

//m_mt32->start(nullptr);

// test

// reset MT32
//m_mt32->m_service.playSysex(MT32SysEx(0x1FC000, 0, 0));
// utils::delayMillis(250);

// Setup master tune, reverb mode, reverb time, reverb level, channel mapping, partial reserve and master volume
//static const uint8_t initSysex1[] = "\x40\x00\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x64";
//sendSysEx(MT32SysEx(0x40000, initSysex1, sizeof(initSysex1) - 1));
//utils::delayMillis(40);
// Map percussion to notes 24 - 34 without reverb. It still happens in the DOTT driver, but not in the SAMNMAX one.
//static const uint8_t initSysex2[] = "\x40\x64\x07\x00\x4a\x64\x06\x00\x41\x64\x07\x00\x4b\x64\x08\x00\x45\x64\x06\x00\x44\x64"
// "\x0b\x00\x51\x64\x05\x00\x43\x64\x08\x00\x50\x64\x07\x00\x42\x64\x03\x00\x4c\x64\x07\x00";
//sendSysEx(MT32SysEx(0xC090, initSysex2, sizeof(initSysex2) - 1));
//utils::delayMillis(40);


//const uint8_t pbRange = 0x10;
//for (int i = 0; i < 128; ++i) {
// //sendSysEx(MT32SysEx(0x014004 + (i << 3), &pbRange, 1));
// utils::delayMillis(5);
//}
}

bool MT32::init() noexcept
Expand All @@ -96,4 +31,9 @@ namespace HyperSonicDrivers::devices
m_mt32->stop();
return false;
}

void MT32::lcd_message(const std::string& msg) noexcept
{
m_mt32->sysEx(hardware::mt32::mt32_LCD_addr, reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <memory>
#include <filesystem>
#include <string>
#include <HyperSonicDrivers/audio/IMixer.hpp>
#include <HyperSonicDrivers/audio/midi/MIDIEvent.hpp>
#include <HyperSonicDrivers/devices/IDevice.hpp>
Expand All @@ -24,6 +25,8 @@ namespace HyperSonicDrivers::devices
bool shutdown() noexcept override;

inline std::shared_ptr<hardware::mt32::MT32> getMt32() const noexcept { return m_mt32; };

void lcd_message(const std::string& msg) noexcept;
private:
std::shared_ptr<hardware::mt32::MT32> m_mt32;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <format>
#include <HyperSonicDrivers/audio/midi/types.hpp>
#include <HyperSonicDrivers/audio/streams/EmulatedStream.hpp>
#include <HyperSonicDrivers/files/File.hpp>
#include <HyperSonicDrivers/hardware/mt32/MT32.hpp>
Expand Down Expand Up @@ -85,6 +86,38 @@ namespace HyperSonicDrivers::hardware::mt32
IHardware::start(callback, group, volume, pan, timerFrequency);
}

void MT32::sysEx(const uint32_t addr, const uint8_t* data, uint32_t dataSize)
{
static const uint8_t header[] = {
static_cast<uint8_t>(audio::midi::MIDI_META_EVENT_VAL::SYS_EX0),
0x41, 0x10, 0x16, 0x12
};

uint8_t* msg = new uint8_t[sizeof(header) + 4 + dataSize + 1];
memcpy(msg, header, sizeof(header));
uint8_t* dst = msg + sizeof(header);
const uint8_t* src = dst;

*dst++ = (addr >> 14) & 0x7F;
*dst++ = (addr >> 7) & 0x7F;
*dst++ = addr & 0x7F;

while (dataSize) {
*dst++ = *data++;
--dataSize;
}

uint8_t checkSum = 0;
while (src < dst)
checkSum -= *src++;

*dst++ = checkSum & 0x7F;
*dst++ = static_cast<uint8_t>(audio::midi::MIDI_META_EVENT_VAL::SYS_EX7);

dataSize = dst - msg;
m_service.playSysex(msg, dataSize);
}

void MT32::startCallbacks(
const audio::mixer::eChannelGroup group,
const uint8_t volume,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace HyperSonicDrivers::hardware::mt32
constexpr int mt32_frequency_internal = 32000; // 32 KHz
constexpr int mt32_frequency = 250;

constexpr uint32_t mt32_LCD_addr = 0x80000;

class MT32 : public IHardware
{
public:
Expand All @@ -46,6 +48,9 @@ namespace HyperSonicDrivers::hardware::mt32
const int timerFrequency = mt32_frequency) override;

inline MT32Emu::Service& getService() noexcept { return m_service; };

void sysEx(const uint32_t addr, const uint8_t* data, const uint32_t dataSize);

protected:
void startCallbacks(
const audio::mixer::eChannelGroup group,
Expand Down

0 comments on commit 2f7d639

Please sign in to comment.