X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fsmf_source.cc;h=d1a82eb685cd5ed794f7af34fc92842b9b4be466;hb=c35e94a3c83028220f6eb494fdfe9c1960aaf751;hp=1ffcdac1b2e18b39992e4b527b59f53deb31490e;hpb=c1cfa12d6e5136d2e3e5501e83ff74c5009a9e60;p=ardour.git diff --git a/libs/ardour/smf_source.cc b/libs/ardour/smf_source.cc index 1ffcdac1b2..d1a82eb685 100644 --- a/libs/ardour/smf_source.cc +++ b/libs/ardour/smf_source.cc @@ -51,6 +51,7 @@ using namespace ARDOUR; using namespace Glib; using namespace PBD; using namespace Evoral; +using namespace std; /** Constructor used for new internal-to-session files. File cannot exist. */ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags) @@ -58,6 +59,7 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags) , MidiSource(s, path, flags) , FileSource(s, DataType::MIDI, path, string(), flags) , Evoral::SMF() + , _open (false) , _last_ev_time_beats(0.0) , _last_ev_time_frames(0) , _smf_last_read_end (0) @@ -93,6 +95,7 @@ SMFSource::SMFSource (Session& s, const string& path) , MidiSource(s, path, Source::Flag (0)) , FileSource(s, DataType::MIDI, path, string(), Source::Flag (0)) , Evoral::SMF() + , _open (false) , _last_ev_time_beats(0.0) , _last_ev_time_frames(0) , _smf_last_read_end (0) @@ -124,6 +127,7 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist) : Source(s, node) , MidiSource(s, node) , FileSource(s, node, must_exist) + , _open (false) , _last_ev_time_beats(0.0) , _last_ev_time_frames(0) , _smf_last_read_end (0) @@ -173,7 +177,7 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist) return; } - if (open(_path)) { + if (open (_path)) { throw failed_constructor (); } @@ -199,7 +203,8 @@ SMFSource::open_for_write () /** All stamps in audio frames */ framecnt_t -SMFSource::read_unlocked (Evoral::EventSink& destination, +SMFSource::read_unlocked (const Lock& lock, + Evoral::EventSink& destination, framepos_t const source_start, framepos_t start, framecnt_t duration, @@ -277,13 +282,8 @@ SMFSource::read_unlocked (Evoral::EventSink& destination, if (ev_frame_time < start + duration) { destination.write (ev_frame_time, ev_type, ev_size, ev_buffer); - if (tracker) { - if (ev_buffer[0] & MIDI_CMD_NOTE_ON) { - tracker->add (ev_buffer[1], ev_buffer[0] & 0xf); - } else if (ev_buffer[0] & MIDI_CMD_NOTE_OFF) { - tracker->remove (ev_buffer[1], ev_buffer[0] & 0xf); - } + tracker->track(ev_buffer); } } else { break; @@ -299,12 +299,13 @@ SMFSource::read_unlocked (Evoral::EventSink& destination, } framecnt_t -SMFSource::write_unlocked (MidiRingBuffer& source, +SMFSource::write_unlocked (const Lock& lock, + MidiRingBuffer& source, framepos_t position, framecnt_t cnt) { if (!_writing) { - mark_streaming_write_started (); + mark_streaming_write_started (lock); } framepos_t time; @@ -368,7 +369,7 @@ SMFSource::write_unlocked (MidiRingBuffer& source, continue; } - append_event_unlocked_frames(ev, position); + append_event_frames(lock, ev, position); } Evoral::SMF::flush (); @@ -379,13 +380,14 @@ SMFSource::write_unlocked (MidiRingBuffer& source, /** Append an event with a timestamp in beats */ void -SMFSource::append_event_unlocked_beats (const Evoral::Event& ev) +SMFSource::append_event_beats (const Glib::Threads::Mutex::Lock& lock, + const Evoral::Event& ev) { if (!_writing || ev.size() == 0) { return; } - /*printf("SMFSource: %s - append_event_unlocked_beats ID = %d time = %lf, size = %u, data = ", + /*printf("SMFSource: %s - append_event_beats ID = %d time = %lf, size = %u, data = ", name().c_str(), ev.id(), ev.time(), ev.size()); for (size_t i = 0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");*/ @@ -431,13 +433,15 @@ SMFSource::append_event_unlocked_beats (const Evoral::Event /** Append an event with a timestamp in frames (framepos_t) */ void -SMFSource::append_event_unlocked_frames (const Evoral::Event& ev, framepos_t position) +SMFSource::append_event_frames (const Glib::Threads::Mutex::Lock& lock, + const Evoral::Event& ev, + framepos_t position) { if (!_writing || ev.size() == 0) { return; } - // printf("SMFSource: %s - append_event_unlocked_frames ID = %d time = %u, size = %u, data = ", + // printf("SMFSource: %s - append_event_frames ID = %d time = %u, size = %u, data = ", // name().c_str(), ev.id(), ev.time(), ev.size()); // for (size_t i=0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n"); @@ -504,33 +508,30 @@ SMFSource::set_state (const XMLNode& node, int version) } void -SMFSource::mark_streaming_midi_write_started (NoteMode mode) +SMFSource::mark_streaming_midi_write_started (const Lock& lock, NoteMode mode) { - /* CALLER MUST HOLD LOCK */ - if (!_open && open_for_write()) { error << string_compose (_("cannot open MIDI file %1 for write"), _path) << endmsg; /* XXX should probably throw or return something */ return; } - MidiSource::mark_streaming_midi_write_started (mode); + MidiSource::mark_streaming_midi_write_started (lock, mode); Evoral::SMF::begin_write (); _last_ev_time_beats = Evoral::MusicalTime(); _last_ev_time_frames = 0; } void -SMFSource::mark_streaming_write_completed () +SMFSource::mark_streaming_write_completed (const Lock& lock) { - mark_midi_streaming_write_completed (Evoral::Sequence::DeleteStuckNotes); + mark_midi_streaming_write_completed (lock, Evoral::Sequence::DeleteStuckNotes); } void -SMFSource::mark_midi_streaming_write_completed (Evoral::Sequence::StuckNoteOption stuck_notes_option, Evoral::MusicalTime when) +SMFSource::mark_midi_streaming_write_completed (const Lock& lm, Evoral::Sequence::StuckNoteOption stuck_notes_option, Evoral::MusicalTime when) { - Glib::Threads::Mutex::Lock lm (_lock); - MidiSource::mark_midi_streaming_write_completed (stuck_notes_option, when); + MidiSource::mark_midi_streaming_write_completed (lm, stuck_notes_option, when); if (!writable()) { warning << string_compose ("attempt to write to unwritable SMF file %1", _path) << endmsg; @@ -592,16 +593,12 @@ static bool compare_eventlist ( } void -SMFSource::load_model (bool lock, bool force_reload) +SMFSource::load_model (const Glib::Threads::Mutex::Lock& lock, bool force_reload) { if (_writing) { return; } - boost::shared_ptr lm; - if (lock) - lm = boost::shared_ptr(new Glib::Threads::Mutex::Lock(_lock)); - if (_model && !force_reload) { return; } @@ -612,6 +609,8 @@ SMFSource::load_model (bool lock, bool force_reload) _model->clear(); } + invalidate(lock); + if (writable() && !_open) { return; } @@ -701,32 +700,33 @@ SMFSource::load_model (bool lock, bool force_reload) _model->end_write (Evoral::Sequence::ResolveStuckNotes, _length_beats); _model->set_edited (false); - invalidate(); + invalidate(lock); free(buf); } void -SMFSource::destroy_model () +SMFSource::destroy_model (const Glib::Threads::Mutex::Lock& lock) { //cerr << _name << " destroying model " << _model.get() << endl; _model.reset(); + invalidate(lock); } void -SMFSource::flush_midi () +SMFSource::flush_midi (const Lock& lock) { if (!writable() || _length_beats == 0.0) { return; } - ensure_disk_file (); + ensure_disk_file (lock); Evoral::SMF::end_write (); /* data in the file means its no longer removable */ mark_nonremovable (); - invalidate(); + invalidate(lock); } void @@ -738,7 +738,7 @@ SMFSource::set_path (const string& p) /** Ensure that this source has some file on disk, even if it's just a SMF header */ void -SMFSource::ensure_disk_file () +SMFSource::ensure_disk_file (const Lock& lock) { if (!writable()) { return; @@ -750,8 +750,9 @@ SMFSource::ensure_disk_file () */ boost::shared_ptr mm = _model; _model.reset (); - mm->sync_to_source (); + mm->sync_to_source (lock); _model = mm; + invalidate(lock); } else { /* No model; if it's not already open, it's an empty source, so create and open it for writing.