2 Copyright (C) 2006 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "pbd/pathscanner.h"
30 #include "pbd/stl_delete.h"
31 #include "pbd/strsplit.h"
33 #include <glib/gstdio.h>
34 #include <glibmm/miscutils.h>
35 #include <glibmm/fileutils.h>
37 #include "evoral/Control.hpp"
38 #include "evoral/SMF.hpp"
40 #include "ardour/event_type_map.h"
41 #include "ardour/midi_model.h"
42 #include "ardour/midi_ring_buffer.h"
43 #include "ardour/midi_state_tracker.h"
44 #include "ardour/session.h"
45 #include "ardour/smf_source.h"
46 #include "ardour/debug.h"
50 using namespace ARDOUR;
53 using namespace Evoral;
55 /** Constructor used for new internal-to-session files. File cannot exist. */
56 SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
57 : Source(s, DataType::MIDI, path, flags)
58 , MidiSource(s, path, flags)
59 , FileSource(s, DataType::MIDI, path, string(), flags)
61 , _last_ev_time_beats(0.0)
62 , _last_ev_time_frames(0)
63 , _smf_last_read_end (0)
64 , _smf_last_read_time (0)
66 /* note that origin remains empty */
68 if (init (_path, false)) {
69 throw failed_constructor ();
72 assert (!Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
75 /* file is not opened until write */
77 if (flags & Writable) {
82 throw failed_constructor ();
88 /** Constructor used for external-to-session files. File must exist. */
89 SMFSource::SMFSource (Session& s, const string& path)
90 : Source(s, DataType::MIDI, path, Source::Flag (0))
91 , MidiSource(s, path, Source::Flag (0))
92 , FileSource(s, DataType::MIDI, path, string(), Source::Flag (0))
94 , _last_ev_time_beats(0.0)
95 , _last_ev_time_frames(0)
96 , _smf_last_read_end (0)
97 , _smf_last_read_time (0)
99 /* note that origin remains empty */
101 if (init (_path, false)) {
102 throw failed_constructor ();
105 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
108 /* file is not opened until write */
110 if (_flags & Writable) {
115 throw failed_constructor ();
121 /** Constructor used for existing internal-to-session files. */
122 SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
124 , MidiSource(s, node)
125 , FileSource(s, node, must_exist)
126 , _last_ev_time_beats(0.0)
127 , _last_ev_time_frames(0)
128 , _smf_last_read_end (0)
129 , _smf_last_read_time (0)
131 if (set_state(node, Stateful::loading_state_version)) {
132 throw failed_constructor ();
135 if (init (_path, true)) {
136 throw failed_constructor ();
139 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
143 throw failed_constructor ();
149 SMFSource::~SMFSource ()
152 ::g_unlink (_path.c_str());
157 SMFSource::open_for_write ()
159 if (create (_path)) {
166 /** All stamps in audio frames */
168 SMFSource::read_unlocked (Evoral::EventSink<framepos_t>& destination,
169 framepos_t const source_start,
172 MidiStateTracker* tracker) const
175 uint64_t time = 0; // in SMF ticks, 1 tick per _ppqn
177 if (writable() && !_open) {
178 /* nothing to read since nothing has ben written */
182 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: start %1 duration %2\n", start, duration));
184 // Output parameters for read_event (which will allocate scratch in buffer as needed)
185 uint32_t ev_delta_t = 0;
186 uint32_t ev_type = 0;
187 uint32_t ev_size = 0;
188 uint8_t* ev_buffer = 0;
190 size_t scratch_size = 0; // keep track of scratch to minimize reallocs
192 BeatsFramesConverter converter(_session.tempo_map(), source_start);
194 const uint64_t start_ticks = (uint64_t)(converter.from(start) * ppqn());
195 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: start in ticks %1\n", start_ticks));
197 if (_smf_last_read_end == 0 || start != _smf_last_read_end) {
198 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: seek to %1\n", start));
199 Evoral::SMF::seek_to_start();
200 while (time < start_ticks) {
203 ret = read_event(&ev_delta_t, &ev_size, &ev_buffer, &ignored);
204 if (ret == -1) { // EOF
205 _smf_last_read_end = start + duration;
208 time += ev_delta_t; // accumulate delta time
211 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: set time to %1\n", _smf_last_read_time));
212 time = _smf_last_read_time;
215 _smf_last_read_end = start + duration;
218 gint ignored; /* XXX don't ignore note id's ??*/
220 ret = read_event(&ev_delta_t, &ev_size, &ev_buffer, &ignored);
221 if (ret == -1) { // EOF
225 time += ev_delta_t; // accumulate delta time
226 _smf_last_read_time = time;
228 if (ret == 0) { // meta-event (skipped, just accumulate time)
232 ev_type = EventTypeMap::instance().midi_event_type(ev_buffer[0]);
234 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked delta %1, time %2, buf[0] %3, type %4\n",
235 ev_delta_t, time, ev_buffer[0], ev_type));
237 assert(time >= start_ticks);
239 /* Note that we add on the source start time (in session frames) here so that ev_frame_time
240 is in session frames.
242 const framepos_t ev_frame_time = converter.to(time / (double)ppqn()) + source_start;
244 if (ev_frame_time < start + duration) {
245 destination.write (ev_frame_time, ev_type, ev_size, ev_buffer);
248 if (ev_buffer[0] & MIDI_CMD_NOTE_ON) {
249 tracker->add (ev_buffer[1], ev_buffer[0] & 0xf);
250 } else if (ev_buffer[0] & MIDI_CMD_NOTE_OFF) {
251 tracker->remove (ev_buffer[1], ev_buffer[0] & 0xf);
258 if (ev_size > scratch_size) {
259 scratch_size = ev_size;
261 ev_size = scratch_size; // ensure read_event only allocates if necessary
268 SMFSource::write_unlocked (MidiRingBuffer<framepos_t>& source,
273 mark_streaming_write_started ();
277 Evoral::EventType type;
280 size_t buf_capacity = 4;
281 uint8_t* buf = (uint8_t*)malloc(buf_capacity);
283 if (_model && !_model->writing()) {
284 _model->start_write();
287 Evoral::MIDIEvent<framepos_t> ev;
289 /* Get the event time, in frames since session start but ignoring looping. */
291 if (!(ret = source.peek ((uint8_t*)&time, sizeof (time)))) {
292 /* Ring is empty, no more events. */
296 if ((cnt != max_framecnt) &&
297 (time > position + _capture_length + cnt)) {
298 /* The diskstream doesn't want us to write everything, and this
299 event is past the end of this block, so we're done for now. */
303 /* Read the time, type, and size of the event. */
304 if (!(ret = source.read_prefix (&time, &type, &size))) {
305 error << _("Unable to read event prefix, corrupt MIDI ring") << endmsg;
309 /* Enlarge body buffer if necessary now that we know the size. */
310 if (size > buf_capacity) {
312 buf = (uint8_t*)realloc(buf, size);
315 /* Read the event body into buffer. */
316 ret = source.read_contents(size, buf);
318 error << _("Event has time and size but no body, corrupt MIDI ring") << endmsg;
322 /* Convert event time from absolute to source relative. */
323 if (time < position) {
324 error << _("Event time is before MIDI source position") << endmsg;
329 ev.set(buf, size, time);
330 ev.set_event_type(EventTypeMap::instance().midi_event_type(ev.buffer()[0]));
331 ev.set_id(Evoral::next_event_id());
333 if (!(ev.is_channel_event() || ev.is_smf_meta_event() || ev.is_sysex())) {
337 append_event_unlocked_frames(ev, position);
340 Evoral::SMF::flush ();
346 /** Append an event with a timestamp in beats (double) */
348 SMFSource::append_event_unlocked_beats (const Evoral::Event<double>& ev)
350 if (!_writing || ev.size() == 0) {
354 /*printf("SMFSource: %s - append_event_unlocked_beats ID = %d time = %lf, size = %u, data = ",
355 name().c_str(), ev.id(), ev.time(), ev.size());
356 for (size_t i = 0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");*/
358 if (ev.time() < _last_ev_time_beats) {
359 warning << string_compose(_("Skipping event with unordered time %1"), ev.time())
364 Evoral::event_id_t event_id;
367 event_id = Evoral::next_event_id();
373 _model->append (ev, event_id);
376 _length_beats = max(_length_beats, ev.time());
378 const double delta_time_beats = ev.time() - _last_ev_time_beats;
379 const uint32_t delta_time_ticks = (uint32_t)lrint(delta_time_beats * (double)ppqn());
381 Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer(), event_id);
382 _last_ev_time_beats = ev.time();
385 /** Append an event with a timestamp in frames (framepos_t) */
387 SMFSource::append_event_unlocked_frames (const Evoral::Event<framepos_t>& ev, framepos_t position)
389 if (!_writing || ev.size() == 0) {
393 // printf("SMFSource: %s - append_event_unlocked_frames ID = %d time = %u, size = %u, data = ",
394 // name().c_str(), ev.id(), ev.time(), ev.size());
395 // for (size_t i=0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");
397 if (ev.time() < _last_ev_time_frames) {
398 warning << string_compose(_("Skipping event with unordered time %1"), ev.time())
403 BeatsFramesConverter converter(_session.tempo_map(), position);
404 const double ev_time_beats = converter.from(ev.time());
405 Evoral::event_id_t event_id;
408 event_id = Evoral::next_event_id();
414 const Evoral::Event<double> beat_ev (ev.event_type(),
417 const_cast<uint8_t*>(ev.buffer()));
418 _model->append (beat_ev, event_id);
421 _length_beats = max(_length_beats, ev_time_beats);
423 const Evoral::MusicalTime last_time_beats = converter.from (_last_ev_time_frames);
424 const Evoral::MusicalTime delta_time_beats = ev_time_beats - last_time_beats;
425 const uint32_t delta_time_ticks = (uint32_t)(lrint(delta_time_beats * (double)ppqn()));
427 Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer(), event_id);
428 _last_ev_time_frames = ev.time();
432 SMFSource::get_state ()
434 XMLNode& node = MidiSource::get_state();
435 node.add_property (X_("origin"), _origin);
440 SMFSource::set_state (const XMLNode& node, int version)
442 if (Source::set_state (node, version)) {
446 if (MidiSource::set_state (node, version)) {
450 if (FileSource::set_state (node, version)) {
458 SMFSource::mark_streaming_midi_write_started (NoteMode mode)
460 /* CALLER MUST HOLD LOCK */
462 if (!_open && open_for_write()) {
463 error << string_compose (_("cannot open MIDI file %1 for write"), _path) << endmsg;
464 /* XXX should probably throw or return something */
468 MidiSource::mark_streaming_midi_write_started (mode);
469 Evoral::SMF::begin_write ();
470 _last_ev_time_beats = 0.0;
471 _last_ev_time_frames = 0;
475 SMFSource::mark_streaming_write_completed ()
477 mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::MusicalTime>::DeleteStuckNotes);
481 SMFSource::mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::MusicalTime>::StuckNoteOption stuck_notes_option, Evoral::MusicalTime when)
483 Glib::Threads::Mutex::Lock lm (_lock);
484 MidiSource::mark_midi_streaming_write_completed (stuck_notes_option, when);
487 warning << string_compose ("attempt to write to unwritable SMF file %1", _path) << endmsg;
492 _model->set_edited(false);
495 Evoral::SMF::end_write ();
497 /* data in the file now, not removable */
499 mark_nonremovable ();
503 SMFSource::valid_midi_file (const string& file)
505 if (safe_midi_file_extension (file) ) {
506 return (SMF::test (file) );
512 SMFSource::safe_midi_file_extension (const string& file)
514 static regex_t compiled_pattern;
515 static bool compile = true;
516 const int nmatches = 2;
517 regmatch_t matches[nmatches];
519 if (Glib::file_test (file, Glib::FILE_TEST_EXISTS)) {
520 if (!Glib::file_test (file, Glib::FILE_TEST_IS_REGULAR)) {
521 /* exists but is not a regular file */
526 if (compile && regcomp (&compiled_pattern, "\\.[mM][iI][dD][iI]?$", REG_EXTENDED)) {
532 if (regexec (&compiled_pattern, file.c_str(), nmatches, matches, 0)) {
539 static bool compare_eventlist (
540 const std::pair< Evoral::Event<double>*, gint >& a,
541 const std::pair< Evoral::Event<double>*, gint >& b) {
542 return ( a.first->time() < b.first->time() );
546 SMFSource::load_model (bool lock, bool force_reload)
552 boost::shared_ptr<Glib::Threads::Mutex::Lock> lm;
554 lm = boost::shared_ptr<Glib::Threads::Mutex::Lock>(new Glib::Threads::Mutex::Lock(_lock));
556 if (_model && !force_reload) {
561 _model = boost::shared_ptr<MidiModel> (new MidiModel (shared_from_this ()));
566 if (writable() && !_open) {
570 _model->start_write();
571 Evoral::SMF::seek_to_start();
573 uint64_t time = 0; /* in SMF ticks */
574 Evoral::Event<double> ev;
576 uint32_t scratch_size = 0; // keep track of scratch and minimize reallocs
578 uint32_t delta_t = 0;
585 // TODO simplify event allocation
586 std::list< std::pair< Evoral::Event<double>*, gint > > eventlist;
588 for (unsigned i = 1; i <= num_tracks(); ++i) {
589 if (seek_to_track(i)) continue;
592 have_event_id = false;
594 while ((ret = read_event (&delta_t, &size, &buf, &event_id)) >= 0) {
599 /* meta-event : did we get an event ID ? */
601 have_event_id = true;
607 /* not a meta-event */
609 if (!have_event_id) {
610 event_id = Evoral::next_event_id();
612 uint32_t event_type = EventTypeMap::instance().midi_event_type(buf[0]);
613 double event_time = time / (double) ppqn();
617 for (uint32_t xx = 0; xx < size; ++xx) {
619 snprintf (b, sizeof (b), "0x%x ", buf[xx]);
623 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF %6 load model delta %1, time %2, size %3 buf %4, type %5\n",
624 delta_t, time, size, ss , event_type, name()));
627 eventlist.push_back(make_pair (
628 new Evoral::Event<double> (
629 event_type, event_time,
633 // Set size to max capacity to minimize allocs in read_event
634 scratch_size = std::max(size, scratch_size);
637 _length_beats = max(_length_beats, event_time);
640 /* event ID's must immediately precede the event they are for */
641 have_event_id = false;
645 eventlist.sort(compare_eventlist);
647 std::list< std::pair< Evoral::Event<double>*, gint > >::iterator it;
648 for (it=eventlist.begin(); it!=eventlist.end(); ++it) {
649 _model->append (*it->first, it->second);
653 _model->end_write (Evoral::Sequence<Evoral::MusicalTime>::ResolveStuckNotes, _length_beats);
654 _model->set_edited (false);
656 _model_iter = _model->begin();
662 SMFSource::destroy_model ()
664 //cerr << _name << " destroying model " << _model.get() << endl;
669 SMFSource::flush_midi ()
671 if (!writable() || (writable() && !_open)) {
675 Evoral::SMF::end_write ();
676 /* data in the file means its no longer removable */
677 mark_nonremovable ();
681 SMFSource::set_path (const string& p)
683 FileSource::set_path (p);
684 SMF::set_path (_path);
687 /** Ensure that this source has some file on disk, even if it's just a SMF header */
689 SMFSource::ensure_disk_file ()
692 /* We have a model, so write it to disk; see MidiSource::session_saved
693 for an explanation of what we are doing here.
695 boost::shared_ptr<MidiModel> mm = _model;
697 mm->sync_to_source ();
700 /* No model; if it's not already open, it's an empty source, so create
701 and open it for writing.
707 /* Flush, which will definitely put something on disk */
713 SMFSource::prevent_deletion ()
715 /* Unlike the audio case, the MIDI file remains mutable (because we can
719 _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
723 SMFSource::rename (const string& newname)
725 Glib::Threads::Mutex::Lock lm (_lock);
726 string oldpath = _path;
727 string newpath = _session.new_source_path_from_name (DataType::MIDI, newname);
729 if (newpath.empty()) {
730 error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg;
734 // Test whether newpath exists, if yes notify the user but continue.
735 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
736 error << string_compose (_("Programming error! %1 tried to rename a file over another file! It's safe to continue working, but please report this to the developers."), PROGRAM_NAME) << endmsg;
740 if (Glib::file_test (oldpath.c_str(), Glib::FILE_TEST_EXISTS)) {
741 /* rename only needed if file exists on disk */
742 if (::rename (oldpath.c_str(), newpath.c_str()) != 0) {
743 error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, strerror(errno)) << endmsg;
748 _name = Glib::path_get_basename (newpath);