Moved from custom ALSA adapter to RtMidi
This should allow for easy support for other operating systems and other Linux sound systems like Jack in the future
This commit is contained in:
parent
6389535443
commit
57ac46cf9b
@ -10,10 +10,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
### Added
|
||||
|
||||
* Added `scan` builtin, which computes prefix sum of passed values when provided with addition operator
|
||||
* Added [rtmidi](https://github.com/thestk/rtmidi/) dependency which should provide multiplatform MIDI support
|
||||
|
||||
### Changed
|
||||
|
||||
* Integrated libmidi library into Musique codebase
|
||||
* Moved from custom ALSA interaction to using rtmidi for MIDI I/O operations
|
||||
|
||||
### Removed
|
||||
|
||||
* Support for incoming MIDI messages handling due to poor implementation that didn't statisfy user needs
|
||||
|
||||
### Fixed
|
||||
|
||||
|
5
Makefile
5
Makefile
@ -13,6 +13,11 @@ bin/bestline.o: lib/bestline/bestline.c lib/bestline/bestline.h
|
||||
@echo "CC $@"
|
||||
@$(CC) $< -c -O3 -o $@
|
||||
|
||||
# http://www.music.mcgill.ca/~gary/rtmidi/#compiling
|
||||
bin/rtmidi.o: lib/rtmidi/RtMidi.cpp lib/rtmidi/RtMidi.h
|
||||
@echo "CXX $@"
|
||||
@$(CXX) $< -c -O2 -o $@ -D__LINUX_ALSA__
|
||||
|
||||
doc: Doxyfile musique/*.cc musique/*.hh
|
||||
doxygen
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)"
|
||||
|
||||
CXXFLAGS:=$(CXXFLAGS) -std=c++20 -Wall -Wextra -Werror=switch -Werror=return-type -Werror=unused-result -Wno-maybe-uninitialized
|
||||
CPPFLAGS:=$(CPPFLAGS) -Ilib/expected/ -I. -Ilib/bestline/
|
||||
CPPFLAGS:=$(CPPFLAGS) -Ilib/expected/ -I. -Ilib/bestline/ -Ilib/rtmidi/
|
||||
|
||||
RELEASE_FLAGS=-O2
|
||||
DEBUG_FLAGS=-O0 -ggdb -fsanitize=undefined -DDebug
|
||||
|
4
lib/rtmidi/.gitattributes
vendored
Normal file
4
lib/rtmidi/.gitattributes
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# make sure that .gitignore, .travis.yml,... are not part of a
|
||||
# source-package generated via 'git archive'
|
||||
.git* export-ignore
|
||||
/.* export-ignore
|
64
lib/rtmidi/.gitignore
vendored
Normal file
64
lib/rtmidi/.gitignore
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
/aclocal.m4
|
||||
/autom4te.cache
|
||||
/config
|
||||
/config.log
|
||||
/config.status
|
||||
/configure
|
||||
/libtool
|
||||
/rtmidi-config
|
||||
/rtmidi.pc
|
||||
/m4
|
||||
|
||||
/Makefile
|
||||
/Makefile.in
|
||||
/doc/Makefile
|
||||
/doc/Makefile.in
|
||||
/tests/Makefile
|
||||
/tests/Makefile.in
|
||||
|
||||
/doc/html
|
||||
/doc/doxygen-build.stamp
|
||||
/doc/doxygen/Doxyfile
|
||||
|
||||
/build*/
|
||||
|
||||
/.deps
|
||||
/.libs
|
||||
/tests/.deps
|
||||
/tests/.libs
|
||||
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# Go
|
||||
!/contrib/go/rtmidi/go.mod
|
27
lib/rtmidi/LICENSE
Normal file
27
lib/rtmidi/LICENSE
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
RtMidi: realtime MIDI i/o C++ classes
|
||||
Copyright (c) 2003-2021 Gary P. Scavone
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
Any person wishing to distribute modifications to the Software is
|
||||
asked to send the modifications to the original developer so that
|
||||
they can be incorporated into the canonical version. This is,
|
||||
however, not a binding provision of this license.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
||||
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
3938
lib/rtmidi/RtMidi.cpp
Normal file
3938
lib/rtmidi/RtMidi.cpp
Normal file
File diff suppressed because it is too large
Load Diff
658
lib/rtmidi/RtMidi.h
Normal file
658
lib/rtmidi/RtMidi.h
Normal file
@ -0,0 +1,658 @@
|
||||
/**********************************************************************/
|
||||
/*! \class RtMidi
|
||||
\brief An abstract base class for realtime MIDI input/output.
|
||||
|
||||
This class implements some common functionality for the realtime
|
||||
MIDI input/output subclasses RtMidiIn and RtMidiOut.
|
||||
|
||||
RtMidi GitHub site: https://github.com/thestk/rtmidi
|
||||
RtMidi WWW site: http://www.music.mcgill.ca/~gary/rtmidi/
|
||||
|
||||
RtMidi: realtime MIDI i/o C++ classes
|
||||
Copyright (c) 2003-2021 Gary P. Scavone
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
Any person wishing to distribute modifications to the Software is
|
||||
asked to send the modifications to the original developer so that
|
||||
they can be incorporated into the canonical version. This is,
|
||||
however, not a binding provision of this license.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
||||
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
/**********************************************************************/
|
||||
|
||||
/*!
|
||||
\file RtMidi.h
|
||||
*/
|
||||
|
||||
#ifndef RTMIDI_H
|
||||
#define RTMIDI_H
|
||||
|
||||
#if defined _WIN32 || defined __CYGWIN__
|
||||
#if defined(RTMIDI_EXPORT)
|
||||
#define RTMIDI_DLL_PUBLIC __declspec(dllexport)
|
||||
#else
|
||||
#define RTMIDI_DLL_PUBLIC
|
||||
#endif
|
||||
#else
|
||||
#if __GNUC__ >= 4
|
||||
#define RTMIDI_DLL_PUBLIC __attribute__( (visibility( "default" )) )
|
||||
#else
|
||||
#define RTMIDI_DLL_PUBLIC
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define RTMIDI_VERSION "5.0.0"
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/*! \class RtMidiError
|
||||
\brief Exception handling class for RtMidi.
|
||||
|
||||
The RtMidiError class is quite simple but it does allow errors to be
|
||||
"caught" by RtMidiError::Type. See the RtMidi documentation to know
|
||||
which methods can throw an RtMidiError.
|
||||
*/
|
||||
/************************************************************************/
|
||||
|
||||
class RTMIDI_DLL_PUBLIC RtMidiError : public std::exception
|
||||
{
|
||||
public:
|
||||
//! Defined RtMidiError types.
|
||||
enum Type {
|
||||
WARNING, /*!< A non-critical error. */
|
||||
DEBUG_WARNING, /*!< A non-critical error which might be useful for debugging. */
|
||||
UNSPECIFIED, /*!< The default, unspecified error type. */
|
||||
NO_DEVICES_FOUND, /*!< No devices found on system. */
|
||||
INVALID_DEVICE, /*!< An invalid device ID was specified. */
|
||||
MEMORY_ERROR, /*!< An error occurred during memory allocation. */
|
||||
INVALID_PARAMETER, /*!< An invalid parameter was specified to a function. */
|
||||
INVALID_USE, /*!< The function was called incorrectly. */
|
||||
DRIVER_ERROR, /*!< A system driver error occurred. */
|
||||
SYSTEM_ERROR, /*!< A system error occurred. */
|
||||
THREAD_ERROR /*!< A thread error occurred. */
|
||||
};
|
||||
|
||||
//! The constructor.
|
||||
RtMidiError( const std::string& message, Type type = RtMidiError::UNSPECIFIED ) throw()
|
||||
: message_(message), type_(type) {}
|
||||
|
||||
//! The destructor.
|
||||
virtual ~RtMidiError( void ) throw() {}
|
||||
|
||||
//! Prints thrown error message to stderr.
|
||||
virtual void printMessage( void ) const throw() { std::cerr << '\n' << message_ << "\n\n"; }
|
||||
|
||||
//! Returns the thrown error message type.
|
||||
virtual const Type& getType( void ) const throw() { return type_; }
|
||||
|
||||
//! Returns the thrown error message string.
|
||||
virtual const std::string& getMessage( void ) const throw() { return message_; }
|
||||
|
||||
//! Returns the thrown error message as a c-style string.
|
||||
virtual const char* what( void ) const throw() { return message_.c_str(); }
|
||||
|
||||
protected:
|
||||
std::string message_;
|
||||
Type type_;
|
||||
};
|
||||
|
||||
//! RtMidi error callback function prototype.
|
||||
/*!
|
||||
\param type Type of error.
|
||||
\param errorText Error description.
|
||||
|
||||
Note that class behaviour is undefined after a critical error (not
|
||||
a warning) is reported.
|
||||
*/
|
||||
typedef void (*RtMidiErrorCallback)( RtMidiError::Type type, const std::string &errorText, void *userData );
|
||||
|
||||
class MidiApi;
|
||||
|
||||
class RTMIDI_DLL_PUBLIC RtMidi
|
||||
{
|
||||
public:
|
||||
|
||||
RtMidi(RtMidi&& other) noexcept;
|
||||
//! MIDI API specifier arguments.
|
||||
enum Api {
|
||||
UNSPECIFIED, /*!< Search for a working compiled API. */
|
||||
MACOSX_CORE, /*!< Macintosh OS-X CoreMIDI API. */
|
||||
LINUX_ALSA, /*!< The Advanced Linux Sound Architecture API. */
|
||||
UNIX_JACK, /*!< The JACK Low-Latency MIDI Server API. */
|
||||
WINDOWS_MM, /*!< The Microsoft Multimedia MIDI API. */
|
||||
RTMIDI_DUMMY, /*!< A compilable but non-functional API. */
|
||||
WEB_MIDI_API, /*!< W3C Web MIDI API. */
|
||||
NUM_APIS /*!< Number of values in this enum. */
|
||||
};
|
||||
|
||||
//! A static function to determine the current RtMidi version.
|
||||
static std::string getVersion( void ) throw();
|
||||
|
||||
//! A static function to determine the available compiled MIDI APIs.
|
||||
/*!
|
||||
The values returned in the std::vector can be compared against
|
||||
the enumerated list values. Note that there can be more than one
|
||||
API compiled for certain operating systems.
|
||||
*/
|
||||
static void getCompiledApi( std::vector<RtMidi::Api> &apis ) throw();
|
||||
|
||||
//! Return the name of a specified compiled MIDI API.
|
||||
/*!
|
||||
This obtains a short lower-case name used for identification purposes.
|
||||
This value is guaranteed to remain identical across library versions.
|
||||
If the API is unknown, this function will return the empty string.
|
||||
*/
|
||||
static std::string getApiName( RtMidi::Api api );
|
||||
|
||||
//! Return the display name of a specified compiled MIDI API.
|
||||
/*!
|
||||
This obtains a long name used for display purposes.
|
||||
If the API is unknown, this function will return the empty string.
|
||||
*/
|
||||
static std::string getApiDisplayName( RtMidi::Api api );
|
||||
|
||||
//! Return the compiled MIDI API having the given name.
|
||||
/*!
|
||||
A case insensitive comparison will check the specified name
|
||||
against the list of compiled APIs, and return the one which
|
||||
matches. On failure, the function returns UNSPECIFIED.
|
||||
*/
|
||||
static RtMidi::Api getCompiledApiByName( const std::string &name );
|
||||
|
||||
//! Pure virtual openPort() function.
|
||||
virtual void openPort( unsigned int portNumber = 0, const std::string &portName = std::string( "RtMidi" ) ) = 0;
|
||||
|
||||
//! Pure virtual openVirtualPort() function.
|
||||
virtual void openVirtualPort( const std::string &portName = std::string( "RtMidi" ) ) = 0;
|
||||
|
||||
//! Pure virtual getPortCount() function.
|
||||
virtual unsigned int getPortCount() = 0;
|
||||
|
||||
//! Pure virtual getPortName() function.
|
||||
virtual std::string getPortName( unsigned int portNumber = 0 ) = 0;
|
||||
|
||||
//! Pure virtual closePort() function.
|
||||
virtual void closePort( void ) = 0;
|
||||
|
||||
void setClientName( const std::string &clientName );
|
||||
void setPortName( const std::string &portName );
|
||||
|
||||
//! Returns true if a port is open and false if not.
|
||||
/*!
|
||||
Note that this only applies to connections made with the openPort()
|
||||
function, not to virtual ports.
|
||||
*/
|
||||
virtual bool isPortOpen( void ) const = 0;
|
||||
|
||||
//! Set an error callback function to be invoked when an error has occurred.
|
||||
/*!
|
||||
The callback function will be called whenever an error has occurred. It is best
|
||||
to set the error callback function before opening a port.
|
||||
*/
|
||||
virtual void setErrorCallback( RtMidiErrorCallback errorCallback = NULL, void *userData = 0 ) = 0;
|
||||
|
||||
protected:
|
||||
RtMidi();
|
||||
virtual ~RtMidi();
|
||||
MidiApi *rtapi_;
|
||||
|
||||
/* Make the class non-copyable */
|
||||
RtMidi(RtMidi& other) = delete;
|
||||
RtMidi& operator=(RtMidi& other) = delete;
|
||||
};
|
||||
|
||||
/**********************************************************************/
|
||||
/*! \class RtMidiIn
|
||||
\brief A realtime MIDI input class.
|
||||
|
||||
This class provides a common, platform-independent API for
|
||||
realtime MIDI input. It allows access to a single MIDI input
|
||||
port. Incoming MIDI messages are either saved to a queue for
|
||||
retrieval using the getMessage() function or immediately passed to
|
||||
a user-specified callback function. Create multiple instances of
|
||||
this class to connect to more than one MIDI device at the same
|
||||
time. With the OS-X, Linux ALSA, and JACK MIDI APIs, it is also
|
||||
possible to open a virtual input port to which other MIDI software
|
||||
clients can connect.
|
||||
*/
|
||||
/**********************************************************************/
|
||||
|
||||
// **************************************************************** //
|
||||
//
|
||||
// RtMidiIn and RtMidiOut class declarations.
|
||||
//
|
||||
// RtMidiIn / RtMidiOut are "controllers" used to select an available
|
||||
// MIDI input or output interface. They present common APIs for the
|
||||
// user to call but all functionality is implemented by the classes
|
||||
// MidiInApi, MidiOutApi and their subclasses. RtMidiIn and RtMidiOut
|
||||
// each create an instance of a MidiInApi or MidiOutApi subclass based
|
||||
// on the user's API choice. If no choice is made, they attempt to
|
||||
// make a "logical" API selection.
|
||||
//
|
||||
// **************************************************************** //
|
||||
|
||||
class RTMIDI_DLL_PUBLIC RtMidiIn : public RtMidi
|
||||
{
|
||||
public:
|
||||
//! User callback function type definition.
|
||||
typedef void (*RtMidiCallback)( double timeStamp, std::vector<unsigned char> *message, void *userData );
|
||||
|
||||
//! Default constructor that allows an optional api, client name and queue size.
|
||||
/*!
|
||||
An exception will be thrown if a MIDI system initialization
|
||||
error occurs. The queue size defines the maximum number of
|
||||
messages that can be held in the MIDI queue (when not using a
|
||||
callback function). If the queue size limit is reached,
|
||||
incoming messages will be ignored.
|
||||
|
||||
If no API argument is specified and multiple API support has been
|
||||
compiled, the default order of use is ALSA, JACK (Linux) and CORE,
|
||||
JACK (OS-X).
|
||||
|
||||
\param api An optional API id can be specified.
|
||||
\param clientName An optional client name can be specified. This
|
||||
will be used to group the ports that are created
|
||||
by the application.
|
||||
\param queueSizeLimit An optional size of the MIDI input queue can be specified.
|
||||
*/
|
||||
RtMidiIn( RtMidi::Api api=UNSPECIFIED,
|
||||
const std::string& clientName = "RtMidi Input Client",
|
||||
unsigned int queueSizeLimit = 100 );
|
||||
|
||||
RtMidiIn(RtMidiIn&& other) noexcept : RtMidi(std::move(other)) { }
|
||||
|
||||
//! If a MIDI connection is still open, it will be closed by the destructor.
|
||||
~RtMidiIn ( void ) throw();
|
||||
|
||||
//! Returns the MIDI API specifier for the current instance of RtMidiIn.
|
||||
RtMidi::Api getCurrentApi( void ) throw();
|
||||
|
||||
//! Open a MIDI input connection given by enumeration number.
|
||||
/*!
|
||||
\param portNumber An optional port number greater than 0 can be specified.
|
||||
Otherwise, the default or first port found is opened.
|
||||
\param portName An optional name for the application port that is used to connect to portId can be specified.
|
||||
*/
|
||||
void openPort( unsigned int portNumber = 0, const std::string &portName = std::string( "RtMidi Input" ) );
|
||||
|
||||
//! Create a virtual input port, with optional name, to allow software connections (OS X, JACK and ALSA only).
|
||||
/*!
|
||||
This function creates a virtual MIDI input port to which other
|
||||
software applications can connect. This type of functionality
|
||||
is currently only supported by the Macintosh OS-X, any JACK,
|
||||
and Linux ALSA APIs (the function returns an error for the other APIs).
|
||||
|
||||
\param portName An optional name for the application port that is
|
||||
used to connect to portId can be specified.
|
||||
*/
|
||||
void openVirtualPort( const std::string &portName = std::string( "RtMidi Input" ) );
|
||||
|
||||
//! Set a callback function to be invoked for incoming MIDI messages.
|
||||
/*!
|
||||
The callback function will be called whenever an incoming MIDI
|
||||
message is received. While not absolutely necessary, it is best
|
||||
to set the callback function before opening a MIDI port to avoid
|
||||
leaving some messages in the queue.
|
||||
|
||||
\param callback A callback function must be given.
|
||||
\param userData Optionally, a pointer to additional data can be
|
||||
passed to the callback function whenever it is called.
|
||||
*/
|
||||
void setCallback( RtMidiCallback callback, void *userData = 0 );
|
||||
|
||||
//! Cancel use of the current callback function (if one exists).
|
||||
/*!
|
||||
Subsequent incoming MIDI messages will be written to the queue
|
||||
and can be retrieved with the \e getMessage function.
|
||||
*/
|
||||
void cancelCallback();
|
||||
|
||||
//! Close an open MIDI connection (if one exists).
|
||||
void closePort( void );
|
||||
|
||||
//! Returns true if a port is open and false if not.
|
||||
/*!
|
||||
Note that this only applies to connections made with the openPort()
|
||||
function, not to virtual ports.
|
||||
*/
|
||||
virtual bool isPortOpen() const;
|
||||
|
||||
//! Return the number of available MIDI input ports.
|
||||
/*!
|
||||
\return This function returns the number of MIDI ports of the selected API.
|
||||
*/
|
||||
unsigned int getPortCount();
|
||||
|
||||
//! Return a string identifier for the specified MIDI input port number.
|
||||
/*!
|
||||
\return The name of the port with the given Id is returned.
|
||||
\retval An empty string is returned if an invalid port specifier
|
||||
is provided. User code should assume a UTF-8 encoding.
|
||||
*/
|
||||
std::string getPortName( unsigned int portNumber = 0 );
|
||||
|
||||
//! Specify whether certain MIDI message types should be queued or ignored during input.
|
||||
/*!
|
||||
By default, MIDI timing and active sensing messages are ignored
|
||||
during message input because of their relative high data rates.
|
||||
MIDI sysex messages are ignored by default as well. Variable
|
||||
values of "true" imply that the respective message type will be
|
||||
ignored.
|
||||
*/
|
||||
void ignoreTypes( bool midiSysex = true, bool midiTime = true, bool midiSense = true );
|
||||
|
||||
//! Fill the user-provided vector with the data bytes for the next available MIDI message in the input queue and return the event delta-time in seconds.
|
||||
/*!
|
||||
This function returns immediately whether a new message is
|
||||
available or not. A valid message is indicated by a non-zero
|
||||
vector size. An exception is thrown if an error occurs during
|
||||
message retrieval or an input connection was not previously
|
||||
established.
|
||||
*/
|
||||
double getMessage( std::vector<unsigned char> *message );
|
||||
|
||||
//! Set an error callback function to be invoked when an error has occurred.
|
||||
/*!
|
||||
The callback function will be called whenever an error has occurred. It is best
|
||||
to set the error callback function before opening a port.
|
||||
*/
|
||||
virtual void setErrorCallback( RtMidiErrorCallback errorCallback = NULL, void *userData = 0 );
|
||||
|
||||
//! Set maximum expected incoming message size.
|
||||
/*!
|
||||
For APIs that require manual buffer management, it can be useful to set the buffer
|
||||
size and buffer count when expecting to receive large SysEx messages. Note that
|
||||
currently this function has no effect when called after openPort(). The default
|
||||
buffer size is 1024 with a count of 4 buffers, which should be sufficient for most
|
||||
cases; as mentioned, this does not affect all API backends, since most either support
|
||||
dynamically scalable buffers or take care of buffer handling themselves. It is
|
||||
principally intended for users of the Windows MM backend who must support receiving
|
||||
especially large messages.
|
||||
*/
|
||||
virtual void setBufferSize( unsigned int size, unsigned int count );
|
||||
|
||||
protected:
|
||||
void openMidiApi( RtMidi::Api api, const std::string &clientName, unsigned int queueSizeLimit );
|
||||
};
|
||||
|
||||
/**********************************************************************/
|
||||
/*! \class RtMidiOut
|
||||
\brief A realtime MIDI output class.
|
||||
|
||||
This class provides a common, platform-independent API for MIDI
|
||||
output. It allows one to probe available MIDI output ports, to
|
||||
connect to one such port, and to send MIDI bytes immediately over
|
||||
the connection. Create multiple instances of this class to
|
||||
connect to more than one MIDI device at the same time. With the
|
||||
OS-X, Linux ALSA and JACK MIDI APIs, it is also possible to open a
|
||||
virtual port to which other MIDI software clients can connect.
|
||||
*/
|
||||
/**********************************************************************/
|
||||
|
||||
class RTMIDI_DLL_PUBLIC RtMidiOut : public RtMidi
|
||||
{
|
||||
public:
|
||||
//! Default constructor that allows an optional client name.
|
||||
/*!
|
||||
An exception will be thrown if a MIDI system initialization error occurs.
|
||||
|
||||
If no API argument is specified and multiple API support has been
|
||||
compiled, the default order of use is ALSA, JACK (Linux) and CORE,
|
||||
JACK (OS-X).
|
||||
*/
|
||||
RtMidiOut( RtMidi::Api api=UNSPECIFIED,
|
||||
const std::string& clientName = "RtMidi Output Client" );
|
||||
|
||||
RtMidiOut(RtMidiOut&& other) noexcept : RtMidi(std::move(other)) { }
|
||||
|
||||
//! The destructor closes any open MIDI connections.
|
||||
~RtMidiOut( void ) throw();
|
||||
|
||||
//! Returns the MIDI API specifier for the current instance of RtMidiOut.
|
||||
RtMidi::Api getCurrentApi( void ) throw();
|
||||
|
||||
//! Open a MIDI output connection.
|
||||
/*!
|
||||
An optional port number greater than 0 can be specified.
|
||||
Otherwise, the default or first port found is opened. An
|
||||
exception is thrown if an error occurs while attempting to make
|
||||
the port connection.
|
||||
*/
|
||||
void openPort( unsigned int portNumber = 0, const std::string &portName = std::string( "RtMidi Output" ) );
|
||||
|
||||
//! Close an open MIDI connection (if one exists).
|
||||
void closePort( void );
|
||||
|
||||
//! Returns true if a port is open and false if not.
|
||||
/*!
|
||||
Note that this only applies to connections made with the openPort()
|
||||
function, not to virtual ports.
|
||||
*/
|
||||
virtual bool isPortOpen() const;
|
||||
|
||||
//! Create a virtual output port, with optional name, to allow software connections (OS X, JACK and ALSA only).
|
||||
/*!
|
||||
This function creates a virtual MIDI output port to which other
|
||||
software applications can connect. This type of functionality
|
||||
is currently only supported by the Macintosh OS-X, Linux ALSA
|
||||
and JACK APIs (the function does nothing with the other APIs).
|
||||
An exception is thrown if an error occurs while attempting to
|
||||
create the virtual port.
|
||||
*/
|
||||
void openVirtualPort( const std::string &portName = std::string( "RtMidi Output" ) );
|
||||
|
||||
//! Return the number of available MIDI output ports.
|
||||
unsigned int getPortCount( void );
|
||||
|
||||
//! Return a string identifier for the specified MIDI port type and number.
|
||||
/*!
|
||||
\return The name of the port with the given Id is returned.
|
||||
\retval An empty string is returned if an invalid port specifier
|
||||
is provided. User code should assume a UTF-8 encoding.
|
||||
*/
|
||||
std::string getPortName( unsigned int portNumber = 0 );
|
||||
|
||||
//! Immediately send a single message out an open MIDI output port.
|
||||
/*!
|
||||
An exception is thrown if an error occurs during output or an
|
||||
output connection was not previously established.
|
||||
*/
|
||||
void sendMessage( const std::vector<unsigned char> *message );
|
||||
|
||||
//! Immediately send a single message out an open MIDI output port.
|
||||
/*!
|
||||
An exception is thrown if an error occurs during output or an
|
||||
output connection was not previously established.
|
||||
|
||||
\param message A pointer to the MIDI message as raw bytes
|
||||
\param size Length of the MIDI message in bytes
|
||||
*/
|
||||
void sendMessage( const unsigned char *message, size_t size );
|
||||
|
||||
//! Set an error callback function to be invoked when an error has occurred.
|
||||
/*!
|
||||
The callback function will be called whenever an error has occurred. It is best
|
||||
to set the error callback function before opening a port.
|
||||
*/
|
||||
virtual void setErrorCallback( RtMidiErrorCallback errorCallback = NULL, void *userData = 0 );
|
||||
|
||||
protected:
|
||||
void openMidiApi( RtMidi::Api api, const std::string &clientName );
|
||||
};
|
||||
|
||||
|
||||
// **************************************************************** //
|
||||
//
|
||||
// MidiInApi / MidiOutApi class declarations.
|
||||
//
|
||||
// Subclasses of MidiInApi and MidiOutApi contain all API- and
|
||||
// OS-specific code necessary to fully implement the RtMidi API.
|
||||
//
|
||||
// Note that MidiInApi and MidiOutApi are abstract base classes and
|
||||
// cannot be explicitly instantiated. RtMidiIn and RtMidiOut will
|
||||
// create instances of a MidiInApi or MidiOutApi subclass.
|
||||
//
|
||||
// **************************************************************** //
|
||||
|
||||
class RTMIDI_DLL_PUBLIC MidiApi
|
||||
{
|
||||
public:
|
||||
|
||||
MidiApi();
|
||||
virtual ~MidiApi();
|
||||
virtual RtMidi::Api getCurrentApi( void ) = 0;
|
||||
virtual void openPort( unsigned int portNumber, const std::string &portName ) = 0;
|
||||
virtual void openVirtualPort( const std::string &portName ) = 0;
|
||||
virtual void closePort( void ) = 0;
|
||||
virtual void setClientName( const std::string &clientName ) = 0;
|
||||
virtual void setPortName( const std::string &portName ) = 0;
|
||||
|
||||
virtual unsigned int getPortCount( void ) = 0;
|
||||
virtual std::string getPortName( unsigned int portNumber ) = 0;
|
||||
|
||||
inline bool isPortOpen() const { return connected_; }
|
||||
void setErrorCallback( RtMidiErrorCallback errorCallback, void *userData );
|
||||
|
||||
//! A basic error reporting function for RtMidi classes.
|
||||
void error( RtMidiError::Type type, std::string errorString );
|
||||
|
||||
protected:
|
||||
virtual void initialize( const std::string& clientName ) = 0;
|
||||
|
||||
void *apiData_;
|
||||
bool connected_;
|
||||
std::string errorString_;
|
||||
RtMidiErrorCallback errorCallback_;
|
||||
bool firstErrorOccurred_;
|
||||
void *errorCallbackUserData_;
|
||||
|
||||
};
|
||||
|
||||
class RTMIDI_DLL_PUBLIC MidiInApi : public MidiApi
|
||||
{
|
||||
public:
|
||||
|
||||
MidiInApi( unsigned int queueSizeLimit );
|
||||
virtual ~MidiInApi( void );
|
||||
void setCallback( RtMidiIn::RtMidiCallback callback, void *userData );
|
||||
void cancelCallback( void );
|
||||
virtual void ignoreTypes( bool midiSysex, bool midiTime, bool midiSense );
|
||||
double getMessage( std::vector<unsigned char> *message );
|
||||
virtual void setBufferSize( unsigned int size, unsigned int count );
|
||||
|
||||
// A MIDI structure used internally by the class to store incoming
|
||||
// messages. Each message represents one and only one MIDI message.
|
||||
struct MidiMessage {
|
||||
std::vector<unsigned char> bytes;
|
||||
|
||||
//! Time in seconds elapsed since the previous message
|
||||
double timeStamp;
|
||||
|
||||
// Default constructor.
|
||||
MidiMessage()
|
||||
: bytes(0), timeStamp(0.0) {}
|
||||
};
|
||||
|
||||
struct MidiQueue {
|
||||
unsigned int front;
|
||||
unsigned int back;
|
||||
unsigned int ringSize;
|
||||
MidiMessage *ring;
|
||||
|
||||
// Default constructor.
|
||||
MidiQueue()
|
||||
: front(0), back(0), ringSize(0), ring(0) {}
|
||||
bool push( const MidiMessage& );
|
||||
bool pop( std::vector<unsigned char>*, double* );
|
||||
unsigned int size( unsigned int *back=0, unsigned int *front=0 );
|
||||
};
|
||||
|
||||
// The RtMidiInData structure is used to pass private class data to
|
||||
// the MIDI input handling function or thread.
|
||||
struct RtMidiInData {
|
||||
MidiQueue queue;
|
||||
MidiMessage message;
|
||||
unsigned char ignoreFlags;
|
||||
bool doInput;
|
||||
bool firstMessage;
|
||||
void *apiData;
|
||||
bool usingCallback;
|
||||
RtMidiIn::RtMidiCallback userCallback;
|
||||
void *userData;
|
||||
bool continueSysex;
|
||||
unsigned int bufferSize;
|
||||
unsigned int bufferCount;
|
||||
|
||||
// Default constructor.
|
||||
RtMidiInData()
|
||||
: ignoreFlags(7), doInput(false), firstMessage(true), apiData(0), usingCallback(false),
|
||||
userCallback(0), userData(0), continueSysex(false), bufferSize(1024), bufferCount(4) {}
|
||||
};
|
||||
|
||||
protected:
|
||||
RtMidiInData inputData_;
|
||||
};
|
||||
|
||||
class RTMIDI_DLL_PUBLIC MidiOutApi : public MidiApi
|
||||
{
|
||||
public:
|
||||
|
||||
MidiOutApi( void );
|
||||
virtual ~MidiOutApi( void );
|
||||
virtual void sendMessage( const unsigned char *message, size_t size ) = 0;
|
||||
};
|
||||
|
||||
// **************************************************************** //
|
||||
//
|
||||
// Inline RtMidiIn and RtMidiOut definitions.
|
||||
//
|
||||
// **************************************************************** //
|
||||
|
||||
inline RtMidi::Api RtMidiIn :: getCurrentApi( void ) throw() { return rtapi_->getCurrentApi(); }
|
||||
inline void RtMidiIn :: openPort( unsigned int portNumber, const std::string &portName ) { rtapi_->openPort( portNumber, portName ); }
|
||||
inline void RtMidiIn :: openVirtualPort( const std::string &portName ) { rtapi_->openVirtualPort( portName ); }
|
||||
inline void RtMidiIn :: closePort( void ) { rtapi_->closePort(); }
|
||||
inline bool RtMidiIn :: isPortOpen() const { return rtapi_->isPortOpen(); }
|
||||
inline void RtMidiIn :: setCallback( RtMidiCallback callback, void *userData ) { static_cast<MidiInApi *>(rtapi_)->setCallback( callback, userData ); }
|
||||
inline void RtMidiIn :: cancelCallback( void ) { static_cast<MidiInApi *>(rtapi_)->cancelCallback(); }
|
||||
inline unsigned int RtMidiIn :: getPortCount( void ) { return rtapi_->getPortCount(); }
|
||||
inline std::string RtMidiIn :: getPortName( unsigned int portNumber ) { return rtapi_->getPortName( portNumber ); }
|
||||
inline void RtMidiIn :: ignoreTypes( bool midiSysex, bool midiTime, bool midiSense ) { static_cast<MidiInApi *>(rtapi_)->ignoreTypes( midiSysex, midiTime, midiSense ); }
|
||||
inline double RtMidiIn :: getMessage( std::vector<unsigned char> *message ) { return static_cast<MidiInApi *>(rtapi_)->getMessage( message ); }
|
||||
inline void RtMidiIn :: setErrorCallback( RtMidiErrorCallback errorCallback, void *userData ) { rtapi_->setErrorCallback(errorCallback, userData); }
|
||||
inline void RtMidiIn :: setBufferSize( unsigned int size, unsigned int count ) { static_cast<MidiInApi *>(rtapi_)->setBufferSize(size, count); }
|
||||
|
||||
inline RtMidi::Api RtMidiOut :: getCurrentApi( void ) throw() { return rtapi_->getCurrentApi(); }
|
||||
inline void RtMidiOut :: openPort( unsigned int portNumber, const std::string &portName ) { rtapi_->openPort( portNumber, portName ); }
|
||||
inline void RtMidiOut :: openVirtualPort( const std::string &portName ) { rtapi_->openVirtualPort( portName ); }
|
||||
inline void RtMidiOut :: closePort( void ) { rtapi_->closePort(); }
|
||||
inline bool RtMidiOut :: isPortOpen() const { return rtapi_->isPortOpen(); }
|
||||
inline unsigned int RtMidiOut :: getPortCount( void ) { return rtapi_->getPortCount(); }
|
||||
inline std::string RtMidiOut :: getPortName( unsigned int portNumber ) { return rtapi_->getPortName( portNumber ); }
|
||||
inline void RtMidiOut :: sendMessage( const std::vector<unsigned char> *message ) { static_cast<MidiOutApi *>(rtapi_)->sendMessage( &message->at(0), message->size() ); }
|
||||
inline void RtMidiOut :: sendMessage( const unsigned char *message, size_t size ) { static_cast<MidiOutApi *>(rtapi_)->sendMessage( message, size ); }
|
||||
inline void RtMidiOut :: setErrorCallback( RtMidiErrorCallback errorCallback, void *userData ) { rtapi_->setErrorCallback(errorCallback, userData); }
|
||||
|
||||
#endif
|
19
lib/rtmidi/rtmidi-config.in
Normal file
19
lib/rtmidi/rtmidi-config.in
Normal file
@ -0,0 +1,19 @@
|
||||
#! /bin/sh
|
||||
if (test "x$#" != "x1") ; then
|
||||
echo "Usage: $0 [--libs | --cxxflags | --cppflags]"
|
||||
exit;
|
||||
fi
|
||||
|
||||
LIBRARY="@LIBS@"
|
||||
CXXFLAGS="@CXXFLAGS@"
|
||||
CPPFLAGS="@CPPFLAGS@"
|
||||
|
||||
if (test "x$1" = "x--libs") ; then
|
||||
echo "$LIBRARY -lrtmidi"
|
||||
elif (test "x$1" = "x--cxxflags") ; then
|
||||
echo "$CXXFLAGS"
|
||||
elif (test "x$1" = "x--cppflags") ; then
|
||||
echo "$CPPFLAGS"
|
||||
else
|
||||
echo "Unknown option: $1"
|
||||
fi
|
12
lib/rtmidi/rtmidi.pc.in
Normal file
12
lib/rtmidi/rtmidi.pc.in
Normal file
@ -0,0 +1,12 @@
|
||||
prefix=@prefix@
|
||||
exec_prefix=${prefix}
|
||||
libdir=${exec_prefix}/lib
|
||||
includedir=${prefix}/include/rtmidi
|
||||
|
||||
Name: librtmidi
|
||||
Description: RtMidi - a set of C++ classes that provide a common API for realtime MIDI input/output
|
||||
Version: @PACKAGE_VERSION@
|
||||
Requires.private: @req@
|
||||
Libs: -L${libdir} -lrtmidi
|
||||
Libs.private: -lpthread @req_libs@
|
||||
Cflags: -pthread -I${includedir} @api@
|
374
lib/rtmidi/rtmidi_c.cpp
Normal file
374
lib/rtmidi/rtmidi_c.cpp
Normal file
@ -0,0 +1,374 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "rtmidi_c.h"
|
||||
#include "RtMidi.h"
|
||||
|
||||
/* Compile-time assertions that will break if the enums are changed in
|
||||
* the future without synchronizing them properly. If you get (g++)
|
||||
* "error: ‘StaticEnumAssert<b>::StaticEnumAssert() [with bool b = false]’
|
||||
* is private within this context", it means enums are not aligned. */
|
||||
template<bool b> class StaticEnumAssert { private: StaticEnumAssert() {} };
|
||||
template<> class StaticEnumAssert<true>{ public: StaticEnumAssert() {} };
|
||||
#define ENUM_EQUAL(x,y) StaticEnumAssert<(int)x==(int)y>()
|
||||
class StaticEnumAssertions { StaticEnumAssertions() {
|
||||
ENUM_EQUAL( RTMIDI_API_UNSPECIFIED, RtMidi::UNSPECIFIED );
|
||||
ENUM_EQUAL( RTMIDI_API_MACOSX_CORE, RtMidi::MACOSX_CORE );
|
||||
ENUM_EQUAL( RTMIDI_API_LINUX_ALSA, RtMidi::LINUX_ALSA );
|
||||
ENUM_EQUAL( RTMIDI_API_UNIX_JACK, RtMidi::UNIX_JACK );
|
||||
ENUM_EQUAL( RTMIDI_API_WINDOWS_MM, RtMidi::WINDOWS_MM );
|
||||
ENUM_EQUAL( RTMIDI_API_RTMIDI_DUMMY, RtMidi::RTMIDI_DUMMY );
|
||||
|
||||
ENUM_EQUAL( RTMIDI_ERROR_WARNING, RtMidiError::WARNING );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_DEBUG_WARNING, RtMidiError::DEBUG_WARNING );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_UNSPECIFIED, RtMidiError::UNSPECIFIED );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_NO_DEVICES_FOUND, RtMidiError::NO_DEVICES_FOUND );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_INVALID_DEVICE, RtMidiError::INVALID_DEVICE );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_MEMORY_ERROR, RtMidiError::MEMORY_ERROR );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_INVALID_PARAMETER, RtMidiError::INVALID_PARAMETER );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_INVALID_USE, RtMidiError::INVALID_USE );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_DRIVER_ERROR, RtMidiError::DRIVER_ERROR );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_SYSTEM_ERROR, RtMidiError::SYSTEM_ERROR );
|
||||
ENUM_EQUAL( RTMIDI_ERROR_THREAD_ERROR, RtMidiError::THREAD_ERROR );
|
||||
}};
|
||||
|
||||
class CallbackProxyUserData
|
||||
{
|
||||
public:
|
||||
CallbackProxyUserData (RtMidiCCallback cCallback, void *userData)
|
||||
: c_callback (cCallback), user_data (userData)
|
||||
{
|
||||
}
|
||||
RtMidiCCallback c_callback;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifndef RTMIDI_SOURCE_INCLUDED
|
||||
extern "C" const enum RtMidiApi rtmidi_compiled_apis[]; // casting from RtMidi::Api[]
|
||||
#endif
|
||||
extern "C" const unsigned int rtmidi_num_compiled_apis;
|
||||
|
||||
/* RtMidi API */
|
||||
int rtmidi_get_compiled_api (enum RtMidiApi *apis, unsigned int apis_size)
|
||||
{
|
||||
unsigned num = rtmidi_num_compiled_apis;
|
||||
if (apis) {
|
||||
num = (num < apis_size) ? num : apis_size;
|
||||
memcpy(apis, rtmidi_compiled_apis, num * sizeof(enum RtMidiApi));
|
||||
}
|
||||
return (int)num;
|
||||
}
|
||||
|
||||
extern "C" const char* rtmidi_api_names[][2];
|
||||
const char *rtmidi_api_name(enum RtMidiApi api) {
|
||||
if (api < 0 || api >= RTMIDI_API_NUM)
|
||||
return NULL;
|
||||
return rtmidi_api_names[api][0];
|
||||
}
|
||||
|
||||
const char *rtmidi_api_display_name(enum RtMidiApi api)
|
||||
{
|
||||
if (api < 0 || api >= RTMIDI_API_NUM)
|
||||
return "Unknown";
|
||||
return rtmidi_api_names[api][1];
|
||||
}
|
||||
|
||||
enum RtMidiApi rtmidi_compiled_api_by_name(const char *name) {
|
||||
RtMidi::Api api = RtMidi::UNSPECIFIED;
|
||||
if (name) {
|
||||
api = RtMidi::getCompiledApiByName(name);
|
||||
}
|
||||
return (enum RtMidiApi)api;
|
||||
}
|
||||
|
||||
void rtmidi_error (MidiApi *api, enum RtMidiErrorType type, const char* errorString)
|
||||
{
|
||||
std::string msg = errorString;
|
||||
api->error ((RtMidiError::Type) type, msg);
|
||||
}
|
||||
|
||||
void rtmidi_open_port (RtMidiPtr device, unsigned int portNumber, const char *portName)
|
||||
{
|
||||
std::string name = portName;
|
||||
try {
|
||||
((RtMidi*) device->ptr)->openPort (portNumber, name);
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
}
|
||||
}
|
||||
|
||||
void rtmidi_open_virtual_port (RtMidiPtr device, const char *portName)
|
||||
{
|
||||
std::string name = portName;
|
||||
try {
|
||||
((RtMidi*) device->ptr)->openVirtualPort (name);
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rtmidi_close_port (RtMidiPtr device)
|
||||
{
|
||||
try {
|
||||
((RtMidi*) device->ptr)->closePort ();
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int rtmidi_get_port_count (RtMidiPtr device)
|
||||
{
|
||||
try {
|
||||
return ((RtMidi*) device->ptr)->getPortCount ();
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int rtmidi_get_port_name (RtMidiPtr device, unsigned int portNumber, char * bufOut, int * bufLen)
|
||||
{
|
||||
if (bufOut == nullptr && bufLen == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string name;
|
||||
try {
|
||||
name = ((RtMidi*) device->ptr)->getPortName (portNumber);
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bufOut == nullptr) {
|
||||
*bufLen = static_cast<int>(name.size()) + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return snprintf(bufOut, static_cast<size_t>(*bufLen), "%s", name.c_str());
|
||||
}
|
||||
|
||||
/* RtMidiIn API */
|
||||
RtMidiInPtr rtmidi_in_create_default ()
|
||||
{
|
||||
RtMidiWrapper* wrp = new RtMidiWrapper;
|
||||
|
||||
try {
|
||||
RtMidiIn* rIn = new RtMidiIn ();
|
||||
|
||||
wrp->ptr = (void*) rIn;
|
||||
wrp->data = 0;
|
||||
wrp->ok = true;
|
||||
wrp->msg = "";
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
wrp->ptr = 0;
|
||||
wrp->data = 0;
|
||||
wrp->ok = false;
|
||||
wrp->msg = err.what ();
|
||||
}
|
||||
|
||||
return wrp;
|
||||
}
|
||||
|
||||
RtMidiInPtr rtmidi_in_create (enum RtMidiApi api, const char *clientName, unsigned int queueSizeLimit)
|
||||
{
|
||||
std::string name = clientName;
|
||||
RtMidiWrapper* wrp = new RtMidiWrapper;
|
||||
|
||||
try {
|
||||
RtMidiIn* rIn = new RtMidiIn ((RtMidi::Api) api, name, queueSizeLimit);
|
||||
|
||||
wrp->ptr = (void*) rIn;
|
||||
wrp->data = 0;
|
||||
wrp->ok = true;
|
||||
wrp->msg = "";
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
wrp->ptr = 0;
|
||||
wrp->data = 0;
|
||||
wrp->ok = false;
|
||||
wrp->msg = err.what ();
|
||||
}
|
||||
|
||||
return wrp;
|
||||
}
|
||||
|
||||
void rtmidi_in_free (RtMidiInPtr device)
|
||||
{
|
||||
if (device->data)
|
||||
delete (CallbackProxyUserData*) device->data;
|
||||
delete (RtMidiIn*) device->ptr;
|
||||
delete device;
|
||||
}
|
||||
|
||||
enum RtMidiApi rtmidi_in_get_current_api (RtMidiPtr device)
|
||||
{
|
||||
try {
|
||||
return (RtMidiApi) ((RtMidiIn*) device->ptr)->getCurrentApi ();
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
|
||||
return RTMIDI_API_UNSPECIFIED;
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void callback_proxy (double timeStamp, std::vector<unsigned char> *message, void *userData)
|
||||
{
|
||||
CallbackProxyUserData* data = reinterpret_cast<CallbackProxyUserData*> (userData);
|
||||
data->c_callback (timeStamp, message->data (), message->size (), data->user_data);
|
||||
}
|
||||
|
||||
void rtmidi_in_set_callback (RtMidiInPtr device, RtMidiCCallback callback, void *userData)
|
||||
{
|
||||
device->data = (void*) new CallbackProxyUserData (callback, userData);
|
||||
try {
|
||||
((RtMidiIn*) device->ptr)->setCallback (callback_proxy, device->data);
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
delete (CallbackProxyUserData*) device->data;
|
||||
device->data = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void rtmidi_in_cancel_callback (RtMidiInPtr device)
|
||||
{
|
||||
try {
|
||||
((RtMidiIn*) device->ptr)->cancelCallback ();
|
||||
delete (CallbackProxyUserData*) device->data;
|
||||
device->data = 0;
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
}
|
||||
}
|
||||
|
||||
void rtmidi_in_ignore_types (RtMidiInPtr device, bool midiSysex, bool midiTime, bool midiSense)
|
||||
{
|
||||
((RtMidiIn*) device->ptr)->ignoreTypes (midiSysex, midiTime, midiSense);
|
||||
}
|
||||
|
||||
double rtmidi_in_get_message (RtMidiInPtr device,
|
||||
unsigned char *message,
|
||||
size_t *size)
|
||||
{
|
||||
try {
|
||||
// FIXME: use allocator to achieve efficient buffering
|
||||
std::vector<unsigned char> v;
|
||||
double ret = ((RtMidiIn*) device->ptr)->getMessage (&v);
|
||||
|
||||
if (v.size () > 0 && v.size() <= *size) {
|
||||
memcpy (message, v.data (), (int) v.size ());
|
||||
}
|
||||
|
||||
*size = v.size();
|
||||
return ret;
|
||||
}
|
||||
catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
device->ok = false;
|
||||
device->msg = "Unknown error";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* RtMidiOut API */
|
||||
RtMidiOutPtr rtmidi_out_create_default ()
|
||||
{
|
||||
RtMidiWrapper* wrp = new RtMidiWrapper;
|
||||
|
||||
try {
|
||||
RtMidiOut* rOut = new RtMidiOut ();
|
||||
|
||||
wrp->ptr = (void*) rOut;
|
||||
wrp->data = 0;
|
||||
wrp->ok = true;
|
||||
wrp->msg = "";
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
wrp->ptr = 0;
|
||||
wrp->data = 0;
|
||||
wrp->ok = false;
|
||||
wrp->msg = err.what ();
|
||||
}
|
||||
|
||||
return wrp;
|
||||
}
|
||||
|
||||
RtMidiOutPtr rtmidi_out_create (enum RtMidiApi api, const char *clientName)
|
||||
{
|
||||
RtMidiWrapper* wrp = new RtMidiWrapper;
|
||||
std::string name = clientName;
|
||||
|
||||
try {
|
||||
RtMidiOut* rOut = new RtMidiOut ((RtMidi::Api) api, name);
|
||||
|
||||
wrp->ptr = (void*) rOut;
|
||||
wrp->data = 0;
|
||||
wrp->ok = true;
|
||||
wrp->msg = "";
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
wrp->ptr = 0;
|
||||
wrp->data = 0;
|
||||
wrp->ok = false;
|
||||
wrp->msg = err.what ();
|
||||
}
|
||||
|
||||
|
||||
return wrp;
|
||||
}
|
||||
|
||||
void rtmidi_out_free (RtMidiOutPtr device)
|
||||
{
|
||||
delete (RtMidiOut*) device->ptr;
|
||||
delete device;
|
||||
}
|
||||
|
||||
enum RtMidiApi rtmidi_out_get_current_api (RtMidiPtr device)
|
||||
{
|
||||
try {
|
||||
return (RtMidiApi) ((RtMidiOut*) device->ptr)->getCurrentApi ();
|
||||
|
||||
} catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
|
||||
return RTMIDI_API_UNSPECIFIED;
|
||||
}
|
||||
}
|
||||
|
||||
int rtmidi_out_send_message (RtMidiOutPtr device, const unsigned char *message, int length)
|
||||
{
|
||||
try {
|
||||
((RtMidiOut*) device->ptr)->sendMessage (message, length);
|
||||
return 0;
|
||||
}
|
||||
catch (const RtMidiError & err) {
|
||||
device->ok = false;
|
||||
device->msg = err.what ();
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
device->ok = false;
|
||||
device->msg = "Unknown error";
|
||||
return -1;
|
||||
}
|
||||
}
|
251
lib/rtmidi/rtmidi_c.h
Normal file
251
lib/rtmidi/rtmidi_c.h
Normal file
@ -0,0 +1,251 @@
|
||||
/************************************************************************/
|
||||
/*! \defgroup C-interface
|
||||
@{
|
||||
|
||||
\brief C interface to realtime MIDI input/output C++ classes.
|
||||
|
||||
RtMidi offers a C-style interface, principally for use in binding
|
||||
RtMidi to other programming languages. All structs, enums, and
|
||||
functions listed here have direct analogs (and simply call to)
|
||||
items in the C++ RtMidi class and its supporting classes and
|
||||
types
|
||||
*/
|
||||
/************************************************************************/
|
||||
|
||||
/*!
|
||||
\file rtmidi_c.h
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#ifndef RTMIDI_C_H
|
||||
#define RTMIDI_C_H
|
||||
|
||||
#if defined(RTMIDI_EXPORT)
|
||||
#if defined _WIN32 || defined __CYGWIN__
|
||||
#define RTMIDIAPI __declspec(dllexport)
|
||||
#else
|
||||
#define RTMIDIAPI __attribute__((visibility("default")))
|
||||
#endif
|
||||
#else
|
||||
#define RTMIDIAPI //__declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//! \brief Wraps an RtMidi object for C function return statuses.
|
||||
struct RtMidiWrapper {
|
||||
//! The wrapped RtMidi object.
|
||||
void* ptr;
|
||||
void* data;
|
||||
|
||||
//! True when the last function call was OK.
|
||||
bool ok;
|
||||
|
||||
//! If an error occurred (ok != true), set to an error message.
|
||||
const char* msg;
|
||||
};
|
||||
|
||||
//! \brief Typedef for a generic RtMidi pointer.
|
||||
typedef struct RtMidiWrapper* RtMidiPtr;
|
||||
|
||||
//! \brief Typedef for a generic RtMidiIn pointer.
|
||||
typedef struct RtMidiWrapper* RtMidiInPtr;
|
||||
|
||||
//! \brief Typedef for a generic RtMidiOut pointer.
|
||||
typedef struct RtMidiWrapper* RtMidiOutPtr;
|
||||
|
||||
//! \brief MIDI API specifier arguments. See \ref RtMidi::Api.
|
||||
enum RtMidiApi {
|
||||
RTMIDI_API_UNSPECIFIED, /*!< Search for a working compiled API. */
|
||||
RTMIDI_API_MACOSX_CORE, /*!< Macintosh OS-X CoreMIDI API. */
|
||||
RTMIDI_API_LINUX_ALSA, /*!< The Advanced Linux Sound Architecture API. */
|
||||
RTMIDI_API_UNIX_JACK, /*!< The Jack Low-Latency MIDI Server API. */
|
||||
RTMIDI_API_WINDOWS_MM, /*!< The Microsoft Multimedia MIDI API. */
|
||||
RTMIDI_API_RTMIDI_DUMMY, /*!< A compilable but non-functional API. */
|
||||
RTMIDI_API_NUM /*!< Number of values in this enum. */
|
||||
};
|
||||
|
||||
//! \brief Defined RtMidiError types. See \ref RtMidiError::Type.
|
||||
enum RtMidiErrorType {
|
||||
RTMIDI_ERROR_WARNING, /*!< A non-critical error. */
|
||||
RTMIDI_ERROR_DEBUG_WARNING, /*!< A non-critical error which might be useful for debugging. */
|
||||
RTMIDI_ERROR_UNSPECIFIED, /*!< The default, unspecified error type. */
|
||||
RTMIDI_ERROR_NO_DEVICES_FOUND, /*!< No devices found on system. */
|
||||
RTMIDI_ERROR_INVALID_DEVICE, /*!< An invalid device ID was specified. */
|
||||
RTMIDI_ERROR_MEMORY_ERROR, /*!< An error occurred during memory allocation. */
|
||||
RTMIDI_ERROR_INVALID_PARAMETER, /*!< An invalid parameter was specified to a function. */
|
||||
RTMIDI_ERROR_INVALID_USE, /*!< The function was called incorrectly. */
|
||||
RTMIDI_ERROR_DRIVER_ERROR, /*!< A system driver error occurred. */
|
||||
RTMIDI_ERROR_SYSTEM_ERROR, /*!< A system error occurred. */
|
||||
RTMIDI_ERROR_THREAD_ERROR /*!< A thread error occurred. */
|
||||
};
|
||||
|
||||
/*! \brief The type of a RtMidi callback function.
|
||||
*
|
||||
* \param timeStamp The time at which the message has been received.
|
||||
* \param message The midi message.
|
||||
* \param userData Additional user data for the callback.
|
||||
*
|
||||
* See \ref RtMidiIn::RtMidiCallback.
|
||||
*/
|
||||
typedef void(* RtMidiCCallback) (double timeStamp, const unsigned char* message,
|
||||
size_t messageSize, void *userData);
|
||||
|
||||
|
||||
/* RtMidi API */
|
||||
|
||||
/*! \brief Determine the available compiled MIDI APIs.
|
||||
*
|
||||
* If the given `apis` parameter is null, returns the number of available APIs.
|
||||
* Otherwise, fill the given apis array with the RtMidi::Api values.
|
||||
*
|
||||
* \param apis An array or a null value.
|
||||
* \param apis_size Number of elements pointed to by apis
|
||||
* \return number of items needed for apis array if apis==NULL, or
|
||||
* number of items written to apis array otherwise. A negative
|
||||
* return value indicates an error.
|
||||
*
|
||||
* See \ref RtMidi::getCompiledApi().
|
||||
*/
|
||||
RTMIDIAPI int rtmidi_get_compiled_api (enum RtMidiApi *apis, unsigned int apis_size);
|
||||
|
||||
//! \brief Return the name of a specified compiled MIDI API.
|
||||
//! See \ref RtMidi::getApiName().
|
||||
RTMIDIAPI const char *rtmidi_api_name(enum RtMidiApi api);
|
||||
|
||||
//! \brief Return the display name of a specified compiled MIDI API.
|
||||
//! See \ref RtMidi::getApiDisplayName().
|
||||
RTMIDIAPI const char *rtmidi_api_display_name(enum RtMidiApi api);
|
||||
|
||||
//! \brief Return the compiled MIDI API having the given name.
|
||||
//! See \ref RtMidi::getCompiledApiByName().
|
||||
RTMIDIAPI enum RtMidiApi rtmidi_compiled_api_by_name(const char *name);
|
||||
|
||||
//! \internal Report an error.
|
||||
RTMIDIAPI void rtmidi_error (enum RtMidiErrorType type, const char* errorString);
|
||||
|
||||
/*! \brief Open a MIDI port.
|
||||
*
|
||||
* \param port Must be greater than 0
|
||||
* \param portName Name for the application port.
|
||||
*
|
||||
* See RtMidi::openPort().
|
||||
*/
|
||||
RTMIDIAPI void rtmidi_open_port (RtMidiPtr device, unsigned int portNumber, const char *portName);
|
||||
|
||||
/*! \brief Creates a virtual MIDI port to which other software applications can
|
||||
* connect.
|
||||
*
|
||||
* \param portName Name for the application port.
|
||||
*
|
||||
* See RtMidi::openVirtualPort().
|
||||
*/
|
||||
RTMIDIAPI void rtmidi_open_virtual_port (RtMidiPtr device, const char *portName);
|
||||
|
||||
/*! \brief Close a MIDI connection.
|
||||
* See RtMidi::closePort().
|
||||
*/
|
||||
RTMIDIAPI void rtmidi_close_port (RtMidiPtr device);
|
||||
|
||||
/*! \brief Return the number of available MIDI ports.
|
||||
* See RtMidi::getPortCount().
|
||||
*/
|
||||
RTMIDIAPI unsigned int rtmidi_get_port_count (RtMidiPtr device);
|
||||
|
||||
/*! \brief Access a string identifier for the specified MIDI input port number.
|
||||
*
|
||||
* To prevent memory leaks a char buffer must be passed to this function.
|
||||
* NULL can be passed as bufOut parameter, and that will write the required buffer length in the bufLen.
|
||||
*
|
||||
* See RtMidi::getPortName().
|
||||
*/
|
||||
RTMIDIAPI int rtmidi_get_port_name (RtMidiPtr device, unsigned int portNumber, char * bufOut, int * bufLen);
|
||||
|
||||
/* RtMidiIn API */
|
||||
|
||||
//! \brief Create a default RtMidiInPtr value, with no initialization.
|
||||
RTMIDIAPI RtMidiInPtr rtmidi_in_create_default (void);
|
||||
|
||||
/*! \brief Create a RtMidiInPtr value, with given api, clientName and queueSizeLimit.
|
||||
*
|
||||
* \param api An optional API id can be specified.
|
||||
* \param clientName An optional client name can be specified. This
|
||||
* will be used to group the ports that are created
|
||||
* by the application.
|
||||
* \param queueSizeLimit An optional size of the MIDI input queue can be
|
||||
* specified.
|
||||
*
|
||||
* See RtMidiIn::RtMidiIn().
|
||||
*/
|
||||
RTMIDIAPI RtMidiInPtr rtmidi_in_create (enum RtMidiApi api, const char *clientName, unsigned int queueSizeLimit);
|
||||
|
||||
//! \brief Free the given RtMidiInPtr.
|
||||
RTMIDIAPI void rtmidi_in_free (RtMidiInPtr device);
|
||||
|
||||
//! \brief Returns the MIDI API specifier for the given instance of RtMidiIn.
|
||||
//! See \ref RtMidiIn::getCurrentApi().
|
||||
RTMIDIAPI enum RtMidiApi rtmidi_in_get_current_api (RtMidiPtr device);
|
||||
|
||||
//! \brief Set a callback function to be invoked for incoming MIDI messages.
|
||||
//! See \ref RtMidiIn::setCallback().
|
||||
RTMIDIAPI void rtmidi_in_set_callback (RtMidiInPtr device, RtMidiCCallback callback, void *userData);
|
||||
|
||||
//! \brief Cancel use of the current callback function (if one exists).
|
||||
//! See \ref RtMidiIn::cancelCallback().
|
||||
RTMIDIAPI void rtmidi_in_cancel_callback (RtMidiInPtr device);
|
||||
|
||||
//! \brief Specify whether certain MIDI message types should be queued or ignored during input.
|
||||
//! See \ref RtMidiIn::ignoreTypes().
|
||||
RTMIDIAPI void rtmidi_in_ignore_types (RtMidiInPtr device, bool midiSysex, bool midiTime, bool midiSense);
|
||||
|
||||
/*! Fill the user-provided array with the data bytes for the next available
|
||||
* MIDI message in the input queue and return the event delta-time in seconds.
|
||||
*
|
||||
* \param message Must point to a char* that is already allocated.
|
||||
* SYSEX messages maximum size being 1024, a statically
|
||||
* allocated array could
|
||||
* be sufficient.
|
||||
* \param size Is used to return the size of the message obtained.
|
||||
* Must be set to the size of \ref message when calling.
|
||||
*
|
||||
* See RtMidiIn::getMessage().
|
||||
*/
|
||||
RTMIDIAPI double rtmidi_in_get_message (RtMidiInPtr device, unsigned char *message, size_t *size);
|
||||
|
||||
/* RtMidiOut API */
|
||||
|
||||
//! \brief Create a default RtMidiInPtr value, with no initialization.
|
||||
RTMIDIAPI RtMidiOutPtr rtmidi_out_create_default (void);
|
||||
|
||||
/*! \brief Create a RtMidiOutPtr value, with given and clientName.
|
||||
*
|
||||
* \param api An optional API id can be specified.
|
||||
* \param clientName An optional client name can be specified. This
|
||||
* will be used to group the ports that are created
|
||||
* by the application.
|
||||
*
|
||||
* See RtMidiOut::RtMidiOut().
|
||||
*/
|
||||
RTMIDIAPI RtMidiOutPtr rtmidi_out_create (enum RtMidiApi api, const char *clientName);
|
||||
|
||||
//! \brief Free the given RtMidiOutPtr.
|
||||
RTMIDIAPI void rtmidi_out_free (RtMidiOutPtr device);
|
||||
|
||||
//! \brief Returns the MIDI API specifier for the given instance of RtMidiOut.
|
||||
//! See \ref RtMidiOut::getCurrentApi().
|
||||
RTMIDIAPI enum RtMidiApi rtmidi_out_get_current_api (RtMidiPtr device);
|
||||
|
||||
//! \brief Immediately send a single message out an open MIDI output port.
|
||||
//! See \ref RtMidiOut::sendMessage().
|
||||
RTMIDIAPI int rtmidi_out_send_message (RtMidiOutPtr device, const unsigned char *message, int length);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*! }@ */
|
@ -50,4 +50,7 @@ constexpr std::size_t hash_combine(std::size_t lhs, std::size_t rhs) {
|
||||
return lhs ^= rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2);
|
||||
}
|
||||
|
||||
template<typename>
|
||||
static constexpr bool always_false = false;
|
||||
|
||||
#endif
|
||||
|
@ -1,10 +1,10 @@
|
||||
#define MIDI_ENABLE_ALSA_SUPPORT
|
||||
|
||||
#include <charconv>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <span>
|
||||
#include <thread>
|
||||
#include <cstring>
|
||||
|
||||
#include <musique/format.hh>
|
||||
#include <musique/interpreter/env.hh>
|
||||
@ -29,11 +29,26 @@ static unsigned repl_line_number = 1;
|
||||
#define Ignore(Call) do { auto const ignore_ ## __LINE__ = (Call); (void) ignore_ ## __LINE__; } while(0)
|
||||
|
||||
/// Pop string from front of an array
|
||||
static std::string_view pop(std::span<char const*> &span)
|
||||
template<typename T = std::string_view>
|
||||
static T pop(std::span<char const*> &span)
|
||||
{
|
||||
auto element = span.front();
|
||||
span = span.subspan(1);
|
||||
return element;
|
||||
|
||||
if constexpr (std::is_same_v<T, std::string_view>) {
|
||||
return element;
|
||||
} else if constexpr (std::is_arithmetic_v<T>) {
|
||||
T result;
|
||||
auto end = element + std::strlen(element);
|
||||
auto [ptr, ec] = std::from_chars(element, end, result);
|
||||
if (ec != decltype(ec){}) {
|
||||
std::cout << "Expected natural number as argument" << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
static_assert(always_false<T>, "Unsupported type for pop operation");
|
||||
}
|
||||
}
|
||||
|
||||
/// Print usage and exit
|
||||
@ -101,33 +116,32 @@ struct Runner
|
||||
{
|
||||
static inline Runner *the;
|
||||
|
||||
midi::ALSA alsa;
|
||||
midi::Rt_Midi midi;
|
||||
Interpreter interpreter;
|
||||
std::thread midi_input_event_loop;
|
||||
std::stop_source stop_source;
|
||||
|
||||
/// Setup interpreter and midi connection with given port
|
||||
Runner(std::string input_port, std::string output_port)
|
||||
: alsa("musique")
|
||||
Runner(std::optional<unsigned> input_port, std::optional<unsigned> output_port)
|
||||
: midi()
|
||||
, interpreter{}
|
||||
{
|
||||
assert(the == nullptr, "Only one instance of runner is supported");
|
||||
the = this;
|
||||
|
||||
bool const alsa_go = output_port.size() || input_port.size();
|
||||
if (alsa_go) {
|
||||
alsa.init_sequencer();
|
||||
interpreter.midi_connection = &alsa;
|
||||
bool const midi_go = bool(input_port) || bool(output_port);
|
||||
if (midi_go) {
|
||||
interpreter.midi_connection = &midi;
|
||||
}
|
||||
if (output_port.size()) {
|
||||
std::cout << "Connected MIDI output to port " << output_port << ". Ready to play!" << std::endl;
|
||||
alsa.connect_output(output_port);
|
||||
if (output_port) {
|
||||
std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl;
|
||||
midi.connect_output(*output_port);
|
||||
}
|
||||
if (input_port.size()) {
|
||||
std::cout << "Connected MIDI input to port " << input_port << ". Ready for incoming messages!" << std::endl;
|
||||
alsa.connect_input(input_port);
|
||||
if (input_port) {
|
||||
std::cout << "Connected MIDI input to port " << *input_port << ". Ready for incoming messages!" << std::endl;
|
||||
midi.connect_input(*input_port);
|
||||
}
|
||||
if (alsa_go) {
|
||||
if (midi_go) {
|
||||
interpreter.register_callbacks();
|
||||
midi_input_event_loop = std::thread([this] { handle_midi_event_loop(); });
|
||||
midi_input_event_loop.detach();
|
||||
@ -156,7 +170,7 @@ struct Runner
|
||||
|
||||
void handle_midi_event_loop()
|
||||
{
|
||||
alsa.input_event_loop(stop_source.get_token());
|
||||
midi.input_event_loop(stop_source.get_token());
|
||||
}
|
||||
|
||||
/// Run given source
|
||||
@ -207,8 +221,8 @@ static std::optional<Error> Main(std::span<char const*> args)
|
||||
};
|
||||
|
||||
// Arbitraly chosen for conviniance of the author
|
||||
std::string_view input_port{};
|
||||
std::string_view output_port{};
|
||||
std::optional<unsigned> input_port{};
|
||||
std::optional<unsigned> output_port{};
|
||||
|
||||
std::vector<Run> runnables;
|
||||
|
||||
@ -221,9 +235,7 @@ static std::optional<Error> Main(std::span<char const*> args)
|
||||
}
|
||||
|
||||
if (arg == "-l" || arg == "--list") {
|
||||
midi::ALSA alsa("musique");
|
||||
alsa.init_sequencer();
|
||||
alsa.list_ports(std::cout);
|
||||
midi::Rt_Midi{}.list_ports(std::cout);
|
||||
return {};
|
||||
}
|
||||
|
||||
@ -251,7 +263,7 @@ static std::optional<Error> Main(std::span<char const*> args)
|
||||
std::cerr << "musique: error: option " << arg << " requires an argument" << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
input_port = pop(args);
|
||||
input_port = pop<unsigned>(args);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -260,7 +272,7 @@ static std::optional<Error> Main(std::span<char const*> args)
|
||||
std::cerr << "musique: error: option " << arg << " requires an argument" << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
output_port = pop(args);
|
||||
output_port = pop<unsigned>(args);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -272,7 +284,7 @@ static std::optional<Error> Main(std::span<char const*> args)
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
Runner runner{std::string(input_port), std::string(output_port)};
|
||||
Runner runner{input_port, output_port};
|
||||
|
||||
for (auto const& [is_file, argument] : runnables) {
|
||||
if (!is_file) {
|
||||
|
@ -1,237 +0,0 @@
|
||||
#define MIDI_ENABLE_ALSA_SUPPORT
|
||||
#include <musique/midi/midi.hh>
|
||||
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
/// Ensures that operation performed by ALSA was successfull
|
||||
static int ensure_snd(int maybe_error, std::string_view message)
|
||||
{
|
||||
if (maybe_error < 0) {
|
||||
std::cerr << "ALSA ERROR: " << message << ": " << snd_strerror(maybe_error) << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
return maybe_error;
|
||||
}
|
||||
|
||||
static void create_source_port(midi::ALSA &alsa);
|
||||
static void create_queue(midi::ALSA &alsa);
|
||||
static void connect_port(midi::ALSA &alsa);
|
||||
static void send_note_event(midi::ALSA &alsa, snd_seq_event_type type, uint8_t channel, uint8_t note, uint8_t velocity);
|
||||
|
||||
midi::ALSA::ALSA(std::string connection_name)
|
||||
: connection_name(connection_name)
|
||||
{
|
||||
}
|
||||
|
||||
void midi::ALSA::init_sequencer()
|
||||
{
|
||||
ensure_snd(snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0), "opening sequencer");
|
||||
ensure_snd(snd_seq_set_client_name(seq, connection_name.c_str()), "setting client name");
|
||||
ensure_snd(client = snd_seq_client_id(seq), "getting client id");
|
||||
}
|
||||
|
||||
void midi::ALSA::connect_output(std::string const& target)
|
||||
{
|
||||
assert(!connected);
|
||||
ensure_snd(snd_seq_parse_address(seq, &output_port_addr, target.c_str()), "Invalid port specification");
|
||||
create_source_port(*this);
|
||||
create_queue(*this);
|
||||
connect_port(*this);
|
||||
connected = true;
|
||||
}
|
||||
|
||||
void midi::ALSA::connect_input(std::string const& target)
|
||||
{
|
||||
ensure_snd(snd_seq_parse_address(seq, &input_port_addr, target.c_str()), "Invalid port specification");
|
||||
input_port = ensure_snd(snd_seq_create_simple_port(seq, connection_name.c_str(),
|
||||
SND_SEQ_PORT_CAP_WRITE |
|
||||
SND_SEQ_PORT_CAP_SUBS_WRITE,
|
||||
SND_SEQ_PORT_TYPE_MIDI_GENERIC |
|
||||
SND_SEQ_PORT_TYPE_APPLICATION), "create simple port");
|
||||
|
||||
ensure_snd(snd_seq_connect_from(seq, input_port, input_port_addr.client, input_port_addr.port),
|
||||
"connect from");
|
||||
}
|
||||
|
||||
void midi::ALSA::list_ports(std::ostream& out) const
|
||||
{
|
||||
snd_seq_client_info_t *c;
|
||||
snd_seq_port_info_t *p;
|
||||
|
||||
// Wonders of C based API <3
|
||||
snd_seq_client_info_alloca(&c);
|
||||
snd_seq_port_info_alloca(&p);
|
||||
|
||||
out << " Port Client name Port name\n";
|
||||
|
||||
snd_seq_client_info_set_client(c, -1);
|
||||
while (snd_seq_query_next_client(seq, c) >= 0) {
|
||||
int client = snd_seq_client_info_get_client(c);
|
||||
|
||||
snd_seq_port_info_set_client(p, client);
|
||||
snd_seq_port_info_set_port(p, -1);
|
||||
while (snd_seq_query_next_port(seq, p) >= 0) {
|
||||
// Port must support MIDI messages
|
||||
if (!(snd_seq_port_info_get_type(p) & SND_SEQ_PORT_TYPE_MIDI_GENERIC)) continue;
|
||||
|
||||
// Both WRITE and SUBS_WRITE are required
|
||||
// TODO Aquire knowladge why
|
||||
auto const exp = SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE;
|
||||
if ((snd_seq_port_info_get_capability(p) & exp) != exp) continue;
|
||||
|
||||
out <<
|
||||
std::setw(3) << std::right << snd_seq_port_info_get_client(p) << ':' <<
|
||||
std::setw(3) << std::left << snd_seq_port_info_get_port(p) << " " <<
|
||||
std::setw(32) << std::left << snd_seq_client_info_get_name(c) << " " <<
|
||||
snd_seq_port_info_get_name(p) << '\n';
|
||||
}
|
||||
}
|
||||
out << std::flush;
|
||||
}
|
||||
|
||||
void midi::ALSA::send_note_on(uint8_t channel, uint8_t note_number, uint8_t velocity)
|
||||
{
|
||||
send_note_event(*this, SND_SEQ_EVENT_NOTEON, channel, note_number, velocity);
|
||||
}
|
||||
|
||||
void midi::ALSA::send_note_off(uint8_t channel, uint8_t note_number, uint8_t velocity)
|
||||
{
|
||||
send_note_event(*this, SND_SEQ_EVENT_NOTEOFF, channel, note_number, velocity);
|
||||
}
|
||||
|
||||
void midi::ALSA::send_program_change(uint8_t channel, uint8_t program)
|
||||
{
|
||||
snd_seq_event_t ev;
|
||||
snd_seq_ev_clear(&ev);
|
||||
ev.queue = queue;
|
||||
ev.source.port = 0;
|
||||
ev.flags = SND_SEQ_TIME_STAMP_TICK;
|
||||
ev.type = SND_SEQ_EVENT_PGMCHANGE;
|
||||
ev.time.tick = 0;
|
||||
ev.dest = output_port_addr;
|
||||
|
||||
snd_seq_ev_set_fixed(&ev);
|
||||
ev.data.control.channel = channel;
|
||||
ev.data.control.value = program;
|
||||
ensure_snd(snd_seq_event_output(seq, &ev), "output event");
|
||||
ensure_snd(snd_seq_drain_output(seq), "drain output queue");
|
||||
}
|
||||
|
||||
void midi::ALSA::send_controller_change(uint8_t channel, uint8_t controller_number, uint8_t value)
|
||||
{
|
||||
snd_seq_event_t ev;
|
||||
snd_seq_ev_clear(&ev);
|
||||
ev.queue = queue;
|
||||
ev.source.port = 0;
|
||||
ev.flags = SND_SEQ_TIME_STAMP_TICK;
|
||||
ev.type = SND_SEQ_EVENT_CONTROLLER;
|
||||
ev.time.tick = 0;
|
||||
ev.dest = output_port_addr;
|
||||
|
||||
snd_seq_ev_set_fixed(&ev);
|
||||
ev.data.control.channel = channel;
|
||||
ev.data.control.param = controller_number;
|
||||
ev.data.control.value = value;
|
||||
ensure_snd(snd_seq_event_output(seq, &ev), "output event");
|
||||
ensure_snd(snd_seq_drain_output(seq), "drain output queue");
|
||||
}
|
||||
|
||||
void midi::ALSA::input_event_loop(std::stop_token stop_token)
|
||||
{
|
||||
int const poll_fd_count = snd_seq_poll_descriptors_count(seq, POLLIN);
|
||||
std::vector<pollfd> poll_fds(poll_fd_count);
|
||||
ensure_snd(snd_seq_poll_descriptors(seq, poll_fds.data(), poll_fds.size(), POLLIN), "poll descriptors");
|
||||
while (!stop_token.stop_requested()) {
|
||||
if (poll(poll_fds.data(), poll_fds.size(), -1) < 0) {
|
||||
return;
|
||||
}
|
||||
while (!stop_token.stop_requested()) {
|
||||
snd_seq_event_t *event;
|
||||
if (snd_seq_event_input(seq, &event) < 0) { break; }
|
||||
if (!event) { continue; }
|
||||
|
||||
switch (event->type) {
|
||||
case SND_SEQ_EVENT_NOTEON:
|
||||
if (!event->data.note.velocity)
|
||||
goto noteoff;
|
||||
if (note_on_callback)
|
||||
note_on_callback(event->data.note.channel, event->data.note.note, event->data.note.velocity);
|
||||
break;
|
||||
|
||||
case SND_SEQ_EVENT_NOTEOFF:
|
||||
noteoff:
|
||||
if (note_off_callback)
|
||||
note_off_callback(event->data.note.channel, event->data.note.note);
|
||||
break;
|
||||
|
||||
default:
|
||||
// TODO Add all events as callbacks
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool midi::ALSA::supports_output() const
|
||||
{
|
||||
return output_port < 0;
|
||||
}
|
||||
|
||||
bool midi::ALSA::supports_input () const
|
||||
{
|
||||
return input_port < 0;
|
||||
}
|
||||
|
||||
static void create_source_port(midi::ALSA &alsa)
|
||||
{
|
||||
snd_seq_port_info_t *pinfo;
|
||||
snd_seq_port_info_alloca(&pinfo);
|
||||
|
||||
snd_seq_port_info_set_port(pinfo, alsa.output_port);
|
||||
snd_seq_port_info_set_port_specified(pinfo, 1);
|
||||
snd_seq_port_info_set_name(pinfo, alsa.connection_name.c_str());
|
||||
|
||||
snd_seq_port_info_set_capability(pinfo, 0);
|
||||
snd_seq_port_info_set_type(pinfo, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
|
||||
ensure_snd(snd_seq_create_port(alsa.seq, pinfo), "create port");
|
||||
}
|
||||
|
||||
static void create_queue(midi::ALSA &alsa)
|
||||
{
|
||||
ensure_snd(alsa.queue = snd_seq_alloc_named_queue(alsa.seq, alsa.connection_name.c_str()), "create queue");
|
||||
snd_seq_queue_tempo_t *queue_tempo;
|
||||
snd_seq_queue_tempo_alloca(&queue_tempo);
|
||||
|
||||
// We assume time division as ticks per quarter
|
||||
snd_seq_queue_tempo_set_tempo(queue_tempo, 500'000); // 120 BPM
|
||||
snd_seq_queue_tempo_set_ppq(queue_tempo, 96);
|
||||
ensure_snd(snd_seq_set_queue_tempo(alsa.seq, alsa.queue, queue_tempo), "set queue tempo");
|
||||
}
|
||||
|
||||
static void connect_port(midi::ALSA &alsa)
|
||||
{
|
||||
ensure_snd(snd_seq_connect_to(alsa.seq, alsa.output_port, alsa.output_port_addr.client, alsa.output_port_addr.port), "connect to port");
|
||||
}
|
||||
|
||||
static void send_note_event(midi::ALSA &alsa, snd_seq_event_type type, uint8_t channel, uint8_t note, uint8_t velocity)
|
||||
{
|
||||
snd_seq_event_t ev;
|
||||
snd_seq_ev_clear(&ev);
|
||||
ev.queue = alsa.queue;
|
||||
ev.source.port = alsa.output_port;
|
||||
ev.flags = SND_SEQ_TIME_STAMP_TICK;
|
||||
|
||||
ev.type = type;
|
||||
ev.time.tick = 0;
|
||||
ev.dest = alsa.output_port_addr;
|
||||
|
||||
snd_seq_ev_set_fixed(&ev);
|
||||
ev.data.note.channel = channel;
|
||||
ev.data.note.note = note;
|
||||
ev.data.note.velocity = velocity;
|
||||
|
||||
ensure_snd(snd_seq_event_output(alsa.seq, &ev), "output event");
|
||||
ensure_snd(snd_seq_drain_output(alsa.seq), "drain output queue");
|
||||
}
|
@ -1,23 +1,18 @@
|
||||
#pragma once
|
||||
#include <RtMidi.h>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
#ifdef MIDI_ENABLE_ALSA_SUPPORT
|
||||
#include <alsa/asoundlib.h>
|
||||
#include <ostream>
|
||||
#include <optional>
|
||||
#include <stop_token>
|
||||
|
||||
#ifdef assert
|
||||
#undef assert
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#include <string>
|
||||
|
||||
// Documentation of midi messages available at http://midi.teragonaudio.com/tech/midispec.htm
|
||||
namespace midi
|
||||
{
|
||||
struct Connection
|
||||
{
|
||||
virtual ~Connection() = default;
|
||||
|
||||
virtual bool supports_output() const = 0;
|
||||
virtual bool supports_input () const = 0;
|
||||
|
||||
@ -32,19 +27,15 @@ namespace midi
|
||||
std::function<void(uint8_t, uint8_t)> note_off_callback = nullptr;
|
||||
};
|
||||
|
||||
#ifdef MIDI_ENABLE_ALSA_SUPPORT
|
||||
struct ALSA : Connection
|
||||
struct Rt_Midi : Connection
|
||||
{
|
||||
explicit ALSA(std::string connection_name);
|
||||
~Rt_Midi() override = default;
|
||||
|
||||
/// Initialize connection with ALSA
|
||||
void init_sequencer();
|
||||
/// Connect with specific MIDI port for outputing MIDI messages
|
||||
void connect_output(unsigned target);
|
||||
|
||||
/// Connect with specific ALSA port for outputing MIDI messages
|
||||
void connect_output(std::string const& target);
|
||||
|
||||
/// Connect with specific ALSA port for reading MIDI messages
|
||||
void connect_input(std::string const& target);
|
||||
/// Connect with specific MIDI port for reading MIDI messages
|
||||
void connect_input(unsigned target);
|
||||
|
||||
/// List available ports
|
||||
void list_ports(std::ostream &out) const;
|
||||
@ -59,18 +50,9 @@ namespace midi
|
||||
|
||||
void input_event_loop(std::stop_token);
|
||||
|
||||
/// Name that is used by ALSA to describe our connection
|
||||
std::string connection_name = {};
|
||||
bool connected = false;
|
||||
snd_seq_t *seq = nullptr;
|
||||
long client = 0;
|
||||
int queue = 0;
|
||||
snd_seq_addr_t input_port_addr{};
|
||||
snd_seq_addr_t output_port_addr{};
|
||||
int input_port = -1;
|
||||
int output_port = -1;
|
||||
std::optional<RtMidiIn> input;
|
||||
std::optional<RtMidiOut> output;
|
||||
};
|
||||
#endif
|
||||
|
||||
/// All defined controllers for controller change message
|
||||
enum class Controller : uint8_t
|
||||
|
103
musique/midi/rt_midi.cc
Normal file
103
musique/midi/rt_midi.cc
Normal file
@ -0,0 +1,103 @@
|
||||
#include <musique/midi/midi.hh>
|
||||
#include <musique/errors.hh>
|
||||
|
||||
void midi::Rt_Midi::list_ports(std::ostream &out) const
|
||||
try {
|
||||
RtMidiIn input;
|
||||
RtMidiOut output;
|
||||
{
|
||||
unsigned ports_count = input.getPortCount();
|
||||
out << "Found " << ports_count << " MIDI input ports\n";
|
||||
for (auto i = 0u; i < ports_count; ++i) {
|
||||
std::string port_name = input.getPortName(i);
|
||||
std::cout << " Input Port #" << i+1 << ": " << port_name << "\n";
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
{
|
||||
unsigned ports_count = output.getPortCount();
|
||||
out << "Found " << ports_count << " MIDI output ports\n";
|
||||
for (auto i = 0u; i < ports_count; ++i) {
|
||||
std::string port_name = output.getPortName(i);
|
||||
std::cout << " Output Port #" << i+1 << ": " << port_name << "\n";
|
||||
}
|
||||
}
|
||||
} catch (RtMidiError &error) {
|
||||
// TODO(error)
|
||||
std::cerr << "Failed to use MIDI connection: " << error.getMessage() << std::endl;
|
||||
std::exit(33);
|
||||
}
|
||||
|
||||
void midi::Rt_Midi::connect_output(unsigned target)
|
||||
try {
|
||||
assert(not output.has_value(), "Reconeccting is not supported yet");
|
||||
output.emplace();
|
||||
output->openPort(target, "Musique output port");
|
||||
} catch (RtMidiError &error) {
|
||||
// TODO(error)
|
||||
std::cerr << "Failed to use MIDI connection: " << error.getMessage() << std::endl;
|
||||
std::exit(33);
|
||||
}
|
||||
|
||||
void midi::Rt_Midi::connect_input(unsigned target)
|
||||
try {
|
||||
assert(not input.has_value(), "Reconeccting is not supported yet");
|
||||
input.emplace();
|
||||
input->openPort(target, "Musique input port");
|
||||
} catch (RtMidiError &error) {
|
||||
// TODO(error)
|
||||
std::cerr << "Failed to use MIDI connection: " << error.getMessage() << std::endl;
|
||||
std::exit(33);
|
||||
}
|
||||
|
||||
bool midi::Rt_Midi::supports_output() const
|
||||
{
|
||||
return bool(output);
|
||||
}
|
||||
|
||||
bool midi::Rt_Midi::supports_input() const
|
||||
{
|
||||
return bool(input);
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
inline void send_message(RtMidiOut &out, std::array<std::uint8_t, N> message)
|
||||
try {
|
||||
out.sendMessage(message.data(), message.size());
|
||||
} catch (RtMidiError &error) {
|
||||
// TODO(error)
|
||||
std::cerr << "Failed to use MIDI connection: " << error.getMessage() << std::endl;
|
||||
std::exit(33);
|
||||
}
|
||||
|
||||
enum : std::uint8_t
|
||||
{
|
||||
Control_Change = 0b1011'0000,
|
||||
Note_Off = 0b1000'0000,
|
||||
Note_On = 0b1001'0000,
|
||||
Program_Change = 0b1100'0000,
|
||||
};
|
||||
|
||||
void midi::Rt_Midi::send_note_on(uint8_t channel, uint8_t note_number, uint8_t velocity)
|
||||
{
|
||||
send_message(*output, std::array { std::uint8_t(Note_On + channel), note_number, velocity });
|
||||
}
|
||||
|
||||
void midi::Rt_Midi::send_note_off(uint8_t channel, uint8_t note_number, uint8_t velocity)
|
||||
{
|
||||
send_message(*output, std::array { std::uint8_t(Note_Off + channel), note_number, velocity });
|
||||
}
|
||||
|
||||
void midi::Rt_Midi::send_program_change(uint8_t channel, uint8_t program)
|
||||
{
|
||||
send_message(*output, std::array { std::uint8_t(channel), program });
|
||||
}
|
||||
|
||||
void midi::Rt_Midi::send_controller_change(uint8_t channel, uint8_t controller_number, uint8_t value)
|
||||
{
|
||||
send_message(*output, std::array { std::uint8_t(Control_Change + channel), controller_number, value });
|
||||
}
|
||||
|
||||
void midi::Rt_Midi::input_event_loop(std::stop_token)
|
||||
{
|
||||
}
|
@ -4,6 +4,6 @@ bin/%.o: musique/%.cc
|
||||
@echo "CXX $@"
|
||||
@$(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) $(CPPFLAGS) -o $@ $< -c
|
||||
|
||||
bin/musique: $(Release_Obj) bin/main.o bin/bestline.o
|
||||
bin/musique: $(Release_Obj) bin/main.o bin/bestline.o bin/rtmidi.o
|
||||
@echo "CXX $@"
|
||||
@$(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) $(CPPFLAGS) -o $@ $(Release_Obj) bin/bestline.o $(LDFLAGS) $(LDLIBS)
|
||||
@$(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) $(CPPFLAGS) -o $@ $(Release_Obj) bin/bestline.o bin/rtmidi.o $(LDFLAGS) $(LDLIBS)
|
||||
|
Loading…
Reference in New Issue
Block a user