2 Copyright (C) 2006 Paul Davis
3 Written by Dave Robillard, 2006
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.
28 #include <pbd/mountpoint.h>
29 #include <pbd/pathscanner.h>
30 #include <pbd/stl_delete.h>
31 #include <pbd/strsplit.h>
33 #include <glibmm/miscutils.h>
35 #include <ardour/smf_source.h>
36 #include <ardour/session.h>
37 #include <ardour/midi_ring_buffer.h>
38 #include <ardour/midi_util.h>
39 #include <ardour/tempo.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/smf_reader.h>
42 #include <ardour/event_type_map.h>
46 using namespace ARDOUR;
48 string SMFSource::_search_path;
50 /*sigc::signal<void,struct tm*, time_t> SMFSource::HeaderPositionOffsetChanged;
51 bool SMFSource::header_position_negative;
52 uint64_t SMFSource::header_position_offset;
55 SMFSource::SMFSource (Session& s, std::string path, Flag flags)
56 : MidiSource (s, region_name_from_path(path, false))
57 , _flags (Flag(flags | Writable)) // FIXME: this needs to be writable for now
58 , _allow_remove_if_empty(true)
61 , _track_size(4) // 4 bytes for the ever-present EOT event
65 /* constructor used for new internal-to-session files. file cannot exist */
67 if (init (path, false)) {
68 throw failed_constructor ();
72 throw failed_constructor ();
75 assert(_name.find("/") == string::npos);
78 SMFSource::SMFSource (Session& s, const XMLNode& node)
79 : MidiSource (s, node)
80 , _flags (Flag (Writable|CanRename))
81 , _allow_remove_if_empty(true)
84 , _track_size(4) // 4 bytes for the ever-present EOT event
88 /* constructor used for existing internal-to-session files. file must exist */
90 if (set_state (node)) {
91 throw failed_constructor ();
94 if (init (_name, true)) {
95 throw failed_constructor ();
99 throw failed_constructor ();
102 assert(_name.find("/") == string::npos);
105 SMFSource::~SMFSource ()
108 unlink (_path.c_str());
113 SMFSource::removable () const
115 return (_flags & Removable) && ((_flags & RemoveAtDestroy) ||
116 ((_flags & RemovableIfEmpty) && is_empty()));
120 SMFSource::init (string pathstr, bool must_exist)
124 if (!find (pathstr, must_exist, is_new)) {
125 cerr << "cannot find " << pathstr << " with me = " << must_exist << endl;
129 if (is_new && must_exist) {
133 assert(_name.find("/") == string::npos);
137 /** Attempt to open the SMF file for reading and writing.
139 * Currently SMFSource is always read/write.
141 * \return 0 on success
142 * -1 if the file can not be opened for reading,
143 * -2 if the file can not be opened for writing
148 //cerr << "Opening SMF file " << path() << " writeable: " << writable() << endl;
150 assert(writable()); // FIXME;
152 _fd = fopen(path().c_str(), "r+");
154 // File already exists
156 fseek(_fd, _header_size - 4, 0);
157 uint32_t track_size_be = 0;
158 fread(&track_size_be, 4, 1, _fd);
159 _track_size = GUINT32_FROM_BE(track_size_be);
160 _empty = _track_size > 4;
161 //cerr << "SMF - read track size " << _track_size << endl;
163 // We're making a new file
165 _fd = fopen(path().c_str(), "w+");
167 cerr << "ERROR: Can not open SMF file " << path() << " for writing: " <<
168 strerror(errno) << endl;
174 // Write a tentative header just to pad things out so writing happens in the right spot
179 return (_fd == 0) ? -1 : 0;
194 SMFSource::seek_to_footer_position()
198 // lets check if there is a track end marker at the end of the data
199 fseek(_fd, -4, SEEK_END);
200 //cerr << "SMFSource::seek_to_footer_position: At position: " << ftell(_fd);
201 size_t read_bytes = fread(buffer, sizeof(uint8_t), 4, _fd);
202 /*cerr << " read size: " << read_bytes << " buffer: ";
203 for (size_t i=0; i < read_bytes; ++i) {
204 printf("%x ", buffer[i]);
209 if( (read_bytes == 4) &&
214 // there is one, so overwrite it
215 fseek(_fd, -4, SEEK_END);
217 // there is none, so append
218 fseek(_fd, 0, SEEK_END);
223 SMFSource::flush_header()
225 // FIXME: write timeline position somehow?
227 //cerr << path() << " SMF Flushing header\n";
231 const uint16_t type = GUINT16_TO_BE(0); // SMF Type 0 (single track)
232 const uint16_t ntracks = GUINT16_TO_BE(1); // Number of tracks (always 1 for Type 0)
233 const uint16_t division = GUINT16_TO_BE(_ppqn); // Pulses per quarter note (beat)
236 memcpy(data, &type, 2);
237 memcpy(data+2, &ntracks, 2);
238 memcpy(data+4, &division, 2);
240 _fd = freopen(path().c_str(), "r+", _fd);
242 fseek(_fd, 0, SEEK_SET);
243 write_chunk("MThd", 6, data);
244 write_chunk_header("MTrk", _track_size);
252 SMFSource::flush_footer()
254 //cerr << path() << " SMF Flushing footer\n";
255 seek_to_footer_position();
257 seek_to_footer_position();
263 SMFSource::write_footer()
266 char eot[3] = { 0xFF, 0x2F, 0x00 }; // end-of-track meta-event
267 fwrite(eot, 1, 3, _fd);
271 /** Returns the offset of the first event in the file with a time past @a start,
272 * relative to the start of the source.
274 * Returns -1 if not found.
278 SMFSource::find_first_event_after(nframes_t start)
280 // FIXME: obviously this is slooow
282 fseek(_fd, _header_size, 0);
284 while ( ! feof(_fd) ) {
285 const uint32_t delta_time = read_var_len();
287 if (delta_time > start)
295 /** Read an event from the current position in file.
297 * File position MUST be at the beginning of a delta time, or this will die very messily.
298 * ev.buffer must be of size ev.size, and large enough for the event. The returned event
299 * will have it's time field set to it's delta time, in SMF tempo-based ticks, using the
300 * rate given by ppqn() (it is the caller's responsibility to calculate a real time).
302 * \a size should be the capacity of \a buf. If it is not large enough, \a buf will
303 * be freed and a new buffer allocated in its place, the size of which will be placed
306 * Returns event length (including status byte) on success, 0 if event was
307 * skipped (eg a meta event), or -1 on EOF (or end of track).
310 SMFSource::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const
321 *delta_t = SMFReader::read_var_len(_fd);
323 return -1; // Premature EOF
327 return -1; // Premature EOF
330 const int status = fgetc(_fd);
333 return -1; // Premature EOF
336 //printf("Status @ %X = %X\n", (unsigned)ftell(_fd) - 1, status);
338 if (status == 0xFF) {
340 return -1; // Premature EOF
342 const int type = fgetc(_fd);
343 if ((unsigned char)type == 0x2F) {
344 return -1; // hit end of track
351 const int event_size = midi_event_size((unsigned char)status) + 1;
352 if (event_size <= 0) {
357 // Make sure we have enough scratch buffer
358 if (*size < (unsigned)event_size)
359 *buf = (uint8_t*)realloc(*buf, event_size);
363 (*buf)[0] = (unsigned char)status;
365 fread((*buf) + 1, 1, *size - 1, _fd);
367 /*printf("SMFSource %s read event: delta = %u, size = %u, data = ", _name.c_str(), *delta_t, *size);
368 for (size_t i=0; i < *size; ++i) {
369 printf("%X ", (*buf)[i]);
376 /** All stamps in audio frames */
378 SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset, nframes_t negative_stamp_offset) const
380 //cerr << "SMF read_unlocked " << name() << " read " << start << ", count=" << cnt << ", offset=" << stamp_offset << endl;
382 // 64 bits ought to be enough for anybody
383 uint64_t time = 0; // in SMF ticks, 1 tick per _ppqn
385 _read_data_count = 0;
387 // Output parameters for read_event (which will allocate scratch in buffer as needed)
388 uint32_t ev_delta_t = 0;
389 uint32_t ev_type = 0;
390 uint32_t ev_size = 0;
391 uint8_t* ev_buffer = 0;
393 size_t scratch_size = 0; // keep track of scratch to minimize reallocs
395 // FIXME: don't seek to start and search every read (brutal!)
396 fseek(_fd, _header_size, SEEK_SET);
398 // FIXME: assumes tempo never changes after start
399 const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat(
400 _session.engine().frame_rate(),
401 _session.tempo_map().meter_at(_timeline_position));
403 const uint64_t start_ticks = (uint64_t)((start / frames_per_beat) * _ppqn);
406 int ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
407 if (ret == -1) { // EOF
408 //cerr << "SMF - EOF\n";
412 ev_type = EventTypeMap::instance().midi_event_type(ev_buffer[0]);
414 time += ev_delta_t; // accumulate delta time
416 if (ret == 0) { // meta-event (skipped, just accumulate time)
417 //cerr << "SMF - META\n";
421 if (time >= start_ticks) {
422 const nframes_t ev_frame_time = (nframes_t)(
423 ((time / (double)_ppqn) * frames_per_beat)) + stamp_offset;
425 if (ev_frame_time <= start + cnt)
426 dst.write(ev_frame_time - negative_stamp_offset, ev_type, ev_size, ev_buffer);
431 _read_data_count += ev_size;
433 if (ev_size > scratch_size)
434 scratch_size = ev_size;
436 ev_size = scratch_size; // minimize realloc in read_event
442 /** All stamps in audio frames */
444 SMFSource::write_unlocked (MidiRingBuffer& src, nframes_t cnt)
446 _write_data_count = 0;
452 size_t buf_capacity = 4;
453 uint8_t* buf = (uint8_t*)malloc(buf_capacity);
455 if (_model && ! _model->writing())
456 _model->start_write();
458 Evoral::MIDIEvent ev(0, 0.0, 4, NULL, true);
461 bool ret = src.peek_time(&time);
462 if (!ret || time - _timeline_position > _length + cnt)
465 ret = src.read_prefix(&time, &type, &size);
469 if (size > buf_capacity) {
471 buf = (uint8_t*)realloc(buf, size);
474 ret = src.read_contents(size, buf);
476 cerr << "ERROR: Read time/size but not buffer, corrupt MIDI ring buffer" << endl;
480 assert(time >= _timeline_position);
481 time -= _timeline_position;
483 ev.set(buf, size, time);
484 ev.set_event_type(EventTypeMap::instance().midi_event_type(ev.buffer()[0]));
485 if (! (ev.is_channel_event() || ev.is_smf_meta_event() || ev.is_sysex()) ) {
486 //cerr << "SMFSource: WARNING: caller tried to write non SMF-Event of type " << std::hex << int(ev.buffer()[0]) << endl;
490 append_event_unlocked(Frames, ev);
499 const nframes_t oldlen = _length;
500 update_length(oldlen, cnt);
502 ViewDataRangeReady (_timeline_position + oldlen, cnt); /* EMIT SIGNAL */
509 SMFSource::append_event_unlocked(EventTimeUnit unit, const Evoral::Event& ev)
514 /*printf("SMFSource: %s - append_event_unlocked chan = %u, time = %lf, size = %u, data = ",
515 name().c_str(), (unsigned)ev.channel(), ev.time(), ev.size());
516 for (size_t i=0; i < ev.size(); ++i) {
517 printf("%X ", ev.buffer()[i]);
521 assert(ev.time() >= 0);
523 if (ev.time() < _last_ev_time) {
524 cerr << "SMFSource: Warning: Skipping event with ev.time() < _last_ev_time" << endl;
528 uint32_t delta_time = 0;
530 if (unit == Frames) {
531 // FIXME: assumes tempo never changes after start
532 const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat(
533 _session.engine().frame_rate(),
534 _session.tempo_map().meter_at(_timeline_position));
536 delta_time = (uint32_t)((ev.time() - _last_ev_time) / frames_per_beat * _ppqn);
538 assert(unit == Beats);
539 delta_time = (uint32_t)((ev.time() - _last_ev_time) * _ppqn);
543 const size_t stamp_size = write_var_len(delta_time);
544 fwrite(ev.buffer(), 1, ev.size(), _fd);
546 _track_size += stamp_size + ev.size();
547 _write_data_count += ev.size();
548 _last_ev_time = ev.time();
556 SMFSource::get_state ()
558 XMLNode& root (MidiSource::get_state());
560 snprintf (buf, sizeof (buf), "0x%x", (int)_flags);
561 root.add_property ("flags", buf);
566 SMFSource::set_state (const XMLNode& node)
568 const XMLProperty* prop;
570 if (MidiSource::set_state (node)) {
574 if ((prop = node.property (X_("flags"))) != 0) {
577 sscanf (prop->value().c_str(), "0x%x", &ival);
578 _flags = Flag (ival);
586 assert(_name.find("/") == string::npos);
592 SMFSource::mark_for_remove ()
597 _flags = Flag (_flags | RemoveAtDestroy);
601 SMFSource::mark_streaming_midi_write_started (NoteMode mode, nframes_t start_frame)
603 MidiSource::mark_streaming_midi_write_started (mode, start_frame);
605 fseek(_fd, _header_size, SEEK_SET);
609 SMFSource::mark_streaming_write_completed ()
611 MidiSource::mark_streaming_write_completed();
617 _model->set_edited(false);
623 SMFSource::mark_take (string id)
631 SMFSource::move_to_trash (const string trash_dir_name)
639 /* don't move the file across filesystems, just
640 stick it in the 'trash_dir_name' directory
641 on whichever filesystem it was already on.
644 newpath = Glib::path_get_dirname (_path);
645 newpath = Glib::path_get_dirname (newpath);
648 newpath += trash_dir_name;
650 newpath += Glib::path_get_basename (_path);
652 if (access (newpath.c_str(), F_OK) == 0) {
654 /* the new path already exists, try versioning */
656 char buf[PATH_MAX+1];
660 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
663 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
664 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
668 if (version == 999) {
669 PBD::error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
678 /* it doesn't exist, or we can't read it or something */
682 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
683 PBD::error << string_compose (_("cannot rename midi file source from %1 to %2 (%3)"),
684 _path, newpath, strerror (errno))
689 if (::unlink (peakpath.c_str()) != 0) {
690 PBD::error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
691 peakpath, _path, strerror (errno))
693 /* try to back out */
694 rename (newpath.c_str(), _path.c_str());
701 /* file can not be removed twice, since the operation is not idempotent */
703 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
709 SMFSource::safe_file_extension(const Glib::ustring& file)
711 return (file.rfind(".mid") != Glib::ustring::npos);
714 // FIXME: Merge this with audiofilesource somehow (make a generic filesource?)
716 SMFSource::find (string pathstr, bool must_exist, bool& isnew)
718 string::size_type pos;
723 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
725 if ((pos = pathstr.find_last_of (':')) == string::npos) {
728 pathstr = pathstr.substr (0, pos);
731 if (pathstr[0] != '/') {
733 /* non-absolute pathname: find pathstr in search path */
740 if (_search_path.length() == 0) {
741 PBD::error << _("FileSource: search path not set") << endmsg;
745 split (_search_path, dirs, ':');
749 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
752 if (fullpath[fullpath.length()-1] != '/') {
757 if (access (fullpath.c_str(), R_OK) == 0) {
765 PBD::error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, _search_path) << endmsg;
768 } else if (cnt == 0) {
771 PBD::error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, _search_path) << endmsg;
784 /* external files and/or very very old style sessions include full paths */
787 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
789 if (access (_path.c_str(), R_OK) != 0) {
791 /* file does not exist or we cannot read it */
794 PBD::error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
798 if (errno != ENOENT) {
799 PBD::error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
821 SMFSource::set_search_path (string p)
828 SMFSource::set_allow_remove_if_empty (bool yn)
831 _allow_remove_if_empty = yn;
836 SMFSource::set_source_name (string newname, bool destructive)
838 //Glib::Mutex::Lock lm (_lock); FIXME
839 string oldpath = _path;
840 string newpath = Session::change_midi_path_by_name (oldpath, _name, newname, destructive);
842 if (newpath.empty()) {
843 PBD::error << string_compose (_("programming error: %1"), "cannot generate a changed midi path") << endmsg;
847 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
848 PBD::error << string_compose (_("cannot rename midi file for %1 to %2"), _name, newpath) << endmsg;
852 _name = Glib::path_get_basename (newpath);
855 return 0;//rename_peakfile (peak_path (_path));
859 SMFSource::is_empty () const
866 SMFSource::write_chunk_header(const char id[4], uint32_t length)
868 const uint32_t length_be = GUINT32_TO_BE(length);
870 fwrite(id, 1, 4, _fd);
871 fwrite(&length_be, 4, 1, _fd);
875 SMFSource::write_chunk(const char id[4], uint32_t length, void* data)
877 write_chunk_header(id, length);
879 fwrite(data, 1, length, _fd);
882 /** Returns the size (in bytes) of the value written. */
884 SMFSource::write_var_len(uint32_t value)
888 uint32_t buffer = value & 0x7F;
890 while ( (value >>= 7) ) {
892 buffer |= ((value & 0x7F) | 0x80);
896 //printf("Writing var len byte %X\n", (unsigned char)buffer);
909 SMFSource::load_model(bool lock, bool force_reload)
915 Glib::Mutex::Lock lm (_lock);
917 if (_model && !force_reload && !_model->empty())
921 _model = boost::shared_ptr<MidiModel>(new MidiModel(this));
922 cerr << _name << " loaded new model " << _model.get() << endl;
924 cerr << _name << " reloading model " << _model.get()
925 << " (" << _model->n_notes() << " notes)" <<endl;
929 _model->start_write();
931 fseek(_fd, _header_size, SEEK_SET);
933 uint64_t time = 0; /* in SMF ticks */
936 size_t scratch_size = 0; // keep track of scratch and minimize reallocs
938 // FIXME: assumes tempo never changes after start
939 const double frames_per_beat = _session.tempo_map().tempo_at(_timeline_position).frames_per_beat(
940 _session.engine().frame_rate(),
941 _session.tempo_map().meter_at(_timeline_position));
943 uint32_t delta_t = 0;
947 while ((ret = read_event(&delta_t, &size, &buf)) >= 0) {
949 ev.set(buf, size, 0.0);
952 if (ret > 0) { // didn't skip (meta) event
953 // make ev.time absolute time in frames
954 ev.time() = (double)time * frames_per_beat / (double)_ppqn;
955 ev.set_event_type(EventTypeMap::instance().midi_event_type(buf[0]));
959 if (ev.size() > scratch_size)
960 scratch_size = ev.size();
962 ev.size() = scratch_size;
965 _model->end_write(false);
966 _model->set_edited(false);
973 SMFSource::destroy_model()
975 //cerr << _name << " destroying model " << _model.get() << endl;