*/
#include "pbd/compose.h"
+#include "pbd/error.h"
#include "ardour/debug.h"
#include "ardour/midi_ring_buffer.h"
#include "ardour/event_type_map.h"
using namespace std;
-using namespace ARDOUR;
using namespace PBD;
+namespace ARDOUR {
+
/** Read a block of MIDI events from this buffer into a MidiBuffer.
*
* Timestamps of events returned are relative to start (i.e. event with stamp 0
break;
} else if (ev_time + loop_offset < start) {
DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
- break;
+ this->increment_read_ptr (prefix_size);
+ this->increment_read_ptr (ev_size);
+ continue;
} else {
DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
}
- /* lets see if we are going to be able to write this event into dst.
- */
-
assert(ev_time >= start);
ev_time -= start;
ev_time += offset;
- // write the timestamp to address (write_loc - 1)
- uint8_t* write_loc = dst.reserve(ev_time, ev_size);
- if (write_loc == NULL) {
- 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;
- }
- cerr << "MRB: Unable to reserve space in buffer, event skipped";
- this->increment_read_ptr (prefix_size + ev_size); // Advance read pointer to next event
- continue;
- }
-
- /* 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);
-
- // This event marks a loop end (i.e. the next event's timestamp will be non-monotonic)
+ // This event marks a loop end (i.e. the next event's timestamp
+ // will be non-monotonic). Don't write it into the buffer - the
+ // significance of this event ends here.
+
if (ev_type == LoopEventType) {
assert (ev_size == sizeof (framepos_t));
framepos_t loop_start;
read_contents (ev_size, (uint8_t *) &loop_start);
-
loop_offset = ev_time - loop_start;
+ _tracker.resolve_notes (dst, ev_time);
continue;
}
+ /* 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);
ev_time += loop_offset;
uint8_t status;
if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
const uint8_t channel = status & 0x0F;
if (!(get_channel_mask() & (1L << channel))) {
- // cerr << "MRB skipping event due to channel mask" << endl;
+ DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB skipping event (%3 bytes) due to channel mask (mask = %1 chn = %2)\n",
+ get_channel_mask(), (int) channel, ev_size));
this->increment_read_ptr (ev_size); // Advance read pointer to next event
continue;
}
}
+ /* 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;
+ }
+
// write MIDI buffer contents
success = read_contents (ev_size, write_loc);
#endif
if (success) {
+
+ 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);
+ }
+
if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
}
return count;
}
+template<typename T>
+void
+MidiRingBuffer<T>::flush (framepos_t start, framepos_t end)
+{
+ 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);
+ }
+}
+
template<typename T>
void
MidiRingBuffer<T>::dump(ostream& str)
delete [] buf;
}
+template<typename T>
+void
+MidiRingBuffer<T>::reset_tracker ()
+{
+ _tracker.reset ();
+}
+
template class MidiRingBuffer<framepos_t>;
+} // namespace ARDOUR