1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David 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/visibility.h"
28 #include "evoral/types.hpp"
30 /** If this is not defined, all methods of MidiEvent are RT safe
31 * but MidiEvent will never deep copy and (depending on the scenario)
32 * may not be usable in STL containers, signals, etc.
34 #define EVORAL_EVENT_ALLOC 1
38 LIBEVORAL_API event_id_t event_id_counter();
39 LIBEVORAL_API event_id_t next_event_id();
40 LIBEVORAL_API void init_event_id_counter(event_id_t n);
42 /** An event (much like a type generic jack_midi_event_t)
44 * Template parameter Time is the type of the time stamp used for this event.
46 template<typename Time>
47 class LIBEVORAL_API Event {
49 #ifdef EVORAL_EVENT_ALLOC
50 Event (EventType type=0, Time time=Time(), uint32_t size=0, uint8_t* buf=NULL, bool alloc=false);
52 Event (EventType type, Time time, uint32_t size, const uint8_t* buf);
56 * If \a alloc is true, the buffer will be copied and this method
57 * is NOT REALTIME SAFE. Otherwise both events share a buffer and
58 * memory management semantics are the caller's problem.
60 Event(const Event& copy, bool alloc);
64 void assign (const Event& other);
66 void set(const uint8_t* buf, uint32_t size, Time t);
68 inline bool operator==(const Event& other) const {
69 if (_type != other._type)
72 if (_nominal_time != other._nominal_time)
75 if (_original_time != other._original_time)
78 if (_size != other._size)
81 if (_buf == other._buf)
84 for (uint32_t i=0; i < _size; ++i)
85 if (_buf[i] != other._buf[i])
91 inline bool operator!=(const Event& other) const { return ! operator==(other); }
93 inline bool owns_buffer() const { return _owns_buf; }
95 /** set event data (e.g. midi data)
96 * @param size number of bytes
97 * @param buf raw 8bit data
98 * @param own set to true if the buffer owns the data (copy, allocate/free) or false to reference previously allocated data.
100 inline void set_buffer(uint32_t size, uint8_t* buf, bool own) {
110 inline void realloc(uint32_t size) {
113 _buf = (uint8_t*) ::realloc(_buf, size);
115 _buf = (uint8_t*) ::malloc(size);
122 inline void clear() {
124 _original_time = Time();
125 _nominal_time = Time();
132 inline void set_buffer(uint8_t* buf) { _buf = buf; }
134 #endif // EVORAL_EVENT_ALLOC
136 inline EventType event_type() const { return _type; }
137 inline Time time() const { return _nominal_time; }
138 inline Time original_time() const { return _original_time; }
139 inline uint32_t size() const { return _size; }
140 inline const uint8_t* buffer() const { return _buf; }
141 inline uint8_t* buffer() { return _buf; }
143 inline void set_event_type(EventType t) { _type = t; }
146 void set_original_time(Time);
148 inline event_id_t id() const { return _id; }
149 inline void set_id(event_id_t n) { _id = n; }
152 EventType _type; /**< Type of event (application relative, NOT MIDI 'type') */
153 Time _original_time; /**< Sample index (or beat time) at which event is valid */
154 Time _nominal_time; /**< Quantized version of _time, used in preference */
155 uint32_t _size; /**< Number of uint8_ts of data in \a buffer */
156 uint8_t* _buf; /**< Raw MIDI data */
157 event_id_t _id; /** UUID for each event, should probably be 64bit or at least unsigned */
158 #ifdef EVORAL_EVENT_ALLOC
159 bool _owns_buf; /**< Whether buffer is locally allocated */
163 template<typename Time>
164 /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Event<Time>& ev) {
165 o << "Event #" << ev.id() << " type = " << ev.event_type() << " @ " << ev.time();
167 for (uint32_t n = 0; n < ev.size(); ++n) {
168 o << ' ' << (int) ev.buffer()[n];
174 } // namespace Evoral
176 #endif // EVORAL_EVENT_HPP