1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
5 * Evoral is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef EVORAL_EVENT_HPP
20 #define EVORAL_EVENT_HPP
27 #include "evoral/types.hpp"
29 /** If this is not defined, all methods of MidiEvent are RT safe
30 * but MidiEvent will never deep copy and (depending on the scenario)
31 * may not be usable in STL containers, signals, etc.
33 #define EVORAL_EVENT_ALLOC 1
37 event_id_t event_id_counter();
38 event_id_t next_event_id();
39 void init_event_id_counter (event_id_t n);
41 /** An event (much like a type generic jack_midi_event_t)
43 * Template parameter Time is the type of the time stamp used for this event.
45 template<typename Time>
47 #ifdef EVORAL_EVENT_ALLOC
48 Event (EventType type=0, Time time=0, uint32_t size=0, uint8_t* buf=NULL, bool alloc=false);
52 * If \a alloc is true, the buffer will be copied and this method
53 * is NOT REALTIME SAFE. Otherwise both events share a buffer and
54 * memory management semantics are the caller's problem.
56 Event(const Event& copy, bool alloc);
60 inline const Event& operator=(const Event& copy) {
61 _id = copy.id(); // XXX is this right? do we want ID copy semantics?
63 _original_time = copy._original_time;
64 _nominal_time = copy._nominal_time;
67 if (copy._size > _size) {
68 _buf = (uint8_t*)::realloc(_buf, copy._size);
70 memcpy(_buf, copy._buf, copy._size);
83 inline void set(uint8_t* buf, uint32_t size, Time t) {
86 _buf = (uint8_t*) ::realloc(_buf, size);
88 memcpy (_buf, buf, size);
98 inline bool operator==(const Event& other) const {
99 if (_type != other._type)
102 if (_nominal_time != other._nominal_time)
105 if (_original_time != other._original_time)
108 if (_size != other._size)
111 if (_buf == other._buf)
114 for (uint32_t i=0; i < _size; ++i)
115 if (_buf[i] != other._buf[i])
121 inline bool operator!=(const Event& other) const { return ! operator==(other); }
123 inline bool owns_buffer() const { return _owns_buf; }
125 inline void set_buffer(uint32_t size, uint8_t* buf, bool own) {
135 inline void realloc(uint32_t size) {
138 _buf = (uint8_t*) ::realloc(_buf, size);
140 _buf = (uint8_t*) ::malloc(size);
147 inline void clear() {
157 inline void set_buffer(uint8_t* buf) { _buf = buf; }
159 #endif // EVORAL_EVENT_ALLOC
161 inline EventType event_type() const { return _type; }
162 inline void set_event_type(EventType t) { _type = t; }
163 inline Time time() const { return _nominal_time; }
164 inline Time& time() { return _nominal_time; }
165 inline Time original_time() const { return _original_time; }
166 inline Time& original_time() { return _original_time; }
167 inline uint32_t size() const { return _size; }
168 inline uint32_t& size() { return _size; }
170 inline const uint8_t* buffer() const { return _buf; }
171 inline uint8_t*& buffer() { return _buf; }
173 inline event_id_t id() const { return _id; }
174 inline void set_id (event_id_t n) { _id = n; }
177 EventType _type; /**< Type of event (application relative, NOT MIDI 'type') */
178 Time _original_time; /**< Sample index (or beat time) at which event is valid */
179 Time _nominal_time; /**< Quantized version of _time, used in preference */
180 uint32_t _size; /**< Number of uint8_ts of data in \a buffer */
181 uint8_t* _buf; /**< Raw MIDI data */
183 #ifdef EVORAL_EVENT_ALLOC
184 bool _owns_buf; /**< Whether buffer is locally allocated */
186 event_id_t _id; /** UUID for each event, should probably be 64bit or at least unsigned */
189 } // namespace Evoral
192 template<typename Time>
193 std::ostream& operator<<(std::ostream& o, const Evoral::Event<Time>& ev) {
194 o << "Event #" << ev.id() << " type = " << ev.event_type() << " @ " << ev.time();
196 for (uint32_t n = 0; n < ev.size(); ++n) {
197 o << ' ' << (int) ev.buffer()[n];
204 #endif // EVORAL_EVENT_HPP