gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

midiOutput.cpp (1590B)


      1 #include "midiOutput.h"
      2 
      3 #include "midiInput.h"
      4 #include "portmidi/pm_common/portmidi.h"
      5 #include "synthLib/midiTypes.h"
      6 #include "dsp56kEmu/logging.h"
      7 
      8 extern PmTimestamp returnTimeProc(void*);
      9 
     10 namespace mqConsoleLib
     11 {
     12 MidiOutput::MidiOutput(const std::string& _deviceName) : MidiDevice(_deviceName, true)
     13 {
     14 	Device::openDevice();
     15 }
     16 
     17 int MidiOutput::getDefaultDeviceId() const
     18 {
     19 	return Pm_GetDefaultOutputDeviceID();
     20 }
     21 
     22 bool MidiOutput::openDevice(int devId)
     23 {
     24 	const auto err = Pm_OpenOutput(&m_stream, devId, nullptr, 1024, returnTimeProc, nullptr, 0);
     25 	if(err != pmNoError)
     26 	{
     27 		LOG("Failed to open Midi output " << devId);
     28 		m_stream = nullptr;
     29 	}
     30 	return err == pmNoError;
     31 }
     32 
     33 MidiOutput::~MidiOutput()
     34 {
     35 	Pm_Close(m_stream);
     36 	m_stream = nullptr;
     37 	m_deviceId = -1;
     38 }
     39 
     40 void MidiOutput::write(const std::vector<uint8_t>& _data)
     41 {
     42 	m_parser.write(_data);
     43 	std::vector<synthLib::SMidiEvent> events;
     44 	m_parser.getEvents(events);
     45 	write(events);
     46 }
     47 
     48 void MidiOutput::write(const std::vector<synthLib::SMidiEvent>& _events) const
     49 {
     50 	if(!m_stream)
     51 		return;
     52 
     53 	for (const auto& e : _events)
     54 	{
     55 		if(!e.sysex.empty())
     56 		{
     57 			LOG("MIDI Out Write Sysex of length " << e.sysex.size());
     58 			const auto err = Pm_WriteSysEx(m_stream, 0, const_cast<unsigned char*>(&e.sysex.front()));
     59 			if(err != pmNoError)
     60 			{
     61 				LOG("Failed to send sysex, err " << err << " => " << Pm_GetErrorText(err));
     62 			}
     63 		}
     64 		else
     65 		{
     66 			PmEvent ev;
     67 			ev.message = Pm_Message(e.a, e.b, e.c);
     68 			LOG("MIDI Out Write " << HEXN(e.a, 2) << " " << HEXN(e.b, 2) << ' ' << HEXN(e.c, 2));
     69 			Pm_Write(m_stream, &ev, 1);
     70 		}
     71 	}
     72 }
     73 }