merge (with conflict fixes) with master (even against rgareus' recommendation)
[ardour.git] / libs / evoral / evoral / Event.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
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
8  * version.
9  *
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.
13  *
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
17  */
18
19 #ifndef EVORAL_EVENT_HPP
20 #define EVORAL_EVENT_HPP
21
22 #include <assert.h>
23 #include <cstdlib>
24 #include <cstring>
25 #include <sstream>
26 #include <stdint.h>
27
28 #include "evoral/visibility.h"
29 #include "evoral/types.hpp"
30
31 /** If this is not defined, all methods of MidiEvent are RT safe
32  * but MidiEvent will never deep copy and (depending on the scenario)
33  * may not be usable in STL containers, signals, etc.
34  */
35 #define EVORAL_EVENT_ALLOC 1
36
37 namespace Evoral {
38
39 LIBEVORAL_API event_id_t event_id_counter();
40 LIBEVORAL_API event_id_t next_event_id();
41 LIBEVORAL_API void init_event_id_counter(event_id_t n);
42
43 /** An event (much like a type generic jack_midi_event_t)
44  *
45  * Template parameter Time is the type of the time stamp used for this event.
46  */
47 template<typename Time>
48 class LIBEVORAL_API Event {
49 public:
50 #ifdef EVORAL_EVENT_ALLOC
51         Event (EventType type=0, Time time=0, uint32_t size=0, uint8_t* buf=NULL, bool alloc=false);
52
53         /** Copy \a copy.
54          *
55          * If \a alloc is true, the buffer will be copied and this method
56          * is NOT REALTIME SAFE.  Otherwise both events share a buffer and
57          * memory management semantics are the caller's problem.
58          */
59         Event(const Event& copy, bool alloc);
60
61         ~Event();
62
63         const Event& operator=(const Event& copy);
64
65         void set(const uint8_t* buf, uint32_t size, Time t);
66
67         inline bool operator==(const Event& other) const {
68                 if (_type != other._type)
69                         return false;
70
71                 if (_nominal_time != other._nominal_time)
72                         return false;
73
74                 if (_original_time != other._original_time)
75                         return false;
76
77                 if (_size != other._size)
78                         return false;
79
80                 if (_buf == other._buf)
81                         return true;
82
83                 for (uint32_t i=0; i < _size; ++i)
84                         if (_buf[i] != other._buf[i])
85                                 return false;
86
87                 return true;
88         }
89
90         inline bool operator!=(const Event& other) const { return ! operator==(other); }
91
92         inline bool owns_buffer() const { return _owns_buf; }
93
94         inline void set_buffer(uint32_t size, uint8_t* buf, bool own) {
95                 if (_owns_buf) {
96                         free(_buf);
97                         _buf = NULL;
98                 }
99                 _size     = size;
100                 _buf      = buf;
101                 _owns_buf = own;
102         }
103
104         inline void realloc(uint32_t size) {
105                 if (_owns_buf) {
106                         if (size > _size)
107                                 _buf = (uint8_t*) ::realloc(_buf, size);
108                 } else {
109                         _buf = (uint8_t*) ::malloc(size);
110                         _owns_buf = true;
111                 }
112
113                 _size = size;
114         }
115
116         inline void clear() {
117                 _type = 0;
118                 _original_time = 0;
119                 _nominal_time = 0;
120                 _size = 0;
121                 _buf  = NULL;
122         }
123
124 #else
125
126         inline void set_buffer(uint8_t* buf) { _buf = buf; }
127
128 #endif // EVORAL_EVENT_ALLOC
129
130         inline EventType      event_type()    const { return _type; }
131         inline Time           time()          const { return _nominal_time; }
132         inline Time           original_time() const { return _original_time; }
133         inline uint32_t       size()          const { return _size; }
134         inline const uint8_t* buffer()        const { return _buf; }
135         inline uint8_t*       buffer()              { return _buf; }
136
137         inline void set_event_type(EventType t) { _type = t; }
138
139         void set_time(Time);
140         void set_original_time(Time);
141
142         inline event_id_t id() const           { return _id; }
143         inline void       set_id(event_id_t n) { _id = n; }
144
145 protected:
146         EventType  _type; /**< Type of event (application relative, NOT MIDI 'type') */
147         Time       _original_time; /**< Sample index (or beat time) at which event is valid */
148         Time       _nominal_time; /**< Quantized version of _time, used in preference */
149         uint32_t   _size; /**< Number of uint8_ts of data in \a buffer */
150         uint8_t*   _buf;  /**< Raw MIDI data */
151         event_id_t _id; /** UUID for each event, should probably be 64bit or at least unsigned */
152 #ifdef EVORAL_EVENT_ALLOC
153         bool       _owns_buf; /**< Whether buffer is locally allocated */
154 #endif
155 };
156
157 } // namespace Evoral
158
159
160 template<typename Time>
161 /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Event<Time>& ev) {
162         o << "Event #" << ev.id() << " type = " << ev.event_type() << " @ " << ev.time();
163         o << std::hex;
164         for (uint32_t n = 0; n < ev.size(); ++n) {
165                 o << ' ' << (int) ev.buffer()[n];
166         }
167         o << std::dec;
168         return o;
169 }
170
171
172 #endif // EVORAL_EVENT_HPP
173