From fc752bc834f62c4b289bf2707fbbef5f6f30a112 Mon Sep 17 00:00:00 2001 From: lathoub Date: Wed, 4 Mar 2020 19:49:27 +0100 Subject: [PATCH] lift and shift of the Serial code into a seperate class, allowing for other serializers lift and shift of the Serial code into a seperate class, allowing for other serializers as AppleMIDI, USBMIDI, ipMIDI, BLE-MIDI --- src/CMakeLists.txt | 5 +- src/MIDI.h | 14 +- src/MIDI.hpp | 400 +++++++++++++++++--------------- src/midi_Defs.h | 35 +-- src/midi_Message.h | 2 +- src/midi_Namespace.h | 2 +- src/midi_RingBuffer.h | 68 ------ src/midi_RingBuffer.hpp | 145 ------------ src/midi_Settings.h | 10 +- src/midi_UsbDefs.h | 135 ----------- src/midi_UsbPacketInterface.h | 44 ---- src/midi_UsbPacketInterface.hpp | 146 ------------ src/midi_UsbTransport.h | 62 ----- src/midi_UsbTransport.hpp | 102 -------- src/serialMIDI.h | 71 ++++++ 15 files changed, 296 insertions(+), 945 deletions(-) delete mode 100644 src/midi_RingBuffer.h delete mode 100644 src/midi_RingBuffer.hpp delete mode 100644 src/midi_UsbDefs.h delete mode 100644 src/midi_UsbPacketInterface.h delete mode 100644 src/midi_UsbPacketInterface.hpp delete mode 100644 src/midi_UsbTransport.h delete mode 100644 src/midi_UsbTransport.hpp create mode 100644 src/serialMIDI.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 66f3355..b2a7169 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,11 +7,8 @@ add_library(midi STATIC midi_Defs.h midi_Message.h midi_Settings.h - midi_RingBuffer.h - midi_RingBuffer.hpp - midi_UsbTransport.h - midi_UsbTransport.hpp MIDI.cpp MIDI.hpp MIDI.h + serialMIDI.h ) diff --git a/src/MIDI.h b/src/MIDI.h index 268c6a9..6bf9a08 100644 --- a/src/MIDI.h +++ b/src/MIDI.h @@ -31,6 +31,8 @@ #include "midi_Settings.h" #include "midi_Message.h" +#include "serialMIDI.h" + // ----------------------------------------------------------------------------- BEGIN_MIDI_NAMESPACE @@ -41,14 +43,14 @@ the hardware interface, meaning you can use HardwareSerial, SoftwareSerial or ak47's Uart classes. The only requirement is that the class implements the begin, read, write and available methods. */ -template +template class MidiInterface { public: typedef _Settings Settings; public: - inline MidiInterface(SerialPort& inSerial); + inline MidiInterface(Encoder&); inline ~MidiInterface(); public: @@ -98,7 +100,7 @@ public: inline void sendSongSelect(DataByte inSongNumber); inline void sendTuneRequest(); inline void sendRealTime(MidiType inType); - + inline void beginRpn(unsigned inNumber, Channel inChannel); inline void sendRpnValue(unsigned inValue, @@ -227,7 +229,7 @@ private: typedef Message MidiMessage; private: - SerialPort& mSerial; + Encoder& mEncoder; private: Channel mInputChannel; @@ -240,10 +242,10 @@ private: unsigned mCurrentNrpnNumber; bool mThruActivated : 1; Thru::Mode mThruFilterMode : 7; - unsigned long mLastMessageSentTime; - bool mSenderActiveSensingActivated; MidiMessage mMessage; + unsigned long mLastMessageSentTime; + bool mSenderActiveSensingActivated; private: inline StatusByte getStatus(MidiType inType, diff --git a/src/MIDI.hpp b/src/MIDI.hpp index fe4afa2..1a53659 100644 --- a/src/MIDI.hpp +++ b/src/MIDI.hpp @@ -30,9 +30,9 @@ BEGIN_MIDI_NAMESPACE /// \brief Constructor for MidiInterface. -template -inline MidiInterface::MidiInterface(SerialPort& inSerial) - : mSerial(inSerial) +template +inline MidiInterface::MidiInterface(Encoder& inEncoder) + : mEncoder(inEncoder) , mInputChannel(0) , mRunningStatus_RX(InvalidType) , mRunningStatus_TX(InvalidType) @@ -40,9 +40,9 @@ inline MidiInterface::MidiInterface(SerialPort& inSerial) , mPendingMessageIndex(0) , mCurrentRpnNumber(0xffff) , mCurrentNrpnNumber(0xffff) - , mSenderActiveSensingActivated(false) , mLastMessageSentTime(0) - , mThruActivated(true) + , mSenderActiveSensingActivated(false) + , mThruActivated(false) , mThruFilterMode(Thru::Full) { mNoteOffCallback = 0; @@ -69,8 +69,8 @@ inline MidiInterface::MidiInterface(SerialPort& inSerial) This is not really useful for the Arduino, as it is never called... */ -template -inline MidiInterface::~MidiInterface() +template +inline MidiInterface::~MidiInterface() { } @@ -82,15 +82,11 @@ inline MidiInterface::~MidiInterface() - Input channel set to 1 if no value is specified - Full thru mirroring */ -template -void MidiInterface::begin(Channel inChannel) +template +void MidiInterface::begin(Channel inChannel) { // Initialise the Serial port -#if defined(AVR_CAKE) - mSerial. template open(); -#else - mSerial.begin(Settings::BaudRate); -#endif + mEncoder.begin(); mInputChannel = inChannel; mRunningStatus_TX = InvalidType; @@ -102,17 +98,17 @@ void MidiInterface::begin(Channel inChannel) mCurrentRpnNumber = 0xffff; mCurrentNrpnNumber = 0xffff; + mSenderActiveSensingActivated = Settings::UseSenderActiveSensing; + mLastMessageSentTime = millis(); + mMessage.valid = false; mMessage.type = InvalidType; mMessage.channel = 0; mMessage.data1 = 0; mMessage.data2 = 0; - mSenderActiveSensingActivated = Settings::UseSenderActiveSensing; - mLastMessageSentTime = millis(); - mThruFilterMode = Thru::Full; - mThruActivated = true; + mThruActivated = false; } // ----------------------------------------------------------------------------- @@ -134,8 +130,8 @@ void MidiInterface::begin(Channel inChannel) This is an internal method, use it only if you need to send raw data from your code, at your own risks. */ -template -void MidiInterface::send(MidiType inType, +template +void MidiInterface::send(MidiType inType, DataByte inData1, DataByte inData2, Channel inChannel) @@ -156,26 +152,31 @@ void MidiInterface::send(MidiType inType, const StatusByte status = getStatus(inType, inChannel); - if (Settings::UseRunningStatus) + if (mEncoder.beginTransmission()) { - if (mRunningStatus_TX != status) + if (Settings::UseRunningStatus) { - // New message, memorise and send header - mRunningStatus_TX = status; - mSerial.write(mRunningStatus_TX); + if (mRunningStatus_TX != status) + { + // New message, memorise and send header + mRunningStatus_TX = status; + mEncoder.write(mRunningStatus_TX); + } + } + else + { + // Don't care about running status, send the status byte. + mEncoder.write(status); } - } - else - { - // Don't care about running status, send the status byte. - mSerial.write(status); - } - // Then send data - mSerial.write(inData1); - if (inType != ProgramChange && inType != AfterTouchChannel) - { - mSerial.write(inData2); + // Then send data + mEncoder.write(inData1); + if (inType != ProgramChange && inType != AfterTouchChannel) + { + mEncoder.write(inData2); + } + + mEncoder.endTransmission(); } } else if (inType >= Clock && inType <= SystemReset) @@ -198,8 +199,8 @@ void MidiInterface::send(MidiType inType, Take a look at the values, names and frequencies of notes here: http://www.phys.unsw.edu.au/jw/notes.html */ -template -void MidiInterface::sendNoteOn(DataByte inNoteNumber, +template +void MidiInterface::sendNoteOn(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel) { @@ -217,8 +218,8 @@ void MidiInterface::sendNoteOn(DataByte inNoteNumber, Take a look at the values, names and frequencies of notes here: http://www.phys.unsw.edu.au/jw/notes.html */ -template -void MidiInterface::sendNoteOff(DataByte inNoteNumber, +template +void MidiInterface::sendNoteOff(DataByte inNoteNumber, DataByte inVelocity, Channel inChannel) { @@ -229,8 +230,8 @@ void MidiInterface::sendNoteOff(DataByte inNoteNumber, \param inProgramNumber The Program to select (0 to 127). \param inChannel The channel on which the message will be sent (1 to 16). */ -template -void MidiInterface::sendProgramChange(DataByte inProgramNumber, +template +void MidiInterface::sendProgramChange(DataByte inProgramNumber, Channel inChannel) { send(ProgramChange, inProgramNumber, 0, inChannel); @@ -242,8 +243,8 @@ void MidiInterface::sendProgramChange(DataByte inProgramNu \param inChannel The channel on which the message will be sent (1 to 16). @see MidiControlChangeNumber */ -template -void MidiInterface::sendControlChange(DataByte inControlNumber, +template +void MidiInterface::sendControlChange(DataByte inControlNumber, DataByte inControlValue, Channel inChannel) { @@ -257,8 +258,8 @@ void MidiInterface::sendControlChange(DataByte inControlNu Note: this method is deprecated and will be removed in a future revision of the library, @see sendAfterTouch to send polyphonic and monophonic AfterTouch messages. */ -template -void MidiInterface::sendPolyPressure(DataByte inNoteNumber, +template +void MidiInterface::sendPolyPressure(DataByte inNoteNumber, DataByte inPressure, Channel inChannel) { @@ -269,8 +270,8 @@ void MidiInterface::sendPolyPressure(DataByte inNoteNumber \param inPressure The amount of AfterTouch to apply to all notes. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -void MidiInterface::sendAfterTouch(DataByte inPressure, +template +void MidiInterface::sendAfterTouch(DataByte inPressure, Channel inChannel) { send(AfterTouchChannel, inPressure, 0, inChannel); @@ -282,8 +283,8 @@ void MidiInterface::sendAfterTouch(DataByte inPressure, \param inChannel The channel on which the message will be sent (1 to 16). @see Replaces sendPolyPressure (which is now deprecated). */ -template -void MidiInterface::sendAfterTouch(DataByte inNoteNumber, +template +void MidiInterface::sendAfterTouch(DataByte inNoteNumber, DataByte inPressure, Channel inChannel) { @@ -296,8 +297,8 @@ void MidiInterface::sendAfterTouch(DataByte inNoteNumber, center value is 0. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -void MidiInterface::sendPitchBend(int inPitchValue, +template +void MidiInterface::sendPitchBend(int inPitchValue, Channel inChannel) { const unsigned bend = unsigned(inPitchValue - int(MIDI_PITCHBEND_MIN)); @@ -311,8 +312,8 @@ void MidiInterface::sendPitchBend(int inPitchValue, and +1.0f (max upwards bend), center value is 0.0f. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -void MidiInterface::sendPitchBend(double inPitchValue, +template +void MidiInterface::sendPitchBend(double inPitchValue, Channel inChannel) { const int scale = inPitchValue > 0.0 ? MIDI_PITCHBEND_MAX : MIDI_PITCHBEND_MIN; @@ -329,26 +330,25 @@ void MidiInterface::sendPitchBend(double inPitchValue, default value for ArrayContainsBoundaries is set to 'false' for compatibility with previous versions of the library. */ -template -void MidiInterface::sendSysEx(unsigned inLength, +template +void MidiInterface::sendSysEx(unsigned inLength, const byte* inArray, bool inArrayContainsBoundaries) { const bool writeBeginEndBytes = !inArrayContainsBoundaries; - if (writeBeginEndBytes) + if (mEncoder.beginTransmission()) { - mSerial.write(0xf0); - } + if (writeBeginEndBytes) + mEncoder.write(0xf0); - for (unsigned i = 0; i < inLength; ++i) - { - mSerial.write(inArray[i]); - } + for (unsigned i = 0; i < inLength; ++i) + mEncoder.write(inArray[i]); - if (writeBeginEndBytes) - { - mSerial.write(0xf7); + if (writeBeginEndBytes) + mEncoder.write(0xf7); + + mEncoder.endTransmission(); } if (Settings::UseRunningStatus) @@ -362,10 +362,14 @@ void MidiInterface::sendSysEx(unsigned inLength, When a MIDI unit receives this message, it should tune its oscillators (if equipped with any). */ -template -void MidiInterface::sendTuneRequest() +template +void MidiInterface::sendTuneRequest() { - mSerial.write(TuneRequest); + if (mEncoder.beginTransmission()) + { + mEncoder.write(TuneRequest); + mEncoder.endTransmission(); + } if (Settings::UseRunningStatus) { @@ -379,8 +383,8 @@ void MidiInterface::sendTuneRequest() \param inValuesNibble MTC data See MIDI Specification for more information. */ -template -void MidiInterface::sendTimeCodeQuarterFrame(DataByte inTypeNibble, +template +void MidiInterface::sendTimeCodeQuarterFrame(DataByte inTypeNibble, DataByte inValuesNibble) { const byte data = byte((((inTypeNibble & 0x07) << 4) | (inValuesNibble & 0x0f))); @@ -393,11 +397,15 @@ void MidiInterface::sendTimeCodeQuarterFrame(DataByte inTy \param inData if you want to encode directly the nibbles in your program, you can send the byte here. */ -template -void MidiInterface::sendTimeCodeQuarterFrame(DataByte inData) +template +void MidiInterface::sendTimeCodeQuarterFrame(DataByte inData) { - mSerial.write((byte)TimeCodeQuarterFrame); - mSerial.write(inData); + if (mEncoder.beginTransmission()) + { + mEncoder.write((byte)TimeCodeQuarterFrame); + mEncoder.write(inData); + mEncoder.endTransmission(); + } if (Settings::UseRunningStatus) { @@ -408,12 +416,16 @@ void MidiInterface::sendTimeCodeQuarterFrame(DataByte inDa /*! \brief Send a Song Position Pointer message. \param inBeats The number of beats since the start of the song. */ -template -void MidiInterface::sendSongPosition(unsigned inBeats) +template +void MidiInterface::sendSongPosition(unsigned inBeats) { - mSerial.write((byte)SongPosition); - mSerial.write(inBeats & 0x7f); - mSerial.write((inBeats >> 7) & 0x7f); + if (mEncoder.beginTransmission()) + { + mEncoder.write((byte)SongPosition); + mEncoder.write(inBeats & 0x7f); + mEncoder.write((inBeats >> 7) & 0x7f); + mEncoder.endTransmission(); + } if (Settings::UseRunningStatus) { @@ -422,11 +434,15 @@ void MidiInterface::sendSongPosition(unsigned inBeats) } /*! \brief Send a Song Select message */ -template -void MidiInterface::sendSongSelect(DataByte inSongNumber) +template +void MidiInterface::sendSongSelect(DataByte inSongNumber) { - mSerial.write((byte)SongSelect); - mSerial.write(inSongNumber & 0x7f); + if (mEncoder.beginTransmission()) + { + mEncoder.write((byte)SongSelect); + mEncoder.write(inSongNumber & 0x7f); + mEncoder.endTransmission(); + } if (Settings::UseRunningStatus) { @@ -440,8 +456,8 @@ void MidiInterface::sendSongSelect(DataByte inSongNumber) Start, Stop, Continue, Clock, ActiveSensing and SystemReset. @see MidiType */ -template -void MidiInterface::sendRealTime(MidiType inType) +template +void MidiInterface::sendRealTime(MidiType inType) { // Do not invalidate Running Status for real-time messages // as they can be interleaved within any message. @@ -454,7 +470,11 @@ void MidiInterface::sendRealTime(MidiType inType) case Continue: case ActiveSensing: case SystemReset: - mSerial.write((byte)inType); + if (mEncoder.beginTransmission()) + { + mEncoder.write((byte)inType); + mEncoder.endTransmission(); + } break; default: // Invalid Real Time marker @@ -466,8 +486,8 @@ void MidiInterface::sendRealTime(MidiType inType) \param inNumber The 14-bit number of the RPN you want to select. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::beginRpn(unsigned inNumber, +template +inline void MidiInterface::beginRpn(unsigned inNumber, Channel inChannel) { if (mCurrentRpnNumber != inNumber) @@ -484,8 +504,8 @@ inline void MidiInterface::beginRpn(unsigned inNumber, \param inValue The 14-bit value of the selected RPN. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::sendRpnValue(unsigned inValue, +template +inline void MidiInterface::sendRpnValue(unsigned inValue, Channel inChannel) {; const byte valMsb = 0x7f & (inValue >> 7); @@ -499,8 +519,8 @@ inline void MidiInterface::sendRpnValue(unsigned inValue, \param inLsb The LSB part of the value to send. Meaning depends on RPN number. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::sendRpnValue(byte inMsb, +template +inline void MidiInterface::sendRpnValue(byte inMsb, byte inLsb, Channel inChannel) { @@ -511,8 +531,8 @@ inline void MidiInterface::sendRpnValue(byte inMsb, /* \brief Increment the value of the currently selected RPN number by the specified amount. \param inAmount The amount to add to the currently selected RPN value. */ -template -inline void MidiInterface::sendRpnIncrement(byte inAmount, +template +inline void MidiInterface::sendRpnIncrement(byte inAmount, Channel inChannel) { sendControlChange(DataIncrement, inAmount, inChannel); @@ -521,8 +541,8 @@ inline void MidiInterface::sendRpnIncrement(byte inAmount, /* \brief Decrement the value of the currently selected RPN number by the specified amount. \param inAmount The amount to subtract to the currently selected RPN value. */ -template -inline void MidiInterface::sendRpnDecrement(byte inAmount, +template +inline void MidiInterface::sendRpnDecrement(byte inAmount, Channel inChannel) { sendControlChange(DataDecrement, inAmount, inChannel); @@ -532,8 +552,8 @@ inline void MidiInterface::sendRpnDecrement(byte inAmount, This will send a Null Function to deselect the currently selected RPN. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::endRpn(Channel inChannel) +template +inline void MidiInterface::endRpn(Channel inChannel) { sendControlChange(RPNLSB, 0x7f, inChannel); sendControlChange(RPNMSB, 0x7f, inChannel); @@ -546,8 +566,8 @@ inline void MidiInterface::endRpn(Channel inChannel) \param inNumber The 14-bit number of the NRPN you want to select. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::beginNrpn(unsigned inNumber, +template +inline void MidiInterface::beginNrpn(unsigned inNumber, Channel inChannel) { if (mCurrentNrpnNumber != inNumber) @@ -564,8 +584,8 @@ inline void MidiInterface::beginNrpn(unsigned inNumber, \param inValue The 14-bit value of the selected NRPN. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::sendNrpnValue(unsigned inValue, +template +inline void MidiInterface::sendNrpnValue(unsigned inValue, Channel inChannel) {; const byte valMsb = 0x7f & (inValue >> 7); @@ -579,8 +599,8 @@ inline void MidiInterface::sendNrpnValue(unsigned inValue, \param inLsb The LSB part of the value to send. Meaning depends on NRPN number. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::sendNrpnValue(byte inMsb, +template +inline void MidiInterface::sendNrpnValue(byte inMsb, byte inLsb, Channel inChannel) { @@ -591,8 +611,8 @@ inline void MidiInterface::sendNrpnValue(byte inMsb, /* \brief Increment the value of the currently selected NRPN number by the specified amount. \param inAmount The amount to add to the currently selected NRPN value. */ -template -inline void MidiInterface::sendNrpnIncrement(byte inAmount, +template +inline void MidiInterface::sendNrpnIncrement(byte inAmount, Channel inChannel) { sendControlChange(DataIncrement, inAmount, inChannel); @@ -601,8 +621,8 @@ inline void MidiInterface::sendNrpnIncrement(byte inAmount /* \brief Decrement the value of the currently selected NRPN number by the specified amount. \param inAmount The amount to subtract to the currently selected NRPN value. */ -template -inline void MidiInterface::sendNrpnDecrement(byte inAmount, +template +inline void MidiInterface::sendNrpnDecrement(byte inAmount, Channel inChannel) { sendControlChange(DataDecrement, inAmount, inChannel); @@ -612,8 +632,8 @@ inline void MidiInterface::sendNrpnDecrement(byte inAmount This will send a Null Function to deselect the currently selected NRPN. \param inChannel The channel on which the message will be sent (1 to 16). */ -template -inline void MidiInterface::endNrpn(Channel inChannel) +template +inline void MidiInterface::endNrpn(Channel inChannel) { sendControlChange(NRPNLSB, 0x7f, inChannel); sendControlChange(NRPNMSB, 0x7f, inChannel); @@ -624,8 +644,8 @@ inline void MidiInterface::endNrpn(Channel inChannel) // ----------------------------------------------------------------------------- -template -StatusByte MidiInterface::getStatus(MidiType inType, +template +StatusByte MidiInterface::getStatus(MidiType inType, Channel inChannel) const { return StatusByte(((byte)inType | ((inChannel - 1) & 0x0f))); @@ -647,16 +667,16 @@ StatusByte MidiInterface::getStatus(MidiType inType, it is sent back on the MIDI output. @see see setInputChannel() */ -template -inline bool MidiInterface::read() +template +inline bool MidiInterface::read() { return read(mInputChannel); } /*! \brief Read messages on a specified channel. */ -template -inline bool MidiInterface::read(Channel inChannel) +template +inline bool MidiInterface::read(Channel inChannel) { // Active Sensing. This message is intended to be sent // repeatedly to tell the receiver that a connection is alive. Use @@ -666,12 +686,12 @@ inline bool MidiInterface::read(Channel inChannel) // assume that the connection has been terminated. At // termination, the receiver will turn off all voices and return to // normal (non- active sensing) operation. - if (mSenderActiveSensingActivated && (millis() - mLastMessageSentTime) > 250) + if (mSenderActiveSensingActivated && (millis() - mLastMessageSentTime) > 270) { sendRealTime(ActiveSensing); mLastMessageSentTime = millis(); } - + if (inChannel >= MIDI_CHANNEL_OFF) return false; // MIDI Input disabled. @@ -694,10 +714,10 @@ inline bool MidiInterface::read(Channel inChannel) // ----------------------------------------------------------------------------- // Private method: MIDI parser -template -bool MidiInterface::parse() +template +bool MidiInterface::parse() { - if (mSerial.available() == 0) + if (mEncoder.available() == 0) { // No data available. return false; @@ -712,7 +732,7 @@ bool MidiInterface::parse() // Else, add the extracted byte to the pending message, and check validity. // When the message is done, store it. - const byte extracted = mSerial.read(); + const byte extracted = mEncoder.read(); // Ignore Undefined if (extracted == 0xf9 || extracted == 0xfd) @@ -803,7 +823,7 @@ bool MidiInterface::parse() mRunningStatus_RX = InvalidType; mMessage.sysexArray[0] = pendingType; break; - + case InvalidType: default: // This is obviously wrong. Let's get the hell out'a here. @@ -873,7 +893,7 @@ bool MidiInterface::parse() mMessage.valid = true; return true; - // End of Exclusive + // Exclusive case SystemExclusiveStart: case SystemExclusiveEnd: if ((mMessage.sysexArray[0] == SystemExclusiveStart) @@ -982,8 +1002,8 @@ bool MidiInterface::parse() } // Private method, see midi_Settings.h for documentation -template -inline void MidiInterface::handleNullVelocityNoteOnAsNoteOff() +template +inline void MidiInterface::handleNullVelocityNoteOnAsNoteOff() { if (Settings::HandleNullVelocityNoteOnAsNoteOff && getType() == NoteOn && getData2() == 0) @@ -993,8 +1013,8 @@ inline void MidiInterface::handleNullVelocityNoteOnAsNoteO } // Private method: check if the received message is on the listened channel -template -inline bool MidiInterface::inputFilter(Channel inChannel) +template +inline bool MidiInterface::inputFilter(Channel inChannel) { // This method handles recognition of channel // (to know if the message is destinated to the Arduino) @@ -1022,8 +1042,8 @@ inline bool MidiInterface::inputFilter(Channel inChannel) } // Private method: reset input attributes -template -inline void MidiInterface::resetInput() +template +inline void MidiInterface::resetInput() { mPendingMessageIndex = 0; mPendingMessageExpectedLenght = 0; @@ -1036,8 +1056,8 @@ inline void MidiInterface::resetInput() Returns an enumerated type. @see MidiType */ -template -inline MidiType MidiInterface::getType() const +template +inline MidiType MidiInterface::getType() const { return mMessage.type; } @@ -1047,22 +1067,22 @@ inline MidiType MidiInterface::getType() const \return Channel range is 1 to 16. For non-channel messages, this will return 0. */ -template -inline Channel MidiInterface::getChannel() const +template +inline Channel MidiInterface::getChannel() const { return mMessage.channel; } /*! \brief Get the first data byte of the last received message. */ -template -inline DataByte MidiInterface::getData1() const +template +inline DataByte MidiInterface::getData1() const { return mMessage.data1; } /*! \brief Get the second data byte of the last received message. */ -template -inline DataByte MidiInterface::getData2() const +template +inline DataByte MidiInterface::getData2() const { return mMessage.data2; } @@ -1071,8 +1091,8 @@ inline DataByte MidiInterface::getData2() const @see getSysExArrayLength to get the array's length in bytes. */ -template -inline const byte* MidiInterface::getSysExArray() const +template +inline const byte* MidiInterface::getSysExArray() const { return mMessage.sysexArray; } @@ -1082,23 +1102,23 @@ inline const byte* MidiInterface::getSysExArray() const It is coded using data1 as LSB and data2 as MSB. \return The array's length, in bytes. */ -template -inline unsigned MidiInterface::getSysExArrayLength() const +template +inline unsigned MidiInterface::getSysExArrayLength() const { return mMessage.getSysExSize(); } /*! \brief Check if a valid message is stored in the structure. */ -template -inline bool MidiInterface::check() const +template +inline bool MidiInterface::check() const { return mMessage.valid; } // ----------------------------------------------------------------------------- -template -inline Channel MidiInterface::getInputChannel() const +template +inline Channel MidiInterface::getInputChannel() const { return mInputChannel; } @@ -1107,8 +1127,8 @@ inline Channel MidiInterface::getInputChannel() const \param inChannel the channel value. Valid values are 1 to 16, MIDI_CHANNEL_OMNI if you want to listen to all channels, and MIDI_CHANNEL_OFF to disable input. */ -template -inline void MidiInterface::setInputChannel(Channel inChannel) +template +inline void MidiInterface::setInputChannel(Channel inChannel) { mInputChannel = inChannel; } @@ -1120,8 +1140,8 @@ inline void MidiInterface::setInputChannel(Channel inChann This is a utility static method, used internally, made public so you can handle MidiTypes more easily. */ -template -MidiType MidiInterface::getTypeFromStatusByte(byte inStatus) +template +MidiType MidiInterface::getTypeFromStatusByte(byte inStatus) { if ((inStatus < 0x80) || (inStatus == 0xf4) || @@ -1143,14 +1163,14 @@ MidiType MidiInterface::getTypeFromStatusByte(byte inStatu /*! \brief Returns channel in the range 1-16 */ -template -inline Channel MidiInterface::getChannelFromStatusByte(byte inStatus) +template +inline Channel MidiInterface::getChannelFromStatusByte(byte inStatus) { return Channel((inStatus & 0x0f) + 1); } -template -bool MidiInterface::isChannelMessage(MidiType inType) +template +bool MidiInterface::isChannelMessage(MidiType inType) { return (inType == NoteOff || inType == NoteOn || @@ -1167,24 +1187,24 @@ bool MidiInterface::isChannelMessage(MidiType inType) @{ */ -template void MidiInterface::setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOffCallback = fptr; } -template void MidiInterface::setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOnCallback = fptr; } -template void MidiInterface::setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure)) { mAfterTouchPolyCallback = fptr; } -template void MidiInterface::setHandleControlChange(void (*fptr)(byte channel, byte number, byte value)) { mControlChangeCallback = fptr; } -template void MidiInterface::setHandleProgramChange(void (*fptr)(byte channel, byte number)) { mProgramChangeCallback = fptr; } -template void MidiInterface::setHandleAfterTouchChannel(void (*fptr)(byte channel, byte pressure)) { mAfterTouchChannelCallback = fptr; } -template void MidiInterface::setHandlePitchBend(void (*fptr)(byte channel, int bend)) { mPitchBendCallback = fptr; } -template void MidiInterface::setHandleSystemExclusive(void (*fptr)(byte* array, unsigned size)) { mSystemExclusiveCallback = fptr; } -template void MidiInterface::setHandleTimeCodeQuarterFrame(void (*fptr)(byte data)) { mTimeCodeQuarterFrameCallback = fptr; } -template void MidiInterface::setHandleSongPosition(void (*fptr)(unsigned beats)) { mSongPositionCallback = fptr; } -template void MidiInterface::setHandleSongSelect(void (*fptr)(byte songnumber)) { mSongSelectCallback = fptr; } -template void MidiInterface::setHandleTuneRequest(void (*fptr)(void)) { mTuneRequestCallback = fptr; } -template void MidiInterface::setHandleClock(void (*fptr)(void)) { mClockCallback = fptr; } -template void MidiInterface::setHandleStart(void (*fptr)(void)) { mStartCallback = fptr; } -template void MidiInterface::setHandleContinue(void (*fptr)(void)) { mContinueCallback = fptr; } -template void MidiInterface::setHandleStop(void (*fptr)(void)) { mStopCallback = fptr; } -template void MidiInterface::setHandleActiveSensing(void (*fptr)(void)) { mActiveSensingCallback = fptr; } -template void MidiInterface::setHandleSystemReset(void (*fptr)(void)) { mSystemResetCallback = fptr; } +template void MidiInterface::setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOffCallback = fptr; } +template void MidiInterface::setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOnCallback = fptr; } +template void MidiInterface::setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure)) { mAfterTouchPolyCallback = fptr; } +template void MidiInterface::setHandleControlChange(void (*fptr)(byte channel, byte number, byte value)) { mControlChangeCallback = fptr; } +template void MidiInterface::setHandleProgramChange(void (*fptr)(byte channel, byte number)) { mProgramChangeCallback = fptr; } +template void MidiInterface::setHandleAfterTouchChannel(void (*fptr)(byte channel, byte pressure)) { mAfterTouchChannelCallback = fptr; } +template void MidiInterface::setHandlePitchBend(void (*fptr)(byte channel, int bend)) { mPitchBendCallback = fptr; } +template void MidiInterface::setHandleSystemExclusive(void (*fptr)(byte* array, unsigned size)) { mSystemExclusiveCallback = fptr; } +template void MidiInterface::setHandleTimeCodeQuarterFrame(void (*fptr)(byte data)) { mTimeCodeQuarterFrameCallback = fptr; } +template void MidiInterface::setHandleSongPosition(void (*fptr)(unsigned beats)) { mSongPositionCallback = fptr; } +template void MidiInterface::setHandleSongSelect(void (*fptr)(byte songnumber)) { mSongSelectCallback = fptr; } +template void MidiInterface::setHandleTuneRequest(void (*fptr)(void)) { mTuneRequestCallback = fptr; } +template void MidiInterface::setHandleClock(void (*fptr)(void)) { mClockCallback = fptr; } +template void MidiInterface::setHandleStart(void (*fptr)(void)) { mStartCallback = fptr; } +template void MidiInterface::setHandleContinue(void (*fptr)(void)) { mContinueCallback = fptr; } +template void MidiInterface::setHandleStop(void (*fptr)(void)) { mStopCallback = fptr; } +template void MidiInterface::setHandleActiveSensing(void (*fptr)(void)) { mActiveSensingCallback = fptr; } +template void MidiInterface::setHandleSystemReset(void (*fptr)(void)) { mSystemResetCallback = fptr; } /*! \brief Detach an external function from the given type. @@ -1192,8 +1212,8 @@ template void MidiInterface -void MidiInterface::disconnectCallbackFromType(MidiType inType) +template +void MidiInterface::disconnectCallbackFromType(MidiType inType) { switch (inType) { @@ -1223,8 +1243,8 @@ void MidiInterface::disconnectCallbackFromType(MidiType in /*! @} */ // End of doc group MIDI Callbacks // Private - launch callback function based on received type. -template -void MidiInterface::launchCallback() +template +void MidiInterface::launchCallback() { // The order is mixed to allow frequent messages to trigger their callback faster. switch (mMessage.type) @@ -1242,7 +1262,7 @@ void MidiInterface::launchCallback() // Continuous controllers case ControlChange: if (mControlChangeCallback != 0) mControlChangeCallback(mMessage.channel, mMessage.data1, mMessage.data2); break; - case PitchBend: if (mPitchBendCallback != 0) mPitchBendCallback(mMessage.channel, (int)((mMessage.data1 & 0x7f) | ((mMessage.data2 & 0x7f) << 7)) + MIDI_PITCHBEND_MIN); break; // TODO: check this + case PitchBend: if (mPitchBendCallback != 0) mPitchBendCallback(mMessage.channel, (int)((mMessage.data1 & 0x7f) | ((mMessage.data2 & 0x7f) << 7)) + MIDI_PITCHBEND_MIN); break; case AfterTouchPoly: if (mAfterTouchPolyCallback != 0) mAfterTouchPolyCallback(mMessage.channel, mMessage.data1, mMessage.data2); break; case AfterTouchChannel: if (mAfterTouchChannelCallback != 0) mAfterTouchChannelCallback(mMessage.channel, mMessage.data1); break; @@ -1278,34 +1298,34 @@ void MidiInterface::launchCallback() @see Thru::Mode */ -template -inline void MidiInterface::setThruFilterMode(Thru::Mode inThruFilterMode) +template +inline void MidiInterface::setThruFilterMode(Thru::Mode inThruFilterMode) { mThruFilterMode = inThruFilterMode; mThruActivated = mThruFilterMode != Thru::Off; } -template -inline Thru::Mode MidiInterface::getFilterMode() const +template +inline Thru::Mode MidiInterface::getFilterMode() const { return mThruFilterMode; } -template -inline bool MidiInterface::getThruState() const +template +inline bool MidiInterface::getThruState() const { return mThruActivated; } -template -inline void MidiInterface::turnThruOn(Thru::Mode inThruFilterMode) +template +inline void MidiInterface::turnThruOn(Thru::Mode inThruFilterMode) { mThruActivated = true; mThruFilterMode = inThruFilterMode; } -template -inline void MidiInterface::turnThruOff() +template +inline void MidiInterface::turnThruOff() { mThruActivated = false; mThruFilterMode = Thru::Off; @@ -1319,8 +1339,8 @@ inline void MidiInterface::turnThruOff() // to output unless filter is set to Off. // - Channel messages are passed to the output whether their channel // is matching the input channel and the filter setting -template -void MidiInterface::thruFilter(Channel inChannel) +template +void MidiInterface::thruFilter(Channel inChannel) { // If the feature is disabled, don't do anything. if (!mThruActivated || (mThruFilterMode == Thru::Off)) diff --git a/src/midi_Defs.h b/src/midi_Defs.h index 5f43098..810e8de 100644 --- a/src/midi_Defs.h +++ b/src/midi_Defs.h @@ -73,12 +73,12 @@ enum MidiType: uint8_t AfterTouchChannel = 0xD0, ///< Channel (monophonic) AfterTouch PitchBend = 0xE0, ///< Pitch Bend SystemExclusive = 0xF0, ///< System Exclusive - SystemExclusiveStart = SystemExclusive, ///< System Exclusive Start + SystemExclusiveStart = SystemExclusive, ///< System Exclusive Start TimeCodeQuarterFrame = 0xF1, ///< System Common - MIDI Time Code Quarter Frame SongPosition = 0xF2, ///< System Common - Song Position Pointer SongSelect = 0xF3, ///< System Common - Song Select TuneRequest = 0xF6, ///< System Common - Tune Request - SystemExclusiveEnd = 0xF7, ///< System Exclusive End + SystemExclusiveEnd = 0xF7, ///< System Exclusive End Clock = 0xF8, ///< System Real Time - Timing Clock Start = 0xFA, ///< System Real Time - Start Continue = 0xFB, ///< System Real Time - Continue @@ -206,35 +206,4 @@ struct RPN }; }; -// ----------------------------------------------------------------------------- - -/*! \brief Create an instance of the library attached to a serial port. - You can use HardwareSerial or SoftwareSerial for the serial port. - Example: MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midi2); - Then call midi2.begin(), midi2.read() etc.. - */ -#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \ - midi::MidiInterface Name((Type&)SerialPort); - -#if defined(SERIAL_PORT_HARDWARE_OPEN) - // Use recommended default external serial port. - #define MIDI_CREATE_DEFAULT_INSTANCE() \ - MIDI_CREATE_INSTANCE(HardwareSerial, SERIAL_PORT_HARDWARE_OPEN, MIDI); -#else - /*! \brief Create an instance of the library with default name, serial port - and settings, for compatibility with sketches written with pre-v4.2 MIDI Lib, - or if you don't bother using custom names, serial port or settings. - */ - #define MIDI_CREATE_DEFAULT_INSTANCE() \ - MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI); -#endif - -/*! \brief Create an instance of the library attached to a serial port with - custom settings. - @see DefaultSettings - @see MIDI_CREATE_INSTANCE - */ -#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \ - midi::MidiInterface Name((Type&)SerialPort); - END_MIDI_NAMESPACE diff --git a/src/midi_Message.h b/src/midi_Message.h index 0285b58..04b56c9 100644 --- a/src/midi_Message.h +++ b/src/midi_Message.h @@ -46,7 +46,7 @@ struct Message */ inline Message() : channel(0) - , type(midi::InvalidType) + , type(MIDI_NAMESPACE::InvalidType) , data1(0) , data2(0) , valid(false) diff --git a/src/midi_Namespace.h b/src/midi_Namespace.h index 0348dfa..cab0a2c 100644 --- a/src/midi_Namespace.h +++ b/src/midi_Namespace.h @@ -27,7 +27,7 @@ #pragma once -#define MIDI_NAMESPACE midi +#define MIDI_NAMESPACE midi_v440 #define BEGIN_MIDI_NAMESPACE namespace MIDI_NAMESPACE { #define END_MIDI_NAMESPACE } diff --git a/src/midi_RingBuffer.h b/src/midi_RingBuffer.h deleted file mode 100644 index 348ad9e..0000000 --- a/src/midi_RingBuffer.h +++ /dev/null @@ -1,68 +0,0 @@ -/*! - * @file midi_RingBuffer.h - * Project Arduino MIDI Library - * @brief MIDI Library for Arduino - Ring Buffer - * @author Francois Best - * @date 10/10/2016 - * @license MIT - Copyright (c) 2016 Francois Best - * - * 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. - * - * 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. - */ - -#pragma once - -#include "midi_Namespace.h" - -BEGIN_MIDI_NAMESPACE - -template -class RingBuffer -{ -private: - static const int sMask = Size - 1; - -public: - RingBuffer(); - ~RingBuffer(); - -public: - inline int getLength() const; - inline bool isEmpty() const; - -public: - void write(DataType inData); - void write(const DataType* inData, int inSize); - void pop(int inNumberOfItems = 1); - void clear(); - -public: - DataType peek(int inOffset = 0) const; - DataType read(); - void read(DataType* outData, int inSize); - -private: - DataType mData[Size]; - int mLength; - int mWriteHead; - int mReadHead; -}; - -END_MIDI_NAMESPACE - -#include "midi_RingBuffer.hpp" diff --git a/src/midi_RingBuffer.hpp b/src/midi_RingBuffer.hpp deleted file mode 100644 index f3c53eb..0000000 --- a/src/midi_RingBuffer.hpp +++ /dev/null @@ -1,145 +0,0 @@ -/*! - * @file midi_RingBuffer.hpp - * Project Arduino MIDI Library - * @brief MIDI Library for Arduino - Ring Buffer - * @author Francois Best - * @date 10/10/2016 - * @license MIT - Copyright (c) 2016 Francois Best - * - * 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. - * - * 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. - */ - -#pragma once - -namespace { - - template - struct isPowerOfTwo - { - static const bool value = N && !(N & (N - 1)); - }; - -} - -// -- - -BEGIN_MIDI_NAMESPACE - -template -RingBuffer::RingBuffer() - : mLength(0) - , mWriteHead(0) - , mReadHead(0) -{ - static_assert(isPowerOfTwo::value, "Size must be a power of two."); - memset(mData, DataType(0), Size * sizeof(DataType)); -} - -template -RingBuffer::~RingBuffer() -{ -} - -// ----------------------------------------------------------------------------- - -template -inline int RingBuffer::getLength() const -{ - return mLength; -} - -template -inline bool RingBuffer::isEmpty() const -{ - return mLength == 0; -} - -// ----------------------------------------------------------------------------- - -template -void RingBuffer::write(DataType inData) -{ - mData[mWriteHead] = inData; - mWriteHead = (mWriteHead + 1) & sMask; - mLength++; - if (mLength > Size) { - mLength = Size; - mReadHead = (mReadHead + 1) & sMask; - } -} - -template -void RingBuffer::write(const DataType* inData, int inSize) -{ - for (int i = 0; i < inSize; ++i) - { - write(inData[i]); - } -} - -template -void RingBuffer::pop(int inNumberOfItems) -{ - for (int i = 0; i < inNumberOfItems; ++i) - { - read(); - } -} - -template -void RingBuffer::clear() -{ - memset(mData, DataType(0), Size * sizeof(DataType)); - mReadHead = 0; - mWriteHead = 0; - mLength = 0; -} - -// ----------------------------------------------------------------------------- - -template -DataType RingBuffer::peek(int inOffset) const -{ - const int head = (mReadHead + inOffset) & sMask; - return mData[head]; -} - -template -DataType RingBuffer::read() -{ - mLength--; - if (mLength < 0) { - mLength = 0; - return 0; - } - const DataType data = mData[mReadHead]; - mReadHead = (mReadHead + 1) & sMask; - return data; -} - -template -void RingBuffer::read(DataType* outData, int inSize) -{ - for (int i = 0; i < inSize; ++i) - { - outData[i] = read(); - } -} - -END_MIDI_NAMESPACE diff --git a/src/midi_Settings.h b/src/midi_Settings.h index 9e1a52c..84716c4 100644 --- a/src/midi_Settings.h +++ b/src/midi_Settings.h @@ -70,19 +70,13 @@ struct DefaultSettings termination, the receiver will turn off all voices and return to normal (non- active sensing) operation.. */ - static const bool UseSenderActiveSensing = true; + static const bool UseSenderActiveSensing = false; /*! Setting this to true will make MIDI.read parse only one byte of data for each call when data is available. This can speed up your application if receiving a lot of traffic, but might induce MIDI Thru and treatment latency. */ - static const bool Use1ByteParsing = true; - - /*! Override the default MIDI baudrate to transmit over USB serial, to - a decoding program such as Hairless MIDI (set baudrate to 115200)\n - http://projectgus.github.io/hairless-midiserial/ - */ - static const long BaudRate = 31250; + static const bool Use1ByteParsing = false; /*! Maximum size of SysEx receivable. Decrease to save RAM if you don't expect to receive SysEx, or adjust accordingly. diff --git a/src/midi_UsbDefs.h b/src/midi_UsbDefs.h deleted file mode 100644 index 54defb5..0000000 --- a/src/midi_UsbDefs.h +++ /dev/null @@ -1,135 +0,0 @@ -/*! - * @file midi_UsbDefs.h - * Project Arduino MIDI Library - * @brief MIDI Library for the Arduino - Definitions - * @author Francois Best - * @date 24/02/11 - * @license MIT - Copyright (c) 2016 Francois Best - * - * 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. - * - * 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. - */ - -#pragma once - -#include "midi_Defs.h" - -BEGIN_MIDI_NAMESPACE - -struct CodeIndexNumbers -{ - enum - { - reserved = 0x00, - misc = reserved, - - cableEvent = 0x01, - systemCommon2Bytes = 0x02, - systemCommon3Bytes = 0x03, - - sysExStart = 0x04, - sysExContinue = sysExStart, - - systemCommon1Byte = 0x05, - sysExEnds1Byte = systemCommon1Byte, - - sysExEnds2Bytes = 0x06, - sysExEnds3Bytes = 0x07, - noteOff = 0x08, - noteOn = 0x09, - polyPressure = 0x0A, - controlChange = 0x0B, - programChange = 0x0C, - channelPressure = 0x0D, - pitchBend = 0x0E, - singleByte = 0x0F, - }; - - static inline byte fromStatus(StatusByte inStatus) - { - const byte statusWithoutChannel = inStatus & 0xf0; - if (statusWithoutChannel >= midi::NoteOff && - statusWithoutChannel <= midi::PitchBend) - { - // Channel Voice Messages - return inStatus >> 4; - } - switch (inStatus) - { - // System Real Time Messages - case midi::Clock: - case midi::Start: - case midi::Continue: - case midi::Stop: - case midi::ActiveSensing: - case midi::SystemReset: - return CodeIndexNumbers::singleByte; - - // System Exclusive - case midi::SystemExclusive: - return CodeIndexNumbers::sysExStart; - case 0xf7: - return CodeIndexNumbers::sysExEnds1Byte; - - // System Common Messages - case midi::TimeCodeQuarterFrame: - return CodeIndexNumbers::systemCommon2Bytes; - case midi::SongPosition: - return CodeIndexNumbers::systemCommon3Bytes; - case midi::SongSelect: - return CodeIndexNumbers::systemCommon2Bytes; - case midi::TuneRequest: - return CodeIndexNumbers::systemCommon1Byte; - - default: - return CodeIndexNumbers::reserved; - } - - } - - static inline byte getSize(byte inCodeIndexNumber) - { - switch (inCodeIndexNumber) - { - case noteOn: - case noteOff: - case controlChange: - case pitchBend: - case polyPressure: - case systemCommon3Bytes: - case sysExEnds3Bytes: - case sysExStart: - return 3; - - case programChange: - case channelPressure: - case systemCommon2Bytes: - case sysExEnds2Bytes: - return 2; - - case systemCommon1Byte: // also sysExEnds1Byte - case singleByte: - return 1; - - default: - return 0; // Can be any length (1, 2 or 3). - } - } -}; - -END_MIDI_NAMESPACE diff --git a/src/midi_UsbPacketInterface.h b/src/midi_UsbPacketInterface.h deleted file mode 100644 index ee27901..0000000 --- a/src/midi_UsbPacketInterface.h +++ /dev/null @@ -1,44 +0,0 @@ -/*! - * @file midi_UsbPacketInterface.h - * Project Arduino MIDI Library - * @brief MIDI Library for the Arduino - Transport layer for USB MIDI - * @author Francois Best - * @date 2018-11-03 - * @license MIT - Copyright (c) 2018 Francois Best - * - * 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. - * - * 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. - */ - -#pragma once - -#include "midi_Defs.h" -#include "midi_UsbDefs.h" -#include - -BEGIN_MIDI_NAMESPACE - -template -bool composeTxPacket(Buffer& inBuffer, midiEventPacket_t& outPacket); - -template -void serialiseRxPacket(const midiEventPacket_t& inPacket, Buffer& outBuffer); - -END_MIDI_NAMESPACE - -#include "midi_UsbPacketInterface.hpp" diff --git a/src/midi_UsbPacketInterface.hpp b/src/midi_UsbPacketInterface.hpp deleted file mode 100644 index 26cb22a..0000000 --- a/src/midi_UsbPacketInterface.hpp +++ /dev/null @@ -1,146 +0,0 @@ -/*! - * @file midi_UsbPacketInterface.hpp - * Project Arduino MIDI Library - * @brief MIDI Library for the Arduino - Transport layer for USB MIDI - * @author Francois Best - * @date 2018-11-03 - * @license MIT - Copyright (c) 2018 Francois Best - * - * 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. - * - * 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. - */ - -#pragma once - -BEGIN_MIDI_NAMESPACE - -template -bool composeTxPacket(Buffer& inBuffer, midiEventPacket_t& outPacket) -{ - if (inBuffer.isEmpty()) { - return false; - } - const int bufferLength = inBuffer.getLength(); - const byte status = inBuffer.peek(); - const byte cin = midi::CodeIndexNumbers::fromStatus(status); - const byte messageLength = midi::CodeIndexNumbers::getSize(cin); - - if (status == 0xf0) - { - // Start of SysEx, check if it can end in one go. - if (bufferLength == 2 && inBuffer.peek(1) == 0xf7) - { - outPacket.header = midi::CodeIndexNumbers::sysExEnds2Bytes; - outPacket.byte1 = status; - outPacket.byte2 = 0xf7; - outPacket.byte3 = 0x00; - inBuffer.pop(2); - return true; - } - if (bufferLength >= 3 && inBuffer.peek(2) == 0xf7) - { - outPacket.header = midi::CodeIndexNumbers::sysExEnds3Bytes; - outPacket.byte1 = status; - outPacket.byte2 = inBuffer.peek(1); - outPacket.byte3 = 0xf7; - inBuffer.pop(3); - return true; - } - } - - if ((status & 0x80) == 0x00) - { - // First byte is data, consider it's part of a running SysEx message. - // We look for the SysEx end byte in the next 2 bytes - // At this point, bufferLength should be 2 or more to continue. - if (bufferLength == 1) - { - return false; // Not enough data - } - if (bufferLength == 2) - { - const bool isSysExEnd = inBuffer.peek(1) == 0xf7; - if (!isSysExEnd) - { - return false; // Not enough data (eg: 0x12 0x42) - } - // eg: 0x42 0xf7 - outPacket.header = midi::CodeIndexNumbers::sysExEnds2Bytes; - outPacket.byte1 = status; - outPacket.byte2 = inBuffer.peek(1); - outPacket.byte3 = 0x00; - inBuffer.pop(2); - return true; - } - else - { - // bufferLength > 2 - const byte byte3 = inBuffer.peek(2); - outPacket.header = byte3 == 0xf7 - ? midi::CodeIndexNumbers::sysExEnds3Bytes - : midi::CodeIndexNumbers::sysExContinue; - outPacket.byte1 = status; - outPacket.byte2 = inBuffer.peek(1); - outPacket.byte3 = byte3; - inBuffer.pop(3); - return true; - } - } - - if (bufferLength < messageLength) { - return false; // Not enough data in the buffer to compose a full packet. - } - - outPacket.header = cin; - outPacket.byte1 = status; - outPacket.byte2 = messageLength >= 2 ? inBuffer.peek(1) : 0x00; - outPacket.byte3 = messageLength >= 3 ? inBuffer.peek(2) : 0x00; - - inBuffer.pop(messageLength); - return true; - - // todo: handle interleaved RealTime messages -} - -template -void serialiseRxPacket(const midiEventPacket_t& inPacket, Buffer& outBuffer) -{ - const byte cin = inPacket.header & 0x0f; - const byte messageLength = midi::CodeIndexNumbers::getSize(cin); - switch (messageLength) - { - case 1: - outBuffer.write(inPacket.byte1); - return; - case 2: - outBuffer.write(inPacket.byte1); - outBuffer.write(inPacket.byte2); - return; - case 3: - outBuffer.write(inPacket.byte1); - outBuffer.write(inPacket.byte2); - outBuffer.write(inPacket.byte3); - return; - case 0: - default: - // Invalid or ignored messages, don't serialise. - return; - } -} - -END_MIDI_NAMESPACE diff --git a/src/midi_UsbTransport.h b/src/midi_UsbTransport.h deleted file mode 100644 index eb869e9..0000000 --- a/src/midi_UsbTransport.h +++ /dev/null @@ -1,62 +0,0 @@ -/*! - * @file midi_UsbTransport.h - * Project Arduino MIDI Library - * @brief MIDI Library for the Arduino - Transport layer for USB MIDI - * @author Francois Best - * @date 10/10/2016 - * @license MIT - Copyright (c) 2016 Francois Best - * - * 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. - * - * 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. - */ - -#pragma once - -#include "midi_Defs.h" -#include "midi_RingBuffer.h" -#include "midi_UsbPacketInterface.h" -#include - -BEGIN_MIDI_NAMESPACE - -template -class UsbTransport -{ -public: - inline UsbTransport(); - inline ~UsbTransport(); - -public: // Serial / Stream API required for template compatibility - inline void begin(unsigned inBaudrate); - inline unsigned available(); - inline byte read(); - inline void write(byte inData); - -private: - inline void pollUsbMidi(); - inline void recomposeAndSendTxPackets(); - -private: - typedef RingBuffer Buffer; - Buffer mTxBuffer; - Buffer mRxBuffer; -}; - -END_MIDI_NAMESPACE - -#include "midi_UsbTransport.hpp" diff --git a/src/midi_UsbTransport.hpp b/src/midi_UsbTransport.hpp deleted file mode 100644 index 5159433..0000000 --- a/src/midi_UsbTransport.hpp +++ /dev/null @@ -1,102 +0,0 @@ -/*! - * @file midi_UsbTransport.hpp - * Project Arduino MIDI Library - * @brief MIDI Library for the Arduino - Transport layer for USB MIDI - * @author Francois Best - * @date 10/10/2016 - * @license MIT - Copyright (c) 2016 Francois Best - * - * 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. - * - * 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. - */ - -#pragma once - -BEGIN_MIDI_NAMESPACE - -template -inline UsbTransport::UsbTransport() -{ - -} - -template -inline UsbTransport::~UsbTransport() -{ - -} - -// ----------------------------------------------------------------------------- - -template -inline void UsbTransport::begin(unsigned inBaudrate) -{ - mTxBuffer.clear(); - mRxBuffer.clear(); -} - -template -inline unsigned UsbTransport::available() -{ - pollUsbMidi(); - return mRxBuffer.getLength(); -} - -template -inline byte UsbTransport::read() -{ - return mRxBuffer.read(); -} - -template -inline void UsbTransport::write(byte inData) -{ - mTxBuffer.write(inData); - recomposeAndSendTxPackets(); -} - -// ----------------------------------------------------------------------------- - -template -inline void UsbTransport::pollUsbMidi() -{ - midiEventPacket_t packet = MidiUSB.read(); - while (packet.header != 0) - { - serialiseRxPacket(packet, mRxBuffer); - packet = MidiUSB.read(); - } -} - -template -inline void UsbTransport::recomposeAndSendTxPackets() -{ - midiEventPacket_t packet; - bool sent = false; - while (composeTxPacket(mTxBuffer, packet)) - { - MidiUSB.sendMIDI(packet); - sent = true; - } - if (sent) - { - MidiUSB.flush(); - } -} - -END_MIDI_NAMESPACE diff --git a/src/serialMIDI.h b/src/serialMIDI.h new file mode 100644 index 0000000..622c32f --- /dev/null +++ b/src/serialMIDI.h @@ -0,0 +1,71 @@ +#pragma once + +BEGIN_MIDI_NAMESPACE + +template +class serialMIDI +{ +public: + serialMIDI(SerialPort& inSerial) + : mSerial(inSerial) + { + }; + +public: + void begin(MIDI_NAMESPACE::Channel inChannel = 1) + { + } + + bool beginTransmission() + { + return true; + }; + + void write(byte byte) + { + mSerial.write(byte); + }; + + void endTransmission() + { + }; + + byte read() + { + return mSerial.read(); + }; + + unsigned available() + { + return mSerial.available(); + }; + +private: + SerialPort& mSerial; +}; + +/*! \brief Create an instance of the library attached to a serial port. + You can use HardwareSerial or SoftwareSerial for the serial port. + Example: MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midi2); + Then call midi2.begin(), midi2.read() etc.. + */ +#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \ + typedef MIDI_NAMESPACE::serialMIDI __amt;\ + __amt serialMidi(SerialPort);\ + MIDI_NAMESPACE::MidiInterface<__amt> Name((__amt&)serialMidi); + +#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) + // Leonardo, Due and other USB boards use Serial1 by default. + #define MIDI_CREATE_DEFAULT_INSTANCE() \ + MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); +#else + /*! \brief Create an instance of the library with default name, serial port + and settings, for compatibility with sketches written with pre-v4.2 MIDI Lib, + or if you don't bother using custom names, serial port or settings. + */ + #define MIDI_CREATE_DEFAULT_INSTANCE() \ + MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI); +#endif + + +END_MIDI_NAMESPACE