lots of details relating to MIDI file management; try to ignore ALSA sequencer MIDI...
[ardour2.git] / libs / ardour / smf_source.cc
blobbf038326d5fc452fcb020a828b3b31ee37d97f41
1 /*
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.
21 #include <vector>
23 #include <sys/time.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
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 "evoral/Control.hpp"
37 #include "ardour/audioengine.h"
38 #include "ardour/event_type_map.h"
39 #include "ardour/midi_model.h"
40 #include "ardour/midi_ring_buffer.h"
41 #include "ardour/midi_state_tracker.h"
42 #include "ardour/session.h"
43 #include "ardour/smf_source.h"
44 #include "ardour/debug.h"
46 #include "i18n.h"
48 using namespace ARDOUR;
49 using namespace Glib;
50 using namespace PBD;
52 /** Constructor used for new internal-to-session files. File cannot exist. */
53 SMFSource::SMFSource (Session& s, const ustring& path, Source::Flag flags)
54 : Source(s, DataType::MIDI, path, flags)
55 , MidiSource(s, path)
56 , FileSource(s, DataType::MIDI, path, flags)
57 , Evoral::SMF()
58 , _last_ev_time_beats(0.0)
59 , _last_ev_time_frames(0)
60 , _smf_last_read_end (0)
61 , _smf_last_read_time (0)
63 if (init(_path, false)) {
64 throw failed_constructor ();
67 if (create(path)) {
68 throw failed_constructor ();
71 load_model(true, true); // FIXME
74 /** Constructor used for existing internal-to-session files. */
75 SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
76 : Source(s, node)
77 , MidiSource(s, node)
78 , FileSource(s, node, must_exist)
79 , _last_ev_time_beats(0.0)
80 , _last_ev_time_frames(0)
81 , _smf_last_read_end (0)
82 , _smf_last_read_time (0)
84 if (set_state(node, Stateful::loading_state_version)) {
85 throw failed_constructor ();
88 if (init(_path, true)) {
89 throw failed_constructor ();
92 if (open(_path)) {
93 throw failed_constructor ();
96 load_model(true, true); // FIXME
99 SMFSource::~SMFSource ()
101 if (removable()) {
102 cerr << name() << " is removable, empty ? " << empty() << " UC " << use_count() << endl;
103 unlink (_path.c_str());
107 /** All stamps in audio frames */
108 nframes_t
109 SMFSource::read_unlocked (Evoral::EventSink<nframes_t>& destination, sframes_t source_start,
110 sframes_t start, nframes_t duration,
111 sframes_t stamp_offset, sframes_t negative_stamp_offset,
112 MidiStateTracker* tracker) const
114 int ret = 0;
115 uint64_t time = 0; // in SMF ticks, 1 tick per _ppqn
117 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: start %1 duration %2\n", start, duration));
119 _read_data_count = 0;
121 // Output parameters for read_event (which will allocate scratch in buffer as needed)
122 uint32_t ev_delta_t = 0;
123 uint32_t ev_type = 0;
124 uint32_t ev_size = 0;
125 uint8_t* ev_buffer = 0;
127 size_t scratch_size = 0; // keep track of scratch to minimize reallocs
129 BeatsFramesConverter converter(_session.tempo_map(), source_start);
131 const uint64_t start_ticks = (uint64_t)(converter.from(start) * ppqn());
132 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: start in ticks %1\n", start_ticks));
134 if (_smf_last_read_end == 0 || start != _smf_last_read_end) {
135 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: seek to %1\n", start));
136 Evoral::SMF::seek_to_start();
137 while (time < start_ticks) {
138 ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
139 if (ret == -1) { // EOF
140 _smf_last_read_end = start + duration;
141 return duration;
143 time += ev_delta_t; // accumulate delta time
145 } else {
146 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: set time to %1\n", _smf_last_read_time));
147 time = _smf_last_read_time;
150 _smf_last_read_end = start + duration;
152 while (true) {
153 ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
154 if (ret == -1) { // EOF
155 break;
158 time += ev_delta_t; // accumulate delta time
159 _smf_last_read_time = time;
161 if (ret == 0) { // meta-event (skipped, just accumulate time)
162 continue;
165 ev_type = EventTypeMap::instance().midi_event_type(ev_buffer[0]);
167 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked delta %1, time %2, buf[0] %3, type %4\n",
168 ev_delta_t, time, ev_buffer[0], ev_type));
170 assert(time >= start_ticks);
171 const sframes_t ev_frame_time = converter.to(time / (double)ppqn()) + stamp_offset;
173 #if 1
174 cerr << " frames = " << ev_frame_time
175 << " w/offset = " << ev_frame_time - negative_stamp_offset
176 << endl;
177 #endif
179 if (ev_frame_time < start + duration) {
180 destination.write(ev_frame_time - negative_stamp_offset, ev_type, ev_size, ev_buffer);
182 if (tracker) {
183 if (ev_buffer[0] & MIDI_CMD_NOTE_ON) {
184 tracker->add (ev_buffer[1], ev_buffer[0] & 0xf);
185 } else if (ev_buffer[0] & MIDI_CMD_NOTE_OFF) {
186 tracker->remove (ev_buffer[1], ev_buffer[0] & 0xf);
189 } else {
190 break;
193 _read_data_count += ev_size;
195 if (ev_size > scratch_size) {
196 scratch_size = ev_size;
198 ev_size = scratch_size; // ensure read_event only allocates if necessary
201 return duration;
204 /** All stamps in audio frames */
205 nframes_t
206 SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position, nframes_t duration)
208 _write_data_count = 0;
210 nframes_t time;
211 Evoral::EventType type;
212 uint32_t size;
214 size_t buf_capacity = 4;
215 uint8_t* buf = (uint8_t*)malloc(buf_capacity);
217 if (_model && ! _model->writing()) {
218 _model->start_write();
221 Evoral::MIDIEvent<nframes_t> ev;
223 while (true) {
224 bool ret = source.peek_time(&time);
225 if (!ret || time > _last_write_end + duration) {
226 break;
229 ret = source.read_prefix(&time, &type, &size);
230 if (!ret) {
231 cerr << "ERROR: Unable to read event prefix, corrupt MIDI ring buffer" << endl;
232 break;
235 if (size > buf_capacity) {
236 buf_capacity = size;
237 buf = (uint8_t*)realloc(buf, size);
240 ret = source.read_contents(size, buf);
241 if (!ret) {
242 cerr << "ERROR: Read time/size but not buffer, corrupt MIDI ring buffer" << endl;
243 break;
246 assert(time >= position);
247 time -= position;
249 ev.set(buf, size, time);
250 ev.set_event_type(EventTypeMap::instance().midi_event_type(ev.buffer()[0]));
251 if (!(ev.is_channel_event() || ev.is_smf_meta_event() || ev.is_sysex())) {
252 /*cerr << "SMFSource: WARNING: caller tried to write non SMF-Event of type "
253 << std::hex << int(ev.buffer()[0]) << endl;*/
254 continue;
257 append_event_unlocked_frames(ev, position);
260 if (_model) {
261 set_default_controls_interpolation();
264 Evoral::SMF::flush();
265 free(buf);
267 ViewDataRangeReady(position + _last_write_end, duration); /* EMIT SIGNAL */
269 return duration;
273 /** Append an event with a timestamp in beats (double) */
274 void
275 SMFSource::append_event_unlocked_beats (const Evoral::Event<double>& ev)
277 assert(_writing);
278 if (ev.size() == 0) {
279 return;
282 /*printf("SMFSource: %s - append_event_unlocked_beats time = %lf, size = %u, data = ",
283 name().c_str(), ev.time(), ev.size());
284 for (size_t i = 0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");*/
286 assert(ev.time() >= 0);
287 if (ev.time() < _last_ev_time_beats) {
288 cerr << "SMFSource: Warning: Skipping event with non-monotonic time" << endl;
289 return;
292 _length_beats = max(_length_beats, ev.time());
294 const double delta_time_beats = ev.time() - _last_ev_time_beats;
295 const uint32_t delta_time_ticks = (uint32_t)lrint(delta_time_beats * (double)ppqn());
297 Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer());
298 _last_ev_time_beats = ev.time();
300 _write_data_count += ev.size();
302 if (_model) {
303 _model->append (ev);
307 /** Append an event with a timestamp in frames (nframes_t) */
308 void
309 SMFSource::append_event_unlocked_frames (const Evoral::Event<nframes_t>& ev, sframes_t position)
311 assert(_writing);
312 if (ev.size() == 0) {
313 return;
316 /*printf("SMFSource: %s - append_event_unlocked_frames time = %u, size = %u, data = ",
317 name().c_str(), ev.time(), ev.size());
318 for (size_t i=0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");*/
320 if (ev.time() < _last_ev_time_frames) {
321 cerr << "SMFSource: Warning: Skipping event with non-monotonic time" << endl;
322 return;
325 BeatsFramesConverter converter(_session.tempo_map(), position);
327 _length_beats = max(_length_beats, converter.from(ev.time()));
329 const sframes_t delta_time_frames = ev.time() - _last_ev_time_frames;
330 const double delta_time_beats = converter.from(delta_time_frames);
331 const uint32_t delta_time_ticks = (uint32_t)(lrint(delta_time_beats * (double)ppqn()));
333 Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer());
334 _last_ev_time_frames = ev.time();
336 _write_data_count += ev.size();
338 if (_model) {
339 const double ev_time_beats = converter.from(ev.time());
340 const Evoral::Event<double> beat_ev(
341 ev.event_type(), ev_time_beats, ev.size(), (uint8_t*)ev.buffer());
342 _model->append (beat_ev);
346 XMLNode&
347 SMFSource::get_state ()
349 return MidiSource::get_state();
353 SMFSource::set_state (const XMLNode& node, int version)
355 if (Source::set_state (node, version)) {
356 return -1;
359 if (MidiSource::set_state (node, version)) {
360 return -1;
363 if (FileSource::set_state (node, version)) {
364 return -1;
367 return 0;
370 void
371 SMFSource::mark_streaming_midi_write_started (NoteMode mode, sframes_t start_frame)
373 Glib::Mutex::Lock lm (_lock);
374 MidiSource::mark_streaming_midi_write_started (mode, start_frame);
375 Evoral::SMF::begin_write ();
376 _last_ev_time_beats = 0.0;
377 _last_ev_time_frames = 0;
380 void
381 SMFSource::mark_streaming_write_completed ()
383 Glib::Mutex::Lock lm (_lock);
384 MidiSource::mark_streaming_write_completed();
386 if (!writable()) {
387 cerr << "\n\n\n[[[[[[[[[ This SMFS is not writable! ]]]]]]]]]]]\n\n\n";
388 return;
391 _model->set_edited(false);
392 Evoral::SMF::end_write ();
395 bool
396 SMFSource::safe_midi_file_extension (const Glib::ustring& file)
398 return (file.rfind(".mid") != Glib::ustring::npos);
401 void
402 SMFSource::load_model (bool lock, bool force_reload)
404 if (_writing) {
405 return;
408 boost::shared_ptr<Glib::Mutex::Lock> lm;
409 if (lock)
410 lm = boost::shared_ptr<Glib::Mutex::Lock>(new Glib::Mutex::Lock(_lock));
412 if (_model && !force_reload) {
413 return;
416 if (! _model) {
417 _model = boost::shared_ptr<MidiModel>(new MidiModel(this));
418 //cerr << _name << " loaded new model " << _model.get() << endl;
419 } else {
420 /*cerr << _name << " reloading model " << _model.get()
421 << " (" << _model->n_notes() << " notes)" << endl;*/
422 _model->clear();
425 _model->start_write();
426 Evoral::SMF::seek_to_start();
428 uint64_t time = 0; /* in SMF ticks */
429 Evoral::Event<double> ev;
431 size_t scratch_size = 0; // keep track of scratch and minimize reallocs
433 uint32_t delta_t = 0;
434 uint32_t size = 0;
435 uint8_t* buf = NULL;
436 int ret;
437 while ((ret = read_event(&delta_t, &size, &buf)) >= 0) {
438 time += delta_t;
439 ev.set(buf, size, time / (double)ppqn());
441 if (ret > 0) { // didn't skip (meta) event
442 ev.set_event_type(EventTypeMap::instance().midi_event_type(buf[0]));
444 #ifndef NDEBUG
445 std::string ss;
447 for (uint32_t xx = 0; xx < size; ++xx) {
448 char b[8];
449 snprintf (b, sizeof (b), "0x%x ", buf[xx]);
450 ss += b;
453 DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF %6 load model delta %1, time %2, size %3 buf %4, type %5\n",
454 delta_t, time, size, ss , ev.event_type(), name()));
455 #endif
457 _model->append (ev);
460 if (ev.size() > scratch_size) {
461 scratch_size = ev.size();
463 ev.size() = scratch_size; // ensure read_event only allocates if necessary
465 _length_beats = max(_length_beats, ev.time());
468 set_default_controls_interpolation();
470 _model->end_write(false);
471 _model->set_edited(false);
473 _model_iter = _model->begin();
475 free(buf);
478 void
479 SMFSource::set_default_controls_interpolation ()
481 // set interpolation style to defaults, can be changed by the GUI later
482 Evoral::ControlSet::Controls controls = _model->controls();
483 for (Evoral::ControlSet::Controls::iterator c = controls.begin(); c != controls.end(); ++c) {
484 (*c).second->list()->set_interpolation(
485 EventTypeMap::instance().interpolation_of((*c).first));
490 void
491 SMFSource::destroy_model ()
493 //cerr << _name << " destroying model " << _model.get() << endl;
494 _model.reset();
497 void
498 SMFSource::flush_midi ()
500 if (!writable()) {
501 cerr << "\n\n\n\n " << name() << " CANNOT FLUSH - not writable\n\n\n\n";
502 return;
505 Evoral::SMF::end_write();