2008-09-18 20:47:49 -04:00
|
|
|
/* This file is part of Evoral.
|
2011-04-06 11:00:16 -04:00
|
|
|
* Copyright (C) 2008 David Robillard <http://drobilla.net>
|
2008-09-18 20:47:49 -04:00
|
|
|
* Copyright (C) 2000-2008 Paul Davis
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2008-09-18 20:47:49 -04:00
|
|
|
* Evoral 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.
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2008-09-18 20:47:49 -04:00
|
|
|
* Evoral 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 details.
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2008-09-18 20:47:49 -04:00
|
|
|
* 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.,
|
|
|
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2010-07-20 12:27:34 -04:00
|
|
|
#include <glib.h>
|
2015-10-05 13:58:35 -04:00
|
|
|
|
2008-12-21 15:36:15 -05:00
|
|
|
#include "evoral/Event.hpp"
|
2015-10-05 13:58:35 -04:00
|
|
|
#include "evoral/Beats.hpp"
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
namespace Evoral {
|
|
|
|
|
2010-07-20 12:27:34 -04:00
|
|
|
static event_id_t _event_id_counter = 0;
|
|
|
|
|
|
|
|
event_id_t
|
|
|
|
event_id_counter()
|
|
|
|
{
|
2015-10-04 15:11:15 -04:00
|
|
|
return g_atomic_int_get (&_event_id_counter);
|
2010-07-20 12:27:34 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 15:11:15 -04:00
|
|
|
void
|
|
|
|
init_event_id_counter(event_id_t n)
|
|
|
|
{
|
|
|
|
g_atomic_int_set (&_event_id_counter, n);
|
2010-07-20 12:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
event_id_t
|
|
|
|
next_event_id ()
|
|
|
|
{
|
2015-12-26 21:40:48 -05:00
|
|
|
/* TODO: handle 31bit overflow , event_id_t is an int32_t,
|
|
|
|
* and libsmf only supports loading uint32_t vlq's, see smf_extract_vlq()
|
2015-12-26 20:09:54 -05:00
|
|
|
*
|
|
|
|
* event-IDs only have to be unique per .mid file.
|
|
|
|
* Previously (Ardour 4.2ish) Ardour re-generated those IDs when loading the
|
|
|
|
* file but that lead to .mid files being modified on every load/save.
|
|
|
|
*
|
2015-12-26 21:40:48 -05:00
|
|
|
* current user-record: is event-counter="276390506" (just abov 2^28)
|
2015-12-26 20:09:54 -05:00
|
|
|
*/
|
2015-12-26 21:40:48 -05:00
|
|
|
return g_atomic_int_add (&_event_id_counter, 1);
|
2010-07-20 12:27:34 -04:00
|
|
|
}
|
|
|
|
|
2008-09-22 12:28:02 -04:00
|
|
|
#ifdef EVORAL_EVENT_ALLOC
|
|
|
|
|
2009-02-03 03:46:44 -05:00
|
|
|
template<typename Timestamp>
|
2009-02-14 15:52:15 -05:00
|
|
|
Event<Timestamp>::Event(EventType type, Timestamp time, uint32_t size, uint8_t* buf, bool alloc)
|
2011-10-19 14:11:31 -04:00
|
|
|
: _type(type)
|
2016-11-07 07:07:42 -05:00
|
|
|
, _time(time)
|
2009-02-03 03:46:44 -05:00
|
|
|
, _size(size)
|
2009-02-14 15:52:15 -05:00
|
|
|
, _buf(buf)
|
2011-10-19 14:11:31 -04:00
|
|
|
, _id(-1)
|
2009-02-03 03:46:44 -05:00
|
|
|
, _owns_buf(alloc)
|
2008-09-18 20:47:49 -04:00
|
|
|
{
|
2009-02-03 03:46:44 -05:00
|
|
|
if (alloc) {
|
2009-02-01 21:36:05 -05:00
|
|
|
_buf = (uint8_t*)malloc(_size);
|
2009-02-14 15:52:15 -05:00
|
|
|
if (buf) {
|
|
|
|
memcpy(_buf, buf, _size);
|
2008-09-18 20:47:49 -04:00
|
|
|
} else {
|
2009-02-01 21:36:05 -05:00
|
|
|
memset(_buf, 0, _size);
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-01 13:33:25 -05:00
|
|
|
template<typename Timestamp>
|
|
|
|
Event<Timestamp>::Event(EventType type,
|
|
|
|
Timestamp time,
|
|
|
|
uint32_t size,
|
|
|
|
const uint8_t* buf)
|
|
|
|
: _type(type)
|
2016-11-07 07:07:42 -05:00
|
|
|
, _time(time)
|
2015-03-01 13:33:25 -05:00
|
|
|
, _size(size)
|
|
|
|
, _buf((uint8_t*)malloc(size))
|
|
|
|
, _id(-1)
|
|
|
|
, _owns_buf(true)
|
|
|
|
{
|
|
|
|
memcpy(_buf, buf, _size);
|
|
|
|
}
|
|
|
|
|
2009-02-03 03:46:44 -05:00
|
|
|
template<typename Timestamp>
|
|
|
|
Event<Timestamp>::Event(const Event& copy, bool owns_buf)
|
2008-09-22 12:28:02 -04:00
|
|
|
: _type(copy._type)
|
2016-11-07 07:07:42 -05:00
|
|
|
, _time(copy._time)
|
2008-09-18 20:47:49 -04:00
|
|
|
, _size(copy._size)
|
2009-02-01 21:36:05 -05:00
|
|
|
, _buf(copy._buf)
|
2015-05-23 11:53:34 -04:00
|
|
|
, _id (next_event_id ())
|
2009-02-01 21:36:05 -05:00
|
|
|
, _owns_buf(owns_buf)
|
2008-09-18 20:47:49 -04:00
|
|
|
{
|
2009-02-01 21:36:05 -05:00
|
|
|
if (owns_buf) {
|
|
|
|
_buf = (uint8_t*)malloc(_size);
|
|
|
|
if (copy._buf) {
|
|
|
|
memcpy(_buf, copy._buf, _size);
|
2008-09-18 20:47:49 -04:00
|
|
|
} else {
|
2009-02-01 21:36:05 -05:00
|
|
|
memset(_buf, 0, _size);
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 03:46:44 -05:00
|
|
|
template<typename Timestamp>
|
|
|
|
Event<Timestamp>::~Event() {
|
2009-02-01 21:36:05 -05:00
|
|
|
if (_owns_buf) {
|
|
|
|
free(_buf);
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 14:11:31 -04:00
|
|
|
template<typename Timestamp>
|
2015-09-18 11:34:12 -04:00
|
|
|
void
|
|
|
|
Event<Timestamp>::assign(const Event& other)
|
2011-10-19 14:11:31 -04:00
|
|
|
{
|
2015-09-18 11:34:12 -04:00
|
|
|
_id = other._id;
|
|
|
|
_type = other._type;
|
2016-11-07 07:07:42 -05:00
|
|
|
_time = other._time;
|
2015-09-18 11:34:12 -04:00
|
|
|
_owns_buf = other._owns_buf;
|
2011-10-19 14:11:31 -04:00
|
|
|
if (_owns_buf) {
|
2015-09-18 11:34:12 -04:00
|
|
|
if (other._buf) {
|
|
|
|
if (other._size > _size) {
|
|
|
|
_buf = (uint8_t*)::realloc(_buf, other._size);
|
2011-10-19 14:11:31 -04:00
|
|
|
}
|
2015-09-18 11:34:12 -04:00
|
|
|
memcpy(_buf, other._buf, other._size);
|
2011-10-19 14:11:31 -04:00
|
|
|
} else {
|
|
|
|
free(_buf);
|
|
|
|
_buf = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2015-09-18 11:34:12 -04:00
|
|
|
_buf = other._buf;
|
2011-10-19 14:11:31 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:34:12 -04:00
|
|
|
_size = other._size;
|
2011-10-19 14:11:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Timestamp>
|
|
|
|
void
|
2012-04-30 11:54:13 -04:00
|
|
|
Event<Timestamp>::set (const uint8_t* buf, uint32_t size, Timestamp t)
|
2011-10-19 14:11:31 -04:00
|
|
|
{
|
|
|
|
if (_owns_buf) {
|
|
|
|
if (_size < size) {
|
|
|
|
_buf = (uint8_t*) ::realloc(_buf, size);
|
|
|
|
}
|
|
|
|
memcpy (_buf, buf, size);
|
|
|
|
} else {
|
2012-04-30 11:54:13 -04:00
|
|
|
/* XXX this is really dangerous given the
|
|
|
|
const-ness of buf. The API should really
|
|
|
|
intervene here.
|
|
|
|
*/
|
|
|
|
_buf = const_cast<uint8_t*> (buf);
|
2011-10-19 14:11:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-07 07:07:42 -05:00
|
|
|
_time = t;
|
2011-10-19 14:11:31 -04:00
|
|
|
_size = size;
|
|
|
|
}
|
|
|
|
|
2008-09-22 12:28:02 -04:00
|
|
|
#endif // EVORAL_EVENT_ALLOC
|
2008-09-18 20:47:49 -04:00
|
|
|
|
2015-01-07 00:12:07 -05:00
|
|
|
template class Event<Evoral::Beats>;
|
2014-11-22 04:05:42 -05:00
|
|
|
template class Event<double>;
|
2010-12-03 17:26:29 -05:00
|
|
|
template class Event<int64_t>;
|
2009-02-01 21:36:05 -05:00
|
|
|
|
|
|
|
} // namespace Evoral
|
2008-09-18 20:47:49 -04:00
|
|
|
|