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
20 #include "evoral/Event.hpp"
24 static event_id_t _event_id_counter = 0;
29 return g_atomic_int_get (&_event_id_counter);
33 init_event_id_counter(event_id_t n)
35 g_atomic_int_set (&_event_id_counter, n);
41 /* libsmf supports reading variable-length data up to 4 bytes only.
42 * so we wrap around at 2^(4 * 7) - 1
44 * interestingly enough libsmf happily writes data longer than that, but then
45 * fails to load it in ./libs/evoral/src/libsmf/smf_load.c:237
46 * g_critical("SMF error: Variable Length Quantities longer than four bytes are not supported yet.");
48 * event-IDs only have to be unique per .mid file.
49 * Previously (Ardour 4.2ish) Ardour re-generated those IDs when loading the
50 * file but that lead to .mid files being modified on every load/save.
52 * For now just assume that by the time 2^28 is reached, files with low ids have vanished.
53 * There is only one user who managed to get to 268 million events so far.
54 * quite a feat: id-counter="6483089" event-counter="276390506"
55 * Eventually a proper solution will have to be implemented.
57 return g_atomic_int_add (&_event_id_counter, 1) & 268435455;
60 #ifdef EVORAL_EVENT_ALLOC
62 template<typename Timestamp>
63 Event<Timestamp>::Event(EventType type, Timestamp time, uint32_t size, uint8_t* buf, bool alloc)
65 , _original_time(time)
73 _buf = (uint8_t*)malloc(_size);
75 memcpy(_buf, buf, _size);
77 memset(_buf, 0, _size);
82 template<typename Timestamp>
83 Event<Timestamp>::Event(EventType type,
88 , _original_time(time)
91 , _buf((uint8_t*)malloc(size))
95 memcpy(_buf, buf, _size);
98 template<typename Timestamp>
99 Event<Timestamp>::Event(const Event& copy, bool owns_buf)
101 , _original_time(copy._original_time)
102 , _nominal_time(copy._nominal_time)
105 , _id (next_event_id ())
106 , _owns_buf(owns_buf)
109 _buf = (uint8_t*)malloc(_size);
111 memcpy(_buf, copy._buf, _size);
113 memset(_buf, 0, _size);
118 template<typename Timestamp>
119 Event<Timestamp>::~Event() {
125 template<typename Timestamp>
127 Event<Timestamp>::assign(const Event& other)
131 _original_time = other._original_time;
132 _nominal_time = other._nominal_time;
133 _owns_buf = other._owns_buf;
136 if (other._size > _size) {
137 _buf = (uint8_t*)::realloc(_buf, other._size);
139 memcpy(_buf, other._buf, other._size);
151 template<typename Timestamp>
153 Event<Timestamp>::set (const uint8_t* buf, uint32_t size, Timestamp t)
157 _buf = (uint8_t*) ::realloc(_buf, size);
159 memcpy (_buf, buf, size);
161 /* XXX this is really dangerous given the
162 const-ness of buf. The API should really
165 _buf = const_cast<uint8_t*> (buf);
173 template<typename Timestamp>
175 Event<Timestamp>::set_time (Timestamp t)
180 template<typename Timestamp>
182 Event<Timestamp>::set_original_time (Timestamp t)
187 #endif // EVORAL_EVENT_ALLOC
189 template class Event<Evoral::Beats>;
190 template class Event<double>;
191 template class Event<int64_t>;
193 } // namespace Evoral