X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fmidi_diskstream.cc;h=cf54bef82ee2e93115531da724364bca456a2379;hb=3eef81daa23ebf8dffe65f02799c8383385e1128;hp=4f7778e09e535b693e388ac9f37030ba0732c799;hpb=6822b37c7fda7e3ce8e1c2977923ce9de72659f8;p=ardour.git diff --git a/libs/ardour/midi_diskstream.cc b/libs/ardour/midi_diskstream.cc index 4f7778e09e..cf54bef82e 100644 --- a/libs/ardour/midi_diskstream.cc +++ b/libs/ardour/midi_diskstream.cc @@ -31,18 +31,15 @@ #include "pbd/error.h" #include "pbd/basename.h" -#include +#include #include "pbd/xml++.h" #include "pbd/memento_command.h" #include "pbd/enumwriter.h" #include "pbd/stateful_diff_command.h" #include "pbd/stacktrace.h" -#include "ardour/ardour.h" #include "ardour/audioengine.h" #include "ardour/butler.h" -#include "ardour/configuration.h" -#include "ardour/cycle_timer.h" #include "ardour/debug.h" #include "ardour/io.h" #include "ardour/midi_diskstream.h" @@ -52,11 +49,10 @@ #include "ardour/midi_region.h" #include "ardour/playlist_factory.h" #include "ardour/region_factory.h" -#include "ardour/route.h" -#include "ardour/send.h" #include "ardour/session.h" #include "ardour/session_playlists.h" #include "ardour/smf_source.h" +#include "ardour/types.h" #include "ardour/utils.h" #include "midi++/types.h" @@ -77,6 +73,8 @@ MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::F , _note_mode(Sustained) , _frames_written_to_ringbuffer(0) , _frames_read_from_ringbuffer(0) + , _frames_pending_write(0) + , _num_captured_loops(0) , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI)) { in_set_state = true; @@ -87,7 +85,9 @@ MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::F in_set_state = false; - assert(!destructive()); + if (destructive()) { + throw failed_constructor(); + } } MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node) @@ -97,6 +97,8 @@ MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node) , _note_mode(Sustained) , _frames_written_to_ringbuffer(0) , _frames_read_from_ringbuffer(0) + , _frames_pending_write(0) + , _num_captured_loops(0) , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI)) { in_set_state = true; @@ -129,13 +131,11 @@ MidiDiskstream::init () _capture_buf = new MidiRingBuffer(size); _n_channels = ChanCount(DataType::MIDI, 1); - - assert(recordable()); } MidiDiskstream::~MidiDiskstream () { - Glib::Mutex::Lock lm (state_lock); + Glib::Threads::Mutex::Lock lm (state_lock); } @@ -153,7 +153,7 @@ void MidiDiskstream::non_realtime_input_change () { { - Glib::Mutex::Lock lm (state_lock); + Glib::Threads::Mutex::Lock lm (state_lock); if (input_change_pending.type == IOChange::NoChange) { return; @@ -201,9 +201,8 @@ MidiDiskstream::non_realtime_input_change () seek (_session.transport_frame()); } - if (_write_source) { - _write_source->set_last_write_end (_session.transport_frame()); - } + g_atomic_int_set(&_frames_pending_write, 0); + g_atomic_int_set(&_num_captured_loops, 0); } int @@ -216,7 +215,7 @@ MidiDiskstream::find_and_use_playlist (const string& name) } if (!playlist) { - error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't an midi playlist"), name) << endmsg; + error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't a midi playlist"), name) << endmsg; return -1; } @@ -226,9 +225,9 @@ MidiDiskstream::find_and_use_playlist (const string& name) int MidiDiskstream::use_playlist (boost::shared_ptr playlist) { - assert(boost::dynamic_pointer_cast(playlist)); - - Diskstream::use_playlist(playlist); + if (boost::dynamic_pointer_cast(playlist)) { + Diskstream::use_playlist(playlist); + } return 0; } @@ -252,7 +251,6 @@ MidiDiskstream::use_new_playlist () if ((playlist = boost::dynamic_pointer_cast (PlaylistFactory::create ( DataType::MIDI, _session, newname, hidden()))) != 0) { - playlist->set_orig_diskstream_id (id()); return use_playlist (playlist); } else { @@ -263,8 +261,6 @@ MidiDiskstream::use_new_playlist () int MidiDiskstream::use_copy_playlist () { - assert(midi_playlist()); - if (destructive()) { return 0; } @@ -280,7 +276,6 @@ MidiDiskstream::use_copy_playlist () newname = Playlist::bump_name (_playlist->name(), _session); if ((playlist = boost::dynamic_pointer_cast(PlaylistFactory::create (midi_playlist(), newname))) != 0) { - playlist->set_orig_diskstream_id (id()); return use_playlist (playlist); } else { return -1; @@ -292,10 +287,7 @@ MidiDiskstream::use_copy_playlist () int MidiDiskstream::set_destructive (bool yn) { - yn = 0; // stop pedantic gcc complaints about unused parameter - assert( ! destructive()); - assert( ! yn); - return -1; + return yn ? -1 : 0; } void @@ -307,174 +299,25 @@ MidiDiskstream::set_note_mode (NoteMode m) _write_source->model()->set_note_mode(m); } -#if 0 +/** Get the start, end, and length of a location "atomically". + * + * Note: Locations don't get deleted, so all we care about when I say "atomic" + * is that we are always pointing to the same one and using start/length values + * obtained just once. Use this function to achieve this since location being + * a parameter achieves this. + */ static void -trace_midi (ostream& o, MIDI::byte *msg, size_t len) +get_location_times(const Location* location, + framepos_t* start, + framepos_t* end, + framepos_t* length) { - using namespace MIDI; - eventType type; - const char trace_prefix = ':'; - - type = (eventType) (msg[0]&0xF0); - - switch (type) { - case off: - o << trace_prefix - << "Channel " - << (msg[0]&0xF)+1 - << " NoteOff NoteNum " - << (int) msg[1] - << " Vel " - << (int) msg[2] - << endl; - break; - - case on: - o << trace_prefix - << "Channel " - << (msg[0]&0xF)+1 - << " NoteOn NoteNum " - << (int) msg[1] - << " Vel " - << (int) msg[2] - << endl; - break; - - case polypress: - o << trace_prefix - << "Channel " - << (msg[0]&0xF)+1 - << " PolyPressure" - << (int) msg[1] - << endl; - break; - - case MIDI::controller: - o << trace_prefix - << "Channel " - << (msg[0]&0xF)+1 - << " Controller " - << (int) msg[1] - << " Value " - << (int) msg[2] - << endl; - break; - - case program: - o << trace_prefix - << "Channel " - << (msg[0]&0xF)+1 - << " Program Change ProgNum " - << (int) msg[1] - << endl; - break; - - case chanpress: - o << trace_prefix - << "Channel " - << (msg[0]&0xF)+1 - << " Channel Pressure " - << (int) msg[1] - << endl; - break; - - case MIDI::pitchbend: - o << trace_prefix - << "Channel " - << (msg[0]&0xF)+1 - << " Pitch Bend " - << ((msg[2]<<7)|msg[1]) - << endl; - break; - - case MIDI::sysex: - if (len == 1) { - switch (msg[0]) { - case 0xf8: - o << trace_prefix - << "Clock" - << endl; - break; - case 0xfa: - o << trace_prefix - << "Start" - << endl; - break; - case 0xfb: - o << trace_prefix - << "Continue" - << endl; - break; - case 0xfc: - o << trace_prefix - << "Stop" - << endl; - break; - case 0xfe: - o << trace_prefix - << "Active Sense" - << endl; - break; - case 0xff: - o << trace_prefix - << "System Reset" - << endl; - break; - default: - o << trace_prefix - << "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')' - << endl; - break; - } - } else { - o << trace_prefix - << "System Exclusive (" << len << ") = [ " << hex; - for (unsigned int i = 0; i < len; ++i) { - o << (int) msg[i] << ' '; - } - o << dec << ']' << endl; - - } - break; - - case MIDI::song: - o << trace_prefix << "Song" << endl; - break; - - case MIDI::tune: - o << trace_prefix << "Tune" << endl; - break; - - case MIDI::eox: - o << trace_prefix << "End-of-System Exclusive" << endl; - break; - - case MIDI::timing: - o << trace_prefix << "Timing" << endl; - break; - - case MIDI::start: - o << trace_prefix << "Start" << endl; - break; - - case MIDI::stop: - o << trace_prefix << "Stop" << endl; - break; - - case MIDI::contineu: - o << trace_prefix << "Continue" << endl; - break; - - case active: - o << trace_prefix << "Active Sense" << endl; - break; - - default: - o << trace_prefix << "Unrecognized MIDI message" << endl; - break; + if (location) { + *start = location->start(); + *end = location->end(); + *length = *end - *start; } } -#endif int MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance) @@ -501,22 +344,39 @@ MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt return 1; } - Glib::Mutex::Lock sm (state_lock, Glib::TRY_LOCK); + Glib::Threads::Mutex::Lock sm (state_lock, Glib::Threads::TRY_LOCK); if (!sm.locked()) { return 1; } + const Location* const loop_loc = loop_location; + framepos_t loop_start = 0; + framepos_t loop_end = 0; + framepos_t loop_length = 0; + get_location_times(loop_loc, &loop_start, &loop_end, &loop_length); + adjust_capture_position = 0; if (nominally_recording || (re && was_recording && _session.get_record_enabled() && _session.config.get_punch_in())) { - OverlapType ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes); + Evoral::OverlapType ot = Evoral::coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes); calculate_record_range(ot, transport_frame, nframes, rec_nframes, rec_offset); if (rec_nframes && !was_recording) { - _write_source->mark_write_starting_now (); - capture_captured = 0; + if (loop_loc) { + /* Loop recording, so pretend the capture started at the loop + start rgardless of what time it is now, so the source starts + at the loop start and can handle time wrapping around. + Otherwise, start the source right now as usual. + */ + capture_captured = transport_frame - loop_start; + capture_start_frame = loop_start; + } + _write_source->mark_write_starting_now( + capture_start_frame, capture_captured, loop_length); + g_atomic_int_set(&_frames_pending_write, 0); + g_atomic_int_set(&_num_captured_loops, 0); was_recording = true; } } @@ -531,7 +391,6 @@ MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt MidiBuffer& buf = sp->get_midi_buffer(nframes); for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) { const Evoral::MIDIEvent ev(*i, false); - assert(ev.buffer()); #ifndef NDEBUG if (DEBUG::MidiIO & PBD::debug_bits) { const uint8_t* __data = ev.buffer(); @@ -547,11 +406,23 @@ MidiDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str()); } #endif - _capture_buf->write(ev.time() + transport_frame, ev.type(), ev.size(), ev.buffer()); + /* Write events to the capture buffer in frames from session start, + but ignoring looping so event time progresses monotonically. + The source knows the loop length so it knows exactly where the + event occurs in the series of recorded loops and can implement + any desirable behaviour. We don't want to send event with + transport time here since that way the source can not + reconstruct their actual time; future clever MIDI looping should + probabl be implemented in the source instead of here. + */ + const framecnt_t loop_offset = _num_captured_loops * loop_length; + _capture_buf->write(transport_frame + loop_offset + ev.time(), + ev.type(), ev.size(), ev.buffer()); } + g_atomic_int_add(&_frames_pending_write, nframes); if (buf.size() != 0) { - Glib::Mutex::Lock lm (_gui_feed_buffer_mutex, Glib::TRY_LOCK); + Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex, Glib::Threads::TRY_LOCK); if (lm.locked ()) { /* Copy this data into our GUI feed buffer and tell the GUI @@ -623,15 +494,28 @@ MidiDiskstream::commit (framecnt_t playback_distance) adjust_capture_position = 0; } - uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer); - uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer); - if ((frames_written - frames_read) + playback_distance < midi_readahead) { - need_butler = true; + uint32_t frames_read = g_atomic_int_get(const_cast(&_frames_read_from_ringbuffer)); + uint32_t frames_written = g_atomic_int_get(const_cast(&_frames_written_to_ringbuffer)); + + /* + cerr << name() << " MDS written: " << frames_written << " - read: " << frames_read << + " = " << frames_written - frames_read + << " + " << playback_distance << " < " << midi_readahead << " = " << need_butler << ")" << endl; + */ + + /* frames_read will generally be less than frames_written, but + * immediately after an overwrite, we can end up having read some data + * before we've written any. we don't need to trip an assert() on this, + * but we do need to check so that the decision on whether or not we + * need the butler is done correctly. + */ + + if (frames_read <= frames_written) { + if ((frames_written - frames_read) + playback_distance < midi_readahead) { + need_butler = true; + } } - /*cerr << "MDS written: " << frames_written << " - read: " << frames_read << - " = " << frames_written - frames_read - << " + " << nframes << " < " << midi_readahead << " = " << need_butler << ")" << endl;*/ return need_butler; } @@ -665,9 +549,17 @@ MidiDiskstream::overwrite_existing_buffers () int MidiDiskstream::seek (framepos_t frame, bool complete_refill) { - Glib::Mutex::Lock lm (state_lock); + Glib::Threads::Mutex::Lock lm (state_lock); int ret = -1; + if (g_atomic_int_get (&_frames_read_from_ringbuffer) == 0) { + /* we haven't read anything since the last seek, + so flush all note trackers to prevent + wierdness + */ + reset_tracker (); + } + _playback_buf->reset(); _capture_buf->reset(); g_atomic_int_set(&_frames_read_from_ringbuffer, 0); @@ -706,29 +598,17 @@ MidiDiskstream::internal_playback_seek (framecnt_t distance) int MidiDiskstream::read (framepos_t& start, framecnt_t dur, bool reversed) { - framecnt_t this_read = 0; - bool reloop = false; - framepos_t loop_end = 0; - framepos_t loop_start = 0; - Location *loc = 0; + framecnt_t this_read = 0; + bool reloop = false; + framepos_t loop_end = 0; + framepos_t loop_start = 0; + framecnt_t loop_length = 0; + Location* loc = 0; if (!reversed) { - framecnt_t loop_length = 0; - - /* Make the use of a Location atomic for this read operation. - - Note: Locations don't get deleted, so all we care about - when I say "atomic" is that we are always pointing to - the same one and using a start/length values obtained - just once. - */ - - if ((loc = loop_location) != 0) { - loop_start = loc->start(); - loop_end = loc->end(); - loop_length = loop_end - loop_start; - } + loc = loop_location; + get_location_times(loc, &loop_start, &loop_end, &loop_length); /* if we are looping, ensure that the first frame we read is at the correct position within the loop. @@ -739,16 +619,16 @@ MidiDiskstream::read (framepos_t& start, framecnt_t dur, bool reversed) start = loop_start + ((start - loop_start) % loop_length); //cerr << "to " << start << endl; } - //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl; + // cerr << "start is " << start << " end " << start+dur << " loopstart: " << loop_start << " loopend: " << loop_end << endl; } while (dur) { /* take any loop into account. we can't read past the end of the loop. */ - if (loc && (loop_end - start < dur)) { + if (loc && (loop_end - start <= dur)) { this_read = loop_end - start; - //cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl; + // cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl; reloop = true; } else { reloop = false; @@ -767,8 +647,8 @@ MidiDiskstream::read (framepos_t& start, framecnt_t dur, bool reversed) id(), this_read, start) << endmsg; return -1; } - - g_atomic_int_add(&_frames_written_to_ringbuffer, this_read); + + g_atomic_int_add (&_frames_written_to_ringbuffer, this_read); if (reversed) { @@ -779,11 +659,9 @@ MidiDiskstream::read (framepos_t& start, framecnt_t dur, bool reversed) } else { /* if we read to the end of the loop, go back to the beginning */ - if (reloop) { // Synthesize LoopEvent here, because the next events // written will have non-monotonic timestamps. - _playback_buf->write(loop_end - 1, LoopEventType, sizeof (framepos_t), (uint8_t *) &loop_start); start = loop_start; } else { start += this_read; @@ -823,12 +701,11 @@ MidiDiskstream::do_refill () return 0; } - // At this point we... - assert(_playback_buf->write_space() > 0); // ... have something to write to, and - assert(file_frame <= max_framepos); // ... something to write + /* no space to write */ + if (_playback_buf->write_space() == 0) { + return 0; + } - // now calculate how much time is in the ringbuffer. - // and lets write as much as we need to get this to be midi_readahead; uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer); uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer); if ((frames_written - frames_read) >= midi_readahead) { @@ -863,16 +740,13 @@ int MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush) { framecnt_t to_write; - framecnt_t total; int32_t ret = 0; if (!_write_source) { return 0; } - assert (!destructive()); - - total = _session.transport_frame() - _write_source->last_write_end(); + const framecnt_t total = g_atomic_int_get(&_frames_pending_write); if (total == 0 || _capture_buf->read_space() == 0 || @@ -907,6 +781,7 @@ MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush) error << string_compose(_("MidiDiskstream %1: cannot write to disk"), id()) << endmsg; return -1; } + g_atomic_int_add(&_frames_pending_write, -to_write); } out: @@ -943,10 +818,10 @@ MidiDiskstream::transport_stopped_wallclock (struct tm& /*when*/, time_t /*twhen } /* XXX is there anything we can do if err != 0 ? */ - Glib::Mutex::Lock lm (capture_info_lock); + Glib::Threads::Mutex::Lock lm (capture_info_lock); if (capture_info.empty()) { - return; + goto no_capture_stuff_to_do; } if (abort_capture) { @@ -961,8 +836,6 @@ MidiDiskstream::transport_stopped_wallclock (struct tm& /*when*/, time_t /*twhen } else { - assert(_write_source); - framecnt_t total_capture = 0; for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) { total_capture += (*ci)->frames; @@ -1099,40 +972,26 @@ MidiDiskstream::transport_stopped_wallclock (struct tm& /*when*/, time_t /*twhen delete *ci; } - if (_playlist) { - midi_playlist()->clear_note_trackers (); - } - capture_info.clear (); capture_start_frame = 0; + + no_capture_stuff_to_do: + + reset_tracker (); } void -MidiDiskstream::transport_looped (framepos_t transport_frame) +MidiDiskstream::transport_looped (framepos_t) { + /* Here we only keep track of the number of captured loops so monotonic + event times can be delivered to the write source in process(). Trying + to be clever here is a world of trouble, it is better to simply record + the input in a straightforward non-destructive way. In the future when + we want to implement more clever MIDI looping modes it should be done in + the Source and/or entirely after the capture is finished. + */ if (was_recording) { - - // adjust the capture length knowing that the data will be recorded to disk - // only necessary after the first loop where we're recording - if (capture_info.size() == 0) { - capture_captured += _capture_offset; - - if (_alignment_style == ExistingMaterial) { - capture_captured += _session.worst_output_latency(); - } else { - capture_captured += _roll_delay; - } - } - - finish_capture (); - - // the next region will start recording via the normal mechanism - // we'll set the start position to the current transport pos - // no latency adjustment or capture offset needs to be made, as that already happened the first time - capture_start_frame = transport_frame; - first_recordable_frame = transport_frame; // mild lie - last_recordable_frame = max_framepos; - was_recording = true; + g_atomic_int_add(&_num_captured_loops, 1); } } @@ -1145,9 +1004,6 @@ MidiDiskstream::finish_capture () return; } - // Why must we destroy? - assert(!destructive()); - CaptureInfo* ci = new CaptureInfo; ci->start = capture_start_frame; @@ -1171,12 +1027,10 @@ MidiDiskstream::finish_capture () void MidiDiskstream::set_record_enabled (bool yn) { - if (!recordable() || !_session.record_enabling_legal()) { + if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_midi() == 0) { return; } - assert(!destructive()); - /* yes, i know that this not proof against race conditions, but its good enough. i think. */ @@ -1187,30 +1041,34 @@ MidiDiskstream::set_record_enabled (bool yn) } else { disengage_record_enable (); } + + RecordEnableChanged (); /* EMIT SIGNAL */ } } -void -MidiDiskstream::engage_record_enable () +bool +MidiDiskstream::prep_record_enable () { - bool const rolling = _session.transport_speed() != 0.0f; + if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_midi() == 0) { + return false; + } - g_atomic_int_set (&_record_enabled, 1); + bool const rolling = _session.transport_speed() != 0.0f; boost::shared_ptr sp = _source_port.lock (); if (sp && Config->get_monitoring_model() == HardwareMonitoring) { - sp->request_monitor_input (!(_session.config.get_auto_input() && rolling)); + sp->request_jack_monitors_input (!(_session.config.get_auto_input() && rolling)); } - RecordEnableChanged (); /* EMIT SIGNAL */ + return true; } -void -MidiDiskstream::disengage_record_enable () +bool +MidiDiskstream::prep_record_disable () { - g_atomic_int_set (&_record_enabled, 0); - RecordEnableChanged (); /* EMIT SIGNAL */ + + return true; } XMLNode& @@ -1264,8 +1122,6 @@ MidiDiskstream::set_state (const XMLNode& node, int version) in_set_state = true; for (niter = nlist.begin(); niter != nlist.end(); ++niter) { - assert ((*niter)->name() != IO::state_node_name); - if ((*niter)->name() == X_("CapturingSources")) { capture_pending_node = *niter; } @@ -1307,8 +1163,6 @@ MidiDiskstream::use_new_write_source (uint32_t n) return 1; } - assert(n == 0); - _write_source.reset(); try { @@ -1373,12 +1227,12 @@ MidiDiskstream::allocate_temporary_buffers () } void -MidiDiskstream::monitor_input (bool yn) +MidiDiskstream::ensure_jack_monitors_input (bool yn) { boost::shared_ptr sp = _source_port.lock (); if (sp) { - sp->ensure_monitor_input (yn); + sp->ensure_jack_monitors_input (yn); } } @@ -1427,6 +1281,13 @@ MidiDiskstream::use_pending_capture_data (XMLNode& /*node*/) return 0; } +void +MidiDiskstream::flush_playback (framepos_t start, framepos_t end) +{ + _playback_buf->flush (start, end); + g_atomic_int_add (&_frames_read_from_ringbuffer, end - start); +} + /** Writes playback events from playback_sample for nframes to dst, translating time stamps * so that an event at playback_sample has time = 0 */ @@ -1434,25 +1295,78 @@ void MidiDiskstream::get_playback (MidiBuffer& dst, framecnt_t nframes) { dst.clear(); - assert(dst.size() == 0); -#ifndef NDEBUG + Location* loc = loop_location; + DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ( - "%1 MDS pre-read read %4..%5 from %2 write to %3\n", _name, - _playback_buf->get_read_ptr(), _playback_buf->get_write_ptr(), playback_sample, playback_sample + nframes)); -// cerr << "================\n"; -// _playback_buf->dump (cerr); -// cerr << "----------------\n"; + "%1 MDS pre-read read %8 @ %4..%5 from %2 write to %3, LOOPED ? %6-%7\n", _name, + _playback_buf->get_read_ptr(), _playback_buf->get_write_ptr(), playback_sample, playback_sample + nframes, + (loc ? loc->start() : -1), (loc ? loc->end() : -1), nframes)); + + // cerr << "================\n"; + // _playback_buf->dump (cerr); + // cerr << "----------------\n"; + + size_t events_read = 0; + + if (loc) { + framepos_t effective_start; + + if (playback_sample >= loc->end()) { + effective_start = loc->start() + ((playback_sample - loc->end()) % loc->length()); + } else { + effective_start = playback_sample; + } + + DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("looped, effective start adjusted to %1\n", effective_start)); + + if (effective_start == loc->start()) { + /* We need to turn off notes that may extend + beyond the loop end. + */ + + _playback_buf->loop_resolve (dst, 0); + } + + if (loc->end() >= effective_start && loc->end() < effective_start + nframes) { + /* end of loop is within the range we are reading, so + split the read in two, and lie about the location + for the 2nd read + */ + framecnt_t first, second; + + first = loc->end() - effective_start; + second = nframes - first; + + DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read for eff %1 end %2: %3 and %4\n", + effective_start, loc->end(), first, second)); + + if (first) { + DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #1, from %1 for %2\n", + effective_start, first)); + events_read = _playback_buf->read (dst, effective_start, first); + } + + if (second) { + DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #2, from %1 for %2\n", + loc->start(), second)); + events_read += _playback_buf->read (dst, loc->start(), second); + } + + } else { + DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #3, adjusted start as %1 for %2\n", + effective_start, nframes)); + events_read = _playback_buf->read (dst, effective_start, effective_start + nframes); + } + } else { + events_read = _playback_buf->read (dst, playback_sample, playback_sample + nframes); + } - const size_t events_read = _playback_buf->read (dst, playback_sample, playback_sample + nframes); DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ( "%1 MDS events read %2 range %3 .. %4 rspace %5 wspace %6 r@%7 w@%8\n", _name, events_read, playback_sample, playback_sample + nframes, _playback_buf->read_space(), _playback_buf->write_space(), _playback_buf->get_read_ptr(), _playback_buf->get_write_ptr())); -#else - _playback_buf->read (dst, playback_sample, playback_sample + nframes); -#endif g_atomic_int_add (&_frames_read_from_ringbuffer, nframes); } @@ -1473,7 +1387,19 @@ MidiDiskstream::get_gui_feed_buffer () const { boost::shared_ptr b (new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI))); - Glib::Mutex::Lock lm (_gui_feed_buffer_mutex); + Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex); b->copy (_gui_feed_buffer); return b; } + +void +MidiDiskstream::reset_tracker () +{ + _playback_buf->reset_tracker (); + + boost::shared_ptr mp (midi_playlist()); + + if (mp) { + mp->clear_note_trackers (); + } +}