2019-08-02 23:10:55 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008-2016 David Robillard <d@drobilla.net>
|
|
|
|
* Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2019-08-02 23:10:55 -04:00
|
|
|
* This program 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
|
|
|
*
|
2019-08-02 23:10:55 -04:00
|
|
|
* This program 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 more 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.,
|
2019-08-02 23:10:55 -04:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-09-18 20:47:49 -04:00
|
|
|
*/
|
|
|
|
|
2023-02-17 02:31:21 -05:00
|
|
|
#include <atomic>
|
|
|
|
|
2010-07-20 12:27:34 -04:00
|
|
|
#include <glib.h>
|
2015-10-05 13:58:35 -04:00
|
|
|
|
2017-09-24 12:03:54 -04:00
|
|
|
#include "temporal/beats.h"
|
2019-10-25 15:13:51 -04:00
|
|
|
#include "evoral/Event.h"
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
namespace Evoral {
|
|
|
|
|
2023-02-17 02:31:21 -05:00
|
|
|
static std::atomic<event_id_t> _event_id_counter (0);
|
2010-07-20 12:27:34 -04:00
|
|
|
|
|
|
|
event_id_t
|
|
|
|
event_id_counter()
|
|
|
|
{
|
2023-02-17 02:31:21 -05:00
|
|
|
return _event_id_counter.load ();
|
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)
|
|
|
|
{
|
2023-02-17 02:31:21 -05:00
|
|
|
_event_id_counter.store (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
|
|
|
*/
|
2023-02-17 02:31:21 -05:00
|
|
|
return _event_id_counter.fetch_add (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>
|
2024-06-22 00:17:30 -04:00
|
|
|
Event<Timestamp>::Event (const Event& copy, bool owns_buf)
|
|
|
|
: _type (copy._type)
|
|
|
|
, _time (copy._time)
|
|
|
|
, _size (copy._size)
|
|
|
|
, _buf (copy._buf)
|
2015-05-23 11:53:34 -04:00
|
|
|
, _id (next_event_id ())
|
2024-06-22 00:17:30 -04:00
|
|
|
, _owns_buf (owns_buf)
|
2008-09-18 20:47:49 -04:00
|
|
|
{
|
2009-02-01 21:36:05 -05:00
|
|
|
if (owns_buf) {
|
2024-06-22 00:17:30 -04:00
|
|
|
_buf = (uint8_t*) malloc (_size);
|
2009-02-01 21:36:05 -05:00
|
|
|
if (copy._buf) {
|
2024-06-22 00:17:30 -04:00
|
|
|
memcpy (_buf, copy._buf, _size);
|
2008-09-18 20:47:49 -04:00
|
|
|
} else {
|
2024-06-22 00:17:30 -04:00
|
|
|
memset (_buf, 0, _size);
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 03:46:44 -05:00
|
|
|
template<typename Timestamp>
|
2024-06-22 00:17:30 -04:00
|
|
|
Event<Timestamp>::~Event()
|
|
|
|
{
|
2009-02-01 21:36:05 -05:00
|
|
|
if (_owns_buf) {
|
2024-06-22 00:17:30 -04:00
|
|
|
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
|
2024-06-22 00:17:30 -04:00
|
|
|
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
|
|
|
|
2017-09-24 12:03:54 -04:00
|
|
|
template class Event<Temporal::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
|
|
|
|