mirror of https://github.com/probonopd/MiniDexed
* CMIDIDevice is the generic MIDI handler * CMIDIKeyboard handles USB audio class MIDI devices * CSerialMIDIDevice handles the serial MIDI device * Now all MIDI inputs can work simultaneous * Program change and bank select work with serial MIDIpull/37/head
parent
ae0a2262c4
commit
b0b62a7640
@ -0,0 +1,143 @@ |
|||||||
|
//
|
||||||
|
// mididevice.cpp
|
||||||
|
//
|
||||||
|
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||||
|
// Copyright (C) 2022 The MiniDexed Team
|
||||||
|
//
|
||||||
|
// Original author of this class:
|
||||||
|
// R. Stange <rsta2@o2online.de>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
#include "mididevice.h" |
||||||
|
#include "minidexed.h" |
||||||
|
#include "config.h" |
||||||
|
#include <stdio.h> |
||||||
|
#include <assert.h> |
||||||
|
|
||||||
|
#define MIDI_NOTE_OFF 0b1000 |
||||||
|
#define MIDI_NOTE_ON 0b1001 |
||||||
|
#define MIDI_AFTERTOUCH 0b1010 // TODO
|
||||||
|
#define MIDI_CONTROL_CHANGE 0b1011 |
||||||
|
#define MIDI_CC_BANK_SELECT_MSB 0 // TODO
|
||||||
|
#define MIDI_CC_BANK_SELECT_LSB 32 |
||||||
|
#define MIDI_PROGRAM_CHANGE 0b1100 |
||||||
|
#define MIDI_PITCH_BEND 0b1110 |
||||||
|
|
||||||
|
CMIDIDevice::CMIDIDevice (CMiniDexed *pSynthesizer, CConfig *pConfig) |
||||||
|
: m_pSynthesizer (pSynthesizer), |
||||||
|
m_pConfig (pConfig) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
CMIDIDevice::~CMIDIDevice (void) |
||||||
|
{ |
||||||
|
m_pSynthesizer = 0; |
||||||
|
} |
||||||
|
|
||||||
|
void CMIDIDevice::MIDIMessageHandler (const u8 *pMessage, size_t nLength, unsigned nCable) |
||||||
|
{ |
||||||
|
assert (m_pSynthesizer != 0); |
||||||
|
|
||||||
|
// The packet contents are just normal MIDI data - see
|
||||||
|
// https://www.midi.org/specifications/item/table-1-summary-of-midi-message
|
||||||
|
|
||||||
|
if (m_pConfig->GetMIDIDumpEnabled ()) |
||||||
|
{ |
||||||
|
switch (nLength) |
||||||
|
{ |
||||||
|
case 1: |
||||||
|
printf ("MIDI %u: %02X\n", nCable, (unsigned) pMessage[0]); |
||||||
|
break; |
||||||
|
|
||||||
|
case 2: |
||||||
|
printf ("MIDI %u: %02X %02X\n", nCable, |
||||||
|
(unsigned) pMessage[0], (unsigned) pMessage[1]); |
||||||
|
break; |
||||||
|
|
||||||
|
case 3: |
||||||
|
printf ("MIDI %u: %02X %02X %02X\n", nCable, |
||||||
|
(unsigned) pMessage[0], (unsigned) pMessage[1], |
||||||
|
(unsigned) pMessage[2]); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (nLength < 2) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
u8 ucStatus = pMessage[0]; |
||||||
|
// TODO: u8 ucChannel = ucStatus & 0x0F;
|
||||||
|
u8 ucType = ucStatus >> 4; |
||||||
|
u8 ucKeyNumber = pMessage[1]; |
||||||
|
u8 ucVelocity = pMessage[2]; |
||||||
|
|
||||||
|
switch (ucType) |
||||||
|
{ |
||||||
|
case MIDI_NOTE_ON: |
||||||
|
if (nLength < 3) |
||||||
|
{ |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
if (ucVelocity > 0) |
||||||
|
{ |
||||||
|
if (ucVelocity <= 127) |
||||||
|
{ |
||||||
|
m_pSynthesizer->keydown (ucKeyNumber, ucVelocity); |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
m_pSynthesizer->keyup (ucKeyNumber); |
||||||
|
} |
||||||
|
break; |
||||||
|
|
||||||
|
case MIDI_NOTE_OFF: |
||||||
|
if (nLength < 3) |
||||||
|
{ |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
m_pSynthesizer->keyup (ucKeyNumber); |
||||||
|
break; |
||||||
|
|
||||||
|
case MIDI_CONTROL_CHANGE: |
||||||
|
if (nLength < 3) |
||||||
|
{ |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
switch (pMessage[1]) |
||||||
|
{ |
||||||
|
case MIDI_CC_BANK_SELECT_LSB: |
||||||
|
m_pSynthesizer->BankSelectLSB (pMessage[2]); |
||||||
|
break; |
||||||
|
} |
||||||
|
break; |
||||||
|
|
||||||
|
case MIDI_PROGRAM_CHANGE: |
||||||
|
m_pSynthesizer->ProgramChange (pMessage[1]); |
||||||
|
break; |
||||||
|
|
||||||
|
case MIDI_PITCH_BEND: |
||||||
|
m_pSynthesizer->setPitchbend (pMessage[1]); |
||||||
|
break; |
||||||
|
|
||||||
|
default: |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,45 @@ |
|||||||
|
//
|
||||||
|
// mididevice.h
|
||||||
|
//
|
||||||
|
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||||
|
// Copyright (C) 2022 The MiniDexed Team
|
||||||
|
//
|
||||||
|
// Original author of this class:
|
||||||
|
// R. Stange <rsta2@o2online.de>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
#ifndef _mididevice_h |
||||||
|
#define _mididevice_h |
||||||
|
|
||||||
|
#include "config.h" |
||||||
|
#include <circle/types.h> |
||||||
|
|
||||||
|
class CMiniDexed; |
||||||
|
|
||||||
|
class CMIDIDevice |
||||||
|
{ |
||||||
|
public: |
||||||
|
CMIDIDevice (CMiniDexed *pSynthesizer, CConfig *pConfig); |
||||||
|
~CMIDIDevice (void); |
||||||
|
|
||||||
|
protected: |
||||||
|
void MIDIMessageHandler (const u8 *pMessage, size_t nLength, unsigned nCable = 0); |
||||||
|
|
||||||
|
private: |
||||||
|
CMiniDexed *m_pSynthesizer; |
||||||
|
CConfig *m_pConfig; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,71 @@ |
|||||||
|
//
|
||||||
|
// midikeyboard.cpp
|
||||||
|
//
|
||||||
|
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||||
|
// Copyright (C) 2022 The MiniDexed Team
|
||||||
|
//
|
||||||
|
// Original author of this class:
|
||||||
|
// R. Stange <rsta2@o2online.de>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
#include "midikeyboard.h" |
||||||
|
#include <circle/devicenameservice.h> |
||||||
|
#include <assert.h> |
||||||
|
|
||||||
|
CMIDIKeyboard *CMIDIKeyboard::s_pThis = 0; |
||||||
|
|
||||||
|
CMIDIKeyboard::CMIDIKeyboard (CMiniDexed *pSynthesizer, CConfig *pConfig) |
||||||
|
: CMIDIDevice (pSynthesizer, pConfig), |
||||||
|
m_pMIDIDevice (0) |
||||||
|
{ |
||||||
|
s_pThis = this; |
||||||
|
} |
||||||
|
|
||||||
|
CMIDIKeyboard::~CMIDIKeyboard (void) |
||||||
|
{ |
||||||
|
s_pThis = 0; |
||||||
|
} |
||||||
|
|
||||||
|
void CMIDIKeyboard::Process (boolean bPlugAndPlayUpdated) |
||||||
|
{ |
||||||
|
if (!bPlugAndPlayUpdated) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (m_pMIDIDevice == 0) |
||||||
|
{ |
||||||
|
m_pMIDIDevice = |
||||||
|
(CUSBMIDIDevice *) CDeviceNameService::Get ()->GetDevice ("umidi1", FALSE); |
||||||
|
if (m_pMIDIDevice != 0) |
||||||
|
{ |
||||||
|
m_pMIDIDevice->RegisterPacketHandler (MIDIPacketHandler); |
||||||
|
|
||||||
|
m_pMIDIDevice->RegisterRemovedHandler (DeviceRemovedHandler); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void CMIDIKeyboard::MIDIPacketHandler (unsigned nCable, u8 *pPacket, unsigned nLength) |
||||||
|
{ |
||||||
|
assert (s_pThis != 0); |
||||||
|
s_pThis->MIDIMessageHandler (pPacket, nLength, nCable); |
||||||
|
} |
||||||
|
|
||||||
|
void CMIDIKeyboard::DeviceRemovedHandler (CDevice *pDevice, void *pContext) |
||||||
|
{ |
||||||
|
assert (s_pThis != 0); |
||||||
|
s_pThis->m_pMIDIDevice = 0; |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
//
|
||||||
|
// midikeyboard.h
|
||||||
|
//
|
||||||
|
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||||
|
// Copyright (C) 2022 The MiniDexed Team
|
||||||
|
//
|
||||||
|
// Original author of this class:
|
||||||
|
// R. Stange <rsta2@o2online.de>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
#ifndef _midikeyboard_h |
||||||
|
#define _midikeyboard_h |
||||||
|
|
||||||
|
#include "mididevice.h" |
||||||
|
#include "config.h" |
||||||
|
#include <circle/usb/usbmidi.h> |
||||||
|
#include <circle/device.h> |
||||||
|
#include <circle/types.h> |
||||||
|
|
||||||
|
class CMiniDexed; |
||||||
|
|
||||||
|
class CMIDIKeyboard : public CMIDIDevice |
||||||
|
{ |
||||||
|
public: |
||||||
|
CMIDIKeyboard (CMiniDexed *pSynthesizer, CConfig *pConfig); |
||||||
|
~CMIDIKeyboard (void); |
||||||
|
|
||||||
|
void Process (boolean bPlugAndPlayUpdated); |
||||||
|
|
||||||
|
private: |
||||||
|
static void MIDIPacketHandler (unsigned nCable, u8 *pPacket, unsigned nLength); |
||||||
|
|
||||||
|
static void DeviceRemovedHandler (CDevice *pDevice, void *pContext); |
||||||
|
|
||||||
|
private: |
||||||
|
CUSBMIDIDevice * volatile m_pMIDIDevice; |
||||||
|
|
||||||
|
static CMIDIKeyboard *s_pThis; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,98 @@ |
|||||||
|
//
|
||||||
|
// serialmididevice.cpp
|
||||||
|
//
|
||||||
|
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||||
|
// Copyright (C) 2022 The MiniDexed Team
|
||||||
|
//
|
||||||
|
// Original author of this class:
|
||||||
|
// R. Stange <rsta2@o2online.de>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
#include "serialmididevice.h" |
||||||
|
#include <assert.h> |
||||||
|
|
||||||
|
CSerialMIDIDevice::CSerialMIDIDevice (CMiniDexed *pSynthesizer, CInterruptSystem *pInterrupt, |
||||||
|
CConfig *pConfig) |
||||||
|
: CMIDIDevice (pSynthesizer, pConfig), |
||||||
|
m_pConfig (pConfig), |
||||||
|
m_Serial (pInterrupt, TRUE), |
||||||
|
m_nSerialState (0) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
CSerialMIDIDevice::~CSerialMIDIDevice (void) |
||||||
|
{ |
||||||
|
m_nSerialState = 255; |
||||||
|
} |
||||||
|
|
||||||
|
boolean CSerialMIDIDevice::Initialize (void) |
||||||
|
{ |
||||||
|
assert (m_pConfig); |
||||||
|
return m_Serial.Initialize (m_pConfig->GetMIDIBaudRate ()); |
||||||
|
} |
||||||
|
|
||||||
|
void CSerialMIDIDevice::Process (void) |
||||||
|
{ |
||||||
|
// Read serial MIDI data
|
||||||
|
u8 Buffer[100]; |
||||||
|
int nResult = m_Serial.Read (Buffer, sizeof Buffer); |
||||||
|
if (nResult <= 0) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// Process MIDI messages
|
||||||
|
// See: https://www.midi.org/specifications/item/table-1-summary-of-midi-message
|
||||||
|
for (int i = 0; i < nResult; i++) |
||||||
|
{ |
||||||
|
u8 uchData = Buffer[i]; |
||||||
|
|
||||||
|
switch (m_nSerialState) |
||||||
|
{ |
||||||
|
case 0: |
||||||
|
MIDIRestart: |
||||||
|
if ( (uchData & 0x80) == 0x80 // status byte, all channels
|
||||||
|
&& (uchData & 0xF0) != 0xF0) // ignore system messages
|
||||||
|
{ |
||||||
|
m_SerialMessage[m_nSerialState++] = uchData; |
||||||
|
} |
||||||
|
break; |
||||||
|
|
||||||
|
case 1: |
||||||
|
case 2: |
||||||
|
if (uchData & 0x80) // got status when parameter expected
|
||||||
|
{ |
||||||
|
m_nSerialState = 0; |
||||||
|
|
||||||
|
goto MIDIRestart; |
||||||
|
} |
||||||
|
|
||||||
|
m_SerialMessage[m_nSerialState++] = uchData; |
||||||
|
|
||||||
|
if ( (m_SerialMessage[0] & 0xE0) == 0xC0 |
||||||
|
|| m_nSerialState == 3) // message is complete
|
||||||
|
{ |
||||||
|
MIDIMessageHandler (m_SerialMessage, m_nSerialState); |
||||||
|
|
||||||
|
m_nSerialState = 0; |
||||||
|
} |
||||||
|
break; |
||||||
|
|
||||||
|
default: |
||||||
|
assert (0); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,52 @@ |
|||||||
|
//
|
||||||
|
// serialmididevice.h
|
||||||
|
//
|
||||||
|
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||||
|
// Copyright (C) 2022 The MiniDexed Team
|
||||||
|
//
|
||||||
|
// Original author of this class:
|
||||||
|
// R. Stange <rsta2@o2online.de>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
#ifndef _serialmididevice_h |
||||||
|
#define _serialmididevice_h |
||||||
|
|
||||||
|
#include "mididevice.h" |
||||||
|
#include "config.h" |
||||||
|
#include <circle/interrupt.h> |
||||||
|
#include <circle/serial.h> |
||||||
|
#include <circle/types.h> |
||||||
|
|
||||||
|
class CMiniDexed; |
||||||
|
|
||||||
|
class CSerialMIDIDevice : public CMIDIDevice |
||||||
|
{ |
||||||
|
public: |
||||||
|
CSerialMIDIDevice (CMiniDexed *pSynthesizer, CInterruptSystem *pInterrupt, CConfig *pConfig); |
||||||
|
~CSerialMIDIDevice (void); |
||||||
|
|
||||||
|
boolean Initialize (void); |
||||||
|
|
||||||
|
void Process (void); |
||||||
|
|
||||||
|
private: |
||||||
|
CConfig *m_pConfig; |
||||||
|
|
||||||
|
CSerialDevice m_Serial; |
||||||
|
unsigned m_nSerialState; |
||||||
|
u8 m_SerialMessage[3]; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif |
Loading…
Reference in new issue