1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
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.
*/
#ifndef __ardour_midi_event_h__
#define __ardour_midi_event_h__
#include <ardour/midi_events.h>
/** If this is not defined, all methods of MidiEvent are RT safe
* but MidiEvent will never deep copy and (depending on the scenario)
* may not be usable in STL containers, signals, etc. */
#define MIDI_EVENT_ALLOW_ALLOC 1
namespace ARDOUR {
/** 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.
*/
struct MidiEvent {
#ifdef MIDI_EVENT_ALLOW_ALLOC
MidiEvent(double t=0, size_t s=0, Byte* b=NULL, bool owns_buffer=false)
: _time(t)
, _size(s)
, _buffer(b)
, _owns_buffer(owns_buffer)
{
if (owns_buffer) {
_buffer = (Byte*)malloc(_size);
if (b)
memcpy(_buffer, b, _size);
else
memset(_buffer, 0, _size);
}
}
/** 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.
*/
MidiEvent(const MidiEvent& copy, bool owns_buffer)
: _time(copy._time)
, _size(copy._size)
, _buffer(copy._buffer)
, _owns_buffer(owns_buffer)
{
if (owns_buffer) {
_buffer = (Byte*)malloc(_size);
if (copy._buffer)
memcpy(_buffer, copy._buffer, _size);
else
memset(_buffer, 0, _size);
}
}
~MidiEvent() {
if (_owns_buffer)
free(_buffer);
}
inline const MidiEvent& operator=(const MidiEvent& copy) {
_time = copy._time;
if (!_owns_buffer) {
_buffer = copy._buffer;
} else if (copy._buffer) {
if (!_buffer || _size < copy._size)
_buffer = (Byte*)realloc(_buffer, copy._size);
memcpy(_buffer, copy._buffer, copy._size);
} else {
free(_buffer);
_buffer = NULL;
}
_size = copy._size;
return *this;
}
inline bool owns_buffer() const { return _owns_buffer; }
inline void set_buffer(Byte* buf) { assert(!_owns_buffer); _buffer = buf; }
#else
inline void set_buffer(Byte* buf) { _buffer = buf; }
#endif // MIDI_EVENT_ALLOW_ALLOC
inline double time() const { return _time; }
inline double& time() { return _time; }
inline uint32_t size() const { return _size; }
inline uint32_t& size() { return _size; }
inline uint8_t type() const { return (_buffer[0] & 0xF0); }
inline uint8_t channel() const { return (_buffer[0] & 0x0F); }
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_note() const { return (is_note_on() || is_note_off()); }
inline uint8_t note() const { return (_buffer[1]); }
inline uint8_t velocity() const { return (_buffer[2]); }
inline const Byte* buffer() const { return _buffer; }
inline Byte* buffer() { return _buffer; }
private:
double _time; /**< Sample index (or beat time) at which event is valid */
uint32_t _size; /**< Number of bytes of data in \a buffer */
Byte* _buffer; /**< Raw MIDI data */
#ifdef MIDI_EVENT_ALLOW_ALLOC
bool _owns_buffer; /**< Whether buffer is locally allocated */
#endif
};
} // namespace ARDOUR
#endif /* __ardour_midi_event_h__ */
|