2007-07-14 21:56:11 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2007 Paul Davis
|
|
|
|
Author: Dave Robillard
|
|
|
|
|
|
|
|
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; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
#ifndef __libmidipp_midi_event_h__
|
|
|
|
#define __libmidipp_midi_event_h__
|
2007-07-14 21:56:11 -04:00
|
|
|
|
2007-08-28 13:48:37 -04:00
|
|
|
#include <stdint.h>
|
2008-03-17 23:42:32 -04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <midi++/types.h>
|
|
|
|
#include <midi++/events.h>
|
2007-08-02 17:08:18 -04:00
|
|
|
|
|
|
|
/** If this is not defined, all methods of MidiEvent are RT safe
|
|
|
|
* but MidiEvent will never deep copy and (depending on the scenario)
|
2008-03-17 23:42:32 -04:00
|
|
|
* may not be usable in STL containers, signals, etc.
|
|
|
|
*/
|
2007-08-02 17:08:18 -04:00
|
|
|
#define MIDI_EVENT_ALLOW_ALLOC 1
|
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
namespace MIDI {
|
2007-07-14 21:56:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
/** Identical to jack_midi_event_t, but with double timestamp
|
|
|
|
*
|
|
|
|
* time is either a frame time (from/to Jack) or a beat time (internal
|
|
|
|
* tempo time, used in MidiModel) depending on context.
|
|
|
|
*/
|
2008-03-17 23:42:32 -04:00
|
|
|
struct Event {
|
2007-08-02 17:08:18 -04:00
|
|
|
#ifdef MIDI_EVENT_ALLOW_ALLOC
|
2008-03-17 23:42:32 -04:00
|
|
|
Event(double t=0, uint32_t s=0, uint8_t* b=NULL, bool owns_buffer=false)
|
2007-08-02 17:08:18 -04:00
|
|
|
: _time(t)
|
2007-08-01 16:50:09 -04:00
|
|
|
, _size(s)
|
|
|
|
, _buffer(b)
|
2007-08-02 17:08:18 -04:00
|
|
|
, _owns_buffer(owns_buffer)
|
2007-08-01 16:50:09 -04:00
|
|
|
{
|
|
|
|
if (owns_buffer) {
|
2008-03-17 23:42:32 -04:00
|
|
|
_buffer = (uint8_t*)malloc(_size);
|
2007-08-01 16:50:09 -04:00
|
|
|
if (b)
|
|
|
|
memcpy(_buffer, b, _size);
|
|
|
|
else
|
|
|
|
memset(_buffer, 0, _size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-02 17:08:18 -04:00
|
|
|
/** Copy \a copy.
|
|
|
|
*
|
|
|
|
* If \a owns_buffer is true, the buffer will be copied and this method
|
|
|
|
* is NOT REALTIME SAFE. Otherwise both events share a buffer and
|
|
|
|
* memory management semantics are the caller's problem.
|
|
|
|
*/
|
2008-03-17 23:42:32 -04:00
|
|
|
Event(const Event& copy, bool owns_buffer)
|
2007-08-02 17:08:18 -04:00
|
|
|
: _time(copy._time)
|
2007-08-01 16:50:09 -04:00
|
|
|
, _size(copy._size)
|
|
|
|
, _buffer(copy._buffer)
|
2007-08-02 17:08:18 -04:00
|
|
|
, _owns_buffer(owns_buffer)
|
2007-08-01 16:50:09 -04:00
|
|
|
{
|
|
|
|
if (owns_buffer) {
|
2008-03-17 23:42:32 -04:00
|
|
|
_buffer = (uint8_t*)malloc(_size);
|
2007-08-01 16:50:09 -04:00
|
|
|
if (copy._buffer)
|
|
|
|
memcpy(_buffer, copy._buffer, _size);
|
|
|
|
else
|
|
|
|
memset(_buffer, 0, _size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
~Event() {
|
2007-08-01 16:50:09 -04:00
|
|
|
if (_owns_buffer)
|
|
|
|
free(_buffer);
|
|
|
|
}
|
2007-07-14 21:56:11 -04:00
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
inline const Event& operator=(const Event& copy) {
|
2007-08-01 16:50:09 -04:00
|
|
|
_time = copy._time;
|
2007-10-14 01:45:31 -04:00
|
|
|
if (_owns_buffer) {
|
|
|
|
if (copy._buffer) {
|
|
|
|
if (!_buffer || _size < copy._size)
|
2008-03-17 23:42:32 -04:00
|
|
|
_buffer = (uint8_t*)::realloc(_buffer, copy._size);
|
2007-10-14 01:45:31 -04:00
|
|
|
memcpy(_buffer, copy._buffer, copy._size);
|
|
|
|
} else {
|
|
|
|
free(_buffer);
|
|
|
|
_buffer = NULL;
|
|
|
|
}
|
2007-08-01 16:50:09 -04:00
|
|
|
} else {
|
2007-10-14 01:45:31 -04:00
|
|
|
_buffer = copy._buffer;
|
2007-08-01 16:50:09 -04:00
|
|
|
}
|
2007-10-14 01:45:31 -04:00
|
|
|
|
2007-08-01 16:50:09 -04:00
|
|
|
_size = copy._size;
|
|
|
|
return *this;
|
|
|
|
}
|
2007-08-31 01:02:45 -04:00
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
inline void set (uint8_t* msg, size_t msglen, timestamp_t t) {
|
|
|
|
if (_owns_buffer) {
|
|
|
|
if (_size < msglen) {
|
|
|
|
free (_buffer);
|
|
|
|
_buffer = (uint8_t*) malloc (msglen);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_buffer = (uint8_t*) malloc (msglen);
|
|
|
|
_owns_buffer = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (_buffer, msg, msglen);
|
|
|
|
_time = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const Event& other) const {
|
2007-08-31 01:02:45 -04:00
|
|
|
if (_time != other._time)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (_size != other._size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (_buffer == other._buffer)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (size_t i=0; i < _size; ++i)
|
|
|
|
if (_buffer[i] != other._buffer[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
inline bool operator!=(const Event& other) const { return ! operator==(other); }
|
2007-07-14 21:56:11 -04:00
|
|
|
|
2007-08-02 17:08:18 -04:00
|
|
|
inline bool owns_buffer() const { return _owns_buffer; }
|
2007-08-28 13:48:37 -04:00
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
inline void set_buffer(uint8_t* buf, bool own) {
|
2007-08-28 13:48:37 -04:00
|
|
|
if (_owns_buffer) {
|
|
|
|
free(_buffer);
|
|
|
|
_buffer = NULL;
|
|
|
|
}
|
|
|
|
_buffer = buf;
|
2007-08-31 01:02:45 -04:00
|
|
|
_owns_buffer = own;
|
2007-08-28 13:48:37 -04:00
|
|
|
}
|
2007-08-02 17:08:18 -04:00
|
|
|
|
2008-02-18 14:45:52 -05:00
|
|
|
inline void realloc(size_t size) {
|
|
|
|
assert(_owns_buffer);
|
2008-03-17 23:42:32 -04:00
|
|
|
_buffer = (uint8_t*) ::realloc(_buffer, size);
|
2008-02-18 14:45:52 -05:00
|
|
|
}
|
|
|
|
|
2007-08-02 17:08:18 -04:00
|
|
|
#else
|
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
inline void set_buffer(uint8_t* buf) { _buffer = buf; }
|
2007-08-02 17:08:18 -04:00
|
|
|
|
|
|
|
#endif // MIDI_EVENT_ALLOW_ALLOC
|
|
|
|
|
2007-08-01 16:50:09 -04:00
|
|
|
inline double time() const { return _time; }
|
|
|
|
inline double& time() { return _time; }
|
2007-08-02 17:08:18 -04:00
|
|
|
inline uint32_t size() const { return _size; }
|
|
|
|
inline uint32_t& size() { return _size; }
|
2007-08-01 16:50:09 -04:00
|
|
|
inline uint8_t type() const { return (_buffer[0] & 0xF0); }
|
2007-08-02 17:08:18 -04:00
|
|
|
inline uint8_t channel() const { return (_buffer[0] & 0x0F); }
|
2008-04-09 11:33:01 -04:00
|
|
|
inline void set_channel(uint8_t channel) { _buffer[0] = (0xF0 & _buffer[0]) | channel; }
|
2008-03-17 23:42:32 -04:00
|
|
|
inline bool is_note_on() const { return (type() == MIDI_CMD_NOTE_ON); }
|
|
|
|
inline bool is_note_off() const { return (type() == MIDI_CMD_NOTE_OFF); }
|
|
|
|
inline bool is_cc() const { return (type() == MIDI_CMD_CONTROL); }
|
2007-08-02 17:08:18 -04:00
|
|
|
inline bool is_note() const { return (is_note_on() || is_note_off()); }
|
2007-08-01 16:50:09 -04:00
|
|
|
inline uint8_t note() const { return (_buffer[1]); }
|
|
|
|
inline uint8_t velocity() const { return (_buffer[2]); }
|
2007-08-15 21:25:58 -04:00
|
|
|
inline uint8_t cc_number() const { return (_buffer[1]); }
|
|
|
|
inline uint8_t cc_value() const { return (_buffer[2]); }
|
2008-03-17 23:42:32 -04:00
|
|
|
inline const uint8_t* buffer() const { return _buffer; }
|
|
|
|
inline uint8_t*& buffer() { return _buffer; }
|
2007-08-01 16:50:09 -04:00
|
|
|
|
|
|
|
private:
|
2007-08-02 17:08:18 -04:00
|
|
|
double _time; /**< Sample index (or beat time) at which event is valid */
|
2008-03-17 23:42:32 -04:00
|
|
|
uint32_t _size; /**< Number of uint8_ts of data in \a buffer */
|
|
|
|
uint8_t* _buffer; /**< Raw MIDI data */
|
2007-08-02 17:08:18 -04:00
|
|
|
|
|
|
|
#ifdef MIDI_EVENT_ALLOW_ALLOC
|
|
|
|
bool _owns_buffer; /**< Whether buffer is locally allocated */
|
|
|
|
#endif
|
2007-07-14 21:56:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
}
|
2007-07-14 21:56:11 -04:00
|
|
|
|
2008-03-17 23:42:32 -04:00
|
|
|
#endif /* __libmidipp_midi_event_h__ */
|