8 #if defined _WIN32 || defined WIN32
10 #else // only if not Windows
13 #include "MidiTimer.h"
15 using namespace jdkmidi;
19 MIDIDriverAlsa::MIDIDriverAlsa (
int queue_size )
21 MIDIDriver ( queue_size ),
28 MIDIDriverAlsa::~MIDIDriverAlsa()
35 bool MIDIDriverAlsa::OpenMIDIInPort (
int id )
46 m_pMidiIn =
new RtMidiIn(
"MIDIDriverAlsa Client");
48 catch ( RtError &error )
58 m_pMidiIn->openPort(
id);
70 bool MIDIDriverAlsa::OpenMIDIOutPort (
int id )
72 if(m_pMidiOut != NULL)
77 if(m_pMidiOut == NULL)
81 m_pMidiOut =
new RtMidiOut(
"MIDIDriverAlsa Client");
89 if(m_pMidiOut != NULL)
93 m_pMidiOut->openPort(
id);
104 bool MIDIDriverAlsa::HardwareMsgOut (
const jdkmidi::MIDITimedBigMessage &msg )
106 if(m_pMidiOut != NULL)
108 unsigned char status = msg.GetStatus();
112 if ( status <0xff && status !=0xf0 )
114 unsigned char msgBytes[] = {status, msg.GetByte1(), msg.GetByte2(), msg.GetByte3()};
116 std::vector<unsigned char> vec(msgBytes, msgBytes+3);
118 m_pMidiOut->sendMessage(&vec);
128 bool AlsaDriverThreadProc(MIDIDriverAlsa* pAlsaDriver,
int nTimerResMS)
130 MidiTimer::TimePoint tBefore, tAfter;
131 unsigned long nElapsed, nTimeToSleep;
135 tBefore = MidiTimer::Now();
137 if(pAlsaDriver->TimeTick(tBefore) ==
false)
break;
139 tAfter = MidiTimer::Now();
141 nElapsed = std::chrono::duration_cast<MidiTimer::Duration>(tAfter - tBefore).count();
143 nTimeToSleep = (nElapsed > nTimerResMS ? 0 : nTimerResMS - nElapsed);
145 MidiTimer::Sleep(nTimeToSleep);
151 bool MIDIDriverAlsa::StartTimer (
int res )
153 if(m_bgTaskResult.valid())
156 m_bgTaskResult = std::async(std::launch::async, &AlsaDriverThreadProc,
this, res);
158 return m_bgTaskResult.valid();
161 void MIDIDriverAlsa::WaitTillDone()
163 if(m_bgTaskResult.valid() ==
false)
return;
165 auto waitStatus = m_bgTaskResult.wait_for(std::chrono::milliseconds(0));
167 while(waitStatus != std::future_status::ready)
169 waitStatus = m_bgTaskResult.wait_for(std::chrono::milliseconds(500));
173 void MIDIDriverAlsa::StopTimer()
178 if(m_bgTaskResult.valid())
179 m_bgTaskResult.get();
182 void MIDIDriverAlsa::CloseMIDIInPort()
184 if(m_pMidiIn != NULL)
186 m_pMidiIn->closePort();
192 void MIDIDriverAlsa::CloseMIDIOutPort()
194 if(m_pMidiOut != NULL)
196 m_pMidiOut->closePort();
206 #endif // _ifndef _Win32
Declares MIDIDriverAlsa class for CFugue.