Refactor writing MIDI events to buffers

This moves out the code from the carla plugin into the core, because
this code will be re-used for Lv2 MIDI handling soon.
This commit is contained in:
Johannes Lorenz
2020-10-17 20:00:13 +02:00
committed by Johannes Lorenz
parent b64fe8e7c0
commit 9db671c7ae
4 changed files with 159 additions and 63 deletions

View File

@@ -0,0 +1,47 @@
/*
* MidiEventToByteSeq.h - writeToByteSeq declaration
*
* Copyright (c) 2020-2020 Johannes Lorenz <jlsf2013$users.sourceforge.net, $=@>
*
* This file is part of LMMS - https://lmms.io
*
* 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 2 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 (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#ifndef MIDIEVENTTOBYTESEQ_H
#define MIDIEVENTTOBYTESEQ_H
#include <cstddef>
#include <cstdint>
#include "lmms_export.h"
/**
Write MIDI event into byte sequence.
Conforming to http://lv2plug.in/ns/ext/midi#MidiEvent
@param data Pointer to the target buffer for the byte sequence. Must
point to existing memory with at least 3 bytes size.
@param bufsize Available size of the target buffer.
@return Used size of the target buffer, or 0 if the MidiEvent could not
be converted.
*/
std::size_t LMMS_EXPORT writeToByteSeq( const class MidiEvent& ev,
uint8_t* data, std::size_t bufsize );
#endif // MIDIEVENTTOBYTESEQ_H

View File

@@ -29,6 +29,7 @@
#include "gui_templates.h"
#include "InstrumentPlayHandle.h"
#include "InstrumentTrack.h"
#include "MidiEventToByteSeq.h"
#include "Mixer.h"
#include <QApplication>
@@ -372,69 +373,9 @@ bool CarlaInstrument::handleMidiEvent(const MidiEvent& event, const MidiTime&, f
nEvent.port = 0;
nEvent.time = offset;
nEvent.data[0] = event.type() | (event.channel() & 0x0F);
switch (event.type())
{
case MidiNoteOn:
if (event.velocity() > 0)
{
if (event.key() < 0 || event.key() > MidiMaxKey)
break;
nEvent.data[1] = event.key();
nEvent.data[2] = event.velocity();
nEvent.size = 3;
break;
}
else
{
nEvent.data[0] = MidiNoteOff | (event.channel() & 0x0F);
// nobreak
}
case MidiNoteOff:
if (event.key() < 0 || event.key() > MidiMaxKey)
break;
nEvent.data[1] = event.key();
nEvent.data[2] = event.velocity();
nEvent.size = 3;
break;
case MidiKeyPressure:
nEvent.data[1] = event.key();
nEvent.data[2] = event.velocity();
nEvent.size = 3;
break;
case MidiControlChange:
nEvent.data[1] = event.controllerNumber();
nEvent.data[2] = event.controllerValue();
nEvent.size = 3;
break;
case MidiProgramChange:
nEvent.data[1] = event.program();
nEvent.size = 2;
break;
case MidiChannelPressure:
nEvent.data[1] = event.channelPressure();
nEvent.size = 2;
break;
case MidiPitchBend:
nEvent.data[1] = event.pitchBend() & 0x7f;
nEvent.data[2] = event.pitchBend() >> 7;
nEvent.size = 3;
break;
default:
// unhandled
--fMidiEventCount;
break;
}
std::size_t written = writeToByteSeq(event, nEvent.data, sizeof(NativeMidiEvent::data));
if(written) { nEvent.size = written; }
else { --fMidiEventCount; }
return true;
}

View File

@@ -104,6 +104,7 @@ set(LMMS_SRCS
core/midi/MidiAlsaSeq.cpp
core/midi/MidiClient.cpp
core/midi/MidiController.cpp
core/midi/MidiEventToByteSeq.cpp
core/midi/MidiJack.cpp
core/midi/MidiOss.cpp
core/midi/MidiSndio.cpp

View File

@@ -0,0 +1,107 @@
/*
* MidiEventToByteSeq.cpp - writeToByteSeq implementation
*
* Copyright (c) 2020-2020 Johannes Lorenz <jlsf2013$users.sourceforge.net, $=@>
*
* This file is part of LMMS - https://lmms.io
*
* 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 2 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 (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "MidiEventToByteSeq.h"
#include <QtGlobal>
#include "MidiEvent.h"
std::size_t writeToByteSeq(
const MidiEvent& ev, uint8_t *data, std::size_t bufsize)
{
Q_ASSERT(bufsize >= 3);
std::size_t size = 0;
data[0] = ev.type() | (ev.channel() & 0x0F);
switch (ev.type())
{
case MidiNoteOn:
if (ev.velocity() > 0)
{
if (ev.key() < 0 || ev.key() > MidiMaxKey)
break;
data[1] = ev.key();
data[2] = ev.velocity();
size = 3;
break;
}
else
{
// Lv2 MIDI specs:
// "Note On messages with velocity 0 are not allowed.
// These messages are equivalent to Note Off in standard
// MIDI streams, but here only proper Note Off messages
// are allowed."
data[0] = MidiNoteOff | (ev.channel() & 0x0F);
// nobreak
}
case MidiNoteOff:
if (ev.key() < 0 || ev.key() > MidiMaxKey)
break;
data[1] = ev.key();
data[2] = ev.velocity(); // release time
size = 3;
break;
case MidiKeyPressure:
data[1] = ev.key();
data[2] = ev.velocity();
size = 3;
break;
case MidiControlChange:
data[1] = ev.controllerNumber();
data[2] = ev.controllerValue();
size = 3;
break;
case MidiProgramChange:
data[1] = ev.program();
size = 2;
break;
case MidiChannelPressure:
data[1] = ev.channelPressure();
size = 2;
break;
case MidiPitchBend:
data[1] = ev.pitchBend() & 0x7f;
data[2] = ev.pitchBend() >> 7;
size = 3;
break;
default:
// unhandled
break;
}
return size;
}