2008-12-27 18:39:11 +00:00
|
|
|
/*
|
|
|
|
|
Copyright (C) 2006-2008 Paul Davis
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-10-25 14:42:46 +00:00
|
|
|
#include "pbd/compose.h"
|
2013-03-27 21:50:18 -04:00
|
|
|
#include "pbd/enumwriter.h"
|
2011-07-08 17:50:02 +00:00
|
|
|
#include "pbd/error.h"
|
2009-10-25 14:42:46 +00:00
|
|
|
|
|
|
|
|
#include "ardour/debug.h"
|
2008-12-27 18:39:11 +00:00
|
|
|
#include "ardour/midi_ring_buffer.h"
|
|
|
|
|
#include "ardour/midi_buffer.h"
|
|
|
|
|
#include "ardour/event_type_map.h"
|
|
|
|
|
|
2008-12-27 19:52:02 +00:00
|
|
|
using namespace std;
|
2010-03-02 00:00:00 +00:00
|
|
|
using namespace PBD;
|
2008-12-27 18:39:11 +00:00
|
|
|
|
2012-05-24 02:54:10 +00:00
|
|
|
namespace ARDOUR {
|
|
|
|
|
|
2010-09-23 21:48:17 +00:00
|
|
|
/** Read a block of MIDI events from this buffer into a MidiBuffer.
|
2008-12-27 18:39:11 +00:00
|
|
|
*
|
|
|
|
|
* Timestamps of events returned are relative to start (i.e. event with stamp 0
|
|
|
|
|
* occurred at start), with offset added.
|
|
|
|
|
*/
|
2009-02-02 02:36:05 +00:00
|
|
|
template<typename T>
|
2008-12-27 18:39:11 +00:00
|
|
|
size_t
|
2011-05-29 17:35:21 +00:00
|
|
|
MidiRingBuffer<T>::read(MidiBuffer& dst, framepos_t start, framepos_t end, framecnt_t offset, bool stop_on_overflow_in_dst)
|
2008-12-27 18:39:11 +00:00
|
|
|
{
|
2009-02-02 02:36:05 +00:00
|
|
|
if (this->read_space() == 0) {
|
2008-12-27 18:39:11 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-02 02:36:05 +00:00
|
|
|
T ev_time;
|
2008-12-27 18:39:11 +00:00
|
|
|
uint32_t ev_size;
|
2013-01-08 21:36:42 +00:00
|
|
|
size_t count = 0;
|
|
|
|
|
const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
|
2008-12-27 18:39:11 +00:00
|
|
|
|
2011-05-29 17:35:21 +00:00
|
|
|
while (this->read_space() >= prefix_size) {
|
|
|
|
|
|
|
|
|
|
uint8_t peekbuf[prefix_size];
|
|
|
|
|
|
|
|
|
|
/* this cannot fail, because we've already verified that there
|
|
|
|
|
is prefix_space to read
|
|
|
|
|
*/
|
2013-01-26 23:20:17 +00:00
|
|
|
this->peek (peekbuf, prefix_size);
|
2011-05-29 17:35:21 +00:00
|
|
|
|
|
|
|
|
ev_time = *((T*) peekbuf);
|
|
|
|
|
ev_size = *((uint32_t*)(peekbuf + sizeof(T) + sizeof (Evoral::EventType)));
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2013-01-08 21:36:42 +00:00
|
|
|
if (ev_time >= end) {
|
2009-10-25 14:42:46 +00:00
|
|
|
DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
|
2008-12-27 18:39:11 +00:00
|
|
|
break;
|
2013-01-08 21:36:42 +00:00
|
|
|
} else if (ev_time < start) {
|
2009-10-25 14:42:46 +00:00
|
|
|
DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
|
2013-01-08 21:36:42 +00:00
|
|
|
break;
|
2009-10-25 14:42:46 +00:00
|
|
|
} else {
|
|
|
|
|
DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
|
2008-12-27 18:39:11 +00:00
|
|
|
}
|
2011-06-01 16:50:12 +00:00
|
|
|
|
2011-05-29 17:35:21 +00:00
|
|
|
ev_time -= start;
|
|
|
|
|
ev_time += offset;
|
2008-12-27 18:39:11 +00:00
|
|
|
|
2011-05-29 17:35:21 +00:00
|
|
|
/* we're good to go ahead and read the data now but since we
|
|
|
|
|
* have the prefix data already, just skip over that
|
|
|
|
|
*/
|
|
|
|
|
this->increment_read_ptr (prefix_size);
|
2010-08-13 02:13:03 +00:00
|
|
|
|
2008-12-27 18:39:11 +00:00
|
|
|
uint8_t status;
|
2013-01-28 15:18:52 +00:00
|
|
|
bool r = this->peek (&status, sizeof(uint8_t));
|
|
|
|
|
assert (r); // If this failed, buffer is corrupt, all hope is lost
|
2008-12-27 18:39:11 +00:00
|
|
|
|
2011-07-08 17:50:02 +00:00
|
|
|
/* lets see if we are going to be able to write this event into dst.
|
|
|
|
|
*/
|
|
|
|
|
uint8_t* write_loc = dst.reserve (ev_time, ev_size);
|
|
|
|
|
if (write_loc == 0) {
|
|
|
|
|
if (stop_on_overflow_in_dst) {
|
|
|
|
|
DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events\n", count));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
error << "MRB: Unable to reserve space in buffer, event skipped" << endmsg;
|
|
|
|
|
this->increment_read_ptr (ev_size); // Advance read pointer to next event
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-02 08:50:36 +00:00
|
|
|
// write MIDI buffer contents
|
2013-01-08 21:36:42 +00:00
|
|
|
bool success = read_contents (ev_size, write_loc);
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2009-10-25 14:42:46 +00:00
|
|
|
#ifndef NDEBUG
|
2011-01-10 17:31:43 +00:00
|
|
|
if (DEBUG::MidiDiskstreamIO && PBD::debug_bits) {
|
|
|
|
|
DEBUG_STR_DECL(a);
|
|
|
|
|
DEBUG_STR_APPEND(a, string_compose ("wrote MidiEvent to Buffer (time=%1, start=%2 offset=%3)", ev_time, start, offset));
|
|
|
|
|
for (size_t i=0; i < ev_size; ++i) {
|
|
|
|
|
DEBUG_STR_APPEND(a,hex);
|
|
|
|
|
DEBUG_STR_APPEND(a,"0x");
|
|
|
|
|
DEBUG_STR_APPEND(a,(int)write_loc[i]);
|
|
|
|
|
DEBUG_STR_APPEND(a,' ');
|
|
|
|
|
}
|
|
|
|
|
DEBUG_STR_APPEND(a,'\n');
|
|
|
|
|
DEBUG_TRACE (DEBUG::MidiDiskstreamIO, DEBUG_STR(a).str());
|
2009-02-02 08:50:36 +00:00
|
|
|
}
|
2009-08-27 03:09:30 +00:00
|
|
|
#endif
|
2008-12-27 18:39:11 +00:00
|
|
|
|
|
|
|
|
if (success) {
|
2011-06-21 21:29:22 +00:00
|
|
|
|
|
|
|
|
if (is_note_on(write_loc[0]) ) {
|
|
|
|
|
_tracker.add (write_loc[1], write_loc[0] & 0xf);
|
|
|
|
|
} else if (is_note_off(write_loc[0])) {
|
|
|
|
|
_tracker.remove (write_loc[1], write_loc[0] & 0xf);
|
|
|
|
|
}
|
2013-03-27 21:50:18 -04:00
|
|
|
|
2008-12-27 18:39:11 +00:00
|
|
|
++count;
|
|
|
|
|
} else {
|
2009-02-01 21:04:12 +00:00
|
|
|
cerr << "WARNING: error reading event contents from MIDI ring" << endl;
|
2008-12-27 18:39:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2008-12-27 18:39:11 +00:00
|
|
|
return count;
|
|
|
|
|
}
|
2011-03-26 19:01:12 +00:00
|
|
|
|
2012-06-18 19:20:59 +00:00
|
|
|
template<typename T>
|
|
|
|
|
void
|
2013-01-08 21:36:42 +00:00
|
|
|
MidiRingBuffer<T>::flush (framepos_t /*start*/, framepos_t end)
|
2012-06-18 19:20:59 +00:00
|
|
|
{
|
|
|
|
|
const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
|
|
|
|
|
|
|
|
|
|
while (this->read_space() >= prefix_size) {
|
|
|
|
|
uint8_t peekbuf[prefix_size];
|
|
|
|
|
bool success;
|
|
|
|
|
uint32_t ev_size;
|
|
|
|
|
T ev_time;
|
|
|
|
|
|
|
|
|
|
success = this->peek (peekbuf, prefix_size);
|
|
|
|
|
/* this cannot fail, because we've already verified that there
|
|
|
|
|
is prefix_space to read
|
|
|
|
|
*/
|
|
|
|
|
assert (success);
|
|
|
|
|
|
|
|
|
|
ev_time = *((T*) peekbuf);
|
|
|
|
|
|
|
|
|
|
if (ev_time >= end) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ev_size = *((uint32_t*)(peekbuf + sizeof(T) + sizeof (Evoral::EventType)));
|
|
|
|
|
this->increment_read_ptr (prefix_size);
|
|
|
|
|
this->increment_read_ptr (ev_size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-16 01:08:51 +00:00
|
|
|
template<typename T>
|
|
|
|
|
void
|
2009-10-14 16:10:01 +00:00
|
|
|
MidiRingBuffer<T>::dump(ostream& str)
|
2009-09-16 01:08:51 +00:00
|
|
|
{
|
|
|
|
|
size_t rspace;
|
|
|
|
|
|
|
|
|
|
if ((rspace = this->read_space()) == 0) {
|
|
|
|
|
str << "MRB::dump: empty\n";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T ev_time;
|
|
|
|
|
Evoral::EventType ev_type;
|
|
|
|
|
uint32_t ev_size;
|
|
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
RingBufferNPT<uint8_t>::rw_vector vec;
|
|
|
|
|
RingBufferNPT<uint8_t>::get_read_vector (&vec);
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
if (vec.len[0] == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
str << this << ": Dump size = " << vec.len[0] + vec.len[1]
|
|
|
|
|
<< " r@ " << RingBufferNPT<uint8_t>::get_read_ptr()
|
2011-04-04 23:00:48 +00:00
|
|
|
<< " w@" << RingBufferNPT<uint8_t>::get_write_ptr() << endl;
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
uint8_t *buf = new uint8_t[vec.len[0] + vec.len[1]];
|
|
|
|
|
memcpy (buf, vec.buf[0], vec.len[0]);
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
if (vec.len[1]) {
|
|
|
|
|
memcpy (buf+vec.len[1], vec.buf[1], vec.len[1]);
|
|
|
|
|
}
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
uint8_t* data = buf;
|
|
|
|
|
const uint8_t* end = buf + vec.len[0] + vec.len[1];
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
while (data < end) {
|
2011-06-01 16:50:12 +00:00
|
|
|
|
2011-04-04 21:43:40 +00:00
|
|
|
memcpy (&ev_time, data, sizeof (T));
|
2011-04-04 23:00:48 +00:00
|
|
|
data += sizeof (T);
|
2011-04-04 21:43:40 +00:00
|
|
|
str << "\ttime " << ev_time;
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
if (data >= end) {
|
|
|
|
|
str << "(incomplete)\n ";
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 21:43:40 +00:00
|
|
|
memcpy (&ev_type, data, sizeof (ev_type));
|
2011-04-04 23:00:48 +00:00
|
|
|
data += sizeof (ev_type);
|
2011-04-04 21:43:40 +00:00
|
|
|
str << " type " << ev_type;
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
if (data >= end) {
|
|
|
|
|
str << "(incomplete)\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-04-04 21:43:40 +00:00
|
|
|
|
|
|
|
|
memcpy (&ev_size, data, sizeof (ev_size));
|
2011-04-04 23:00:48 +00:00
|
|
|
data += sizeof (ev_size);
|
2011-04-04 21:43:40 +00:00
|
|
|
str << " size " << ev_size;
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
if (data >= end) {
|
|
|
|
|
str << "(incomplete)\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 21:43:40 +00:00
|
|
|
for (uint32_t i = 0; i != ev_size && data < end; ++i) {
|
2009-09-16 01:08:51 +00:00
|
|
|
str << ' ' << hex << (int) data[i] << dec;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
data += ev_size;
|
2009-09-16 01:08:51 +00:00
|
|
|
|
2011-04-04 21:43:40 +00:00
|
|
|
str << endl;
|
2009-09-16 01:08:51 +00:00
|
|
|
}
|
2011-04-04 21:43:40 +00:00
|
|
|
|
2011-04-04 23:00:48 +00:00
|
|
|
delete [] buf;
|
2009-09-16 01:08:51 +00:00
|
|
|
}
|
2008-12-27 18:39:11 +00:00
|
|
|
|
2011-12-26 17:01:31 +00:00
|
|
|
template<typename T>
|
|
|
|
|
void
|
|
|
|
|
MidiRingBuffer<T>::reset_tracker ()
|
|
|
|
|
{
|
|
|
|
|
_tracker.reset ();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-08 21:36:42 +00:00
|
|
|
template<typename T>
|
|
|
|
|
void
|
|
|
|
|
MidiRingBuffer<T>::loop_resolve (MidiBuffer& dst, framepos_t t)
|
|
|
|
|
{
|
|
|
|
|
_tracker.resolve_notes (dst, t);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-03 22:26:29 +00:00
|
|
|
template class MidiRingBuffer<framepos_t>;
|
2008-12-27 18:39:11 +00:00
|
|
|
|
2012-05-24 02:54:10 +00:00
|
|
|
} // namespace ARDOUR
|