a few more if (_mmc) tests
[ardour.git] / libs / ardour / smf_source.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3         Written by Dave Robillard, 2006
4
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.
9
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.
14
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.
18
19 */
20
21 #include <vector>
22
23 #include <sys/time.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27
28 #include "pbd/mountpoint.h"
29 #include "pbd/pathscanner.h"
30 #include "pbd/stl_delete.h"
31 #include "pbd/strsplit.h"
32
33 #include <glibmm/miscutils.h>
34
35 #include "evoral/Control.hpp"
36
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"
45
46 #include "i18n.h"
47
48 using namespace ARDOUR;
49 using namespace Glib;
50 using namespace PBD;
51
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)
62 {
63         if (init(_path, false)) {
64                 throw failed_constructor ();
65         }
66
67         if (create(path)) {
68                 throw failed_constructor ();
69         }
70
71         load_model(true, true); // FIXME
72 }
73
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)
83 {
84         if (set_state(node, Stateful::loading_state_version)) {
85                 throw failed_constructor ();
86         }
87
88         if (init(_path, true)) {
89                 throw failed_constructor ();
90         }
91
92         if (open(_path)) {
93                 throw failed_constructor ();
94         }
95
96         load_model(true, true); // FIXME
97 }
98
99 SMFSource::~SMFSource ()
100 {
101         if (removable()) {
102                 cerr << name() << " is removable, empty ? " << empty() << " UC " << use_count() << endl;
103                 unlink (_path.c_str());
104         }
105 }
106
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
113 {
114         int      ret  = 0;
115         uint64_t time = 0; // in SMF ticks, 1 tick per _ppqn
116
117         DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF read_unlocked: start %1 duration %2\n", start, duration));
118
119         _read_data_count = 0;
120
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;
126
127         size_t scratch_size = 0; // keep track of scratch to minimize reallocs
128
129         BeatsFramesConverter converter(_session.tempo_map(), source_start);
130
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));
133
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;
142                         }
143                         time += ev_delta_t; // accumulate delta time
144                 }
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;
148         }
149
150         _smf_last_read_end = start + duration;
151
152         while (true) {
153                 ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
154                 if (ret == -1) { // EOF
155                         break;
156                 }
157
158                 time += ev_delta_t; // accumulate delta time
159                 _smf_last_read_time = time;
160
161                 if (ret == 0) { // meta-event (skipped, just accumulate time)
162                         continue;
163                 }
164
165                 ev_type = EventTypeMap::instance().midi_event_type(ev_buffer[0]);
166
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));
169
170                 assert(time >= start_ticks);
171                 const sframes_t ev_frame_time = converter.to(time / (double)ppqn()) + stamp_offset;
172
173 #if 1
174                 cerr << " frames = " << ev_frame_time
175                      << " w/offset = " << ev_frame_time - negative_stamp_offset
176                      << endl;
177 #endif
178
179                 if (ev_frame_time < start + duration) {
180                         destination.write(ev_frame_time - negative_stamp_offset, ev_type, ev_size, ev_buffer);
181
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);
187                                 }
188                         }
189                 } else {
190                         break;
191                 }
192
193                 _read_data_count += ev_size;
194
195                 if (ev_size > scratch_size) {
196                         scratch_size = ev_size;
197                 }
198                 ev_size = scratch_size; // ensure read_event only allocates if necessary
199         }
200
201         return duration;
202 }
203
204 /** All stamps in audio frames */
205 nframes_t
206 SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& source, sframes_t position, nframes_t duration)
207 {
208         _write_data_count = 0;
209
210         nframes_t         time;
211         Evoral::EventType type;
212         uint32_t          size;
213
214         size_t   buf_capacity = 4;
215         uint8_t* buf          = (uint8_t*)malloc(buf_capacity);
216
217         if (_model && ! _model->writing()) {
218                 _model->start_write();
219         }
220
221         Evoral::MIDIEvent<nframes_t> ev;
222
223         while (true) {
224                 bool ret = source.peek_time(&time);
225                 if (!ret || time > _last_write_end + duration) {
226                         break;
227                 }
228
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;
233                 }
234
235                 if (size > buf_capacity) {
236                         buf_capacity = size;
237                         buf = (uint8_t*)realloc(buf, size);
238                 }
239
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;
244                 }
245
246                 assert(time >= position);
247                 time -= position;
248
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;
255                 }
256
257                 append_event_unlocked_frames(ev, position);
258         }
259
260         if (_model) {
261                 set_default_controls_interpolation();
262         }
263
264         Evoral::SMF::flush();
265         free(buf);
266
267         ViewDataRangeReady(position + _last_write_end, duration); /* EMIT SIGNAL */
268
269         return duration;
270 }
271
272
273 /** Append an event with a timestamp in beats (double) */
274 void
275 SMFSource::append_event_unlocked_beats (const Evoral::Event<double>& ev)
276 {
277         assert(_writing);
278         if (ev.size() == 0)  {
279                 return;
280         }
281
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");*/
285
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;
290         }
291
292         _length_beats = max(_length_beats, ev.time());
293
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());
296
297         Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer());
298         _last_ev_time_beats = ev.time();
299
300         _write_data_count += ev.size();
301
302         if (_model) {
303                 _model->append (ev);
304         }
305 }
306
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)
310 {
311         assert(_writing);
312         if (ev.size() == 0)  {
313                 return;
314         }
315
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");*/
319
320         if (ev.time() < _last_ev_time_frames) {
321                 cerr << "SMFSource: Warning: Skipping event with non-monotonic time" << endl;
322                 return;
323         }
324
325         BeatsFramesConverter converter(_session.tempo_map(), position);
326
327         _length_beats = max(_length_beats, converter.from(ev.time()));
328
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()));
332
333         Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer());
334         _last_ev_time_frames = ev.time();
335
336         _write_data_count += ev.size();
337
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);
343         }
344 }
345
346 XMLNode&
347 SMFSource::get_state ()
348 {
349         return MidiSource::get_state();
350 }
351
352 int
353 SMFSource::set_state (const XMLNode& node, int version)
354 {
355         if (Source::set_state (node, version)) {
356                 return -1;
357         }
358
359         if (MidiSource::set_state (node, version)) {
360                 return -1;
361         }
362
363         if (FileSource::set_state (node, version)) {
364                 return -1;
365         }
366
367         return 0;
368 }
369
370 void
371 SMFSource::mark_streaming_midi_write_started (NoteMode mode, sframes_t start_frame)
372 {
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;
378 }
379
380 void
381 SMFSource::mark_streaming_write_completed ()
382 {
383         Glib::Mutex::Lock lm (_lock);
384         MidiSource::mark_streaming_write_completed();
385
386         if (!writable()) {
387                 return;
388         }
389
390         if (_model) {
391                 _model->set_edited(false);
392         }
393         
394         Evoral::SMF::end_write ();
395
396         /* data in the file now, not removable */
397
398         mark_nonremovable (); 
399 }
400
401 bool
402 SMFSource::safe_midi_file_extension (const Glib::ustring& file)
403 {
404         return (file.rfind(".mid") != Glib::ustring::npos);
405 }
406
407 void
408 SMFSource::load_model (bool lock, bool force_reload)
409 {
410         if (_writing) {
411                 return;
412         }
413
414         boost::shared_ptr<Glib::Mutex::Lock> lm;
415         if (lock)
416                 lm = boost::shared_ptr<Glib::Mutex::Lock>(new Glib::Mutex::Lock(_lock));
417
418         if (_model && !force_reload) {
419                 return;
420         }
421
422         if (! _model) {
423                 _model = boost::shared_ptr<MidiModel>(new MidiModel(this));
424                 //cerr << _name << " loaded new model " << _model.get() << endl;
425         } else {
426                 /*cerr << _name << " reloading model " << _model.get()
427                         << " (" << _model->n_notes() << " notes)" << endl;*/
428                 _model->clear();
429         }
430
431         _model->start_write();
432         Evoral::SMF::seek_to_start();
433
434         uint64_t time = 0; /* in SMF ticks */
435         Evoral::Event<double> ev;
436
437         size_t scratch_size = 0; // keep track of scratch and minimize reallocs
438
439         uint32_t delta_t = 0;
440         uint32_t size    = 0;
441         uint8_t* buf     = NULL;
442         int ret;
443         while ((ret = read_event(&delta_t, &size, &buf)) >= 0) {
444                 time += delta_t;
445                 ev.set(buf, size, time / (double)ppqn());
446
447                 if (ret > 0) { // didn't skip (meta) event
448                         ev.set_event_type(EventTypeMap::instance().midi_event_type(buf[0]));
449
450 #ifndef NDEBUG
451                         std::string ss;
452                         
453                         for (uint32_t xx = 0; xx < size; ++xx) {
454                                 char b[8];
455                                 snprintf (b, sizeof (b), "0x%x ", buf[xx]);
456                                 ss += b;
457                         }
458
459                         DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("SMF %6 load model delta %1, time %2, size %3 buf %4, type %5\n",
460                                                                           delta_t, time, size, ss , ev.event_type(), name()));
461 #endif
462                         
463                         _model->append (ev);
464                 }
465
466                 if (ev.size() > scratch_size) {
467                         scratch_size = ev.size();
468                 }
469                 ev.size() = scratch_size; // ensure read_event only allocates if necessary
470
471                 _length_beats = max(_length_beats, ev.time());
472         }
473
474         set_default_controls_interpolation();
475
476         _model->end_write(false);
477         _model->set_edited(false);
478
479         _model_iter = _model->begin();
480
481         free(buf);
482 }
483
484 void
485 SMFSource::set_default_controls_interpolation ()
486 {
487         // set interpolation style to defaults, can be changed by the GUI later
488         Evoral::ControlSet::Controls controls = _model->controls();
489         for (Evoral::ControlSet::Controls::iterator c = controls.begin(); c != controls.end(); ++c) {
490                 (*c).second->list()->set_interpolation(
491                         EventTypeMap::instance().interpolation_of((*c).first));
492         }
493 }
494
495
496 void
497 SMFSource::destroy_model ()
498 {
499         //cerr << _name << " destroying model " << _model.get() << endl;
500         _model.reset();
501 }
502
503 void
504 SMFSource::flush_midi ()
505 {
506         if (!writable()) {
507                 return;
508         }
509
510         Evoral::SMF::end_write();
511         /* data in the file means its no longer removable */
512         mark_nonremovable (); 
513 }
514