| 1 |
/**********************************************************************/ |
| 2 |
/*! \class RtMidi |
| 3 |
\brief An abstract base class for realtime MIDI input/output. |
| 4 |
|
| 5 |
This class implements some common functionality for the realtime |
| 6 |
MIDI input/output subclasses RtMidiIn and RtMidiOut. |
| 7 |
|
| 8 |
RtMidi GitHub site: https://github.com/thestk/rtmidi |
| 9 |
RtMidi WWW site: http://www.music.mcgill.ca/~gary/rtmidi/ |
| 10 |
|
| 11 |
RtMidi: realtime MIDI i/o C++ classes |
| 12 |
Copyright (c) 2003-2021 Gary P. Scavone |
| 13 |
|
| 14 |
Permission is hereby granted, free of charge, to any person |
| 15 |
obtaining a copy of this software and associated documentation files |
| 16 |
(the "Software"), to deal in the Software without restriction, |
| 17 |
including without limitation the rights to use, copy, modify, merge, |
| 18 |
publish, distribute, sublicense, and/or sell copies of the Software, |
| 19 |
and to permit persons to whom the Software is furnished to do so, |
| 20 |
subject to the following conditions: |
| 21 |
|
| 22 |
The above copyright notice and this permission notice shall be |
| 23 |
included in all copies or substantial portions of the Software. |
| 24 |
|
| 25 |
Any person wishing to distribute modifications to the Software is |
| 26 |
asked to send the modifications to the original developer so that |
| 27 |
they can be incorporated into the canonical version. This is, |
| 28 |
however, not a binding provision of this license. |
| 29 |
|
| 30 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 31 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 32 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 33 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 34 |
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 |
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 36 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 |
*/ |
| 38 |
/**********************************************************************/ |
| 39 |
|
| 40 |
#include "RtMidi.h" |
| 41 |
#include <sstream> |
| 42 |
|
| 43 |
#ifdef TARGET_OS_IPHONE |
| 44 |
#if (TARGET_OS_IPHONE == 1) |
| 45 |
|
| 46 |
#define AudioGetCurrentHostTime CAHostTimeBase::GetCurrentTime |
| 47 |
#define AudioConvertHostTimeToNanos CAHostTimeBase::ConvertToNanos |
| 48 |
|
| 49 |
#include <mach/mach_time.h> |
| 50 |
class CTime2nsFactor |
| 51 |
{ |
| 52 |
public: |
| 53 |
CTime2nsFactor() |
| 54 |
{ |
| 55 |
mach_timebase_info_data_t tinfo; |
| 56 |
mach_timebase_info(&tinfo); |
| 57 |
Factor = (double)tinfo.numer / tinfo.denom; |
| 58 |
} |
| 59 |
static double Factor; |
| 60 |
}; |
| 61 |
double CTime2nsFactor::Factor; |
| 62 |
static CTime2nsFactor InitTime2nsFactor; |
| 63 |
#undef AudioGetCurrentHostTime |
| 64 |
#undef AudioConvertHostTimeToNanos |
| 65 |
#define AudioGetCurrentHostTime (uint64_t) mach_absolute_time |
| 66 |
#define AudioConvertHostTimeToNanos(t) t *CTime2nsFactor::Factor |
| 67 |
#define EndianS32_BtoN(n) n |
| 68 |
|
| 69 |
#endif |
| 70 |
#endif |
| 71 |
|
| 72 |
// Default for Windows is to add an identifier to the port names; this |
| 73 |
// flag can be defined (e.g. in your project file) to disable this behaviour. |
| 74 |
//#define RTMIDI_DO_NOT_ENSURE_UNIQUE_PORTNAMES |
| 75 |
|
| 76 |
// **************************************************************** // |
| 77 |
// |
| 78 |
// MidiInApi and MidiOutApi subclass prototypes. |
| 79 |
// |
| 80 |
// **************************************************************** // |
| 81 |
|
| 82 |
#if !defined(__LINUX_ALSA__) && !defined(__UNIX_JACK__) && !defined(__MACOSX_CORE__) && !defined(__WINDOWS_MM__) && !defined(TARGET_IPHONE_OS) && !defined(__WEB_MIDI_API__) |
| 83 |
#define __RTMIDI_DUMMY__ |
| 84 |
#endif |
| 85 |
|
| 86 |
#if defined(__MACOSX_CORE__) |
| 87 |
#include <CoreMIDI/CoreMIDI.h> |
| 88 |
|
| 89 |
class MidiInCore: public MidiInApi |
| 90 |
{ |
| 91 |
public: |
| 92 |
MidiInCore( const std::string &clientName, unsigned int queueSizeLimit ); |
| 93 |
~MidiInCore( void ); |
| 94 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::MACOSX_CORE; }; |
| 95 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 96 |
void openVirtualPort( const std::string &portName ); |
| 97 |
void closePort( void ); |
| 98 |
void setClientName( const std::string &clientName ); |
| 99 |
void setPortName( const std::string &portName ); |
| 100 |
unsigned int getPortCount( void ); |
| 101 |
std::string getPortName( unsigned int portNumber ); |
| 102 |
|
| 103 |
protected: |
| 104 |
MIDIClientRef getCoreMidiClientSingleton(const std::string& clientName) throw(); |
| 105 |
void initialize( const std::string& clientName ); |
| 106 |
}; |
| 107 |
|
| 108 |
class MidiOutCore: public MidiOutApi |
| 109 |
{ |
| 110 |
public: |
| 111 |
MidiOutCore( const std::string &clientName ); |
| 112 |
~MidiOutCore( void ); |
| 113 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::MACOSX_CORE; }; |
| 114 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 115 |
void openVirtualPort( const std::string &portName ); |
| 116 |
void closePort( void ); |
| 117 |
void setClientName( const std::string &clientName ); |
| 118 |
void setPortName( const std::string &portName ); |
| 119 |
unsigned int getPortCount( void ); |
| 120 |
std::string getPortName( unsigned int portNumber ); |
| 121 |
void sendMessage( const unsigned char *message, size_t size ); |
| 122 |
|
| 123 |
protected: |
| 124 |
MIDIClientRef getCoreMidiClientSingleton(const std::string& clientName) throw(); |
| 125 |
void initialize( const std::string& clientName ); |
| 126 |
}; |
| 127 |
|
| 128 |
#endif |
| 129 |
|
| 130 |
#if defined(__UNIX_JACK__) |
| 131 |
|
| 132 |
class MidiInJack: public MidiInApi |
| 133 |
{ |
| 134 |
public: |
| 135 |
MidiInJack( const std::string &clientName, unsigned int queueSizeLimit ); |
| 136 |
~MidiInJack( void ); |
| 137 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::UNIX_JACK; }; |
| 138 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 139 |
void openVirtualPort( const std::string &portName ); |
| 140 |
void closePort( void ); |
| 141 |
void setClientName( const std::string &clientName ); |
| 142 |
void setPortName( const std::string &portName); |
| 143 |
unsigned int getPortCount( void ); |
| 144 |
std::string getPortName( unsigned int portNumber ); |
| 145 |
|
| 146 |
protected: |
| 147 |
std::string clientName; |
| 148 |
|
| 149 |
void connect( void ); |
| 150 |
void initialize( const std::string& clientName ); |
| 151 |
}; |
| 152 |
|
| 153 |
class MidiOutJack: public MidiOutApi |
| 154 |
{ |
| 155 |
public: |
| 156 |
MidiOutJack( const std::string &clientName ); |
| 157 |
~MidiOutJack( void ); |
| 158 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::UNIX_JACK; }; |
| 159 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 160 |
void openVirtualPort( const std::string &portName ); |
| 161 |
void closePort( void ); |
| 162 |
void setClientName( const std::string &clientName ); |
| 163 |
void setPortName( const std::string &portName); |
| 164 |
unsigned int getPortCount( void ); |
| 165 |
std::string getPortName( unsigned int portNumber ); |
| 166 |
void sendMessage( const unsigned char *message, size_t size ); |
| 167 |
|
| 168 |
protected: |
| 169 |
std::string clientName; |
| 170 |
|
| 171 |
void connect( void ); |
| 172 |
void initialize( const std::string& clientName ); |
| 173 |
}; |
| 174 |
|
| 175 |
#endif |
| 176 |
|
| 177 |
#if defined(__LINUX_ALSA__) |
| 178 |
|
| 179 |
class MidiInAlsa: public MidiInApi |
| 180 |
{ |
| 181 |
public: |
| 182 |
MidiInAlsa( const std::string &clientName, unsigned int queueSizeLimit ); |
| 183 |
~MidiInAlsa( void ); |
| 184 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::LINUX_ALSA; }; |
| 185 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 186 |
void openVirtualPort( const std::string &portName ); |
| 187 |
void closePort( void ); |
| 188 |
void setClientName( const std::string &clientName ); |
| 189 |
void setPortName( const std::string &portName); |
| 190 |
unsigned int getPortCount( void ); |
| 191 |
std::string getPortName( unsigned int portNumber ); |
| 192 |
|
| 193 |
protected: |
| 194 |
void initialize( const std::string& clientName ); |
| 195 |
}; |
| 196 |
|
| 197 |
class MidiOutAlsa: public MidiOutApi |
| 198 |
{ |
| 199 |
public: |
| 200 |
MidiOutAlsa( const std::string &clientName ); |
| 201 |
~MidiOutAlsa( void ); |
| 202 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::LINUX_ALSA; }; |
| 203 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 204 |
void openVirtualPort( const std::string &portName ); |
| 205 |
void closePort( void ); |
| 206 |
void setClientName( const std::string &clientName ); |
| 207 |
void setPortName( const std::string &portName ); |
| 208 |
unsigned int getPortCount( void ); |
| 209 |
std::string getPortName( unsigned int portNumber ); |
| 210 |
void sendMessage( const unsigned char *message, size_t size ); |
| 211 |
|
| 212 |
protected: |
| 213 |
void initialize( const std::string& clientName ); |
| 214 |
}; |
| 215 |
|
| 216 |
#endif |
| 217 |
|
| 218 |
#if defined(__WINDOWS_MM__) |
| 219 |
|
| 220 |
class MidiInWinMM: public MidiInApi |
| 221 |
{ |
| 222 |
public: |
| 223 |
MidiInWinMM( const std::string &clientName, unsigned int queueSizeLimit ); |
| 224 |
~MidiInWinMM( void ); |
| 225 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::WINDOWS_MM; }; |
| 226 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 227 |
void openVirtualPort( const std::string &portName ); |
| 228 |
void closePort( void ); |
| 229 |
void setClientName( const std::string &clientName ); |
| 230 |
void setPortName( const std::string &portName ); |
| 231 |
unsigned int getPortCount( void ); |
| 232 |
std::string getPortName( unsigned int portNumber ); |
| 233 |
|
| 234 |
protected: |
| 235 |
void initialize( const std::string& clientName ); |
| 236 |
}; |
| 237 |
|
| 238 |
class MidiOutWinMM: public MidiOutApi |
| 239 |
{ |
| 240 |
public: |
| 241 |
MidiOutWinMM( const std::string &clientName ); |
| 242 |
~MidiOutWinMM( void ); |
| 243 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::WINDOWS_MM; }; |
| 244 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 245 |
void openVirtualPort( const std::string &portName ); |
| 246 |
void closePort( void ); |
| 247 |
void setClientName( const std::string &clientName ); |
| 248 |
void setPortName( const std::string &portName ); |
| 249 |
unsigned int getPortCount( void ); |
| 250 |
std::string getPortName( unsigned int portNumber ); |
| 251 |
void sendMessage( const unsigned char *message, size_t size ); |
| 252 |
|
| 253 |
protected: |
| 254 |
void initialize( const std::string& clientName ); |
| 255 |
}; |
| 256 |
|
| 257 |
#endif |
| 258 |
|
| 259 |
#if defined(__WEB_MIDI_API__) |
| 260 |
|
| 261 |
class MidiInWeb : public MidiInApi |
| 262 |
{ |
| 263 |
std::string client_name{}; |
| 264 |
std::string web_midi_id{}; |
| 265 |
int open_port_number{-1}; |
| 266 |
|
| 267 |
public: |
| 268 |
MidiInWeb(const std::string &/*clientName*/, unsigned int queueSizeLimit ); |
| 269 |
~MidiInWeb( void ); |
| 270 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::WEB_MIDI_API; }; |
| 271 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 272 |
void openVirtualPort( const std::string &portName ); |
| 273 |
void closePort( void ); |
| 274 |
void setClientName( const std::string &clientName ); |
| 275 |
void setPortName( const std::string &portName ); |
| 276 |
unsigned int getPortCount( void ); |
| 277 |
std::string getPortName( unsigned int portNumber ); |
| 278 |
|
| 279 |
void onMidiMessage( uint8_t* data, double domHishResTimeStamp ); |
| 280 |
|
| 281 |
protected: |
| 282 |
void initialize( const std::string& clientName ); |
| 283 |
}; |
| 284 |
|
| 285 |
class MidiOutWeb: public MidiOutApi |
| 286 |
{ |
| 287 |
std::string client_name{}; |
| 288 |
std::string web_midi_id{}; |
| 289 |
int open_port_number{-1}; |
| 290 |
|
| 291 |
public: |
| 292 |
MidiOutWeb( const std::string &clientName ); |
| 293 |
~MidiOutWeb( void ); |
| 294 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::WEB_MIDI_API; }; |
| 295 |
void openPort( unsigned int portNumber, const std::string &portName ); |
| 296 |
void openVirtualPort( const std::string &portName ); |
| 297 |
void closePort( void ); |
| 298 |
void setClientName( const std::string &clientName ); |
| 299 |
void setPortName( const std::string &portName ); |
| 300 |
unsigned int getPortCount( void ); |
| 301 |
std::string getPortName( unsigned int portNumber ); |
| 302 |
void sendMessage( const unsigned char *message, size_t size ); |
| 303 |
|
| 304 |
protected: |
| 305 |
void initialize( const std::string& clientName ); |
| 306 |
}; |
| 307 |
|
| 308 |
#endif |
| 309 |
|
| 310 |
#if defined(__RTMIDI_DUMMY__) |
| 311 |
|
| 312 |
class MidiInDummy: public MidiInApi |
| 313 |
{ |
| 314 |
public: |
| 315 |
MidiInDummy( const std::string &/*clientName*/, unsigned int queueSizeLimit ) : MidiInApi( queueSizeLimit ) { errorString_ = "MidiInDummy: This class provides no functionality."; error( RtMidiError::WARNING, errorString_ ); } |
| 316 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::RTMIDI_DUMMY; } |
| 317 |
void openPort( unsigned int /*portNumber*/, const std::string &/*portName*/ ) {} |
| 318 |
void openVirtualPort( const std::string &/*portName*/ ) {} |
| 319 |
void closePort( void ) {} |
| 320 |
void setClientName( const std::string &/*clientName*/ ) {}; |
| 321 |
void setPortName( const std::string &/*portName*/ ) {}; |
| 322 |
unsigned int getPortCount( void ) { return 0; } |
| 323 |
std::string getPortName( unsigned int /*portNumber*/ ) { return ""; } |
| 324 |
|
| 325 |
protected: |
| 326 |
void initialize( const std::string& /*clientName*/ ) {} |
| 327 |
}; |
| 328 |
|
| 329 |
class MidiOutDummy: public MidiOutApi |
| 330 |
{ |
| 331 |
public: |
| 332 |
MidiOutDummy( const std::string &/*clientName*/ ) { errorString_ = "MidiOutDummy: This class provides no functionality."; error( RtMidiError::WARNING, errorString_ ); } |
| 333 |
RtMidi::Api getCurrentApi( void ) { return RtMidi::RTMIDI_DUMMY; } |
| 334 |
void openPort( unsigned int /*portNumber*/, const std::string &/*portName*/ ) {} |
| 335 |
void openVirtualPort( const std::string &/*portName*/ ) {} |
| 336 |
void closePort( void ) {} |
| 337 |
void setClientName( const std::string &/*clientName*/ ) {}; |
| 338 |
void setPortName( const std::string &/*portName*/ ) {}; |
| 339 |
unsigned int getPortCount( void ) { return 0; } |
| 340 |
std::string getPortName( unsigned int /*portNumber*/ ) { return ""; } |
| 341 |
void sendMessage( const unsigned char * /*message*/, size_t /*size*/ ) {} |
| 342 |
|
| 343 |
protected: |
| 344 |
void initialize( const std::string& /*clientName*/ ) {} |
| 345 |
}; |
| 346 |
|
| 347 |
#endif |
| 348 |
|
| 349 |
//*********************************************************************// |
| 350 |
// RtMidi Definitions |
| 351 |
//*********************************************************************// |
| 352 |
|
| 353 |
RtMidi :: RtMidi() |
| 354 |
: rtapi_(0) |
| 355 |
{ |
| 356 |
} |
| 357 |
|
| 358 |
RtMidi :: ~RtMidi() |
| 359 |
{ |
| 360 |
delete rtapi_; |
| 361 |
rtapi_ = 0; |
| 362 |
} |
| 363 |
|
| 364 |
RtMidi::RtMidi(RtMidi&& other) noexcept { |
| 365 |
rtapi_ = other.rtapi_; |
| 366 |
other.rtapi_ = nullptr; |
| 367 |
} |
| 368 |
|
| 369 |
std::string RtMidi :: getVersion( void ) throw() |
| 370 |
{ |
| 371 |
return std::string( RTMIDI_VERSION ); |
| 372 |
} |
| 373 |
|
| 374 |
// Define API names and display names. |
| 375 |
// Must be in same order as API enum. |
| 376 |
extern "C" { |
| 377 |
const char* rtmidi_api_names[][2] = { |
| 378 |
{ "unspecified" , "Unknown" }, |
| 379 |
{ "core" , "CoreMidi" }, |
| 380 |
{ "alsa" , "ALSA" }, |
| 381 |
{ "jack" , "Jack" }, |
| 382 |
{ "winmm" , "Windows MultiMedia" }, |
| 383 |
{ "web" , "Web MIDI API" }, |
| 384 |
{ "dummy" , "Dummy" }, |
| 385 |
}; |
| 386 |
const unsigned int rtmidi_num_api_names = |
| 387 |
sizeof(rtmidi_api_names)/sizeof(rtmidi_api_names[0]); |
| 388 |
|
| 389 |
// The order here will control the order of RtMidi's API search in |
| 390 |
// the constructor. |
| 391 |
extern "C" const RtMidi::Api rtmidi_compiled_apis[] = { |
| 392 |
#if defined(__MACOSX_CORE__) |
| 393 |
RtMidi::MACOSX_CORE, |
| 394 |
#endif |
| 395 |
#if defined(__LINUX_ALSA__) |
| 396 |
RtMidi::LINUX_ALSA, |
| 397 |
#endif |
| 398 |
#if defined(__UNIX_JACK__) |
| 399 |
RtMidi::UNIX_JACK, |
| 400 |
#endif |
| 401 |
#if defined(__WINDOWS_MM__) |
| 402 |
RtMidi::WINDOWS_MM, |
| 403 |
#endif |
| 404 |
#if defined(__WEB_MIDI_API__) |
| 405 |
RtMidi::WEB_MIDI_API, |
| 406 |
#endif |
| 407 |
#if defined(__RTMIDI_DUMMY__) |
| 408 |
RtMidi::RTMIDI_DUMMY, |
| 409 |
#endif |
| 410 |
RtMidi::UNSPECIFIED, |
| 411 |
}; |
| 412 |
extern "C" const unsigned int rtmidi_num_compiled_apis = |
| 413 |
sizeof(rtmidi_compiled_apis)/sizeof(rtmidi_compiled_apis[0])-1; |
| 414 |
} |
| 415 |
|
| 416 |
// This is a compile-time check that rtmidi_num_api_names == RtMidi::NUM_APIS. |
| 417 |
// If the build breaks here, check that they match. |
| 418 |
template<bool b> class StaticAssert { private: StaticAssert() {} }; |
| 419 |
template<> class StaticAssert<true>{ public: StaticAssert() {} }; |
| 420 |
class StaticAssertions { StaticAssertions() { |
| 421 |
StaticAssert<rtmidi_num_api_names == RtMidi::NUM_APIS>(); |
| 422 |
}}; |
| 423 |
|
| 424 |
void RtMidi :: getCompiledApi( std::vector<RtMidi::Api> &apis ) throw() |
| 425 |
{ |
| 426 |
apis = std::vector<RtMidi::Api>(rtmidi_compiled_apis, |
| 427 |
rtmidi_compiled_apis + rtmidi_num_compiled_apis); |
| 428 |
} |
| 429 |
|
| 430 |
std::string RtMidi :: getApiName( RtMidi::Api api ) |
| 431 |
{ |
| 432 |
if (api < RtMidi::UNSPECIFIED || api >= RtMidi::NUM_APIS) |
| 433 |
return ""; |
| 434 |
return rtmidi_api_names[api][0]; |
| 435 |
} |
| 436 |
|
| 437 |
std::string RtMidi :: getApiDisplayName( RtMidi::Api api ) |
| 438 |
{ |
| 439 |
if (api < RtMidi::UNSPECIFIED || api >= RtMidi::NUM_APIS) |
| 440 |
return "Unknown"; |
| 441 |
return rtmidi_api_names[api][1]; |
| 442 |
} |
| 443 |
|
| 444 |
RtMidi::Api RtMidi :: getCompiledApiByName( const std::string &name ) |
| 445 |
{ |
| 446 |
unsigned int i=0; |
| 447 |
for (i = 0; i < rtmidi_num_compiled_apis; ++i) |
| 448 |
if (name == rtmidi_api_names[rtmidi_compiled_apis[i]][0]) |
| 449 |
return rtmidi_compiled_apis[i]; |
| 450 |
return RtMidi::UNSPECIFIED; |
| 451 |
} |
| 452 |
|
| 453 |
void RtMidi :: setClientName( const std::string &clientName ) |
| 454 |
{ |
| 455 |
rtapi_->setClientName( clientName ); |
| 456 |
} |
| 457 |
|
| 458 |
void RtMidi :: setPortName( const std::string &portName ) |
| 459 |
{ |
| 460 |
rtapi_->setPortName( portName ); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
//*********************************************************************// |
| 465 |
// RtMidiIn Definitions |
| 466 |
//*********************************************************************// |
| 467 |
|
| 468 |
void RtMidiIn :: openMidiApi( RtMidi::Api api, const std::string &clientName, unsigned int queueSizeLimit ) |
| 469 |
{ |
| 470 |
delete rtapi_; |
| 471 |
rtapi_ = 0; |
| 472 |
|
| 473 |
#if defined(__UNIX_JACK__) |
| 474 |
if ( api == UNIX_JACK ) |
| 475 |
rtapi_ = new MidiInJack( clientName, queueSizeLimit ); |
| 476 |
#endif |
| 477 |
#if defined(__LINUX_ALSA__) |
| 478 |
if ( api == LINUX_ALSA ) |
| 479 |
rtapi_ = new MidiInAlsa( clientName, queueSizeLimit ); |
| 480 |
#endif |
| 481 |
#if defined(__WINDOWS_MM__) |
| 482 |
if ( api == WINDOWS_MM ) |
| 483 |
rtapi_ = new MidiInWinMM( clientName, queueSizeLimit ); |
| 484 |
#endif |
| 485 |
#if defined(__MACOSX_CORE__) |
| 486 |
if ( api == MACOSX_CORE ) |
| 487 |
rtapi_ = new MidiInCore( clientName, queueSizeLimit ); |
| 488 |
#endif |
| 489 |
#if defined(__WEB_MIDI_API__) |
| 490 |
if ( api == WEB_MIDI_API ) |
| 491 |
rtapi_ = new MidiInWeb( clientName, queueSizeLimit ); |
| 492 |
#endif |
| 493 |
#if defined(__RTMIDI_DUMMY__) |
| 494 |
if ( api == RTMIDI_DUMMY ) |
| 495 |
rtapi_ = new MidiInDummy( clientName, queueSizeLimit ); |
| 496 |
#endif |
| 497 |
} |
| 498 |
|
| 499 |
RTMIDI_DLL_PUBLIC RtMidiIn :: RtMidiIn( RtMidi::Api api, const std::string &clientName, unsigned int queueSizeLimit ) |
| 500 |
: RtMidi() |
| 501 |
{ |
| 502 |
if ( api != UNSPECIFIED ) { |
| 503 |
// Attempt to open the specified API. |
| 504 |
openMidiApi( api, clientName, queueSizeLimit ); |
| 505 |
if ( rtapi_ ) return; |
| 506 |
|
| 507 |
// No compiled support for specified API value. Issue a warning |
| 508 |
// and continue as if no API was specified. |
| 509 |
std::cerr << "\nRtMidiIn: no compiled support for specified API argument!\n\n" << std::endl; |
| 510 |
} |
| 511 |
|
| 512 |
// Iterate through the compiled APIs and return as soon as we find |
| 513 |
// one with at least one port or we reach the end of the list. |
| 514 |
std::vector< RtMidi::Api > apis; |
| 515 |
getCompiledApi( apis ); |
| 516 |
for ( unsigned int i=0; i<apis.size(); i++ ) { |
| 517 |
openMidiApi( apis[i], clientName, queueSizeLimit ); |
| 518 |
if ( rtapi_ && rtapi_->getPortCount() ) break; |
| 519 |
} |
| 520 |
|
| 521 |
if ( rtapi_ ) return; |
| 522 |
|
| 523 |
// It should not be possible to get here because the preprocessor |
| 524 |
// definition __RTMIDI_DUMMY__ is automatically defined if no |
| 525 |
// API-specific definitions are passed to the compiler. But just in |
| 526 |
// case something weird happens, we'll throw an error. |
| 527 |
std::string errorText = "RtMidiIn: no compiled API support found ... critical error!!"; |
| 528 |
throw( RtMidiError( errorText, RtMidiError::UNSPECIFIED ) ); |
| 529 |
} |
| 530 |
|
| 531 |
RtMidiIn :: ~RtMidiIn() throw() |
| 532 |
{ |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
//*********************************************************************// |
| 537 |
// RtMidiOut Definitions |
| 538 |
//*********************************************************************// |
| 539 |
|
| 540 |
void RtMidiOut :: openMidiApi( RtMidi::Api api, const std::string &clientName ) |
| 541 |
{ |
| 542 |
delete rtapi_; |
| 543 |
rtapi_ = 0; |
| 544 |
|
| 545 |
#if defined(__UNIX_JACK__) |
| 546 |
if ( api == UNIX_JACK ) |
| 547 |
rtapi_ = new MidiOutJack( clientName ); |
| 548 |
#endif |
| 549 |
#if defined(__LINUX_ALSA__) |
| 550 |
if ( api == LINUX_ALSA ) |
| 551 |
rtapi_ = new MidiOutAlsa( clientName ); |
| 552 |
#endif |
| 553 |
#if defined(__WINDOWS_MM__) |
| 554 |
if ( api == WINDOWS_MM ) |
| 555 |
rtapi_ = new MidiOutWinMM( clientName ); |
| 556 |
#endif |
| 557 |
#if defined(__MACOSX_CORE__) |
| 558 |
if ( api == MACOSX_CORE ) |
| 559 |
rtapi_ = new MidiOutCore( clientName ); |
| 560 |
#endif |
| 561 |
#if defined(__WEB_MIDI_API__) |
| 562 |
if ( api == WEB_MIDI_API ) |
| 563 |
rtapi_ = new MidiOutWeb( clientName ); |
| 564 |
#endif |
| 565 |
#if defined(__RTMIDI_DUMMY__) |
| 566 |
if ( api == RTMIDI_DUMMY ) |
| 567 |
rtapi_ = new MidiOutDummy( clientName ); |
| 568 |
#endif |
| 569 |
} |
| 570 |
|
| 571 |
RTMIDI_DLL_PUBLIC RtMidiOut :: RtMidiOut( RtMidi::Api api, const std::string &clientName) |
| 572 |
{ |
| 573 |
if ( api != UNSPECIFIED ) { |
| 574 |
// Attempt to open the specified API. |
| 575 |
openMidiApi( api, clientName ); |
| 576 |
if ( rtapi_ ) return; |
| 577 |
|
| 578 |
// No compiled support for specified API value. Issue a warning |
| 579 |
// and continue as if no API was specified. |
| 580 |
std::cerr << "\nRtMidiOut: no compiled support for specified API argument!\n\n" << std::endl; |
| 581 |
} |
| 582 |
|
| 583 |
// Iterate through the compiled APIs and return as soon as we find |
| 584 |
// one with at least one port or we reach the end of the list. |
| 585 |
std::vector< RtMidi::Api > apis; |
| 586 |
getCompiledApi( apis ); |
| 587 |
for ( unsigned int i=0; i<apis.size(); i++ ) { |
| 588 |
openMidiApi( apis[i], clientName ); |
| 589 |
if ( rtapi_ && rtapi_->getPortCount() ) break; |
| 590 |
} |
| 591 |
|
| 592 |
if ( rtapi_ ) return; |
| 593 |
|
| 594 |
// It should not be possible to get here because the preprocessor |
| 595 |
// definition __RTMIDI_DUMMY__ is automatically defined if no |
| 596 |
// API-specific definitions are passed to the compiler. But just in |
| 597 |
// case something weird happens, we'll thrown an error. |
| 598 |
std::string errorText = "RtMidiOut: no compiled API support found ... critical error!!"; |
| 599 |
throw( RtMidiError( errorText, RtMidiError::UNSPECIFIED ) ); |
| 600 |
} |
| 601 |
|
| 602 |
RtMidiOut :: ~RtMidiOut() throw() |
| 603 |
{ |
| 604 |
} |
| 605 |
|
| 606 |
//*********************************************************************// |
| 607 |
// Common MidiApi Definitions |
| 608 |
//*********************************************************************// |
| 609 |
|
| 610 |
MidiApi :: MidiApi( void ) |
| 611 |
: apiData_( 0 ), connected_( false ), errorCallback_(0), firstErrorOccurred_(false), errorCallbackUserData_(0) |
| 612 |
{ |
| 613 |
} |
| 614 |
|
| 615 |
MidiApi :: ~MidiApi( void ) |
| 616 |
{ |
| 617 |
} |
| 618 |
|
| 619 |
void MidiApi :: setErrorCallback( RtMidiErrorCallback errorCallback, void *userData = 0 ) |
| 620 |
{ |
| 621 |
errorCallback_ = errorCallback; |
| 622 |
errorCallbackUserData_ = userData; |
| 623 |
} |
| 624 |
|
| 625 |
void MidiApi :: error( RtMidiError::Type type, std::string errorString ) |
| 626 |
{ |
| 627 |
if ( errorCallback_ ) { |
| 628 |
|
| 629 |
if ( firstErrorOccurred_ ) |
| 630 |
return; |
| 631 |
|
| 632 |
firstErrorOccurred_ = true; |
| 633 |
const std::string errorMessage = errorString; |
| 634 |
|
| 635 |
errorCallback_( type, errorMessage, errorCallbackUserData_ ); |
| 636 |
firstErrorOccurred_ = false; |
| 637 |
return; |
| 638 |
} |
| 639 |
|
| 640 |
if ( type == RtMidiError::WARNING ) { |
| 641 |
std::cerr << '\n' << errorString << "\n\n"; |
| 642 |
} |
| 643 |
else if ( type == RtMidiError::DEBUG_WARNING ) { |
| 644 |
#if defined(__RTMIDI_DEBUG__) |
| 645 |
std::cerr << '\n' << errorString << "\n\n"; |
| 646 |
#endif |
| 647 |
} |
| 648 |
else { |
| 649 |
std::cerr << '\n' << errorString << "\n\n"; |
| 650 |
throw RtMidiError( errorString, type ); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
//*********************************************************************// |
| 655 |
// Common MidiInApi Definitions |
| 656 |
//*********************************************************************// |
| 657 |
|
| 658 |
MidiInApi :: MidiInApi( unsigned int queueSizeLimit ) |
| 659 |
: MidiApi() |
| 660 |
{ |
| 661 |
// Allocate the MIDI queue. |
| 662 |
inputData_.queue.ringSize = queueSizeLimit; |
| 663 |
if ( inputData_.queue.ringSize > 0 ) |
| 664 |
inputData_.queue.ring = new MidiMessage[ inputData_.queue.ringSize ]; |
| 665 |
} |
| 666 |
|
| 667 |
MidiInApi :: ~MidiInApi( void ) |
| 668 |
{ |
| 669 |
// Delete the MIDI queue. |
| 670 |
if ( inputData_.queue.ringSize > 0 ) delete [] inputData_.queue.ring; |
| 671 |
} |
| 672 |
|
| 673 |
void MidiInApi :: setCallback( RtMidiIn::RtMidiCallback callback, void *userData ) |
| 674 |
{ |
| 675 |
if ( inputData_.usingCallback ) { |
| 676 |
errorString_ = "MidiInApi::setCallback: a callback function is already set!"; |
| 677 |
error( RtMidiError::WARNING, errorString_ ); |
| 678 |
return; |
| 679 |
} |
| 680 |
|
| 681 |
if ( !callback ) { |
| 682 |
errorString_ = "RtMidiIn::setCallback: callback function value is invalid!"; |
| 683 |
error( RtMidiError::WARNING, errorString_ ); |
| 684 |
return; |
| 685 |
} |
| 686 |
|
| 687 |
inputData_.userCallback = callback; |
| 688 |
inputData_.userData = userData; |
| 689 |
inputData_.usingCallback = true; |
| 690 |
} |
| 691 |
|
| 692 |
void MidiInApi :: cancelCallback() |
| 693 |
{ |
| 694 |
if ( !inputData_.usingCallback ) { |
| 695 |
errorString_ = "RtMidiIn::cancelCallback: no callback function was set!"; |
| 696 |
error( RtMidiError::WARNING, errorString_ ); |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
inputData_.userCallback = 0; |
| 701 |
inputData_.userData = 0; |
| 702 |
inputData_.usingCallback = false; |
| 703 |
} |
| 704 |
|
| 705 |
void MidiInApi :: ignoreTypes( bool midiSysex, bool midiTime, bool midiSense ) |
| 706 |
{ |
| 707 |
inputData_.ignoreFlags = 0; |
| 708 |
if ( midiSysex ) inputData_.ignoreFlags = 0x01; |
| 709 |
if ( midiTime ) inputData_.ignoreFlags |= 0x02; |
| 710 |
if ( midiSense ) inputData_.ignoreFlags |= 0x04; |
| 711 |
} |
| 712 |
|
| 713 |
double MidiInApi :: getMessage( std::vector<unsigned char> *message ) |
| 714 |
{ |
| 715 |
message->clear(); |
| 716 |
|
| 717 |
if ( inputData_.usingCallback ) { |
| 718 |
errorString_ = "RtMidiIn::getNextMessage: a user callback is currently set for this port."; |
| 719 |
error( RtMidiError::WARNING, errorString_ ); |
| 720 |
return 0.0; |
| 721 |
} |
| 722 |
|
| 723 |
double timeStamp; |
| 724 |
if ( !inputData_.queue.pop( message, &timeStamp ) ) |
| 725 |
return 0.0; |
| 726 |
|
| 727 |
return timeStamp; |
| 728 |
} |
| 729 |
|
| 730 |
void MidiInApi :: setBufferSize( unsigned int size, unsigned int count ) |
| 731 |
{ |
| 732 |
inputData_.bufferSize = size; |
| 733 |
inputData_.bufferCount = count; |
| 734 |
} |
| 735 |
|
| 736 |
unsigned int MidiInApi::MidiQueue::size( unsigned int *__back, |
| 737 |
unsigned int *__front ) |
| 738 |
{ |
| 739 |
// Access back/front members exactly once and make stack copies for |
| 740 |
// size calculation |
| 741 |
unsigned int _back = back, _front = front, _size; |
| 742 |
if ( _back >= _front ) |
| 743 |
_size = _back - _front; |
| 744 |
else |
| 745 |
_size = ringSize - _front + _back; |
| 746 |
|
| 747 |
// Return copies of back/front so no new and unsynchronized accesses |
| 748 |
// to member variables are needed. |
| 749 |
if ( __back ) *__back = _back; |
| 750 |
if ( __front ) *__front = _front; |
| 751 |
return _size; |
| 752 |
} |
| 753 |
|
| 754 |
// As long as we haven't reached our queue size limit, push the message. |
| 755 |
bool MidiInApi::MidiQueue::push( const MidiInApi::MidiMessage& msg ) |
| 756 |
{ |
| 757 |
// Local stack copies of front/back |
| 758 |
unsigned int _back, _front, _size; |
| 759 |
|
| 760 |
// Get back/front indexes exactly once and calculate current size |
| 761 |
_size = size( &_back, &_front ); |
| 762 |
|
| 763 |
if ( _size < ringSize-1 ) |
| 764 |
{ |
| 765 |
ring[_back] = msg; |
| 766 |
back = (back+1)%ringSize; |
| 767 |
return true; |
| 768 |
} |
| 769 |
|
| 770 |
return false; |
| 771 |
} |
| 772 |
|
| 773 |
bool MidiInApi::MidiQueue::pop( std::vector<unsigned char> *msg, double* timeStamp ) |
| 774 |
{ |
| 775 |
// Local stack copies of front/back |
| 776 |
unsigned int _back, _front, _size; |
| 777 |
|
| 778 |
// Get back/front indexes exactly once and calculate current size |
| 779 |
_size = size( &_back, &_front ); |
| 780 |
|
| 781 |
if ( _size == 0 ) |
| 782 |
return false; |
| 783 |
|
| 784 |
// Copy queued message to the vector pointer argument and then "pop" it. |
| 785 |
msg->assign( ring[_front].bytes.begin(), ring[_front].bytes.end() ); |
| 786 |
*timeStamp = ring[_front].timeStamp; |
| 787 |
|
| 788 |
// Update front |
| 789 |
front = (front+1)%ringSize; |
| 790 |
return true; |
| 791 |
} |
| 792 |
|
| 793 |
//*********************************************************************// |
| 794 |
// Common MidiOutApi Definitions |
| 795 |
//*********************************************************************// |
| 796 |
|
| 797 |
MidiOutApi :: MidiOutApi( void ) |
| 798 |
: MidiApi() |
| 799 |
{ |
| 800 |
} |
| 801 |
|
| 802 |
MidiOutApi :: ~MidiOutApi( void ) |
| 803 |
{ |
| 804 |
} |
| 805 |
|
| 806 |
// *************************************************** // |
| 807 |
// |
| 808 |
// OS/API-specific methods. |
| 809 |
// |
| 810 |
// *************************************************** // |
| 811 |
|
| 812 |
#if defined(__MACOSX_CORE__) |
| 813 |
|
| 814 |
// The CoreMIDI API is based on the use of a callback function for |
| 815 |
// MIDI input. We convert the system specific time stamps to delta |
| 816 |
// time values. |
| 817 |
|
| 818 |
// These are not available on iOS. |
| 819 |
#ifdef TARGET_OS_IPHONE |
| 820 |
#include <CoreAudio/HostTime.h> |
| 821 |
#include <CoreServices/CoreServices.h> |
| 822 |
#endif |
| 823 |
|
| 824 |
// A structure to hold variables related to the CoreMIDI API |
| 825 |
// implementation. |
| 826 |
struct CoreMidiData { |
| 827 |
MIDIClientRef client; |
| 828 |
MIDIPortRef port; |
| 829 |
MIDIEndpointRef endpoint; |
| 830 |
MIDIEndpointRef destinationId; |
| 831 |
unsigned long long lastTime; |
| 832 |
MIDISysexSendRequest sysexreq; |
| 833 |
}; |
| 834 |
|
| 835 |
static MIDIClientRef CoreMidiClientSingleton = 0; |
| 836 |
|
| 837 |
void RtMidi_setCoreMidiClientSingleton(MIDIClientRef client){ |
| 838 |
CoreMidiClientSingleton = client; |
| 839 |
} |
| 840 |
|
| 841 |
void RtMidi_disposeCoreMidiClientSingleton(){ |
| 842 |
if (CoreMidiClientSingleton == 0){ |
| 843 |
return; |
| 844 |
} |
| 845 |
MIDIClientDispose( CoreMidiClientSingleton ); |
| 846 |
CoreMidiClientSingleton = 0; |
| 847 |
} |
| 848 |
|
| 849 |
//*********************************************************************// |
| 850 |
// API: OS-X |
| 851 |
// Class Definitions: MidiInCore |
| 852 |
//*********************************************************************// |
| 853 |
|
| 854 |
static void midiInputCallback( const MIDIPacketList *list, void *procRef, void */*srcRef*/ ) |
| 855 |
{ |
| 856 |
MidiInApi::RtMidiInData *data = static_cast<MidiInApi::RtMidiInData *> (procRef); |
| 857 |
CoreMidiData *apiData = static_cast<CoreMidiData *> (data->apiData); |
| 858 |
|
| 859 |
unsigned char status; |
| 860 |
unsigned short nBytes, iByte, size; |
| 861 |
unsigned long long time; |
| 862 |
|
| 863 |
bool& continueSysex = data->continueSysex; |
| 864 |
MidiInApi::MidiMessage& message = data->message; |
| 865 |
|
| 866 |
const MIDIPacket *packet = &list->packet[0]; |
| 867 |
for ( unsigned int i=0; i<list->numPackets; ++i ) { |
| 868 |
|
| 869 |
// My interpretation of the CoreMIDI documentation: all message |
| 870 |
// types, except sysex, are complete within a packet and there may |
| 871 |
// be several of them in a single packet. Sysex messages can be |
| 872 |
// broken across multiple packets and PacketLists but are bundled |
| 873 |
// alone within each packet (these packets do not contain other |
| 874 |
// message types). If sysex messages are split across multiple |
| 875 |
// MIDIPacketLists, they must be handled by multiple calls to this |
| 876 |
// function. |
| 877 |
|
| 878 |
nBytes = packet->length; |
| 879 |
if ( nBytes == 0 ) { |
| 880 |
packet = MIDIPacketNext( packet ); |
| 881 |
continue; |
| 882 |
} |
| 883 |
|
| 884 |
// Calculate time stamp. |
| 885 |
if ( data->firstMessage ) { |
| 886 |
message.timeStamp = 0.0; |
| 887 |
data->firstMessage = false; |
| 888 |
} |
| 889 |
else { |
| 890 |
time = packet->timeStamp; |
| 891 |
if ( time == 0 ) { // this happens when receiving asynchronous sysex messages |
| 892 |
time = AudioGetCurrentHostTime(); |
| 893 |
} |
| 894 |
time -= apiData->lastTime; |
| 895 |
time = AudioConvertHostTimeToNanos( time ); |
| 896 |
if ( !continueSysex ) |
| 897 |
message.timeStamp = time * 0.000000001; |
| 898 |
} |
| 899 |
|
| 900 |
// Track whether any non-filtered messages were found in this |
| 901 |
// packet for timestamp calculation |
| 902 |
bool foundNonFiltered = false; |
| 903 |
|
| 904 |
iByte = 0; |
| 905 |
if ( continueSysex ) { |
| 906 |
// We have a continuing, segmented sysex message. |
| 907 |
if ( !( data->ignoreFlags & 0x01 ) ) { |
| 908 |
// If we're not ignoring sysex messages, copy the entire packet. |
| 909 |
for ( unsigned int j=0; j<nBytes; ++j ) |
| 910 |
message.bytes.push_back( packet->data[j] ); |
| 911 |
} |
| 912 |
continueSysex = packet->data[nBytes-1] != 0xF7; |
| 913 |
|
| 914 |
if ( !( data->ignoreFlags & 0x01 ) && !continueSysex ) { |
| 915 |
// If not a continuing sysex message, invoke the user callback function or queue the message. |
| 916 |
if ( data->usingCallback ) { |
| 917 |
RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback; |
| 918 |
callback( message.timeStamp, &message.bytes, data->userData ); |
| 919 |
} |
| 920 |
else { |
| 921 |
// As long as we haven't reached our queue size limit, push the message. |
| 922 |
if ( !data->queue.push( message ) ) |
| 923 |
std::cerr << "\nMidiInCore: message queue limit reached!!\n\n"; |
| 924 |
} |
| 925 |
message.bytes.clear(); |
| 926 |
} |
| 927 |
} |
| 928 |
else { |
| 929 |
while ( iByte < nBytes ) { |
| 930 |
size = 0; |
| 931 |
// We are expecting that the next byte in the packet is a status byte. |
| 932 |
status = packet->data[iByte]; |
| 933 |
if ( !(status & 0x80) ) break; |
| 934 |
// Determine the number of bytes in the MIDI message. |
| 935 |
if ( status < 0xC0 ) size = 3; |
| 936 |
else if ( status < 0xE0 ) size = 2; |
| 937 |
else if ( status < 0xF0 ) size = 3; |
| 938 |
else if ( status == 0xF0 ) { |
| 939 |
// A MIDI sysex |
| 940 |
if ( data->ignoreFlags & 0x01 ) { |
| 941 |
size = 0; |
| 942 |
iByte = nBytes; |
| 943 |
} |
| 944 |
else size = nBytes - iByte; |
| 945 |
continueSysex = packet->data[nBytes-1] != 0xF7; |
| 946 |
} |
| 947 |
else if ( status == 0xF1 ) { |
| 948 |
// A MIDI time code message |
| 949 |
if ( data->ignoreFlags & 0x02 ) { |
| 950 |
size = 0; |
| 951 |
iByte += 2; |
| 952 |
} |
| 953 |
else size = 2; |
| 954 |
} |
| 955 |
else if ( status == 0xF2 ) size = 3; |
| 956 |
else if ( status == 0xF3 ) size = 2; |
| 957 |
else if ( status == 0xF8 && ( data->ignoreFlags & 0x02 ) ) { |
| 958 |
// A MIDI timing tick message and we're ignoring it. |
| 959 |
size = 0; |
| 960 |
iByte += 1; |
| 961 |
} |
| 962 |
else if ( status == 0xFE && ( data->ignoreFlags & 0x04 ) ) { |
| 963 |
// A MIDI active sensing message and we're ignoring it. |
| 964 |
size = 0; |
| 965 |
iByte += 1; |
| 966 |
} |
| 967 |
else size = 1; |
| 968 |
|
| 969 |
// Copy the MIDI data to our vector. |
| 970 |
if ( size ) { |
| 971 |
foundNonFiltered = true; |
| 972 |
message.bytes.assign( &packet->data[iByte], &packet->data[iByte+size] ); |
| 973 |
if ( !continueSysex ) { |
| 974 |
// If not a continuing sysex message, invoke the user callback function or queue the message. |
| 975 |
if ( data->usingCallback ) { |
| 976 |
RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback; |
| 977 |
callback( message.timeStamp, &message.bytes, data->userData ); |
| 978 |
} |
| 979 |
else { |
| 980 |
// As long as we haven't reached our queue size limit, push the message. |
| 981 |
if ( !data->queue.push( message ) ) |
| 982 |
std::cerr << "\nMidiInCore: message queue limit reached!!\n\n"; |
| 983 |
} |
| 984 |
message.bytes.clear(); |
| 985 |
} |
| 986 |
iByte += size; |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
// Save the time of the last non-filtered message |
| 992 |
if ( foundNonFiltered ) { |
| 993 |
apiData->lastTime = packet->timeStamp; |
| 994 |
if ( apiData->lastTime == 0 ) { // this happens when receiving asynchronous sysex messages |
| 995 |
apiData->lastTime = AudioGetCurrentHostTime(); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
packet = MIDIPacketNext(packet); |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
MidiInCore :: MidiInCore( const std::string &clientName, unsigned int queueSizeLimit ) |
| 1004 |
: MidiInApi( queueSizeLimit ) |
| 1005 |
{ |
| 1006 |
MidiInCore::initialize( clientName ); |
| 1007 |
} |
| 1008 |
|
| 1009 |
MidiInCore :: ~MidiInCore( void ) |
| 1010 |
{ |
| 1011 |
// Close a connection if it exists. |
| 1012 |
MidiInCore::closePort(); |
| 1013 |
|
| 1014 |
// Cleanup. |
| 1015 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1016 |
if ( data->endpoint ) MIDIEndpointDispose( data->endpoint ); |
| 1017 |
delete data; |
| 1018 |
} |
| 1019 |
|
| 1020 |
MIDIClientRef MidiInCore::getCoreMidiClientSingleton(const std::string& clientName) throw() { |
| 1021 |
|
| 1022 |
if (CoreMidiClientSingleton == 0){ |
| 1023 |
// Set up our client. |
| 1024 |
MIDIClientRef client; |
| 1025 |
|
| 1026 |
CFStringRef name = CFStringCreateWithCString( NULL, clientName.c_str(), kCFStringEncodingASCII ); |
| 1027 |
OSStatus result = MIDIClientCreate(name, NULL, NULL, &client ); |
| 1028 |
if ( result != noErr ) { |
| 1029 |
std::ostringstream ost; |
| 1030 |
ost << "MidiInCore::initialize: error creating OS-X MIDI client object (" << result << ")."; |
| 1031 |
errorString_ = ost.str(); |
| 1032 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1033 |
return 0; |
| 1034 |
} |
| 1035 |
CFRelease( name ); |
| 1036 |
|
| 1037 |
CoreMidiClientSingleton = client; |
| 1038 |
} |
| 1039 |
|
| 1040 |
return CoreMidiClientSingleton; |
| 1041 |
} |
| 1042 |
|
| 1043 |
void MidiInCore :: initialize( const std::string& clientName ) |
| 1044 |
{ |
| 1045 |
// Set up our client. |
| 1046 |
MIDIClientRef client = getCoreMidiClientSingleton(clientName); |
| 1047 |
|
| 1048 |
// Save our api-specific connection information. |
| 1049 |
CoreMidiData *data = (CoreMidiData *) new CoreMidiData; |
| 1050 |
data->client = client; |
| 1051 |
data->endpoint = 0; |
| 1052 |
apiData_ = (void *) data; |
| 1053 |
inputData_.apiData = (void *) data; |
| 1054 |
} |
| 1055 |
|
| 1056 |
void MidiInCore :: openPort( unsigned int portNumber, const std::string &portName ) |
| 1057 |
{ |
| 1058 |
if ( connected_ ) { |
| 1059 |
errorString_ = "MidiInCore::openPort: a valid connection already exists!"; |
| 1060 |
error( RtMidiError::WARNING, errorString_ ); |
| 1061 |
return; |
| 1062 |
} |
| 1063 |
|
| 1064 |
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false ); |
| 1065 |
unsigned int nSrc = MIDIGetNumberOfSources(); |
| 1066 |
if ( nSrc < 1 ) { |
| 1067 |
errorString_ = "MidiInCore::openPort: no MIDI input sources found!"; |
| 1068 |
error( RtMidiError::NO_DEVICES_FOUND, errorString_ ); |
| 1069 |
return; |
| 1070 |
} |
| 1071 |
|
| 1072 |
if ( portNumber >= nSrc ) { |
| 1073 |
std::ostringstream ost; |
| 1074 |
ost << "MidiInCore::openPort: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 1075 |
errorString_ = ost.str(); |
| 1076 |
error( RtMidiError::INVALID_PARAMETER, errorString_ ); |
| 1077 |
return; |
| 1078 |
} |
| 1079 |
|
| 1080 |
MIDIPortRef port; |
| 1081 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1082 |
CFStringRef portNameRef = CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ); |
| 1083 |
OSStatus result = MIDIInputPortCreate( data->client, |
| 1084 |
portNameRef, |
| 1085 |
midiInputCallback, (void *)&inputData_, &port ); |
| 1086 |
CFRelease( portNameRef ); |
| 1087 |
|
| 1088 |
if ( result != noErr ) { |
| 1089 |
errorString_ = "MidiInCore::openPort: error creating OS-X MIDI input port."; |
| 1090 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1091 |
return; |
| 1092 |
} |
| 1093 |
|
| 1094 |
// Get the desired input source identifier. |
| 1095 |
MIDIEndpointRef endpoint = MIDIGetSource( portNumber ); |
| 1096 |
if ( endpoint == 0 ) { |
| 1097 |
MIDIPortDispose( port ); |
| 1098 |
errorString_ = "MidiInCore::openPort: error getting MIDI input source reference."; |
| 1099 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1100 |
return; |
| 1101 |
} |
| 1102 |
|
| 1103 |
// Make the connection. |
| 1104 |
result = MIDIPortConnectSource( port, endpoint, NULL ); |
| 1105 |
if ( result != noErr ) { |
| 1106 |
MIDIPortDispose( port ); |
| 1107 |
errorString_ = "MidiInCore::openPort: error connecting OS-X MIDI input port."; |
| 1108 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1109 |
return; |
| 1110 |
} |
| 1111 |
|
| 1112 |
// Save our api-specific port information. |
| 1113 |
data->port = port; |
| 1114 |
|
| 1115 |
connected_ = true; |
| 1116 |
} |
| 1117 |
|
| 1118 |
void MidiInCore :: openVirtualPort( const std::string &portName ) |
| 1119 |
{ |
| 1120 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1121 |
|
| 1122 |
// Create a virtual MIDI input destination. |
| 1123 |
MIDIEndpointRef endpoint; |
| 1124 |
CFStringRef portNameRef = CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ); |
| 1125 |
OSStatus result = MIDIDestinationCreate( data->client, |
| 1126 |
portNameRef, |
| 1127 |
midiInputCallback, (void *)&inputData_, &endpoint ); |
| 1128 |
CFRelease( portNameRef ); |
| 1129 |
|
| 1130 |
if ( result != noErr ) { |
| 1131 |
errorString_ = "MidiInCore::openVirtualPort: error creating virtual OS-X MIDI destination."; |
| 1132 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1133 |
return; |
| 1134 |
} |
| 1135 |
|
| 1136 |
// Save our api-specific connection information. |
| 1137 |
data->endpoint = endpoint; |
| 1138 |
} |
| 1139 |
|
| 1140 |
void MidiInCore :: closePort( void ) |
| 1141 |
{ |
| 1142 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1143 |
|
| 1144 |
if ( data->endpoint ) { |
| 1145 |
MIDIEndpointDispose( data->endpoint ); |
| 1146 |
data->endpoint = 0; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if ( data->port ) { |
| 1150 |
MIDIPortDispose( data->port ); |
| 1151 |
data->port = 0; |
| 1152 |
} |
| 1153 |
|
| 1154 |
connected_ = false; |
| 1155 |
} |
| 1156 |
|
| 1157 |
void MidiInCore :: setClientName ( const std::string& ) |
| 1158 |
{ |
| 1159 |
|
| 1160 |
errorString_ = "MidiInCore::setClientName: this function is not implemented for the MACOSX_CORE API!"; |
| 1161 |
error( RtMidiError::WARNING, errorString_ ); |
| 1162 |
|
| 1163 |
} |
| 1164 |
|
| 1165 |
void MidiInCore :: setPortName ( const std::string& ) |
| 1166 |
{ |
| 1167 |
|
| 1168 |
errorString_ = "MidiInCore::setPortName: this function is not implemented for the MACOSX_CORE API!"; |
| 1169 |
error( RtMidiError::WARNING, errorString_ ); |
| 1170 |
|
| 1171 |
} |
| 1172 |
|
| 1173 |
unsigned int MidiInCore :: getPortCount() |
| 1174 |
{ |
| 1175 |
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false ); |
| 1176 |
return MIDIGetNumberOfSources(); |
| 1177 |
} |
| 1178 |
|
| 1179 |
// This function was submitted by Douglas Casey Tucker and apparently |
| 1180 |
// derived largely from PortMidi. |
| 1181 |
CFStringRef EndpointName( MIDIEndpointRef endpoint, bool isExternal ) |
| 1182 |
{ |
| 1183 |
CFMutableStringRef result = CFStringCreateMutable( NULL, 0 ); |
| 1184 |
CFStringRef str; |
| 1185 |
|
| 1186 |
// Begin with the endpoint's name. |
| 1187 |
str = NULL; |
| 1188 |
MIDIObjectGetStringProperty( endpoint, kMIDIPropertyName, &str ); |
| 1189 |
if ( str != NULL ) { |
| 1190 |
CFStringAppend( result, str ); |
| 1191 |
CFRelease( str ); |
| 1192 |
} |
| 1193 |
|
| 1194 |
// some MIDI devices have a leading space in endpoint name. trim |
| 1195 |
CFStringRef space = CFStringCreateWithCString(NULL, " ", kCFStringEncodingUTF8); |
| 1196 |
CFStringTrim(result, space); |
| 1197 |
CFRelease(space); |
| 1198 |
|
| 1199 |
MIDIEntityRef entity = 0; |
| 1200 |
MIDIEndpointGetEntity( endpoint, &entity ); |
| 1201 |
if ( entity == 0 ) |
| 1202 |
// probably virtual |
| 1203 |
return result; |
| 1204 |
|
| 1205 |
if ( CFStringGetLength( result ) == 0 ) { |
| 1206 |
// endpoint name has zero length -- try the entity |
| 1207 |
str = NULL; |
| 1208 |
MIDIObjectGetStringProperty( entity, kMIDIPropertyName, &str ); |
| 1209 |
if ( str != NULL ) { |
| 1210 |
CFStringAppend( result, str ); |
| 1211 |
CFRelease( str ); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
// now consider the device's name |
| 1215 |
MIDIDeviceRef device = 0; |
| 1216 |
MIDIEntityGetDevice( entity, &device ); |
| 1217 |
if ( device == 0 ) |
| 1218 |
return result; |
| 1219 |
|
| 1220 |
str = NULL; |
| 1221 |
MIDIObjectGetStringProperty( device, kMIDIPropertyName, &str ); |
| 1222 |
if ( CFStringGetLength( result ) == 0 ) { |
| 1223 |
CFRelease( result ); |
| 1224 |
return str; |
| 1225 |
} |
| 1226 |
if ( str != NULL ) { |
| 1227 |
// if an external device has only one entity, throw away |
| 1228 |
// the endpoint name and just use the device name |
| 1229 |
if ( isExternal && MIDIDeviceGetNumberOfEntities( device ) < 2 ) { |
| 1230 |
CFRelease( result ); |
| 1231 |
return str; |
| 1232 |
} else { |
| 1233 |
if ( CFStringGetLength( str ) == 0 ) { |
| 1234 |
CFRelease( str ); |
| 1235 |
return result; |
| 1236 |
} |
| 1237 |
// does the entity name already start with the device name? |
| 1238 |
// (some drivers do this though they shouldn't) |
| 1239 |
// if so, do not prepend |
| 1240 |
if ( CFStringCompareWithOptions( result, /* endpoint name */ |
| 1241 |
str /* device name */, |
| 1242 |
CFRangeMake(0, CFStringGetLength( str ) ), 0 ) != kCFCompareEqualTo ) { |
| 1243 |
// prepend the device name to the entity name |
| 1244 |
if ( CFStringGetLength( result ) > 0 ) |
| 1245 |
CFStringInsert( result, 0, CFSTR(" ") ); |
| 1246 |
|
| 1247 |
CFStringInsert( result, 0, str ); |
| 1248 |
} |
| 1249 |
CFRelease( str ); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
return result; |
| 1253 |
} |
| 1254 |
|
| 1255 |
// This function was submitted by Douglas Casey Tucker and apparently |
| 1256 |
// derived largely from PortMidi. |
| 1257 |
static CFStringRef ConnectedEndpointName( MIDIEndpointRef endpoint ) |
| 1258 |
{ |
| 1259 |
CFMutableStringRef result = CFStringCreateMutable( NULL, 0 ); |
| 1260 |
CFStringRef str; |
| 1261 |
OSStatus err; |
| 1262 |
int i; |
| 1263 |
|
| 1264 |
// Does the endpoint have connections? |
| 1265 |
CFDataRef connections = NULL; |
| 1266 |
int nConnected = 0; |
| 1267 |
bool anyStrings = false; |
| 1268 |
err = MIDIObjectGetDataProperty( endpoint, kMIDIPropertyConnectionUniqueID, &connections ); |
| 1269 |
if ( connections != NULL ) { |
| 1270 |
// It has connections, follow them |
| 1271 |
// Concatenate the names of all connected devices |
| 1272 |
nConnected = CFDataGetLength( connections ) / sizeof(MIDIUniqueID); |
| 1273 |
if ( nConnected ) { |
| 1274 |
const SInt32 *pid = (const SInt32 *)(CFDataGetBytePtr(connections)); |
| 1275 |
for ( i=0; i<nConnected; ++i, ++pid ) { |
| 1276 |
MIDIUniqueID id = EndianS32_BtoN( *pid ); |
| 1277 |
MIDIObjectRef connObject; |
| 1278 |
MIDIObjectType connObjectType; |
| 1279 |
err = MIDIObjectFindByUniqueID( id, &connObject, &connObjectType ); |
| 1280 |
if ( err == noErr ) { |
| 1281 |
if ( connObjectType == kMIDIObjectType_ExternalSource || |
| 1282 |
connObjectType == kMIDIObjectType_ExternalDestination ) { |
| 1283 |
// Connected to an external device's endpoint (10.3 and later). |
| 1284 |
str = EndpointName( (MIDIEndpointRef)(connObject), true ); |
| 1285 |
} else { |
| 1286 |
// Connected to an external device (10.2) (or something else, catch- |
| 1287 |
str = NULL; |
| 1288 |
MIDIObjectGetStringProperty( connObject, kMIDIPropertyName, &str ); |
| 1289 |
} |
| 1290 |
if ( str != NULL ) { |
| 1291 |
if ( anyStrings ) |
| 1292 |
CFStringAppend( result, CFSTR(", ") ); |
| 1293 |
else |
| 1294 |
anyStrings = true; |
| 1295 |
CFStringAppend( result, str ); |
| 1296 |
CFRelease( str ); |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
} |
| 1301 |
CFRelease( connections ); |
| 1302 |
} |
| 1303 |
if ( anyStrings ) |
| 1304 |
return result; |
| 1305 |
|
| 1306 |
CFRelease( result ); |
| 1307 |
|
| 1308 |
// Here, either the endpoint had no connections, or we failed to obtain names |
| 1309 |
return EndpointName( endpoint, false ); |
| 1310 |
} |
| 1311 |
|
| 1312 |
std::string MidiInCore :: getPortName( unsigned int portNumber ) |
| 1313 |
{ |
| 1314 |
CFStringRef nameRef; |
| 1315 |
MIDIEndpointRef portRef; |
| 1316 |
char name[128]; |
| 1317 |
|
| 1318 |
std::string stringName; |
| 1319 |
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false ); |
| 1320 |
if ( portNumber >= MIDIGetNumberOfSources() ) { |
| 1321 |
std::ostringstream ost; |
| 1322 |
ost << "MidiInCore::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 1323 |
errorString_ = ost.str(); |
| 1324 |
error( RtMidiError::WARNING, errorString_ ); |
| 1325 |
return stringName; |
| 1326 |
} |
| 1327 |
|
| 1328 |
portRef = MIDIGetSource( portNumber ); |
| 1329 |
nameRef = ConnectedEndpointName( portRef ); |
| 1330 |
CFStringGetCString( nameRef, name, sizeof(name), kCFStringEncodingUTF8 ); |
| 1331 |
CFRelease( nameRef ); |
| 1332 |
|
| 1333 |
return stringName = name; |
| 1334 |
} |
| 1335 |
|
| 1336 |
//*********************************************************************// |
| 1337 |
// API: OS-X |
| 1338 |
// Class Definitions: MidiOutCore |
| 1339 |
//*********************************************************************// |
| 1340 |
|
| 1341 |
MidiOutCore :: MidiOutCore( const std::string &clientName ) |
| 1342 |
: MidiOutApi() |
| 1343 |
{ |
| 1344 |
MidiOutCore::initialize( clientName ); |
| 1345 |
} |
| 1346 |
|
| 1347 |
MidiOutCore :: ~MidiOutCore( void ) |
| 1348 |
{ |
| 1349 |
// Close a connection if it exists. |
| 1350 |
MidiOutCore::closePort(); |
| 1351 |
|
| 1352 |
// Cleanup. |
| 1353 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1354 |
if ( data->endpoint ) MIDIEndpointDispose( data->endpoint ); |
| 1355 |
delete data; |
| 1356 |
} |
| 1357 |
|
| 1358 |
MIDIClientRef MidiOutCore::getCoreMidiClientSingleton(const std::string& clientName) throw() { |
| 1359 |
|
| 1360 |
if (CoreMidiClientSingleton == 0){ |
| 1361 |
// Set up our client. |
| 1362 |
MIDIClientRef client; |
| 1363 |
|
| 1364 |
CFStringRef name = CFStringCreateWithCString( NULL, clientName.c_str(), kCFStringEncodingASCII ); |
| 1365 |
OSStatus result = MIDIClientCreate(name, NULL, NULL, &client ); |
| 1366 |
if ( result != noErr ) { |
| 1367 |
std::ostringstream ost; |
| 1368 |
ost << "MidiInCore::initialize: error creating OS-X MIDI client object (" << result << ")."; |
| 1369 |
errorString_ = ost.str(); |
| 1370 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1371 |
return 0; |
| 1372 |
} |
| 1373 |
CFRelease( name ); |
| 1374 |
|
| 1375 |
CoreMidiClientSingleton = client; |
| 1376 |
} |
| 1377 |
|
| 1378 |
return CoreMidiClientSingleton; |
| 1379 |
} |
| 1380 |
|
| 1381 |
void MidiOutCore :: initialize( const std::string& clientName ) |
| 1382 |
{ |
| 1383 |
// Set up our client. |
| 1384 |
MIDIClientRef client = getCoreMidiClientSingleton(clientName); |
| 1385 |
|
| 1386 |
// Save our api-specific connection information. |
| 1387 |
CoreMidiData *data = (CoreMidiData *) new CoreMidiData; |
| 1388 |
data->client = client; |
| 1389 |
data->endpoint = 0; |
| 1390 |
apiData_ = (void *) data; |
| 1391 |
} |
| 1392 |
|
| 1393 |
unsigned int MidiOutCore :: getPortCount() |
| 1394 |
{ |
| 1395 |
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false ); |
| 1396 |
return MIDIGetNumberOfDestinations(); |
| 1397 |
} |
| 1398 |
|
| 1399 |
std::string MidiOutCore :: getPortName( unsigned int portNumber ) |
| 1400 |
{ |
| 1401 |
CFStringRef nameRef; |
| 1402 |
MIDIEndpointRef portRef; |
| 1403 |
char name[128]; |
| 1404 |
|
| 1405 |
std::string stringName; |
| 1406 |
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false ); |
| 1407 |
if ( portNumber >= MIDIGetNumberOfDestinations() ) { |
| 1408 |
std::ostringstream ost; |
| 1409 |
ost << "MidiOutCore::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 1410 |
errorString_ = ost.str(); |
| 1411 |
error( RtMidiError::WARNING, errorString_ ); |
| 1412 |
return stringName; |
| 1413 |
} |
| 1414 |
|
| 1415 |
portRef = MIDIGetDestination( portNumber ); |
| 1416 |
nameRef = ConnectedEndpointName(portRef); |
| 1417 |
CFStringGetCString( nameRef, name, sizeof(name), kCFStringEncodingUTF8 ); |
| 1418 |
CFRelease( nameRef ); |
| 1419 |
|
| 1420 |
return stringName = name; |
| 1421 |
} |
| 1422 |
|
| 1423 |
void MidiOutCore :: openPort( unsigned int portNumber, const std::string &portName ) |
| 1424 |
{ |
| 1425 |
if ( connected_ ) { |
| 1426 |
errorString_ = "MidiOutCore::openPort: a valid connection already exists!"; |
| 1427 |
error( RtMidiError::WARNING, errorString_ ); |
| 1428 |
return; |
| 1429 |
} |
| 1430 |
|
| 1431 |
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false ); |
| 1432 |
unsigned int nDest = MIDIGetNumberOfDestinations(); |
| 1433 |
if (nDest < 1) { |
| 1434 |
errorString_ = "MidiOutCore::openPort: no MIDI output destinations found!"; |
| 1435 |
error( RtMidiError::NO_DEVICES_FOUND, errorString_ ); |
| 1436 |
return; |
| 1437 |
} |
| 1438 |
|
| 1439 |
if ( portNumber >= nDest ) { |
| 1440 |
std::ostringstream ost; |
| 1441 |
ost << "MidiOutCore::openPort: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 1442 |
errorString_ = ost.str(); |
| 1443 |
error( RtMidiError::INVALID_PARAMETER, errorString_ ); |
| 1444 |
return; |
| 1445 |
} |
| 1446 |
|
| 1447 |
MIDIPortRef port; |
| 1448 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1449 |
CFStringRef portNameRef = CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ); |
| 1450 |
OSStatus result = MIDIOutputPortCreate( data->client, portNameRef, &port ); |
| 1451 |
CFRelease( portNameRef ); |
| 1452 |
if ( result != noErr ) { |
| 1453 |
errorString_ = "MidiOutCore::openPort: error creating OS-X MIDI output port."; |
| 1454 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1455 |
return; |
| 1456 |
} |
| 1457 |
|
| 1458 |
// Get the desired output port identifier. |
| 1459 |
MIDIEndpointRef destination = MIDIGetDestination( portNumber ); |
| 1460 |
if ( destination == 0 ) { |
| 1461 |
MIDIPortDispose( port ); |
| 1462 |
errorString_ = "MidiOutCore::openPort: error getting MIDI output destination reference."; |
| 1463 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1464 |
return; |
| 1465 |
} |
| 1466 |
|
| 1467 |
// Save our api-specific connection information. |
| 1468 |
data->port = port; |
| 1469 |
data->destinationId = destination; |
| 1470 |
connected_ = true; |
| 1471 |
} |
| 1472 |
|
| 1473 |
void MidiOutCore :: closePort( void ) |
| 1474 |
{ |
| 1475 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1476 |
|
| 1477 |
if ( data->endpoint ) { |
| 1478 |
MIDIEndpointDispose( data->endpoint ); |
| 1479 |
data->endpoint = 0; |
| 1480 |
} |
| 1481 |
|
| 1482 |
if ( data->port ) { |
| 1483 |
MIDIPortDispose( data->port ); |
| 1484 |
data->port = 0; |
| 1485 |
} |
| 1486 |
|
| 1487 |
connected_ = false; |
| 1488 |
} |
| 1489 |
|
| 1490 |
void MidiOutCore :: setClientName ( const std::string& ) |
| 1491 |
{ |
| 1492 |
|
| 1493 |
errorString_ = "MidiOutCore::setClientName: this function is not implemented for the MACOSX_CORE API!"; |
| 1494 |
error( RtMidiError::WARNING, errorString_ ); |
| 1495 |
|
| 1496 |
} |
| 1497 |
|
| 1498 |
void MidiOutCore :: setPortName ( const std::string& ) |
| 1499 |
{ |
| 1500 |
|
| 1501 |
errorString_ = "MidiOutCore::setPortName: this function is not implemented for the MACOSX_CORE API!"; |
| 1502 |
error( RtMidiError::WARNING, errorString_ ); |
| 1503 |
|
| 1504 |
} |
| 1505 |
|
| 1506 |
void MidiOutCore :: openVirtualPort( const std::string &portName ) |
| 1507 |
{ |
| 1508 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1509 |
|
| 1510 |
if ( data->endpoint ) { |
| 1511 |
errorString_ = "MidiOutCore::openVirtualPort: a virtual output port already exists!"; |
| 1512 |
error( RtMidiError::WARNING, errorString_ ); |
| 1513 |
return; |
| 1514 |
} |
| 1515 |
|
| 1516 |
// Create a virtual MIDI output source. |
| 1517 |
MIDIEndpointRef endpoint; |
| 1518 |
CFStringRef portNameRef = CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ); |
| 1519 |
OSStatus result = MIDISourceCreate( data->client, portNameRef, &endpoint ); |
| 1520 |
CFRelease( portNameRef ); |
| 1521 |
|
| 1522 |
if ( result != noErr ) { |
| 1523 |
errorString_ = "MidiOutCore::initialize: error creating OS-X virtual MIDI source."; |
| 1524 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1525 |
return; |
| 1526 |
} |
| 1527 |
|
| 1528 |
// Save our api-specific connection information. |
| 1529 |
data->endpoint = endpoint; |
| 1530 |
} |
| 1531 |
|
| 1532 |
void MidiOutCore :: sendMessage( const unsigned char *message, size_t size ) |
| 1533 |
{ |
| 1534 |
// We use the MIDISendSysex() function to asynchronously send sysex |
| 1535 |
// messages. Otherwise, we use a single CoreMidi MIDIPacket. |
| 1536 |
unsigned int nBytes = static_cast<unsigned int> (size); |
| 1537 |
if ( nBytes == 0 ) { |
| 1538 |
errorString_ = "MidiOutCore::sendMessage: no data in message argument!"; |
| 1539 |
error( RtMidiError::WARNING, errorString_ ); |
| 1540 |
return; |
| 1541 |
} |
| 1542 |
|
| 1543 |
if ( message[0] != 0xF0 && nBytes > 3 ) { |
| 1544 |
errorString_ = "MidiOutCore::sendMessage: message format problem ... not sysex but > 3 bytes?"; |
| 1545 |
error( RtMidiError::WARNING, errorString_ ); |
| 1546 |
return; |
| 1547 |
} |
| 1548 |
|
| 1549 |
MIDITimeStamp timeStamp = AudioGetCurrentHostTime(); |
| 1550 |
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_); |
| 1551 |
OSStatus result; |
| 1552 |
|
| 1553 |
ByteCount bufsize = nBytes > 65535 ? 65535 : nBytes; |
| 1554 |
Byte buffer[bufsize+16]; // pad for other struct members |
| 1555 |
ByteCount listSize = sizeof( buffer ); |
| 1556 |
MIDIPacketList *packetList = (MIDIPacketList*)buffer; |
| 1557 |
|
| 1558 |
ByteCount remainingBytes = nBytes; |
| 1559 |
while ( remainingBytes ) { |
| 1560 |
MIDIPacket *packet = MIDIPacketListInit( packetList ); |
| 1561 |
// A MIDIPacketList can only contain a maximum of 64K of data, so if our message is longer, |
| 1562 |
// break it up into chunks of 64K or less and send out as a MIDIPacketList with only one |
| 1563 |
// MIDIPacket. Here, we reuse the memory allocated above on the stack for all. |
| 1564 |
ByteCount bytesForPacket = remainingBytes > 65535 ? 65535 : remainingBytes; |
| 1565 |
const Byte* dataStartPtr = (const Byte *) &message[nBytes - remainingBytes]; |
| 1566 |
packet = MIDIPacketListAdd( packetList, listSize, packet, timeStamp, bytesForPacket, dataStartPtr ); |
| 1567 |
remainingBytes -= bytesForPacket; |
| 1568 |
|
| 1569 |
if ( !packet ) { |
| 1570 |
errorString_ = "MidiOutCore::sendMessage: could not allocate packet list"; |
| 1571 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1572 |
return; |
| 1573 |
} |
| 1574 |
|
| 1575 |
// Send to any destinations that may have connected to us. |
| 1576 |
if ( data->endpoint ) { |
| 1577 |
result = MIDIReceived( data->endpoint, packetList ); |
| 1578 |
if ( result != noErr ) { |
| 1579 |
errorString_ = "MidiOutCore::sendMessage: error sending MIDI to virtual destinations."; |
| 1580 |
error( RtMidiError::WARNING, errorString_ ); |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
// And send to an explicit destination port if we're connected. |
| 1585 |
if ( connected_ ) { |
| 1586 |
result = MIDISend( data->port, data->destinationId, packetList ); |
| 1587 |
if ( result != noErr ) { |
| 1588 |
errorString_ = "MidiOutCore::sendMessage: error sending MIDI message to port."; |
| 1589 |
error( RtMidiError::WARNING, errorString_ ); |
| 1590 |
} |
| 1591 |
} |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
#endif // __MACOSX_CORE__ |
| 1596 |
|
| 1597 |
|
| 1598 |
//*********************************************************************// |
| 1599 |
// API: LINUX ALSA SEQUENCER |
| 1600 |
//*********************************************************************// |
| 1601 |
|
| 1602 |
// API information found at: |
| 1603 |
// - http://www.alsa-project.org/documentation.php#Library |
| 1604 |
|
| 1605 |
#if defined(__LINUX_ALSA__) |
| 1606 |
|
| 1607 |
// The ALSA Sequencer API is based on the use of a callback function for |
| 1608 |
// MIDI input. |
| 1609 |
// |
| 1610 |
// Thanks to Pedro Lopez-Cabanillas for help with the ALSA sequencer |
| 1611 |
// time stamps and other assorted fixes!!! |
| 1612 |
|
| 1613 |
// If you don't need timestamping for incoming MIDI events, define the |
| 1614 |
// preprocessor definition AVOID_TIMESTAMPING to save resources |
| 1615 |
// associated with the ALSA sequencer queues. |
| 1616 |
|
| 1617 |
#include <pthread.h> |
| 1618 |
#include <sys/time.h> |
| 1619 |
|
| 1620 |
// ALSA header file. |
| 1621 |
#include <alsa/asoundlib.h> |
| 1622 |
|
| 1623 |
// A structure to hold variables related to the ALSA API |
| 1624 |
// implementation. |
| 1625 |
struct AlsaMidiData { |
| 1626 |
snd_seq_t *seq; |
| 1627 |
unsigned int portNum; |
| 1628 |
int vport; |
| 1629 |
snd_seq_port_subscribe_t *subscription; |
| 1630 |
snd_midi_event_t *coder; |
| 1631 |
unsigned int bufferSize; |
| 1632 |
unsigned int requestedBufferSize; |
| 1633 |
unsigned char *buffer; |
| 1634 |
pthread_t thread; |
| 1635 |
pthread_t dummy_thread_id; |
| 1636 |
snd_seq_real_time_t lastTime; |
| 1637 |
int queue_id; // an input queue is needed to get timestamped events |
| 1638 |
int trigger_fds[2]; |
| 1639 |
}; |
| 1640 |
|
| 1641 |
#define PORT_TYPE( pinfo, bits ) ((snd_seq_port_info_get_capability(pinfo) & (bits)) == (bits)) |
| 1642 |
|
| 1643 |
//*********************************************************************// |
| 1644 |
// API: LINUX ALSA |
| 1645 |
// Class Definitions: MidiInAlsa |
| 1646 |
//*********************************************************************// |
| 1647 |
|
| 1648 |
static void *alsaMidiHandler( void *ptr ) |
| 1649 |
{ |
| 1650 |
MidiInApi::RtMidiInData *data = static_cast<MidiInApi::RtMidiInData *> (ptr); |
| 1651 |
AlsaMidiData *apiData = static_cast<AlsaMidiData *> (data->apiData); |
| 1652 |
|
| 1653 |
long nBytes; |
| 1654 |
double time; |
| 1655 |
bool continueSysex = false; |
| 1656 |
bool doDecode = false; |
| 1657 |
MidiInApi::MidiMessage message; |
| 1658 |
int poll_fd_count; |
| 1659 |
struct pollfd *poll_fds; |
| 1660 |
|
| 1661 |
snd_seq_event_t *ev; |
| 1662 |
int result; |
| 1663 |
result = snd_midi_event_new( 0, &apiData->coder ); |
| 1664 |
if ( result < 0 ) { |
| 1665 |
data->doInput = false; |
| 1666 |
std::cerr << "\nMidiInAlsa::alsaMidiHandler: error initializing MIDI event parser!\n\n"; |
| 1667 |
return 0; |
| 1668 |
} |
| 1669 |
unsigned char *buffer = (unsigned char *) malloc( apiData->bufferSize ); |
| 1670 |
if ( buffer == NULL ) { |
| 1671 |
data->doInput = false; |
| 1672 |
snd_midi_event_free( apiData->coder ); |
| 1673 |
apiData->coder = 0; |
| 1674 |
std::cerr << "\nMidiInAlsa::alsaMidiHandler: error initializing buffer memory!\n\n"; |
| 1675 |
return 0; |
| 1676 |
} |
| 1677 |
snd_midi_event_init( apiData->coder ); |
| 1678 |
snd_midi_event_no_status( apiData->coder, 1 ); // suppress running status messages |
| 1679 |
|
| 1680 |
poll_fd_count = snd_seq_poll_descriptors_count( apiData->seq, POLLIN ) + 1; |
| 1681 |
poll_fds = (struct pollfd*)alloca( poll_fd_count * sizeof( struct pollfd )); |
| 1682 |
snd_seq_poll_descriptors( apiData->seq, poll_fds + 1, poll_fd_count - 1, POLLIN ); |
| 1683 |
poll_fds[0].fd = apiData->trigger_fds[0]; |
| 1684 |
poll_fds[0].events = POLLIN; |
| 1685 |
|
| 1686 |
while ( data->doInput ) { |
| 1687 |
|
| 1688 |
if ( snd_seq_event_input_pending( apiData->seq, 1 ) == 0 ) { |
| 1689 |
// No data pending |
| 1690 |
if ( poll( poll_fds, poll_fd_count, -1) >= 0 ) { |
| 1691 |
if ( poll_fds[0].revents & POLLIN ) { |
| 1692 |
bool dummy; |
| 1693 |
int res = read( poll_fds[0].fd, &dummy, sizeof(dummy) ); |
| 1694 |
(void) res; |
| 1695 |
} |
| 1696 |
} |
| 1697 |
continue; |
| 1698 |
} |
| 1699 |
|
| 1700 |
// If here, there should be data. |
| 1701 |
result = snd_seq_event_input( apiData->seq, &ev ); |
| 1702 |
if ( result == -ENOSPC ) { |
| 1703 |
std::cerr << "\nMidiInAlsa::alsaMidiHandler: MIDI input buffer overrun!\n\n"; |
| 1704 |
continue; |
| 1705 |
} |
| 1706 |
else if ( result <= 0 ) { |
| 1707 |
std::cerr << "\nMidiInAlsa::alsaMidiHandler: unknown MIDI input error!\n"; |
| 1708 |
perror("System reports"); |
| 1709 |
continue; |
| 1710 |
} |
| 1711 |
|
| 1712 |
// This is a bit weird, but we now have to decode an ALSA MIDI |
| 1713 |
// event (back) into MIDI bytes. We'll ignore non-MIDI types. |
| 1714 |
if ( !continueSysex ) message.bytes.clear(); |
| 1715 |
|
| 1716 |
doDecode = false; |
| 1717 |
switch ( ev->type ) { |
| 1718 |
|
| 1719 |
case SND_SEQ_EVENT_PORT_SUBSCRIBED: |
| 1720 |
#if defined(__RTMIDI_DEBUG__) |
| 1721 |
std::cerr << "MidiInAlsa::alsaMidiHandler: port connection made!\n"; |
| 1722 |
#endif |
| 1723 |
break; |
| 1724 |
|
| 1725 |
case SND_SEQ_EVENT_PORT_UNSUBSCRIBED: |
| 1726 |
#if defined(__RTMIDI_DEBUG__) |
| 1727 |
std::cerr << "MidiInAlsa::alsaMidiHandler: port connection has closed!\n"; |
| 1728 |
std::cerr << "sender = " << (int) ev->data.connect.sender.client << ":" |
| 1729 |
<< (int) ev->data.connect.sender.port |
| 1730 |
<< ", dest = " << (int) ev->data.connect.dest.client << ":" |
| 1731 |
<< (int) ev->data.connect.dest.port |
| 1732 |
<< std::endl; |
| 1733 |
#endif |
| 1734 |
break; |
| 1735 |
|
| 1736 |
case SND_SEQ_EVENT_QFRAME: // MIDI time code |
| 1737 |
if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true; |
| 1738 |
break; |
| 1739 |
|
| 1740 |
case SND_SEQ_EVENT_TICK: // 0xF9 ... MIDI timing tick |
| 1741 |
if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true; |
| 1742 |
break; |
| 1743 |
|
| 1744 |
case SND_SEQ_EVENT_CLOCK: // 0xF8 ... MIDI timing (clock) tick |
| 1745 |
if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true; |
| 1746 |
break; |
| 1747 |
|
| 1748 |
case SND_SEQ_EVENT_SENSING: // Active sensing |
| 1749 |
if ( !( data->ignoreFlags & 0x04 ) ) doDecode = true; |
| 1750 |
break; |
| 1751 |
|
| 1752 |
case SND_SEQ_EVENT_SYSEX: |
| 1753 |
if ( (data->ignoreFlags & 0x01) ) break; |
| 1754 |
if ( ev->data.ext.len > apiData->bufferSize ) { |
| 1755 |
apiData->bufferSize = ev->data.ext.len; |
| 1756 |
free( buffer ); |
| 1757 |
buffer = (unsigned char *) malloc( apiData->bufferSize ); |
| 1758 |
if ( buffer == NULL ) { |
| 1759 |
data->doInput = false; |
| 1760 |
std::cerr << "\nMidiInAlsa::alsaMidiHandler: error resizing buffer memory!\n\n"; |
| 1761 |
break; |
| 1762 |
} |
| 1763 |
} |
| 1764 |
doDecode = true; |
| 1765 |
break; |
| 1766 |
|
| 1767 |
default: |
| 1768 |
doDecode = true; |
| 1769 |
} |
| 1770 |
|
| 1771 |
if ( doDecode ) { |
| 1772 |
|
| 1773 |
nBytes = snd_midi_event_decode( apiData->coder, buffer, apiData->bufferSize, ev ); |
| 1774 |
if ( nBytes > 0 ) { |
| 1775 |
// The ALSA sequencer has a maximum buffer size for MIDI sysex |
| 1776 |
// events of 256 bytes. If a device sends sysex messages larger |
| 1777 |
// than this, they are segmented into 256 byte chunks. So, |
| 1778 |
// we'll watch for this and concatenate sysex chunks into a |
| 1779 |
// single sysex message if necessary. |
| 1780 |
if ( !continueSysex ) |
| 1781 |
message.bytes.assign( buffer, &buffer[nBytes] ); |
| 1782 |
else |
| 1783 |
message.bytes.insert( message.bytes.end(), buffer, &buffer[nBytes] ); |
| 1784 |
|
| 1785 |
continueSysex = ( ( ev->type == SND_SEQ_EVENT_SYSEX ) && ( message.bytes.back() != 0xF7 ) ); |
| 1786 |
if ( !continueSysex ) { |
| 1787 |
|
| 1788 |
// Calculate the time stamp: |
| 1789 |
message.timeStamp = 0.0; |
| 1790 |
|
| 1791 |
// Method 1: Use the system time. |
| 1792 |
//(void)gettimeofday(&tv, (struct timezone *)NULL); |
| 1793 |
//time = (tv.tv_sec * 1000000) + tv.tv_usec; |
| 1794 |
|
| 1795 |
// Method 2: Use the ALSA sequencer event time data. |
| 1796 |
// (thanks to Pedro Lopez-Cabanillas!). |
| 1797 |
|
| 1798 |
// Using method from: |
| 1799 |
// https://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html |
| 1800 |
|
| 1801 |
// Perform the carry for the later subtraction by updating y. |
| 1802 |
// Temp var y is timespec because computation requires signed types, |
| 1803 |
// while snd_seq_real_time_t has unsigned types. |
| 1804 |
snd_seq_real_time_t &x( ev->time.time ); |
| 1805 |
struct timespec y; |
| 1806 |
y.tv_nsec = apiData->lastTime.tv_nsec; |
| 1807 |
y.tv_sec = apiData->lastTime.tv_sec; |
| 1808 |
if ( (long int) x.tv_nsec < y.tv_nsec ) { |
| 1809 |
int nsec = (y.tv_nsec - (int)x.tv_nsec) / 1000000000 + 1; |
| 1810 |
y.tv_nsec -= 1000000000 * nsec; |
| 1811 |
y.tv_sec += nsec; |
| 1812 |
} |
| 1813 |
if ( x.tv_nsec - y.tv_nsec > 1000000000 ) { |
| 1814 |
int nsec = ((int)x.tv_nsec - y.tv_nsec) / 1000000000; |
| 1815 |
y.tv_nsec += 1000000000 * nsec; |
| 1816 |
y.tv_sec -= nsec; |
| 1817 |
} |
| 1818 |
|
| 1819 |
// Compute the time difference. |
| 1820 |
time = (int)x.tv_sec - y.tv_sec + ((int)x.tv_nsec - y.tv_nsec)*1e-9; |
| 1821 |
|
| 1822 |
apiData->lastTime = ev->time.time; |
| 1823 |
|
| 1824 |
if ( data->firstMessage == true ) |
| 1825 |
data->firstMessage = false; |
| 1826 |
else |
| 1827 |
message.timeStamp = time; |
| 1828 |
} |
| 1829 |
else { |
| 1830 |
#if defined(__RTMIDI_DEBUG__) |
| 1831 |
std::cerr << "\nMidiInAlsa::alsaMidiHandler: event parsing error or not a MIDI event!\n\n"; |
| 1832 |
#endif |
| 1833 |
} |
| 1834 |
} |
| 1835 |
} |
| 1836 |
|
| 1837 |
snd_seq_free_event( ev ); |
| 1838 |
if ( message.bytes.size() == 0 || continueSysex ) continue; |
| 1839 |
|
| 1840 |
if ( data->usingCallback ) { |
| 1841 |
RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback; |
| 1842 |
callback( message.timeStamp, &message.bytes, data->userData ); |
| 1843 |
} |
| 1844 |
else { |
| 1845 |
// As long as we haven't reached our queue size limit, push the message. |
| 1846 |
if ( !data->queue.push( message ) ) |
| 1847 |
std::cerr << "\nMidiInAlsa: message queue limit reached!!\n\n"; |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
if ( buffer ) free( buffer ); |
| 1852 |
snd_midi_event_free( apiData->coder ); |
| 1853 |
apiData->coder = 0; |
| 1854 |
apiData->thread = apiData->dummy_thread_id; |
| 1855 |
return 0; |
| 1856 |
} |
| 1857 |
|
| 1858 |
MidiInAlsa :: MidiInAlsa( const std::string &clientName, unsigned int queueSizeLimit ) |
| 1859 |
: MidiInApi( queueSizeLimit ) |
| 1860 |
{ |
| 1861 |
MidiInAlsa::initialize( clientName ); |
| 1862 |
} |
| 1863 |
|
| 1864 |
MidiInAlsa :: ~MidiInAlsa() |
| 1865 |
{ |
| 1866 |
// Close a connection if it exists. |
| 1867 |
MidiInAlsa::closePort(); |
| 1868 |
|
| 1869 |
// Shutdown the input thread. |
| 1870 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 1871 |
if ( inputData_.doInput ) { |
| 1872 |
inputData_.doInput = false; |
| 1873 |
int res = write( data->trigger_fds[1], &inputData_.doInput, sizeof( inputData_.doInput ) ); |
| 1874 |
(void) res; |
| 1875 |
if ( !pthread_equal(data->thread, data->dummy_thread_id) ) |
| 1876 |
pthread_join( data->thread, NULL ); |
| 1877 |
} |
| 1878 |
|
| 1879 |
// Cleanup. |
| 1880 |
close ( data->trigger_fds[0] ); |
| 1881 |
close ( data->trigger_fds[1] ); |
| 1882 |
if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport ); |
| 1883 |
#ifndef AVOID_TIMESTAMPING |
| 1884 |
snd_seq_free_queue( data->seq, data->queue_id ); |
| 1885 |
#endif |
| 1886 |
snd_seq_close( data->seq ); |
| 1887 |
delete data; |
| 1888 |
} |
| 1889 |
|
| 1890 |
void MidiInAlsa :: initialize( const std::string& clientName ) |
| 1891 |
{ |
| 1892 |
// Set up the ALSA sequencer client. |
| 1893 |
snd_seq_t *seq; |
| 1894 |
int result = snd_seq_open( &seq, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK ); |
| 1895 |
if ( result < 0 ) { |
| 1896 |
errorString_ = "MidiInAlsa::initialize: error creating ALSA sequencer client object."; |
| 1897 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1898 |
return; |
| 1899 |
} |
| 1900 |
|
| 1901 |
// Set client name. |
| 1902 |
snd_seq_set_client_name( seq, clientName.c_str() ); |
| 1903 |
|
| 1904 |
// Save our api-specific connection information. |
| 1905 |
AlsaMidiData *data = (AlsaMidiData *) new AlsaMidiData; |
| 1906 |
data->seq = seq; |
| 1907 |
data->portNum = -1; |
| 1908 |
data->vport = -1; |
| 1909 |
data->subscription = 0; |
| 1910 |
data->dummy_thread_id = pthread_self(); |
| 1911 |
data->thread = data->dummy_thread_id; |
| 1912 |
data->trigger_fds[0] = -1; |
| 1913 |
data->trigger_fds[1] = -1; |
| 1914 |
data->bufferSize = inputData_.bufferSize; |
| 1915 |
apiData_ = (void *) data; |
| 1916 |
inputData_.apiData = (void *) data; |
| 1917 |
|
| 1918 |
if ( pipe(data->trigger_fds) == -1 ) { |
| 1919 |
errorString_ = "MidiInAlsa::initialize: error creating pipe objects."; |
| 1920 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 1921 |
return; |
| 1922 |
} |
| 1923 |
|
| 1924 |
// Create the input queue |
| 1925 |
#ifndef AVOID_TIMESTAMPING |
| 1926 |
data->queue_id = snd_seq_alloc_named_queue( seq, "RtMidi Queue" ); |
| 1927 |
// Set arbitrary tempo (mm=100) and resolution (240) |
| 1928 |
snd_seq_queue_tempo_t *qtempo; |
| 1929 |
snd_seq_queue_tempo_alloca( &qtempo ); |
| 1930 |
snd_seq_queue_tempo_set_tempo( qtempo, 600000 ); |
| 1931 |
snd_seq_queue_tempo_set_ppq( qtempo, 240 ); |
| 1932 |
snd_seq_set_queue_tempo( data->seq, data->queue_id, qtempo ); |
| 1933 |
snd_seq_drain_output( data->seq ); |
| 1934 |
#endif |
| 1935 |
} |
| 1936 |
|
| 1937 |
// This function is used to count or get the pinfo structure for a given port number. |
| 1938 |
unsigned int portInfo( snd_seq_t *seq, snd_seq_port_info_t *pinfo, unsigned int type, int portNumber ) |
| 1939 |
{ |
| 1940 |
snd_seq_client_info_t *cinfo; |
| 1941 |
int client; |
| 1942 |
int count = 0; |
| 1943 |
snd_seq_client_info_alloca( &cinfo ); |
| 1944 |
|
| 1945 |
snd_seq_client_info_set_client( cinfo, -1 ); |
| 1946 |
while ( snd_seq_query_next_client( seq, cinfo ) >= 0 ) { |
| 1947 |
client = snd_seq_client_info_get_client( cinfo ); |
| 1948 |
if ( client == 0 ) continue; |
| 1949 |
// Reset query info |
| 1950 |
snd_seq_port_info_set_client( pinfo, client ); |
| 1951 |
snd_seq_port_info_set_port( pinfo, -1 ); |
| 1952 |
while ( snd_seq_query_next_port( seq, pinfo ) >= 0 ) { |
| 1953 |
unsigned int atyp = snd_seq_port_info_get_type( pinfo ); |
| 1954 |
if ( ( ( atyp & SND_SEQ_PORT_TYPE_MIDI_GENERIC ) == 0 ) && |
| 1955 |
( ( atyp & SND_SEQ_PORT_TYPE_SYNTH ) == 0 ) && |
| 1956 |
( ( atyp & SND_SEQ_PORT_TYPE_APPLICATION ) == 0 ) ) continue; |
| 1957 |
|
| 1958 |
unsigned int caps = snd_seq_port_info_get_capability( pinfo ); |
| 1959 |
if ( ( caps & type ) != type ) continue; |
| 1960 |
if ( count == portNumber ) return 1; |
| 1961 |
++count; |
| 1962 |
} |
| 1963 |
} |
| 1964 |
|
| 1965 |
// If a negative portNumber was used, return the port count. |
| 1966 |
if ( portNumber < 0 ) return count; |
| 1967 |
return 0; |
| 1968 |
} |
| 1969 |
|
| 1970 |
unsigned int MidiInAlsa :: getPortCount() |
| 1971 |
{ |
| 1972 |
snd_seq_port_info_t *pinfo; |
| 1973 |
snd_seq_port_info_alloca( &pinfo ); |
| 1974 |
|
| 1975 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 1976 |
return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, -1 ); |
| 1977 |
} |
| 1978 |
|
| 1979 |
std::string MidiInAlsa :: getPortName( unsigned int portNumber ) |
| 1980 |
{ |
| 1981 |
snd_seq_client_info_t *cinfo; |
| 1982 |
snd_seq_port_info_t *pinfo; |
| 1983 |
snd_seq_client_info_alloca( &cinfo ); |
| 1984 |
snd_seq_port_info_alloca( &pinfo ); |
| 1985 |
|
| 1986 |
std::string stringName; |
| 1987 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 1988 |
if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) ) { |
| 1989 |
int cnum = snd_seq_port_info_get_client( pinfo ); |
| 1990 |
snd_seq_get_any_client_info( data->seq, cnum, cinfo ); |
| 1991 |
std::ostringstream os; |
| 1992 |
os << snd_seq_client_info_get_name( cinfo ); |
| 1993 |
os << ":"; |
| 1994 |
os << snd_seq_port_info_get_name( pinfo ); |
| 1995 |
os << " "; // These lines added to make sure devices are listed |
| 1996 |
os << snd_seq_port_info_get_client( pinfo ); // with full portnames added to ensure individual device names |
| 1997 |
os << ":"; |
| 1998 |
os << snd_seq_port_info_get_port( pinfo ); |
| 1999 |
stringName = os.str(); |
| 2000 |
return stringName; |
| 2001 |
} |
| 2002 |
|
| 2003 |
// If we get here, we didn't find a match. |
| 2004 |
errorString_ = "MidiInAlsa::getPortName: error looking for port name!"; |
| 2005 |
error( RtMidiError::WARNING, errorString_ ); |
| 2006 |
return stringName; |
| 2007 |
} |
| 2008 |
|
| 2009 |
void MidiInAlsa :: openPort( unsigned int portNumber, const std::string &portName ) |
| 2010 |
{ |
| 2011 |
if ( connected_ ) { |
| 2012 |
errorString_ = "MidiInAlsa::openPort: a valid connection already exists!"; |
| 2013 |
error( RtMidiError::WARNING, errorString_ ); |
| 2014 |
return; |
| 2015 |
} |
| 2016 |
|
| 2017 |
unsigned int nSrc = this->getPortCount(); |
| 2018 |
if ( nSrc < 1 ) { |
| 2019 |
errorString_ = "MidiInAlsa::openPort: no MIDI input sources found!"; |
| 2020 |
error( RtMidiError::NO_DEVICES_FOUND, errorString_ ); |
| 2021 |
return; |
| 2022 |
} |
| 2023 |
|
| 2024 |
snd_seq_port_info_t *src_pinfo; |
| 2025 |
snd_seq_port_info_alloca( &src_pinfo ); |
| 2026 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2027 |
if ( portInfo( data->seq, src_pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) == 0 ) { |
| 2028 |
std::ostringstream ost; |
| 2029 |
ost << "MidiInAlsa::openPort: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 2030 |
errorString_ = ost.str(); |
| 2031 |
error( RtMidiError::INVALID_PARAMETER, errorString_ ); |
| 2032 |
return; |
| 2033 |
} |
| 2034 |
|
| 2035 |
snd_seq_addr_t sender, receiver; |
| 2036 |
sender.client = snd_seq_port_info_get_client( src_pinfo ); |
| 2037 |
sender.port = snd_seq_port_info_get_port( src_pinfo ); |
| 2038 |
receiver.client = snd_seq_client_id( data->seq ); |
| 2039 |
|
| 2040 |
snd_seq_port_info_t *pinfo; |
| 2041 |
snd_seq_port_info_alloca( &pinfo ); |
| 2042 |
if ( data->vport < 0 ) { |
| 2043 |
snd_seq_port_info_set_client( pinfo, 0 ); |
| 2044 |
snd_seq_port_info_set_port( pinfo, 0 ); |
| 2045 |
snd_seq_port_info_set_capability( pinfo, |
| 2046 |
SND_SEQ_PORT_CAP_WRITE | |
| 2047 |
SND_SEQ_PORT_CAP_SUBS_WRITE ); |
| 2048 |
snd_seq_port_info_set_type( pinfo, |
| 2049 |
SND_SEQ_PORT_TYPE_MIDI_GENERIC | |
| 2050 |
SND_SEQ_PORT_TYPE_APPLICATION ); |
| 2051 |
snd_seq_port_info_set_midi_channels(pinfo, 16); |
| 2052 |
#ifndef AVOID_TIMESTAMPING |
| 2053 |
snd_seq_port_info_set_timestamping( pinfo, 1 ); |
| 2054 |
snd_seq_port_info_set_timestamp_real( pinfo, 1 ); |
| 2055 |
snd_seq_port_info_set_timestamp_queue( pinfo, data->queue_id ); |
| 2056 |
#endif |
| 2057 |
snd_seq_port_info_set_name( pinfo, portName.c_str() ); |
| 2058 |
data->vport = snd_seq_create_port( data->seq, pinfo ); |
| 2059 |
|
| 2060 |
if ( data->vport < 0 ) { |
| 2061 |
errorString_ = "MidiInAlsa::openPort: ALSA error creating input port."; |
| 2062 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2063 |
return; |
| 2064 |
} |
| 2065 |
data->vport = snd_seq_port_info_get_port( pinfo ); |
| 2066 |
} |
| 2067 |
|
| 2068 |
receiver.port = data->vport; |
| 2069 |
|
| 2070 |
if ( !data->subscription ) { |
| 2071 |
// Make subscription |
| 2072 |
if ( snd_seq_port_subscribe_malloc( &data->subscription ) < 0 ) { |
| 2073 |
errorString_ = "MidiInAlsa::openPort: ALSA error allocation port subscription."; |
| 2074 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2075 |
return; |
| 2076 |
} |
| 2077 |
snd_seq_port_subscribe_set_sender( data->subscription, &sender ); |
| 2078 |
snd_seq_port_subscribe_set_dest( data->subscription, &receiver ); |
| 2079 |
if ( snd_seq_subscribe_port( data->seq, data->subscription ) ) { |
| 2080 |
snd_seq_port_subscribe_free( data->subscription ); |
| 2081 |
data->subscription = 0; |
| 2082 |
errorString_ = "MidiInAlsa::openPort: ALSA error making port connection."; |
| 2083 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2084 |
return; |
| 2085 |
} |
| 2086 |
} |
| 2087 |
|
| 2088 |
if ( inputData_.doInput == false ) { |
| 2089 |
// Start the input queue |
| 2090 |
#ifndef AVOID_TIMESTAMPING |
| 2091 |
snd_seq_start_queue( data->seq, data->queue_id, NULL ); |
| 2092 |
snd_seq_drain_output( data->seq ); |
| 2093 |
#endif |
| 2094 |
// Start our MIDI input thread. |
| 2095 |
pthread_attr_t attr; |
| 2096 |
pthread_attr_init( &attr ); |
| 2097 |
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); |
| 2098 |
pthread_attr_setschedpolicy( &attr, SCHED_OTHER ); |
| 2099 |
|
| 2100 |
inputData_.doInput = true; |
| 2101 |
int err = pthread_create( &data->thread, &attr, alsaMidiHandler, &inputData_ ); |
| 2102 |
pthread_attr_destroy( &attr ); |
| 2103 |
if ( err ) { |
| 2104 |
snd_seq_unsubscribe_port( data->seq, data->subscription ); |
| 2105 |
snd_seq_port_subscribe_free( data->subscription ); |
| 2106 |
data->subscription = 0; |
| 2107 |
inputData_.doInput = false; |
| 2108 |
errorString_ = "MidiInAlsa::openPort: error starting MIDI input thread!"; |
| 2109 |
error( RtMidiError::THREAD_ERROR, errorString_ ); |
| 2110 |
return; |
| 2111 |
} |
| 2112 |
} |
| 2113 |
|
| 2114 |
connected_ = true; |
| 2115 |
} |
| 2116 |
|
| 2117 |
void MidiInAlsa :: openVirtualPort( const std::string &portName ) |
| 2118 |
{ |
| 2119 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2120 |
if ( data->vport < 0 ) { |
| 2121 |
snd_seq_port_info_t *pinfo; |
| 2122 |
snd_seq_port_info_alloca( &pinfo ); |
| 2123 |
snd_seq_port_info_set_capability( pinfo, |
| 2124 |
SND_SEQ_PORT_CAP_WRITE | |
| 2125 |
SND_SEQ_PORT_CAP_SUBS_WRITE ); |
| 2126 |
snd_seq_port_info_set_type( pinfo, |
| 2127 |
SND_SEQ_PORT_TYPE_MIDI_GENERIC | |
| 2128 |
SND_SEQ_PORT_TYPE_APPLICATION ); |
| 2129 |
snd_seq_port_info_set_midi_channels( pinfo, 16 ); |
| 2130 |
#ifndef AVOID_TIMESTAMPING |
| 2131 |
snd_seq_port_info_set_timestamping( pinfo, 1 ); |
| 2132 |
snd_seq_port_info_set_timestamp_real( pinfo, 1 ); |
| 2133 |
snd_seq_port_info_set_timestamp_queue( pinfo, data->queue_id ); |
| 2134 |
#endif |
| 2135 |
snd_seq_port_info_set_name( pinfo, portName.c_str() ); |
| 2136 |
data->vport = snd_seq_create_port( data->seq, pinfo ); |
| 2137 |
|
| 2138 |
if ( data->vport < 0 ) { |
| 2139 |
errorString_ = "MidiInAlsa::openVirtualPort: ALSA error creating virtual port."; |
| 2140 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2141 |
return; |
| 2142 |
} |
| 2143 |
data->vport = snd_seq_port_info_get_port( pinfo ); |
| 2144 |
} |
| 2145 |
|
| 2146 |
if ( inputData_.doInput == false ) { |
| 2147 |
// Wait for old thread to stop, if still running |
| 2148 |
if ( !pthread_equal( data->thread, data->dummy_thread_id ) ) |
| 2149 |
pthread_join( data->thread, NULL ); |
| 2150 |
|
| 2151 |
// Start the input queue |
| 2152 |
#ifndef AVOID_TIMESTAMPING |
| 2153 |
snd_seq_start_queue( data->seq, data->queue_id, NULL ); |
| 2154 |
snd_seq_drain_output( data->seq ); |
| 2155 |
#endif |
| 2156 |
// Start our MIDI input thread. |
| 2157 |
pthread_attr_t attr; |
| 2158 |
pthread_attr_init( &attr ); |
| 2159 |
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); |
| 2160 |
pthread_attr_setschedpolicy( &attr, SCHED_OTHER ); |
| 2161 |
|
| 2162 |
inputData_.doInput = true; |
| 2163 |
int err = pthread_create( &data->thread, &attr, alsaMidiHandler, &inputData_ ); |
| 2164 |
pthread_attr_destroy( &attr ); |
| 2165 |
if ( err ) { |
| 2166 |
if ( data->subscription ) { |
| 2167 |
snd_seq_unsubscribe_port( data->seq, data->subscription ); |
| 2168 |
snd_seq_port_subscribe_free( data->subscription ); |
| 2169 |
data->subscription = 0; |
| 2170 |
} |
| 2171 |
inputData_.doInput = false; |
| 2172 |
errorString_ = "MidiInAlsa::openPort: error starting MIDI input thread!"; |
| 2173 |
error( RtMidiError::THREAD_ERROR, errorString_ ); |
| 2174 |
return; |
| 2175 |
} |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
void MidiInAlsa :: closePort( void ) |
| 2180 |
{ |
| 2181 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2182 |
|
| 2183 |
if ( connected_ ) { |
| 2184 |
if ( data->subscription ) { |
| 2185 |
snd_seq_unsubscribe_port( data->seq, data->subscription ); |
| 2186 |
snd_seq_port_subscribe_free( data->subscription ); |
| 2187 |
data->subscription = 0; |
| 2188 |
} |
| 2189 |
// Stop the input queue |
| 2190 |
#ifndef AVOID_TIMESTAMPING |
| 2191 |
snd_seq_stop_queue( data->seq, data->queue_id, NULL ); |
| 2192 |
snd_seq_drain_output( data->seq ); |
| 2193 |
#endif |
| 2194 |
connected_ = false; |
| 2195 |
} |
| 2196 |
|
| 2197 |
// Stop thread to avoid triggering the callback, while the port is intended to be closed |
| 2198 |
if ( inputData_.doInput ) { |
| 2199 |
inputData_.doInput = false; |
| 2200 |
int res = write( data->trigger_fds[1], &inputData_.doInput, sizeof( inputData_.doInput ) ); |
| 2201 |
(void) res; |
| 2202 |
if ( !pthread_equal( data->thread, data->dummy_thread_id ) ) |
| 2203 |
pthread_join( data->thread, NULL ); |
| 2204 |
} |
| 2205 |
} |
| 2206 |
|
| 2207 |
void MidiInAlsa :: setClientName( const std::string &clientName ) |
| 2208 |
{ |
| 2209 |
|
| 2210 |
AlsaMidiData *data = static_cast<AlsaMidiData *> ( apiData_ ); |
| 2211 |
snd_seq_set_client_name( data->seq, clientName.c_str() ); |
| 2212 |
|
| 2213 |
} |
| 2214 |
|
| 2215 |
void MidiInAlsa :: setPortName( const std::string &portName ) |
| 2216 |
{ |
| 2217 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2218 |
snd_seq_port_info_t *pinfo; |
| 2219 |
snd_seq_port_info_alloca( &pinfo ); |
| 2220 |
snd_seq_get_port_info( data->seq, data->vport, pinfo ); |
| 2221 |
snd_seq_port_info_set_name( pinfo, portName.c_str() ); |
| 2222 |
snd_seq_set_port_info( data->seq, data->vport, pinfo ); |
| 2223 |
} |
| 2224 |
|
| 2225 |
//*********************************************************************// |
| 2226 |
// API: LINUX ALSA |
| 2227 |
// Class Definitions: MidiOutAlsa |
| 2228 |
//*********************************************************************// |
| 2229 |
|
| 2230 |
MidiOutAlsa :: MidiOutAlsa( const std::string &clientName ) : MidiOutApi() |
| 2231 |
{ |
| 2232 |
MidiOutAlsa::initialize( clientName ); |
| 2233 |
} |
| 2234 |
|
| 2235 |
MidiOutAlsa :: ~MidiOutAlsa() |
| 2236 |
{ |
| 2237 |
// Close a connection if it exists. |
| 2238 |
MidiOutAlsa::closePort(); |
| 2239 |
|
| 2240 |
// Cleanup. |
| 2241 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2242 |
if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport ); |
| 2243 |
if ( data->coder ) snd_midi_event_free( data->coder ); |
| 2244 |
if ( data->buffer ) free( data->buffer ); |
| 2245 |
snd_seq_close( data->seq ); |
| 2246 |
delete data; |
| 2247 |
} |
| 2248 |
|
| 2249 |
void MidiOutAlsa :: initialize( const std::string& clientName ) |
| 2250 |
{ |
| 2251 |
// Set up the ALSA sequencer client. |
| 2252 |
snd_seq_t *seq; |
| 2253 |
int result1 = snd_seq_open( &seq, "default", SND_SEQ_OPEN_OUTPUT, SND_SEQ_NONBLOCK ); |
| 2254 |
if ( result1 < 0 ) { |
| 2255 |
errorString_ = "MidiOutAlsa::initialize: error creating ALSA sequencer client object."; |
| 2256 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2257 |
return; |
| 2258 |
} |
| 2259 |
|
| 2260 |
// Set client name. |
| 2261 |
snd_seq_set_client_name( seq, clientName.c_str() ); |
| 2262 |
|
| 2263 |
// Save our api-specific connection information. |
| 2264 |
AlsaMidiData *data = (AlsaMidiData *) new AlsaMidiData; |
| 2265 |
data->seq = seq; |
| 2266 |
data->portNum = -1; |
| 2267 |
data->vport = -1; |
| 2268 |
data->bufferSize = 32; |
| 2269 |
data->coder = 0; |
| 2270 |
data->buffer = 0; |
| 2271 |
int result = snd_midi_event_new( data->bufferSize, &data->coder ); |
| 2272 |
if ( result < 0 ) { |
| 2273 |
delete data; |
| 2274 |
errorString_ = "MidiOutAlsa::initialize: error initializing MIDI event parser!\n\n"; |
| 2275 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2276 |
return; |
| 2277 |
} |
| 2278 |
data->buffer = (unsigned char *) malloc( data->bufferSize ); |
| 2279 |
if ( data->buffer == NULL ) { |
| 2280 |
delete data; |
| 2281 |
errorString_ = "MidiOutAlsa::initialize: error allocating buffer memory!\n\n"; |
| 2282 |
error( RtMidiError::MEMORY_ERROR, errorString_ ); |
| 2283 |
return; |
| 2284 |
} |
| 2285 |
snd_midi_event_init( data->coder ); |
| 2286 |
apiData_ = (void *) data; |
| 2287 |
} |
| 2288 |
|
| 2289 |
unsigned int MidiOutAlsa :: getPortCount() |
| 2290 |
{ |
| 2291 |
snd_seq_port_info_t *pinfo; |
| 2292 |
snd_seq_port_info_alloca( &pinfo ); |
| 2293 |
|
| 2294 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2295 |
return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, -1 ); |
| 2296 |
} |
| 2297 |
|
| 2298 |
std::string MidiOutAlsa :: getPortName( unsigned int portNumber ) |
| 2299 |
{ |
| 2300 |
snd_seq_client_info_t *cinfo; |
| 2301 |
snd_seq_port_info_t *pinfo; |
| 2302 |
snd_seq_client_info_alloca( &cinfo ); |
| 2303 |
snd_seq_port_info_alloca( &pinfo ); |
| 2304 |
|
| 2305 |
std::string stringName; |
| 2306 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2307 |
if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) ) { |
| 2308 |
int cnum = snd_seq_port_info_get_client( pinfo ); |
| 2309 |
snd_seq_get_any_client_info( data->seq, cnum, cinfo ); |
| 2310 |
std::ostringstream os; |
| 2311 |
os << snd_seq_client_info_get_name( cinfo ); |
| 2312 |
os << ":"; |
| 2313 |
os << snd_seq_port_info_get_name( pinfo ); |
| 2314 |
os << " "; // These lines added to make sure devices are listed |
| 2315 |
os << snd_seq_port_info_get_client( pinfo ); // with full portnames added to ensure individual device names |
| 2316 |
os << ":"; |
| 2317 |
os << snd_seq_port_info_get_port( pinfo ); |
| 2318 |
stringName = os.str(); |
| 2319 |
return stringName; |
| 2320 |
} |
| 2321 |
|
| 2322 |
// If we get here, we didn't find a match. |
| 2323 |
errorString_ = "MidiOutAlsa::getPortName: error looking for port name!"; |
| 2324 |
error( RtMidiError::WARNING, errorString_ ); |
| 2325 |
return stringName; |
| 2326 |
} |
| 2327 |
|
| 2328 |
void MidiOutAlsa :: openPort( unsigned int portNumber, const std::string &portName ) |
| 2329 |
{ |
| 2330 |
if ( connected_ ) { |
| 2331 |
errorString_ = "MidiOutAlsa::openPort: a valid connection already exists!"; |
| 2332 |
error( RtMidiError::WARNING, errorString_ ); |
| 2333 |
return; |
| 2334 |
} |
| 2335 |
|
| 2336 |
unsigned int nSrc = this->getPortCount(); |
| 2337 |
if ( nSrc < 1 ) { |
| 2338 |
errorString_ = "MidiOutAlsa::openPort: no MIDI output sources found!"; |
| 2339 |
error( RtMidiError::NO_DEVICES_FOUND, errorString_ ); |
| 2340 |
return; |
| 2341 |
} |
| 2342 |
|
| 2343 |
snd_seq_port_info_t *pinfo; |
| 2344 |
snd_seq_port_info_alloca( &pinfo ); |
| 2345 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2346 |
if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) == 0 ) { |
| 2347 |
std::ostringstream ost; |
| 2348 |
ost << "MidiOutAlsa::openPort: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 2349 |
errorString_ = ost.str(); |
| 2350 |
error( RtMidiError::INVALID_PARAMETER, errorString_ ); |
| 2351 |
return; |
| 2352 |
} |
| 2353 |
|
| 2354 |
snd_seq_addr_t sender, receiver; |
| 2355 |
receiver.client = snd_seq_port_info_get_client( pinfo ); |
| 2356 |
receiver.port = snd_seq_port_info_get_port( pinfo ); |
| 2357 |
sender.client = snd_seq_client_id( data->seq ); |
| 2358 |
|
| 2359 |
if ( data->vport < 0 ) { |
| 2360 |
data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(), |
| 2361 |
SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, |
| 2362 |
SND_SEQ_PORT_TYPE_MIDI_GENERIC|SND_SEQ_PORT_TYPE_APPLICATION ); |
| 2363 |
if ( data->vport < 0 ) { |
| 2364 |
errorString_ = "MidiOutAlsa::openPort: ALSA error creating output port."; |
| 2365 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2366 |
return; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
sender.port = data->vport; |
| 2371 |
|
| 2372 |
// Make subscription |
| 2373 |
if ( snd_seq_port_subscribe_malloc( &data->subscription ) < 0 ) { |
| 2374 |
snd_seq_port_subscribe_free( data->subscription ); |
| 2375 |
errorString_ = "MidiOutAlsa::openPort: error allocating port subscription."; |
| 2376 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2377 |
return; |
| 2378 |
} |
| 2379 |
snd_seq_port_subscribe_set_sender( data->subscription, &sender ); |
| 2380 |
snd_seq_port_subscribe_set_dest( data->subscription, &receiver ); |
| 2381 |
snd_seq_port_subscribe_set_time_update( data->subscription, 1 ); |
| 2382 |
snd_seq_port_subscribe_set_time_real( data->subscription, 1 ); |
| 2383 |
if ( snd_seq_subscribe_port( data->seq, data->subscription ) ) { |
| 2384 |
snd_seq_port_subscribe_free( data->subscription ); |
| 2385 |
errorString_ = "MidiOutAlsa::openPort: ALSA error making port connection."; |
| 2386 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2387 |
return; |
| 2388 |
} |
| 2389 |
|
| 2390 |
connected_ = true; |
| 2391 |
} |
| 2392 |
|
| 2393 |
void MidiOutAlsa :: closePort( void ) |
| 2394 |
{ |
| 2395 |
if ( connected_ ) { |
| 2396 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2397 |
snd_seq_unsubscribe_port( data->seq, data->subscription ); |
| 2398 |
snd_seq_port_subscribe_free( data->subscription ); |
| 2399 |
data->subscription = 0; |
| 2400 |
connected_ = false; |
| 2401 |
} |
| 2402 |
} |
| 2403 |
|
| 2404 |
void MidiOutAlsa :: setClientName( const std::string &clientName ) |
| 2405 |
{ |
| 2406 |
|
| 2407 |
AlsaMidiData *data = static_cast<AlsaMidiData *> ( apiData_ ); |
| 2408 |
snd_seq_set_client_name( data->seq, clientName.c_str() ); |
| 2409 |
|
| 2410 |
} |
| 2411 |
|
| 2412 |
void MidiOutAlsa :: setPortName( const std::string &portName ) |
| 2413 |
{ |
| 2414 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2415 |
snd_seq_port_info_t *pinfo; |
| 2416 |
snd_seq_port_info_alloca( &pinfo ); |
| 2417 |
snd_seq_get_port_info( data->seq, data->vport, pinfo ); |
| 2418 |
snd_seq_port_info_set_name( pinfo, portName.c_str() ); |
| 2419 |
snd_seq_set_port_info( data->seq, data->vport, pinfo ); |
| 2420 |
} |
| 2421 |
|
| 2422 |
void MidiOutAlsa :: openVirtualPort( const std::string &portName ) |
| 2423 |
{ |
| 2424 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2425 |
if ( data->vport < 0 ) { |
| 2426 |
data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(), |
| 2427 |
SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, |
| 2428 |
SND_SEQ_PORT_TYPE_MIDI_GENERIC|SND_SEQ_PORT_TYPE_APPLICATION ); |
| 2429 |
|
| 2430 |
if ( data->vport < 0 ) { |
| 2431 |
errorString_ = "MidiOutAlsa::openVirtualPort: ALSA error creating virtual port."; |
| 2432 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2433 |
} |
| 2434 |
} |
| 2435 |
} |
| 2436 |
|
| 2437 |
void MidiOutAlsa :: sendMessage( const unsigned char *message, size_t size ) |
| 2438 |
{ |
| 2439 |
long result; |
| 2440 |
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_); |
| 2441 |
unsigned int nBytes = static_cast<unsigned int> (size); |
| 2442 |
if ( nBytes > data->bufferSize ) { |
| 2443 |
data->bufferSize = nBytes; |
| 2444 |
result = snd_midi_event_resize_buffer( data->coder, nBytes ); |
| 2445 |
if ( result != 0 ) { |
| 2446 |
errorString_ = "MidiOutAlsa::sendMessage: ALSA error resizing MIDI event buffer."; |
| 2447 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2448 |
return; |
| 2449 |
} |
| 2450 |
free (data->buffer); |
| 2451 |
data->buffer = (unsigned char *) malloc( data->bufferSize ); |
| 2452 |
if ( data->buffer == NULL ) { |
| 2453 |
errorString_ = "MidiOutAlsa::initialize: error allocating buffer memory!\n\n"; |
| 2454 |
error( RtMidiError::MEMORY_ERROR, errorString_ ); |
| 2455 |
return; |
| 2456 |
} |
| 2457 |
} |
| 2458 |
|
| 2459 |
for ( unsigned int i=0; i<nBytes; ++i ) data->buffer[i] = message[i]; |
| 2460 |
|
| 2461 |
unsigned int offset = 0; |
| 2462 |
while (offset < nBytes) { |
| 2463 |
snd_seq_event_t ev; |
| 2464 |
snd_seq_ev_clear( &ev ); |
| 2465 |
snd_seq_ev_set_source( &ev, data->vport ); |
| 2466 |
snd_seq_ev_set_subs( &ev ); |
| 2467 |
snd_seq_ev_set_direct( &ev ); |
| 2468 |
result = snd_midi_event_encode( data->coder, data->buffer + offset, |
| 2469 |
(long)(nBytes - offset), &ev ); |
| 2470 |
if ( result < 0 ) { |
| 2471 |
errorString_ = "MidiOutAlsa::sendMessage: event parsing error!"; |
| 2472 |
error( RtMidiError::WARNING, errorString_ ); |
| 2473 |
return; |
| 2474 |
} |
| 2475 |
|
| 2476 |
if ( ev.type == SND_SEQ_EVENT_NONE ) { |
| 2477 |
errorString_ = "MidiOutAlsa::sendMessage: incomplete message!"; |
| 2478 |
error( RtMidiError::WARNING, errorString_ ); |
| 2479 |
return; |
| 2480 |
} |
| 2481 |
|
| 2482 |
offset += result; |
| 2483 |
|
| 2484 |
// Send the event. |
| 2485 |
result = snd_seq_event_output( data->seq, &ev ); |
| 2486 |
if ( result < 0 ) { |
| 2487 |
errorString_ = "MidiOutAlsa::sendMessage: error sending MIDI message to port."; |
| 2488 |
error( RtMidiError::WARNING, errorString_ ); |
| 2489 |
return; |
| 2490 |
} |
| 2491 |
} |
| 2492 |
snd_seq_drain_output( data->seq ); |
| 2493 |
} |
| 2494 |
|
| 2495 |
#endif // __LINUX_ALSA__ |
| 2496 |
|
| 2497 |
|
| 2498 |
//*********************************************************************// |
| 2499 |
// API: Windows Multimedia Library (MM) |
| 2500 |
//*********************************************************************// |
| 2501 |
|
| 2502 |
// API information deciphered from: |
| 2503 |
// - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_midi_reference.asp |
| 2504 |
|
| 2505 |
// Thanks to Jean-Baptiste Berruchon for the sysex code. |
| 2506 |
|
| 2507 |
#if defined(__WINDOWS_MM__) |
| 2508 |
|
| 2509 |
// The Windows MM API is based on the use of a callback function for |
| 2510 |
// MIDI input. We convert the system specific time stamps to delta |
| 2511 |
// time values. |
| 2512 |
|
| 2513 |
// Windows MM MIDI header files. |
| 2514 |
#include <windows.h> |
| 2515 |
#include <mmsystem.h> |
| 2516 |
|
| 2517 |
// Convert a null-terminated wide string or ANSI-encoded string to UTF-8. |
| 2518 |
static std::string ConvertToUTF8(const TCHAR *str) |
| 2519 |
{ |
| 2520 |
std::string u8str; |
| 2521 |
const WCHAR *wstr = L""; |
| 2522 |
#if defined( UNICODE ) || defined( _UNICODE ) |
| 2523 |
wstr = str; |
| 2524 |
#else |
| 2525 |
// Convert from ANSI encoding to wide string |
| 2526 |
int wlength = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 ); |
| 2527 |
std::wstring wstrtemp; |
| 2528 |
if ( wlength ) |
| 2529 |
{ |
| 2530 |
wstrtemp.assign( wlength - 1, 0 ); |
| 2531 |
MultiByteToWideChar( CP_ACP, 0, str, -1, &wstrtemp[0], wlength ); |
| 2532 |
wstr = &wstrtemp[0]; |
| 2533 |
} |
| 2534 |
#endif |
| 2535 |
// Convert from wide string to UTF-8 |
| 2536 |
int length = WideCharToMultiByte( CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL ); |
| 2537 |
if ( length ) |
| 2538 |
{ |
| 2539 |
u8str.assign( length - 1, 0 ); |
| 2540 |
length = WideCharToMultiByte( CP_UTF8, 0, wstr, -1, &u8str[0], length, NULL, NULL ); |
| 2541 |
} |
| 2542 |
return u8str; |
| 2543 |
} |
| 2544 |
|
| 2545 |
// A structure to hold variables related to the CoreMIDI API |
| 2546 |
// implementation. |
| 2547 |
struct WinMidiData { |
| 2548 |
HMIDIIN inHandle; // Handle to Midi Input Device |
| 2549 |
HMIDIOUT outHandle; // Handle to Midi Output Device |
| 2550 |
DWORD lastTime; |
| 2551 |
MidiInApi::MidiMessage message; |
| 2552 |
std::vector<LPMIDIHDR> sysexBuffer; |
| 2553 |
CRITICAL_SECTION _mutex; // [Patrice] see https://groups.google.com/forum/#!topic/mididev/6OUjHutMpEo |
| 2554 |
}; |
| 2555 |
|
| 2556 |
//*********************************************************************// |
| 2557 |
// API: Windows MM |
| 2558 |
// Class Definitions: MidiInWinMM |
| 2559 |
//*********************************************************************// |
| 2560 |
|
| 2561 |
static void CALLBACK midiInputCallback( HMIDIIN /*hmin*/, |
| 2562 |
UINT inputStatus, |
| 2563 |
DWORD_PTR instancePtr, |
| 2564 |
DWORD_PTR midiMessage, |
| 2565 |
DWORD timestamp ) |
| 2566 |
{ |
| 2567 |
if ( inputStatus != MIM_DATA && inputStatus != MIM_LONGDATA && inputStatus != MIM_LONGERROR ) return; |
| 2568 |
|
| 2569 |
//MidiInApi::RtMidiInData *data = static_cast<MidiInApi::RtMidiInData *> (instancePtr); |
| 2570 |
MidiInApi::RtMidiInData *data = (MidiInApi::RtMidiInData *)instancePtr; |
| 2571 |
WinMidiData *apiData = static_cast<WinMidiData *> (data->apiData); |
| 2572 |
|
| 2573 |
// Calculate time stamp. |
| 2574 |
if ( data->firstMessage == true ) { |
| 2575 |
apiData->message.timeStamp = 0.0; |
| 2576 |
data->firstMessage = false; |
| 2577 |
} |
| 2578 |
else apiData->message.timeStamp = (double) ( timestamp - apiData->lastTime ) * 0.001; |
| 2579 |
|
| 2580 |
if ( inputStatus == MIM_DATA ) { // Channel or system message |
| 2581 |
|
| 2582 |
// Make sure the first byte is a status byte. |
| 2583 |
unsigned char status = (unsigned char) (midiMessage & 0x000000FF); |
| 2584 |
if ( !(status & 0x80) ) return; |
| 2585 |
|
| 2586 |
// Determine the number of bytes in the MIDI message. |
| 2587 |
unsigned short nBytes = 1; |
| 2588 |
if ( status < 0xC0 ) nBytes = 3; |
| 2589 |
else if ( status < 0xE0 ) nBytes = 2; |
| 2590 |
else if ( status < 0xF0 ) nBytes = 3; |
| 2591 |
else if ( status == 0xF1 ) { |
| 2592 |
if ( data->ignoreFlags & 0x02 ) return; |
| 2593 |
else nBytes = 2; |
| 2594 |
} |
| 2595 |
else if ( status == 0xF2 ) nBytes = 3; |
| 2596 |
else if ( status == 0xF3 ) nBytes = 2; |
| 2597 |
else if ( status == 0xF8 && ( data->ignoreFlags & 0x02 ) ) { |
| 2598 |
// A MIDI timing tick message and we're ignoring it. |
| 2599 |
return; |
| 2600 |
} |
| 2601 |
else if ( status == 0xFE && ( data->ignoreFlags & 0x04 ) ) { |
| 2602 |
// A MIDI active sensing message and we're ignoring it. |
| 2603 |
return; |
| 2604 |
} |
| 2605 |
|
| 2606 |
// Copy bytes to our MIDI message. |
| 2607 |
unsigned char *ptr = (unsigned char *) &midiMessage; |
| 2608 |
for ( int i=0; i<nBytes; ++i ) apiData->message.bytes.push_back( *ptr++ ); |
| 2609 |
} |
| 2610 |
else { // Sysex message ( MIM_LONGDATA or MIM_LONGERROR ) |
| 2611 |
MIDIHDR *sysex = ( MIDIHDR *) midiMessage; |
| 2612 |
if ( !( data->ignoreFlags & 0x01 ) && inputStatus != MIM_LONGERROR ) { |
| 2613 |
// Sysex message and we're not ignoring it |
| 2614 |
for ( int i=0; i<(int)sysex->dwBytesRecorded; ++i ) |
| 2615 |
apiData->message.bytes.push_back( sysex->lpData[i] ); |
| 2616 |
} |
| 2617 |
|
| 2618 |
// The WinMM API requires that the sysex buffer be requeued after |
| 2619 |
// input of each sysex message. Even if we are ignoring sysex |
| 2620 |
// messages, we still need to requeue the buffer in case the user |
| 2621 |
// decides to not ignore sysex messages in the future. However, |
| 2622 |
// it seems that WinMM calls this function with an empty sysex |
| 2623 |
// buffer when an application closes and in this case, we should |
| 2624 |
// avoid requeueing it, else the computer suddenly reboots after |
| 2625 |
// one or two minutes. |
| 2626 |
if ( apiData->sysexBuffer[sysex->dwUser]->dwBytesRecorded > 0 ) { |
| 2627 |
//if ( sysex->dwBytesRecorded > 0 ) { |
| 2628 |
EnterCriticalSection( &(apiData->_mutex) ); |
| 2629 |
MMRESULT result = midiInAddBuffer( apiData->inHandle, apiData->sysexBuffer[sysex->dwUser], sizeof(MIDIHDR) ); |
| 2630 |
LeaveCriticalSection( &(apiData->_mutex) ); |
| 2631 |
if ( result != MMSYSERR_NOERROR ) |
| 2632 |
std::cerr << "\nRtMidiIn::midiInputCallback: error sending sysex to Midi device!!\n\n"; |
| 2633 |
|
| 2634 |
if ( data->ignoreFlags & 0x01 ) return; |
| 2635 |
} |
| 2636 |
else return; |
| 2637 |
} |
| 2638 |
|
| 2639 |
// Save the time of the last non-filtered message |
| 2640 |
apiData->lastTime = timestamp; |
| 2641 |
|
| 2642 |
if ( data->usingCallback ) { |
| 2643 |
RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback; |
| 2644 |
callback( apiData->message.timeStamp, &apiData->message.bytes, data->userData ); |
| 2645 |
} |
| 2646 |
else { |
| 2647 |
// As long as we haven't reached our queue size limit, push the message. |
| 2648 |
if ( !data->queue.push( apiData->message ) ) |
| 2649 |
std::cerr << "\nMidiInWinMM: message queue limit reached!!\n\n"; |
| 2650 |
} |
| 2651 |
|
| 2652 |
// Clear the vector for the next input message. |
| 2653 |
apiData->message.bytes.clear(); |
| 2654 |
} |
| 2655 |
|
| 2656 |
MidiInWinMM :: MidiInWinMM( const std::string &clientName, unsigned int queueSizeLimit ) |
| 2657 |
: MidiInApi( queueSizeLimit ) |
| 2658 |
{ |
| 2659 |
MidiInWinMM::initialize( clientName ); |
| 2660 |
} |
| 2661 |
|
| 2662 |
MidiInWinMM :: ~MidiInWinMM() |
| 2663 |
{ |
| 2664 |
// Close a connection if it exists. |
| 2665 |
MidiInWinMM::closePort(); |
| 2666 |
|
| 2667 |
WinMidiData *data = static_cast<WinMidiData *> (apiData_); |
| 2668 |
DeleteCriticalSection( &(data->_mutex) ); |
| 2669 |
|
| 2670 |
// Cleanup. |
| 2671 |
delete data; |
| 2672 |
} |
| 2673 |
|
| 2674 |
void MidiInWinMM :: initialize( const std::string& /*clientName*/ ) |
| 2675 |
{ |
| 2676 |
// We'll issue a warning here if no devices are available but not |
| 2677 |
// throw an error since the user can plugin something later. |
| 2678 |
unsigned int nDevices = midiInGetNumDevs(); |
| 2679 |
if ( nDevices == 0 ) { |
| 2680 |
errorString_ = "MidiInWinMM::initialize: no MIDI input devices currently available."; |
| 2681 |
error( RtMidiError::WARNING, errorString_ ); |
| 2682 |
} |
| 2683 |
|
| 2684 |
// Save our api-specific connection information. |
| 2685 |
WinMidiData *data = (WinMidiData *) new WinMidiData; |
| 2686 |
apiData_ = (void *) data; |
| 2687 |
inputData_.apiData = (void *) data; |
| 2688 |
data->message.bytes.clear(); // needs to be empty for first input message |
| 2689 |
|
| 2690 |
if ( !InitializeCriticalSectionAndSpinCount( &(data->_mutex), 0x00000400 ) ) { |
| 2691 |
errorString_ = "MidiInWinMM::initialize: InitializeCriticalSectionAndSpinCount failed."; |
| 2692 |
error( RtMidiError::WARNING, errorString_ ); |
| 2693 |
} |
| 2694 |
} |
| 2695 |
|
| 2696 |
void MidiInWinMM :: openPort( unsigned int portNumber, const std::string &/*portName*/ ) |
| 2697 |
{ |
| 2698 |
if ( connected_ ) { |
| 2699 |
errorString_ = "MidiInWinMM::openPort: a valid connection already exists!"; |
| 2700 |
error( RtMidiError::WARNING, errorString_ ); |
| 2701 |
return; |
| 2702 |
} |
| 2703 |
|
| 2704 |
unsigned int nDevices = midiInGetNumDevs(); |
| 2705 |
if (nDevices == 0) { |
| 2706 |
errorString_ = "MidiInWinMM::openPort: no MIDI input sources found!"; |
| 2707 |
error( RtMidiError::NO_DEVICES_FOUND, errorString_ ); |
| 2708 |
return; |
| 2709 |
} |
| 2710 |
|
| 2711 |
if ( portNumber >= nDevices ) { |
| 2712 |
std::ostringstream ost; |
| 2713 |
ost << "MidiInWinMM::openPort: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 2714 |
errorString_ = ost.str(); |
| 2715 |
error( RtMidiError::INVALID_PARAMETER, errorString_ ); |
| 2716 |
return; |
| 2717 |
} |
| 2718 |
|
| 2719 |
WinMidiData *data = static_cast<WinMidiData *> (apiData_); |
| 2720 |
MMRESULT result = midiInOpen( &data->inHandle, |
| 2721 |
portNumber, |
| 2722 |
(DWORD_PTR)&midiInputCallback, |
| 2723 |
(DWORD_PTR)&inputData_, |
| 2724 |
CALLBACK_FUNCTION ); |
| 2725 |
if ( result != MMSYSERR_NOERROR ) { |
| 2726 |
errorString_ = "MidiInWinMM::openPort: error creating Windows MM MIDI input port."; |
| 2727 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2728 |
return; |
| 2729 |
} |
| 2730 |
|
| 2731 |
// Allocate and init the sysex buffers. |
| 2732 |
data->sysexBuffer.resize( inputData_.bufferCount ); |
| 2733 |
for ( unsigned int i=0; i < inputData_.bufferCount; ++i ) { |
| 2734 |
data->sysexBuffer[i] = (MIDIHDR*) new char[ sizeof(MIDIHDR) ]; |
| 2735 |
data->sysexBuffer[i]->lpData = new char[ inputData_.bufferSize ]; |
| 2736 |
data->sysexBuffer[i]->dwBufferLength = inputData_.bufferSize; |
| 2737 |
data->sysexBuffer[i]->dwUser = i; // We use the dwUser parameter as buffer indicator |
| 2738 |
data->sysexBuffer[i]->dwFlags = 0; |
| 2739 |
|
| 2740 |
result = midiInPrepareHeader( data->inHandle, data->sysexBuffer[i], sizeof(MIDIHDR) ); |
| 2741 |
if ( result != MMSYSERR_NOERROR ) { |
| 2742 |
midiInClose( data->inHandle ); |
| 2743 |
data->inHandle = 0; |
| 2744 |
errorString_ = "MidiInWinMM::openPort: error starting Windows MM MIDI input port (PrepareHeader)."; |
| 2745 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2746 |
return; |
| 2747 |
} |
| 2748 |
|
| 2749 |
// Register the buffer. |
| 2750 |
result = midiInAddBuffer( data->inHandle, data->sysexBuffer[i], sizeof(MIDIHDR) ); |
| 2751 |
if ( result != MMSYSERR_NOERROR ) { |
| 2752 |
midiInClose( data->inHandle ); |
| 2753 |
data->inHandle = 0; |
| 2754 |
errorString_ = "MidiInWinMM::openPort: error starting Windows MM MIDI input port (AddBuffer)."; |
| 2755 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2756 |
return; |
| 2757 |
} |
| 2758 |
} |
| 2759 |
|
| 2760 |
result = midiInStart( data->inHandle ); |
| 2761 |
if ( result != MMSYSERR_NOERROR ) { |
| 2762 |
midiInClose( data->inHandle ); |
| 2763 |
data->inHandle = 0; |
| 2764 |
errorString_ = "MidiInWinMM::openPort: error starting Windows MM MIDI input port."; |
| 2765 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2766 |
return; |
| 2767 |
} |
| 2768 |
|
| 2769 |
connected_ = true; |
| 2770 |
} |
| 2771 |
|
| 2772 |
void MidiInWinMM :: openVirtualPort( const std::string &/*portName*/ ) |
| 2773 |
{ |
| 2774 |
// This function cannot be implemented for the Windows MM MIDI API. |
| 2775 |
errorString_ = "MidiInWinMM::openVirtualPort: cannot be implemented in Windows MM MIDI API!"; |
| 2776 |
error( RtMidiError::WARNING, errorString_ ); |
| 2777 |
} |
| 2778 |
|
| 2779 |
void MidiInWinMM :: closePort( void ) |
| 2780 |
{ |
| 2781 |
if ( connected_ ) { |
| 2782 |
WinMidiData *data = static_cast<WinMidiData *> (apiData_); |
| 2783 |
EnterCriticalSection( &(data->_mutex) ); |
| 2784 |
midiInReset( data->inHandle ); |
| 2785 |
midiInStop( data->inHandle ); |
| 2786 |
|
| 2787 |
for ( size_t i=0; i < data->sysexBuffer.size(); ++i ) { |
| 2788 |
int result = midiInUnprepareHeader(data->inHandle, data->sysexBuffer[i], sizeof(MIDIHDR)); |
| 2789 |
delete [] data->sysexBuffer[i]->lpData; |
| 2790 |
delete [] data->sysexBuffer[i]; |
| 2791 |
if ( result != MMSYSERR_NOERROR ) { |
| 2792 |
midiInClose( data->inHandle ); |
| 2793 |
data->inHandle = 0; |
| 2794 |
errorString_ = "MidiInWinMM::openPort: error closing Windows MM MIDI input port (midiInUnprepareHeader)."; |
| 2795 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2796 |
return; |
| 2797 |
} |
| 2798 |
} |
| 2799 |
|
| 2800 |
midiInClose( data->inHandle ); |
| 2801 |
data->inHandle = 0; |
| 2802 |
connected_ = false; |
| 2803 |
LeaveCriticalSection( &(data->_mutex) ); |
| 2804 |
} |
| 2805 |
} |
| 2806 |
|
| 2807 |
void MidiInWinMM :: setClientName ( const std::string& ) |
| 2808 |
{ |
| 2809 |
|
| 2810 |
errorString_ = "MidiInWinMM::setClientName: this function is not implemented for the WINDOWS_MM API!"; |
| 2811 |
error( RtMidiError::WARNING, errorString_ ); |
| 2812 |
|
| 2813 |
} |
| 2814 |
|
| 2815 |
void MidiInWinMM :: setPortName ( const std::string& ) |
| 2816 |
{ |
| 2817 |
|
| 2818 |
errorString_ = "MidiInWinMM::setPortName: this function is not implemented for the WINDOWS_MM API!"; |
| 2819 |
error( RtMidiError::WARNING, errorString_ ); |
| 2820 |
|
| 2821 |
} |
| 2822 |
|
| 2823 |
unsigned int MidiInWinMM :: getPortCount() |
| 2824 |
{ |
| 2825 |
return midiInGetNumDevs(); |
| 2826 |
} |
| 2827 |
|
| 2828 |
std::string MidiInWinMM :: getPortName( unsigned int portNumber ) |
| 2829 |
{ |
| 2830 |
std::string stringName; |
| 2831 |
unsigned int nDevices = midiInGetNumDevs(); |
| 2832 |
if ( portNumber >= nDevices ) { |
| 2833 |
std::ostringstream ost; |
| 2834 |
ost << "MidiInWinMM::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 2835 |
errorString_ = ost.str(); |
| 2836 |
error( RtMidiError::WARNING, errorString_ ); |
| 2837 |
return stringName; |
| 2838 |
} |
| 2839 |
|
| 2840 |
MIDIINCAPS deviceCaps; |
| 2841 |
midiInGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIINCAPS)); |
| 2842 |
stringName = ConvertToUTF8( deviceCaps.szPname ); |
| 2843 |
|
| 2844 |
// Next lines added to add the portNumber to the name so that |
| 2845 |
// the device's names are sure to be listed with individual names |
| 2846 |
// even when they have the same brand name |
| 2847 |
#ifndef RTMIDI_DO_NOT_ENSURE_UNIQUE_PORTNAMES |
| 2848 |
std::ostringstream os; |
| 2849 |
os << " "; |
| 2850 |
os << portNumber; |
| 2851 |
stringName += os.str(); |
| 2852 |
#endif |
| 2853 |
|
| 2854 |
return stringName; |
| 2855 |
} |
| 2856 |
|
| 2857 |
//*********************************************************************// |
| 2858 |
// API: Windows MM |
| 2859 |
// Class Definitions: MidiOutWinMM |
| 2860 |
//*********************************************************************// |
| 2861 |
|
| 2862 |
MidiOutWinMM :: MidiOutWinMM( const std::string &clientName ) : MidiOutApi() |
| 2863 |
{ |
| 2864 |
MidiOutWinMM::initialize( clientName ); |
| 2865 |
} |
| 2866 |
|
| 2867 |
MidiOutWinMM :: ~MidiOutWinMM() |
| 2868 |
{ |
| 2869 |
// Close a connection if it exists. |
| 2870 |
MidiOutWinMM::closePort(); |
| 2871 |
|
| 2872 |
// Cleanup. |
| 2873 |
WinMidiData *data = static_cast<WinMidiData *> (apiData_); |
| 2874 |
delete data; |
| 2875 |
} |
| 2876 |
|
| 2877 |
void MidiOutWinMM :: initialize( const std::string& /*clientName*/ ) |
| 2878 |
{ |
| 2879 |
// We'll issue a warning here if no devices are available but not |
| 2880 |
// throw an error since the user can plug something in later. |
| 2881 |
unsigned int nDevices = midiOutGetNumDevs(); |
| 2882 |
if ( nDevices == 0 ) { |
| 2883 |
errorString_ = "MidiOutWinMM::initialize: no MIDI output devices currently available."; |
| 2884 |
error( RtMidiError::WARNING, errorString_ ); |
| 2885 |
} |
| 2886 |
|
| 2887 |
// Save our api-specific connection information. |
| 2888 |
WinMidiData *data = (WinMidiData *) new WinMidiData; |
| 2889 |
apiData_ = (void *) data; |
| 2890 |
} |
| 2891 |
|
| 2892 |
unsigned int MidiOutWinMM :: getPortCount() |
| 2893 |
{ |
| 2894 |
return midiOutGetNumDevs(); |
| 2895 |
} |
| 2896 |
|
| 2897 |
std::string MidiOutWinMM :: getPortName( unsigned int portNumber ) |
| 2898 |
{ |
| 2899 |
std::string stringName; |
| 2900 |
unsigned int nDevices = midiOutGetNumDevs(); |
| 2901 |
if ( portNumber >= nDevices ) { |
| 2902 |
std::ostringstream ost; |
| 2903 |
ost << "MidiOutWinMM::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 2904 |
errorString_ = ost.str(); |
| 2905 |
error( RtMidiError::WARNING, errorString_ ); |
| 2906 |
return stringName; |
| 2907 |
} |
| 2908 |
|
| 2909 |
MIDIOUTCAPS deviceCaps; |
| 2910 |
midiOutGetDevCaps( portNumber, &deviceCaps, sizeof( MIDIOUTCAPS ) ); |
| 2911 |
stringName = ConvertToUTF8( deviceCaps.szPname ); |
| 2912 |
|
| 2913 |
// Next lines added to add the portNumber to the name so that |
| 2914 |
// the device's names are sure to be listed with individual names |
| 2915 |
// even when they have the same brand name |
| 2916 |
std::ostringstream os; |
| 2917 |
#ifndef RTMIDI_DO_NOT_ENSURE_UNIQUE_PORTNAMES |
| 2918 |
os << " "; |
| 2919 |
os << portNumber; |
| 2920 |
stringName += os.str(); |
| 2921 |
#endif |
| 2922 |
|
| 2923 |
return stringName; |
| 2924 |
} |
| 2925 |
|
| 2926 |
void MidiOutWinMM :: openPort( unsigned int portNumber, const std::string &/*portName*/ ) |
| 2927 |
{ |
| 2928 |
if ( connected_ ) { |
| 2929 |
errorString_ = "MidiOutWinMM::openPort: a valid connection already exists!"; |
| 2930 |
error( RtMidiError::WARNING, errorString_ ); |
| 2931 |
return; |
| 2932 |
} |
| 2933 |
|
| 2934 |
unsigned int nDevices = midiOutGetNumDevs(); |
| 2935 |
if ( nDevices < 1 ) { |
| 2936 |
errorString_ = "MidiOutWinMM::openPort: no MIDI output destinations found!"; |
| 2937 |
error( RtMidiError::NO_DEVICES_FOUND, errorString_ ); |
| 2938 |
return; |
| 2939 |
} |
| 2940 |
|
| 2941 |
if ( portNumber >= nDevices ) { |
| 2942 |
std::ostringstream ost; |
| 2943 |
ost << "MidiOutWinMM::openPort: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 2944 |
errorString_ = ost.str(); |
| 2945 |
error( RtMidiError::INVALID_PARAMETER, errorString_ ); |
| 2946 |
return; |
| 2947 |
} |
| 2948 |
|
| 2949 |
WinMidiData *data = static_cast<WinMidiData *> (apiData_); |
| 2950 |
MMRESULT result = midiOutOpen( &data->outHandle, |
| 2951 |
portNumber, |
| 2952 |
(DWORD)NULL, |
| 2953 |
(DWORD)NULL, |
| 2954 |
CALLBACK_NULL ); |
| 2955 |
if ( result != MMSYSERR_NOERROR ) { |
| 2956 |
errorString_ = "MidiOutWinMM::openPort: error creating Windows MM MIDI output port."; |
| 2957 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 2958 |
return; |
| 2959 |
} |
| 2960 |
|
| 2961 |
connected_ = true; |
| 2962 |
} |
| 2963 |
|
| 2964 |
void MidiOutWinMM :: closePort( void ) |
| 2965 |
{ |
| 2966 |
if ( connected_ ) { |
| 2967 |
WinMidiData *data = static_cast<WinMidiData *> (apiData_); |
| 2968 |
// Disabled because midiOutReset triggers 0x7b (if any note was ON) and 0x79 "Reset All |
| 2969 |
// Controllers" (to all 16 channels) CC messages which is undesirable (see issue #222) |
| 2970 |
// midiOutReset( data->outHandle ); |
| 2971 |
|
| 2972 |
midiOutClose( data->outHandle ); |
| 2973 |
data->outHandle = 0; |
| 2974 |
connected_ = false; |
| 2975 |
} |
| 2976 |
} |
| 2977 |
|
| 2978 |
void MidiOutWinMM :: setClientName ( const std::string& ) |
| 2979 |
{ |
| 2980 |
|
| 2981 |
errorString_ = "MidiOutWinMM::setClientName: this function is not implemented for the WINDOWS_MM API!"; |
| 2982 |
error( RtMidiError::WARNING, errorString_ ); |
| 2983 |
|
| 2984 |
} |
| 2985 |
|
| 2986 |
void MidiOutWinMM :: setPortName ( const std::string& ) |
| 2987 |
{ |
| 2988 |
|
| 2989 |
errorString_ = "MidiOutWinMM::setPortName: this function is not implemented for the WINDOWS_MM API!"; |
| 2990 |
error( RtMidiError::WARNING, errorString_ ); |
| 2991 |
|
| 2992 |
} |
| 2993 |
|
| 2994 |
void MidiOutWinMM :: openVirtualPort( const std::string &/*portName*/ ) |
| 2995 |
{ |
| 2996 |
// This function cannot be implemented for the Windows MM MIDI API. |
| 2997 |
errorString_ = "MidiOutWinMM::openVirtualPort: cannot be implemented in Windows MM MIDI API!"; |
| 2998 |
error( RtMidiError::WARNING, errorString_ ); |
| 2999 |
} |
| 3000 |
|
| 3001 |
void MidiOutWinMM :: sendMessage( const unsigned char *message, size_t size ) |
| 3002 |
{ |
| 3003 |
if ( !connected_ ) return; |
| 3004 |
|
| 3005 |
unsigned int nBytes = static_cast<unsigned int>(size); |
| 3006 |
if ( nBytes == 0 ) { |
| 3007 |
errorString_ = "MidiOutWinMM::sendMessage: message argument is empty!"; |
| 3008 |
error( RtMidiError::WARNING, errorString_ ); |
| 3009 |
return; |
| 3010 |
} |
| 3011 |
|
| 3012 |
MMRESULT result; |
| 3013 |
WinMidiData *data = static_cast<WinMidiData *> (apiData_); |
| 3014 |
if ( message[0] == 0xF0 ) { // Sysex message |
| 3015 |
|
| 3016 |
// Allocate buffer for sysex data. |
| 3017 |
char *buffer = (char *) malloc( nBytes ); |
| 3018 |
if ( buffer == NULL ) { |
| 3019 |
errorString_ = "MidiOutWinMM::sendMessage: error allocating sysex message memory!"; |
| 3020 |
error( RtMidiError::MEMORY_ERROR, errorString_ ); |
| 3021 |
return; |
| 3022 |
} |
| 3023 |
|
| 3024 |
// Copy data to buffer. |
| 3025 |
for ( unsigned int i=0; i<nBytes; ++i ) buffer[i] = message[i]; |
| 3026 |
|
| 3027 |
// Create and prepare MIDIHDR structure. |
| 3028 |
MIDIHDR sysex; |
| 3029 |
sysex.lpData = (LPSTR) buffer; |
| 3030 |
sysex.dwBufferLength = nBytes; |
| 3031 |
sysex.dwFlags = 0; |
| 3032 |
result = midiOutPrepareHeader( data->outHandle, &sysex, sizeof( MIDIHDR ) ); |
| 3033 |
if ( result != MMSYSERR_NOERROR ) { |
| 3034 |
free( buffer ); |
| 3035 |
errorString_ = "MidiOutWinMM::sendMessage: error preparing sysex header."; |
| 3036 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 3037 |
return; |
| 3038 |
} |
| 3039 |
|
| 3040 |
// Send the message. |
| 3041 |
result = midiOutLongMsg( data->outHandle, &sysex, sizeof( MIDIHDR ) ); |
| 3042 |
if ( result != MMSYSERR_NOERROR ) { |
| 3043 |
free( buffer ); |
| 3044 |
errorString_ = "MidiOutWinMM::sendMessage: error sending sysex message."; |
| 3045 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 3046 |
return; |
| 3047 |
} |
| 3048 |
|
| 3049 |
// Unprepare the buffer and MIDIHDR. |
| 3050 |
while ( MIDIERR_STILLPLAYING == midiOutUnprepareHeader( data->outHandle, &sysex, sizeof ( MIDIHDR ) ) ) Sleep( 1 ); |
| 3051 |
free( buffer ); |
| 3052 |
} |
| 3053 |
else { // Channel or system message. |
| 3054 |
|
| 3055 |
// Make sure the message size isn't too big. |
| 3056 |
if ( nBytes > 3 ) { |
| 3057 |
errorString_ = "MidiOutWinMM::sendMessage: message size is greater than 3 bytes (and not sysex)!"; |
| 3058 |
error( RtMidiError::WARNING, errorString_ ); |
| 3059 |
return; |
| 3060 |
} |
| 3061 |
|
| 3062 |
// Pack MIDI bytes into double word. |
| 3063 |
DWORD packet; |
| 3064 |
unsigned char *ptr = (unsigned char *) &packet; |
| 3065 |
for ( unsigned int i=0; i<nBytes; ++i ) { |
| 3066 |
*ptr = message[i]; |
| 3067 |
++ptr; |
| 3068 |
} |
| 3069 |
|
| 3070 |
// Send the message immediately. |
| 3071 |
result = midiOutShortMsg( data->outHandle, packet ); |
| 3072 |
if ( result != MMSYSERR_NOERROR ) { |
| 3073 |
errorString_ = "MidiOutWinMM::sendMessage: error sending MIDI message."; |
| 3074 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 3075 |
} |
| 3076 |
} |
| 3077 |
} |
| 3078 |
|
| 3079 |
#endif // __WINDOWS_MM__ |
| 3080 |
|
| 3081 |
|
| 3082 |
//*********************************************************************// |
| 3083 |
// API: UNIX JACK |
| 3084 |
// |
| 3085 |
// Written primarily by Alexander Svetalkin, with updates for delta |
| 3086 |
// time by Gary Scavone, April 2011. |
| 3087 |
// |
| 3088 |
// *********************************************************************// |
| 3089 |
|
| 3090 |
#if defined(__UNIX_JACK__) |
| 3091 |
|
| 3092 |
// JACK header files |
| 3093 |
#include <jack/jack.h> |
| 3094 |
#include <jack/midiport.h> |
| 3095 |
#include <jack/ringbuffer.h> |
| 3096 |
#include <pthread.h> |
| 3097 |
#ifdef HAVE_SEMAPHORE |
| 3098 |
#include <semaphore.h> |
| 3099 |
#endif |
| 3100 |
|
| 3101 |
#define JACK_RINGBUFFER_SIZE 16384 // Default size for ringbuffer |
| 3102 |
|
| 3103 |
struct JackMidiData { |
| 3104 |
jack_client_t *client; |
| 3105 |
jack_port_t *port; |
| 3106 |
jack_ringbuffer_t *buff; |
| 3107 |
int buffMaxWrite; // actual writable size, usually 1 less than ringbuffer |
| 3108 |
jack_time_t lastTime; |
| 3109 |
#ifdef HAVE_SEMAPHORE |
| 3110 |
sem_t sem_cleanup; |
| 3111 |
sem_t sem_needpost; |
| 3112 |
#endif |
| 3113 |
MidiInApi :: RtMidiInData *rtMidiIn; |
| 3114 |
}; |
| 3115 |
|
| 3116 |
//*********************************************************************// |
| 3117 |
// API: JACK |
| 3118 |
// Class Definitions: MidiInJack |
| 3119 |
//*********************************************************************// |
| 3120 |
|
| 3121 |
static int jackProcessIn( jack_nframes_t nframes, void *arg ) |
| 3122 |
{ |
| 3123 |
JackMidiData *jData = (JackMidiData *) arg; |
| 3124 |
MidiInApi :: RtMidiInData *rtData = jData->rtMidiIn; |
| 3125 |
jack_midi_event_t event; |
| 3126 |
jack_time_t time; |
| 3127 |
|
| 3128 |
// Is port created? |
| 3129 |
if ( jData->port == NULL ) return 0; |
| 3130 |
|
| 3131 |
void *buff = jack_port_get_buffer( jData->port, nframes ); |
| 3132 |
bool& continueSysex = rtData->continueSysex; |
| 3133 |
unsigned char& ignoreFlags = rtData->ignoreFlags; |
| 3134 |
|
| 3135 |
// We have midi events in buffer |
| 3136 |
int evCount = jack_midi_get_event_count( buff ); |
| 3137 |
for (int j = 0; j < evCount; j++) { |
| 3138 |
MidiInApi::MidiMessage& message = rtData->message; |
| 3139 |
jack_midi_event_get( &event, buff, j ); |
| 3140 |
|
| 3141 |
// Compute the delta time. |
| 3142 |
time = jack_get_time(); |
| 3143 |
if ( rtData->firstMessage == true ) { |
| 3144 |
message.timeStamp = 0.0; |
| 3145 |
rtData->firstMessage = false; |
| 3146 |
} else |
| 3147 |
message.timeStamp = ( time - jData->lastTime ) * 0.000001; |
| 3148 |
|
| 3149 |
jData->lastTime = time; |
| 3150 |
|
| 3151 |
if ( !continueSysex ) |
| 3152 |
message.bytes.clear(); |
| 3153 |
|
| 3154 |
if ( !( ( continueSysex || event.buffer[0] == 0xF0 ) && ( ignoreFlags & 0x01 ) ) ) { |
| 3155 |
// Unless this is a (possibly continued) SysEx message and we're ignoring SysEx, |
| 3156 |
// copy the event buffer into the MIDI message struct. |
| 3157 |
for ( unsigned int i = 0; i < event.size; i++ ) |
| 3158 |
message.bytes.push_back( event.buffer[i] ); |
| 3159 |
} |
| 3160 |
|
| 3161 |
switch ( event.buffer[0] ) { |
| 3162 |
case 0xF0: |
| 3163 |
// Start of a SysEx message |
| 3164 |
continueSysex = event.buffer[event.size - 1] != 0xF7; |
| 3165 |
if ( ignoreFlags & 0x01 ) continue; |
| 3166 |
break; |
| 3167 |
case 0xF1: |
| 3168 |
case 0xF8: |
| 3169 |
// MIDI Time Code or Timing Clock message |
| 3170 |
if ( ignoreFlags & 0x02 ) continue; |
| 3171 |
break; |
| 3172 |
case 0xFE: |
| 3173 |
// Active Sensing message |
| 3174 |
if ( ignoreFlags & 0x04 ) continue; |
| 3175 |
break; |
| 3176 |
default: |
| 3177 |
if ( continueSysex ) { |
| 3178 |
// Continuation of a SysEx message |
| 3179 |
continueSysex = event.buffer[event.size - 1] != 0xF7; |
| 3180 |
if ( ignoreFlags & 0x01 ) continue; |
| 3181 |
} |
| 3182 |
// All other MIDI messages |
| 3183 |
} |
| 3184 |
|
| 3185 |
if ( !continueSysex ) { |
| 3186 |
// If not a continuation of a SysEx message, |
| 3187 |
// invoke the user callback function or queue the message. |
| 3188 |
if ( rtData->usingCallback ) { |
| 3189 |
RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) rtData->userCallback; |
| 3190 |
callback( message.timeStamp, &message.bytes, rtData->userData ); |
| 3191 |
} |
| 3192 |
else { |
| 3193 |
// As long as we haven't reached our queue size limit, push the message. |
| 3194 |
if ( !rtData->queue.push( message ) ) |
| 3195 |
std::cerr << "\nMidiInJack: message queue limit reached!!\n\n"; |
| 3196 |
} |
| 3197 |
} |
| 3198 |
} |
| 3199 |
|
| 3200 |
return 0; |
| 3201 |
} |
| 3202 |
|
| 3203 |
MidiInJack :: MidiInJack( const std::string &clientName, unsigned int queueSizeLimit ) |
| 3204 |
: MidiInApi( queueSizeLimit ) |
| 3205 |
{ |
| 3206 |
MidiInJack::initialize( clientName ); |
| 3207 |
} |
| 3208 |
|
| 3209 |
void MidiInJack :: initialize( const std::string& clientName ) |
| 3210 |
{ |
| 3211 |
JackMidiData *data = new JackMidiData; |
| 3212 |
apiData_ = (void *) data; |
| 3213 |
|
| 3214 |
data->rtMidiIn = &inputData_; |
| 3215 |
data->port = NULL; |
| 3216 |
data->client = NULL; |
| 3217 |
this->clientName = clientName; |
| 3218 |
|
| 3219 |
connect(); |
| 3220 |
} |
| 3221 |
|
| 3222 |
void MidiInJack :: connect() |
| 3223 |
{ |
| 3224 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3225 |
if ( data->client ) |
| 3226 |
return; |
| 3227 |
|
| 3228 |
// Initialize JACK client |
| 3229 |
if (( data->client = jack_client_open( clientName.c_str(), JackNoStartServer, NULL )) == 0) { |
| 3230 |
errorString_ = "MidiInJack::initialize: JACK server not running?"; |
| 3231 |
error( RtMidiError::WARNING, errorString_ ); |
| 3232 |
return; |
| 3233 |
} |
| 3234 |
|
| 3235 |
jack_set_process_callback( data->client, jackProcessIn, data ); |
| 3236 |
jack_activate( data->client ); |
| 3237 |
} |
| 3238 |
|
| 3239 |
MidiInJack :: ~MidiInJack() |
| 3240 |
{ |
| 3241 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3242 |
MidiInJack::closePort(); |
| 3243 |
|
| 3244 |
if ( data->client ) |
| 3245 |
jack_client_close( data->client ); |
| 3246 |
delete data; |
| 3247 |
} |
| 3248 |
|
| 3249 |
void MidiInJack :: openPort( unsigned int portNumber, const std::string &portName ) |
| 3250 |
{ |
| 3251 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3252 |
|
| 3253 |
connect(); |
| 3254 |
|
| 3255 |
// Creating new port |
| 3256 |
if ( data->port == NULL ) |
| 3257 |
data->port = jack_port_register( data->client, portName.c_str(), |
| 3258 |
JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 ); |
| 3259 |
|
| 3260 |
if ( data->port == NULL ) { |
| 3261 |
errorString_ = "MidiInJack::openPort: JACK error creating port"; |
| 3262 |
if (portName.size() >= (size_t)jack_port_name_size()) |
| 3263 |
errorString_ += " (port name too long?)"; |
| 3264 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 3265 |
return; |
| 3266 |
} |
| 3267 |
|
| 3268 |
// Connecting to the output |
| 3269 |
std::string name = getPortName( portNumber ); |
| 3270 |
jack_connect( data->client, name.c_str(), jack_port_name( data->port ) ); |
| 3271 |
|
| 3272 |
connected_ = true; |
| 3273 |
} |
| 3274 |
|
| 3275 |
void MidiInJack :: openVirtualPort( const std::string &portName ) |
| 3276 |
{ |
| 3277 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3278 |
|
| 3279 |
connect(); |
| 3280 |
if ( data->port == NULL ) |
| 3281 |
data->port = jack_port_register( data->client, portName.c_str(), |
| 3282 |
JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 ); |
| 3283 |
|
| 3284 |
if ( data->port == NULL ) { |
| 3285 |
errorString_ = "MidiInJack::openVirtualPort: JACK error creating virtual port"; |
| 3286 |
if (portName.size() >= (size_t)jack_port_name_size()) |
| 3287 |
errorString_ += " (port name too long?)"; |
| 3288 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 3289 |
} |
| 3290 |
} |
| 3291 |
|
| 3292 |
unsigned int MidiInJack :: getPortCount() |
| 3293 |
{ |
| 3294 |
int count = 0; |
| 3295 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3296 |
connect(); |
| 3297 |
if ( !data->client ) |
| 3298 |
return 0; |
| 3299 |
|
| 3300 |
// List of available ports |
| 3301 |
const char **ports = jack_get_ports( data->client, NULL, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput ); |
| 3302 |
|
| 3303 |
if ( ports == NULL ) return 0; |
| 3304 |
while ( ports[count] != NULL ) |
| 3305 |
count++; |
| 3306 |
|
| 3307 |
free( ports ); |
| 3308 |
|
| 3309 |
return count; |
| 3310 |
} |
| 3311 |
|
| 3312 |
std::string MidiInJack :: getPortName( unsigned int portNumber ) |
| 3313 |
{ |
| 3314 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3315 |
std::string retStr( "" ); |
| 3316 |
|
| 3317 |
connect(); |
| 3318 |
|
| 3319 |
// List of available ports |
| 3320 |
const char **ports = jack_get_ports( data->client, NULL, |
| 3321 |
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput ); |
| 3322 |
|
| 3323 |
// Check port validity |
| 3324 |
if ( ports == NULL ) { |
| 3325 |
errorString_ = "MidiInJack::getPortName: no ports available!"; |
| 3326 |
error( RtMidiError::WARNING, errorString_ ); |
| 3327 |
return retStr; |
| 3328 |
} |
| 3329 |
|
| 3330 |
unsigned int i; |
| 3331 |
for ( i=0; i<portNumber && ports[i]; i++ ) {} |
| 3332 |
if ( i < portNumber || !ports[portNumber] ) { |
| 3333 |
std::ostringstream ost; |
| 3334 |
ost << "MidiInJack::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 3335 |
errorString_ = ost.str(); |
| 3336 |
error( RtMidiError::WARNING, errorString_ ); |
| 3337 |
} |
| 3338 |
else retStr.assign( ports[portNumber] ); |
| 3339 |
|
| 3340 |
jack_free( ports ); |
| 3341 |
return retStr; |
| 3342 |
} |
| 3343 |
|
| 3344 |
void MidiInJack :: closePort() |
| 3345 |
{ |
| 3346 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3347 |
|
| 3348 |
if ( data->port == NULL ) return; |
| 3349 |
jack_port_unregister( data->client, data->port ); |
| 3350 |
data->port = NULL; |
| 3351 |
|
| 3352 |
connected_ = false; |
| 3353 |
} |
| 3354 |
|
| 3355 |
void MidiInJack:: setClientName( const std::string& ) |
| 3356 |
{ |
| 3357 |
|
| 3358 |
errorString_ = "MidiInJack::setClientName: this function is not implemented for the UNIX_JACK API!"; |
| 3359 |
error( RtMidiError::WARNING, errorString_ ); |
| 3360 |
|
| 3361 |
} |
| 3362 |
|
| 3363 |
void MidiInJack :: setPortName( const std::string &portName ) |
| 3364 |
{ |
| 3365 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3366 |
#ifdef JACK_HAS_PORT_RENAME |
| 3367 |
jack_port_rename( data->client, data->port, portName.c_str() ); |
| 3368 |
#else |
| 3369 |
jack_port_set_name( data->port, portName.c_str() ); |
| 3370 |
#endif |
| 3371 |
} |
| 3372 |
|
| 3373 |
//*********************************************************************// |
| 3374 |
// API: JACK |
| 3375 |
// Class Definitions: MidiOutJack |
| 3376 |
//*********************************************************************// |
| 3377 |
|
| 3378 |
// Jack process callback |
| 3379 |
static int jackProcessOut( jack_nframes_t nframes, void *arg ) |
| 3380 |
{ |
| 3381 |
JackMidiData *data = (JackMidiData *) arg; |
| 3382 |
jack_midi_data_t *midiData; |
| 3383 |
int space; |
| 3384 |
|
| 3385 |
// Is port created? |
| 3386 |
if ( data->port == NULL ) return 0; |
| 3387 |
|
| 3388 |
void *buff = jack_port_get_buffer( data->port, nframes ); |
| 3389 |
jack_midi_clear_buffer( buff ); |
| 3390 |
|
| 3391 |
while ( jack_ringbuffer_peek( data->buff, (char *) &space, sizeof( space ) ) == sizeof(space) && |
| 3392 |
jack_ringbuffer_read_space( data->buff ) >= sizeof(space) + space ) { |
| 3393 |
jack_ringbuffer_read_advance( data->buff, sizeof(space) ); |
| 3394 |
|
| 3395 |
midiData = jack_midi_event_reserve( buff, 0, space ); |
| 3396 |
if ( midiData ) |
| 3397 |
jack_ringbuffer_read( data->buff, (char *) midiData, (size_t) space ); |
| 3398 |
else |
| 3399 |
jack_ringbuffer_read_advance( data->buff, (size_t) space ); |
| 3400 |
} |
| 3401 |
|
| 3402 |
#ifdef HAVE_SEMAPHORE |
| 3403 |
if ( !sem_trywait( &data->sem_needpost ) ) |
| 3404 |
sem_post( &data->sem_cleanup ); |
| 3405 |
#endif |
| 3406 |
|
| 3407 |
return 0; |
| 3408 |
} |
| 3409 |
|
| 3410 |
MidiOutJack :: MidiOutJack( const std::string &clientName ) : MidiOutApi() |
| 3411 |
{ |
| 3412 |
MidiOutJack::initialize( clientName ); |
| 3413 |
} |
| 3414 |
|
| 3415 |
void MidiOutJack :: initialize( const std::string& clientName ) |
| 3416 |
{ |
| 3417 |
JackMidiData *data = new JackMidiData; |
| 3418 |
apiData_ = (void *) data; |
| 3419 |
|
| 3420 |
data->port = NULL; |
| 3421 |
data->client = NULL; |
| 3422 |
#ifdef HAVE_SEMAPHORE |
| 3423 |
sem_init( &data->sem_cleanup, 0, 0 ); |
| 3424 |
sem_init( &data->sem_needpost, 0, 0 ); |
| 3425 |
#endif |
| 3426 |
this->clientName = clientName; |
| 3427 |
|
| 3428 |
connect(); |
| 3429 |
} |
| 3430 |
|
| 3431 |
void MidiOutJack :: connect() |
| 3432 |
{ |
| 3433 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3434 |
if ( data->client ) |
| 3435 |
return; |
| 3436 |
|
| 3437 |
// Initialize output ringbuffers |
| 3438 |
data->buff = jack_ringbuffer_create( JACK_RINGBUFFER_SIZE ); |
| 3439 |
data->buffMaxWrite = (int) jack_ringbuffer_write_space( data->buff ); |
| 3440 |
|
| 3441 |
// Initialize JACK client |
| 3442 |
if ( ( data->client = jack_client_open( clientName.c_str(), JackNoStartServer, NULL ) ) == 0 ) { |
| 3443 |
errorString_ = "MidiOutJack::initialize: JACK server not running?"; |
| 3444 |
error( RtMidiError::WARNING, errorString_ ); |
| 3445 |
return; |
| 3446 |
} |
| 3447 |
|
| 3448 |
jack_set_process_callback( data->client, jackProcessOut, data ); |
| 3449 |
jack_activate( data->client ); |
| 3450 |
} |
| 3451 |
|
| 3452 |
MidiOutJack :: ~MidiOutJack() |
| 3453 |
{ |
| 3454 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3455 |
MidiOutJack::closePort(); |
| 3456 |
|
| 3457 |
// Cleanup |
| 3458 |
jack_ringbuffer_free( data->buff ); |
| 3459 |
if ( data->client ) { |
| 3460 |
jack_client_close( data->client ); |
| 3461 |
} |
| 3462 |
|
| 3463 |
#ifdef HAVE_SEMAPHORE |
| 3464 |
sem_destroy( &data->sem_cleanup ); |
| 3465 |
sem_destroy( &data->sem_needpost ); |
| 3466 |
#endif |
| 3467 |
|
| 3468 |
delete data; |
| 3469 |
} |
| 3470 |
|
| 3471 |
void MidiOutJack :: openPort( unsigned int portNumber, const std::string &portName ) |
| 3472 |
{ |
| 3473 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3474 |
|
| 3475 |
connect(); |
| 3476 |
|
| 3477 |
// Creating new port |
| 3478 |
if ( data->port == NULL ) |
| 3479 |
data->port = jack_port_register( data->client, portName.c_str(), |
| 3480 |
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0 ); |
| 3481 |
|
| 3482 |
if ( data->port == NULL ) { |
| 3483 |
errorString_ = "MidiOutJack::openPort: JACK error creating port"; |
| 3484 |
if (portName.size() >= (size_t)jack_port_name_size()) |
| 3485 |
errorString_ += " (port name too long?)"; |
| 3486 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 3487 |
return; |
| 3488 |
} |
| 3489 |
|
| 3490 |
// Connecting to the output |
| 3491 |
std::string name = getPortName( portNumber ); |
| 3492 |
jack_connect( data->client, jack_port_name( data->port ), name.c_str() ); |
| 3493 |
|
| 3494 |
connected_ = true; |
| 3495 |
} |
| 3496 |
|
| 3497 |
void MidiOutJack :: openVirtualPort( const std::string &portName ) |
| 3498 |
{ |
| 3499 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3500 |
|
| 3501 |
connect(); |
| 3502 |
if ( data->port == NULL ) |
| 3503 |
data->port = jack_port_register( data->client, portName.c_str(), |
| 3504 |
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0 ); |
| 3505 |
|
| 3506 |
if ( data->port == NULL ) { |
| 3507 |
errorString_ = "MidiOutJack::openVirtualPort: JACK error creating virtual port"; |
| 3508 |
if (portName.size() >= (size_t)jack_port_name_size()) |
| 3509 |
errorString_ += " (port name too long?)"; |
| 3510 |
error( RtMidiError::DRIVER_ERROR, errorString_ ); |
| 3511 |
} |
| 3512 |
} |
| 3513 |
|
| 3514 |
unsigned int MidiOutJack :: getPortCount() |
| 3515 |
{ |
| 3516 |
int count = 0; |
| 3517 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3518 |
connect(); |
| 3519 |
if ( !data->client ) |
| 3520 |
return 0; |
| 3521 |
|
| 3522 |
// List of available ports |
| 3523 |
const char **ports = jack_get_ports( data->client, NULL, |
| 3524 |
JACK_DEFAULT_MIDI_TYPE, JackPortIsInput ); |
| 3525 |
|
| 3526 |
if ( ports == NULL ) return 0; |
| 3527 |
while ( ports[count] != NULL ) |
| 3528 |
count++; |
| 3529 |
|
| 3530 |
free( ports ); |
| 3531 |
|
| 3532 |
return count; |
| 3533 |
} |
| 3534 |
|
| 3535 |
std::string MidiOutJack :: getPortName( unsigned int portNumber ) |
| 3536 |
{ |
| 3537 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3538 |
std::string retStr(""); |
| 3539 |
|
| 3540 |
connect(); |
| 3541 |
|
| 3542 |
// List of available ports |
| 3543 |
const char **ports = jack_get_ports( data->client, NULL, |
| 3544 |
JACK_DEFAULT_MIDI_TYPE, JackPortIsInput ); |
| 3545 |
|
| 3546 |
// Check port validity |
| 3547 |
if ( ports == NULL ) { |
| 3548 |
errorString_ = "MidiOutJack::getPortName: no ports available!"; |
| 3549 |
error( RtMidiError::WARNING, errorString_ ); |
| 3550 |
return retStr; |
| 3551 |
} |
| 3552 |
|
| 3553 |
if ( ports[portNumber] == NULL ) { |
| 3554 |
std::ostringstream ost; |
| 3555 |
ost << "MidiOutJack::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid."; |
| 3556 |
errorString_ = ost.str(); |
| 3557 |
error( RtMidiError::WARNING, errorString_ ); |
| 3558 |
} |
| 3559 |
else retStr.assign( ports[portNumber] ); |
| 3560 |
|
| 3561 |
free( ports ); |
| 3562 |
return retStr; |
| 3563 |
} |
| 3564 |
|
| 3565 |
void MidiOutJack :: closePort() |
| 3566 |
{ |
| 3567 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3568 |
|
| 3569 |
if ( data->port == NULL ) return; |
| 3570 |
|
| 3571 |
#ifdef HAVE_SEMAPHORE |
| 3572 |
struct timespec ts; |
| 3573 |
if ( clock_gettime( CLOCK_REALTIME, &ts ) != -1 ) { |
| 3574 |
ts.tv_sec += 1; // wait max one second |
| 3575 |
sem_post( &data->sem_needpost ); |
| 3576 |
sem_timedwait( &data->sem_cleanup, &ts ); |
| 3577 |
} |
| 3578 |
#endif |
| 3579 |
|
| 3580 |
jack_port_unregister( data->client, data->port ); |
| 3581 |
data->port = NULL; |
| 3582 |
|
| 3583 |
connected_ = false; |
| 3584 |
} |
| 3585 |
|
| 3586 |
void MidiOutJack:: setClientName( const std::string& ) |
| 3587 |
{ |
| 3588 |
|
| 3589 |
errorString_ = "MidiOutJack::setClientName: this function is not implemented for the UNIX_JACK API!"; |
| 3590 |
error( RtMidiError::WARNING, errorString_ ); |
| 3591 |
|
| 3592 |
} |
| 3593 |
|
| 3594 |
void MidiOutJack :: setPortName( const std::string &portName ) |
| 3595 |
{ |
| 3596 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3597 |
#ifdef JACK_HAS_PORT_RENAME |
| 3598 |
jack_port_rename( data->client, data->port, portName.c_str() ); |
| 3599 |
#else |
| 3600 |
jack_port_set_name( data->port, portName.c_str() ); |
| 3601 |
#endif |
| 3602 |
} |
| 3603 |
|
| 3604 |
void MidiOutJack :: sendMessage( const unsigned char *message, size_t size ) |
| 3605 |
{ |
| 3606 |
int nBytes = static_cast<int>(size); |
| 3607 |
JackMidiData *data = static_cast<JackMidiData *> (apiData_); |
| 3608 |
|
| 3609 |
if ( size + sizeof(nBytes) > (size_t) data->buffMaxWrite ) |
| 3610 |
return; |
| 3611 |
|
| 3612 |
while ( jack_ringbuffer_write_space(data->buff) < sizeof(nBytes) + size ) |
| 3613 |
pthread_yield(); |
| 3614 |
|
| 3615 |
// Write full message to buffer |
| 3616 |
jack_ringbuffer_write( data->buff, ( char * ) &nBytes, sizeof( nBytes ) ); |
| 3617 |
jack_ringbuffer_write( data->buff, ( const char * ) message, nBytes ); |
| 3618 |
} |
| 3619 |
|
| 3620 |
#endif // __UNIX_JACK__ |
| 3621 |
|
| 3622 |
//*********************************************************************// |
| 3623 |
// API: Web MIDI |
| 3624 |
// |
| 3625 |
// Written primarily by Atsushi Eno, February 2020. |
| 3626 |
// |
| 3627 |
// *********************************************************************// |
| 3628 |
|
| 3629 |
#if defined(__WEB_MIDI_API__) |
| 3630 |
|
| 3631 |
#include <emscripten.h> |
| 3632 |
|
| 3633 |
//*********************************************************************// |
| 3634 |
// API: WEB MIDI |
| 3635 |
// Class Definitions: WebMidiAccessShim |
| 3636 |
//*********************************************************************// |
| 3637 |
|
| 3638 |
class WebMidiAccessShim |
| 3639 |
{ |
| 3640 |
public: |
| 3641 |
WebMidiAccessShim(); |
| 3642 |
~WebMidiAccessShim(); |
| 3643 |
std::string getPortName( unsigned int portNumber, bool isInput ); |
| 3644 |
}; |
| 3645 |
|
| 3646 |
std::unique_ptr<WebMidiAccessShim> shim{nullptr}; |
| 3647 |
|
| 3648 |
void ensureShim() |
| 3649 |
{ |
| 3650 |
if ( shim.get() != nullptr ) |
| 3651 |
return; |
| 3652 |
shim.reset( new WebMidiAccessShim() ); |
| 3653 |
} |
| 3654 |
|
| 3655 |
bool checkWebMidiAvailability() |
| 3656 |
{ |
| 3657 |
ensureShim(); |
| 3658 |
|
| 3659 |
return MAIN_THREAD_EM_ASM_INT( { |
| 3660 |
if ( typeof window._rtmidi_internals_waiting === "undefined" ) { |
| 3661 |
console.log ( "Attempted to use Web MIDI API without trying to open it." ); |
| 3662 |
return false; |
| 3663 |
} |
| 3664 |
if ( window._rtmidi_internals_waiting ) { |
| 3665 |
console.log ( "Attempted to use Web MIDI API while it is being queried." ); |
| 3666 |
return false; |
| 3667 |
} |
| 3668 |
if ( _rtmidi_internals_midi_access == null ) { |
| 3669 |
console.log ( "Attempted to use Web MIDI API while it already turned out to be unavailable." ); |
| 3670 |
return false; |
| 3671 |
} |
| 3672 |
return true; |
| 3673 |
} ); |
| 3674 |
} |
| 3675 |
|
| 3676 |
WebMidiAccessShim::WebMidiAccessShim() |
| 3677 |
{ |
| 3678 |
MAIN_THREAD_ASYNC_EM_ASM( { |
| 3679 |
if( typeof window._rtmidi_internals_midi_access !== "undefined" ) |
| 3680 |
return; |
| 3681 |
if( typeof window._rtmidi_internals_waiting !== "undefined" ) { |
| 3682 |
console.log( "MIDI Access was requested while another request is in progress." ); |
| 3683 |
return; |
| 3684 |
} |
| 3685 |
|
| 3686 |
// define functions |
| 3687 |
window._rtmidi_internals_get_port_by_number = function( portNumber, isInput ) { |
| 3688 |
var midi = window._rtmidi_internals_midi_access; |
| 3689 |
var devices = isInput ? midi.inputs : midi.outputs; |
| 3690 |
var i = 0; |
| 3691 |
for (var device of devices.values()) { |
| 3692 |
if ( i == portNumber ) |
| 3693 |
return device; |
| 3694 |
i++; |
| 3695 |
} |
| 3696 |
console.log( "MIDI " + (isInput ? "input" : "output") + " device of portNumber " + portNumber + " is not found."); |
| 3697 |
return null; |
| 3698 |
}; |
| 3699 |
|
| 3700 |
window._rtmidi_internals_waiting = true; |
| 3701 |
window.navigator.requestMIDIAccess( {"sysex": true} ).then( (midiAccess) => { |
| 3702 |
window._rtmidi_internals_midi_access = midiAccess; |
| 3703 |
window._rtmidi_internals_latest_message_timestamp = 0.0; |
| 3704 |
window._rtmidi_internals_waiting = false; |
| 3705 |
if( midiAccess == null ) { |
| 3706 |
console.log ( "Could not get access to MIDI API" ); |
| 3707 |
} |
| 3708 |
} ); |
| 3709 |
} ); |
| 3710 |
} |
| 3711 |
|
| 3712 |
WebMidiAccessShim::~WebMidiAccessShim() |
| 3713 |
{ |
| 3714 |
} |
| 3715 |
|
| 3716 |
std::string WebMidiAccessShim::getPortName( unsigned int portNumber, bool isInput ) |
| 3717 |
{ |
| 3718 |
if( !checkWebMidiAvailability() ) |
| 3719 |
return ""; |
| 3720 |
char *ret = (char*) MAIN_THREAD_EM_ASM_INT( { |
| 3721 |
var port = window._rtmidi_internals_get_port_by_number($0, $1); |
| 3722 |
if( port == null) |
| 3723 |
return null; |
| 3724 |
var length = lengthBytesUTF8(port.name) + 1; |
| 3725 |
var ret = _malloc(length); |
| 3726 |
stringToUTF8(port.name, ret, length); |
| 3727 |
return ret; |
| 3728 |
}, portNumber, isInput, &ret ); |
| 3729 |
if (ret == nullptr) |
| 3730 |
return ""; |
| 3731 |
std::string s = ret; |
| 3732 |
free(ret); |
| 3733 |
return s; |
| 3734 |
} |
| 3735 |
|
| 3736 |
//*********************************************************************// |
| 3737 |
// API: WEB MIDI |
| 3738 |
// Class Definitions: MidiInWeb |
| 3739 |
//*********************************************************************// |
| 3740 |
|
| 3741 |
MidiInWeb::MidiInWeb( const std::string &clientName, unsigned int queueSizeLimit ) |
| 3742 |
: MidiInApi( queueSizeLimit ) |
| 3743 |
{ |
| 3744 |
initialize( clientName ); |
| 3745 |
} |
| 3746 |
|
| 3747 |
MidiInWeb::~MidiInWeb( void ) |
| 3748 |
{ |
| 3749 |
closePort(); |
| 3750 |
} |
| 3751 |
|
| 3752 |
extern "C" void EMSCRIPTEN_KEEPALIVE rtmidi_onMidiMessageProc( MidiInApi::RtMidiInData* data, uint8_t* inputBytes, int32_t length, double domHighResTimeStamp ) |
| 3753 |
{ |
| 3754 |
auto &message = data->message; |
| 3755 |
message.bytes.resize(message.bytes.size() + length); |
| 3756 |
memcpy(message.bytes.data(), inputBytes, length); |
| 3757 |
// FIXME: handle timestamp |
| 3758 |
if ( data->usingCallback ) { |
| 3759 |
RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback; |
| 3760 |
callback( message.timeStamp, &message.bytes, data->userData ); |
| 3761 |
} |
| 3762 |
} |
| 3763 |
|
| 3764 |
void MidiInWeb::openPort( unsigned int portNumber, const std::string &portName ) |
| 3765 |
{ |
| 3766 |
if( !checkWebMidiAvailability() ) |
| 3767 |
return; |
| 3768 |
if (open_port_number >= 0) |
| 3769 |
return; |
| 3770 |
|
| 3771 |
MAIN_THREAD_EM_ASM( { |
| 3772 |
// In Web MIDI API world, there is no step to open a port, but we have to register the input callback instead. |
| 3773 |
var input = window._rtmidi_internals_get_port_by_number($0, true); |
| 3774 |
input.onmidimessage = function(e) { |
| 3775 |
// In RtMidi world, timestamps are delta time from previous message, while in Web MIDI world |
| 3776 |
// timestamps are relative to window creation time (i.e. kind of absolute time with window "epoch" time). |
| 3777 |
var rtmidiTimestamp = window._rtmidi_internals_latest_message_timestamp == 0.0 ? 0.0 : e.timeStamp - window._rtmidi_internals_latest_message_timestamp; |
| 3778 |
window._rtmidi_internals_latest_message_timestamp = e.timeStamp; |
| 3779 |
Module.ccall( 'rtmidi_onMidiMessageProc', 'void', ['number', 'array', 'number', 'number'], [$1, e.data, e.data.length, rtmidiTimestamp] ); |
| 3780 |
}; |
| 3781 |
}, portNumber, &inputData_ ); |
| 3782 |
open_port_number = portNumber; |
| 3783 |
} |
| 3784 |
|
| 3785 |
void MidiInWeb::openVirtualPort( const std::string &portName ) |
| 3786 |
{ |
| 3787 |
|
| 3788 |
errorString_ = "MidiInWeb::openVirtualPort: this function is not implemented for the Web MIDI API!"; |
| 3789 |
error( RtMidiError::WARNING, errorString_ ); |
| 3790 |
|
| 3791 |
} |
| 3792 |
|
| 3793 |
void MidiInWeb::closePort( void ) |
| 3794 |
{ |
| 3795 |
if( open_port_number < 0 ) |
| 3796 |
return; |
| 3797 |
|
| 3798 |
MAIN_THREAD_EM_ASM( { |
| 3799 |
var input = _rtmidi_internals_get_port_by_number($0, true); |
| 3800 |
if( input == null ) { |
| 3801 |
console.log( "Port #" + $0 + " could not be found."); |
| 3802 |
return; |
| 3803 |
} |
| 3804 |
// unregister event handler |
| 3805 |
input.onmidimessage = null; |
| 3806 |
}, open_port_number ); |
| 3807 |
open_port_number = -1; |
| 3808 |
} |
| 3809 |
|
| 3810 |
void MidiInWeb::setClientName( const std::string &clientName ) |
| 3811 |
{ |
| 3812 |
client_name = clientName; |
| 3813 |
} |
| 3814 |
|
| 3815 |
void MidiInWeb::setPortName( const std::string &portName ) |
| 3816 |
{ |
| 3817 |
|
| 3818 |
errorString_ = "MidiInWeb::setPortName: this function is not implemented for the Web MIDI API!"; |
| 3819 |
error( RtMidiError::WARNING, errorString_ ); |
| 3820 |
|
| 3821 |
} |
| 3822 |
|
| 3823 |
unsigned int MidiInWeb::getPortCount( void ) |
| 3824 |
{ |
| 3825 |
if( !checkWebMidiAvailability() ) |
| 3826 |
return 0; |
| 3827 |
return MAIN_THREAD_EM_ASM_INT( { return _rtmidi_internals_midi_access.inputs.size; } ); |
| 3828 |
} |
| 3829 |
|
| 3830 |
std::string MidiInWeb::getPortName( unsigned int portNumber ) |
| 3831 |
{ |
| 3832 |
if( !checkWebMidiAvailability() ) |
| 3833 |
return ""; |
| 3834 |
return shim->getPortName( portNumber, true ); |
| 3835 |
} |
| 3836 |
|
| 3837 |
void MidiInWeb::initialize( const std::string& clientName ) |
| 3838 |
{ |
| 3839 |
ensureShim(); |
| 3840 |
setClientName( clientName ); |
| 3841 |
} |
| 3842 |
|
| 3843 |
//*********************************************************************// |
| 3844 |
// API: WEB MIDI |
| 3845 |
// Class Definitions: MidiOutWeb |
| 3846 |
//*********************************************************************// |
| 3847 |
|
| 3848 |
MidiOutWeb::MidiOutWeb( const std::string &clientName ) |
| 3849 |
{ |
| 3850 |
initialize( clientName ); |
| 3851 |
} |
| 3852 |
|
| 3853 |
MidiOutWeb::~MidiOutWeb( void ) |
| 3854 |
{ |
| 3855 |
closePort(); |
| 3856 |
} |
| 3857 |
|
| 3858 |
void MidiOutWeb::openPort( unsigned int portNumber, const std::string &portName ) |
| 3859 |
{ |
| 3860 |
if( !checkWebMidiAvailability() ) |
| 3861 |
return; |
| 3862 |
if (open_port_number >= 0) |
| 3863 |
return; |
| 3864 |
// In Web MIDI API world, there is no step to open a port. |
| 3865 |
|
| 3866 |
open_port_number = portNumber; |
| 3867 |
} |
| 3868 |
|
| 3869 |
void MidiOutWeb::openVirtualPort( const std::string &portName ) |
| 3870 |
{ |
| 3871 |
|
| 3872 |
errorString_ = "MidiOutWeb::openVirtualPort: this function is not implemented for the Web MIDI API!"; |
| 3873 |
error( RtMidiError::WARNING, errorString_ ); |
| 3874 |
|
| 3875 |
} |
| 3876 |
|
| 3877 |
void MidiOutWeb::closePort( void ) |
| 3878 |
{ |
| 3879 |
// there is really nothing to do for output at JS side. |
| 3880 |
open_port_number = -1; |
| 3881 |
} |
| 3882 |
|
| 3883 |
void MidiOutWeb::setClientName( const std::string &clientName ) |
| 3884 |
{ |
| 3885 |
client_name = clientName; |
| 3886 |
} |
| 3887 |
|
| 3888 |
void MidiOutWeb::setPortName( const std::string &portName ) |
| 3889 |
{ |
| 3890 |
|
| 3891 |
errorString_ = "MidiOutWeb::setPortName: this function is not implemented for the Web MIDI API!"; |
| 3892 |
error( RtMidiError::WARNING, errorString_ ); |
| 3893 |
|
| 3894 |
} |
| 3895 |
|
| 3896 |
unsigned int MidiOutWeb::getPortCount( void ) |
| 3897 |
{ |
| 3898 |
if( !checkWebMidiAvailability() ) |
| 3899 |
return 0; |
| 3900 |
return MAIN_THREAD_EM_ASM_INT( { return _rtmidi_internals_midi_access.outputs.size; } ); |
| 3901 |
} |
| 3902 |
|
| 3903 |
std::string MidiOutWeb::getPortName( unsigned int portNumber ) |
| 3904 |
{ |
| 3905 |
if( !checkWebMidiAvailability() ) |
| 3906 |
return ""; |
| 3907 |
return shim->getPortName( portNumber, false ); |
| 3908 |
} |
| 3909 |
|
| 3910 |
void MidiOutWeb::sendMessage( const unsigned char *message, size_t size ) |
| 3911 |
{ |
| 3912 |
if( open_port_number < 0 ) |
| 3913 |
return; |
| 3914 |
|
| 3915 |
MAIN_THREAD_EM_ASM( { |
| 3916 |
var output = _rtmidi_internals_get_port_by_number( $0, false ); |
| 3917 |
if( output == null ) { |
| 3918 |
console.log( "Port #" + $0 + " could not be found."); |
| 3919 |
return; |
| 3920 |
} |
| 3921 |
var buf = new ArrayBuffer ($2); |
| 3922 |
var msg = new Uint8Array( buf ); |
| 3923 |
msg.set( new Uint8Array( Module.HEAPU8.buffer.slice( $1, $1 + $2 ) ) ); |
| 3924 |
output.send( msg ); |
| 3925 |
}, open_port_number, message, size ); |
| 3926 |
} |
| 3927 |
|
| 3928 |
void MidiOutWeb::initialize( const std::string& clientName ) |
| 3929 |
{ |
| 3930 |
if ( shim.get() != nullptr ) |
| 3931 |
return; |
| 3932 |
shim.reset( new WebMidiAccessShim() ); |
| 3933 |
setClientName( clientName ); |
| 3934 |
} |
| 3935 |
|
| 3936 |
#endif // __WEB_MIDI_API__ |