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.
30 #include <glibmm/fileutils.h>
31 #include <glibmm/miscutils.h>
33 #include "pbd/xml++.h"
34 #include "pbd/pthread_utils.h"
35 #include "pbd/basename.h"
37 #include "evoral/EventSink.hpp"
39 #include "ardour/debug.h"
40 #include "ardour/midi_model.h"
41 #include "ardour/midi_state_tracker.h"
42 #include "ardour/midi_source.h"
43 #include "ardour/file_source.h"
44 #include "ardour/session.h"
45 #include "ardour/session_directory.h"
46 #include "ardour/source_factory.h"
50 namespace ARDOUR { template <typename T> class MidiRingBuffer; }
53 using namespace ARDOUR;
56 PBD::Signal1<void,MidiSource*> MidiSource::MidiSourceCreated;
58 MidiSource::MidiSource (Session& s, string name, Source::Flag flags)
59 : Source(s, DataType::MIDI, name, flags)
61 , _model_iter_valid(false)
65 , _capture_loop_length(0)
69 MidiSource::MidiSource (Session& s, const XMLNode& node)
72 , _model_iter_valid(false)
76 , _capture_loop_length(0)
78 if (set_state (node, Stateful::loading_state_version)) {
79 throw failed_constructor();
83 MidiSource::~MidiSource ()
88 MidiSource::get_state ()
90 XMLNode& node (Source::get_state());
92 if (_captured_for.length()) {
93 node.add_property ("captured-for", _captured_for);
96 for (InterpolationStyleMap::const_iterator i = _interpolation_style.begin(); i != _interpolation_style.end(); ++i) {
97 XMLNode* child = node.add_child (X_("InterpolationStyle"));
98 child->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (i->first));
99 child->add_property (X_("style"), enum_2_string (i->second));
102 for (AutomationStateMap::const_iterator i = _automation_state.begin(); i != _automation_state.end(); ++i) {
103 XMLNode* child = node.add_child (X_("AutomationState"));
104 child->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (i->first));
105 child->add_property (X_("state"), enum_2_string (i->second));
112 MidiSource::set_state (const XMLNode& node, int /*version*/)
114 const XMLProperty* prop;
115 if ((prop = node.property ("captured-for")) != 0) {
116 _captured_for = prop->value();
119 XMLNodeList children = node.children ();
120 for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
121 if ((*i)->name() == X_("InterpolationStyle")) {
122 if ((prop = (*i)->property (X_("parameter"))) == 0) {
123 error << _("Missing parameter property on InterpolationStyle") << endmsg;
126 Evoral::Parameter p = EventTypeMap::instance().from_symbol (prop->value());
128 if ((prop = (*i)->property (X_("style"))) == 0) {
129 error << _("Missing style property on InterpolationStyle") << endmsg;
132 Evoral::ControlList::InterpolationStyle s = static_cast<Evoral::ControlList::InterpolationStyle>(
133 string_2_enum (prop->value(), s));
134 set_interpolation_of (p, s);
136 } else if ((*i)->name() == X_("AutomationState")) {
137 if ((prop = (*i)->property (X_("parameter"))) == 0) {
138 error << _("Missing parameter property on AutomationState") << endmsg;
141 Evoral::Parameter p = EventTypeMap::instance().from_symbol (prop->value());
143 if ((prop = (*i)->property (X_("state"))) == 0) {
144 error << _("Missing state property on AutomationState") << endmsg;
147 AutoState s = static_cast<AutoState> (string_2_enum (prop->value(), s));
148 set_automation_state_of (p, s);
156 MidiSource::empty () const
158 return !_length_beats;
162 MidiSource::length (framepos_t pos) const
164 if (!_length_beats) {
168 BeatsFramesConverter converter(_session.tempo_map(), pos);
169 return converter.to(_length_beats);
173 MidiSource::update_length (framecnt_t)
175 // You're not the boss of me!
179 MidiSource::invalidate ()
181 _model_iter_valid = false;
182 _model_iter.invalidate();
186 MidiSource::midi_read (Evoral::EventSink<framepos_t>& dst,
187 framepos_t source_start,
190 MidiStateTracker* tracker,
191 const std::set<Evoral::Parameter>& filtered) const
193 Glib::Threads::Mutex::Lock lm (_lock);
195 BeatsFramesConverter converter(_session.tempo_map(), source_start);
197 DEBUG_TRACE (DEBUG::MidiSourceIO,
198 string_compose ("MidiSource::midi_read() %5 sstart %1 start %2 cnt %3 tracker %4\n",
199 source_start, start, cnt, tracker, name()));
202 // Find appropriate model iterator
203 Evoral::Sequence<Evoral::MusicalTime>::const_iterator& i = _model_iter;
204 if (_last_read_end == 0 || start != _last_read_end || !_model_iter_valid) {
205 // Cached iterator is invalid, search for the first event past start
206 i = _model->begin(converter.from(start), false, filtered);
207 _model_iter_valid = true;
210 _last_read_end = start + cnt;
212 // Copy events in [start, start + cnt) into dst
213 for (; i != _model->end(); ++i) {
214 const framecnt_t time_frames = converter.to(i->time());
215 if (time_frames < start + cnt) {
216 // Offset by source start to convert event time to session time
217 dst.write (time_frames + source_start, i->event_type(), i->size(), i->buffer());
219 DEBUG_TRACE (DEBUG::MidiSourceIO,
220 string_compose ("%1: add event @ %2 type %3 size %4\n",
221 _name, time_frames + source_start, i->event_type(), i->size()));
227 DEBUG_TRACE (DEBUG::MidiSourceIO,
228 string_compose ("%1: reached end with event @ %2 vs. %3\n",
229 _name, time_frames, start+cnt));
235 return read_unlocked (dst, source_start, start, cnt, tracker);
240 MidiSource::midi_write (MidiRingBuffer<framepos_t>& source,
241 framepos_t source_start,
244 Glib::Threads::Mutex::Lock lm (_lock);
246 const framecnt_t ret = write_unlocked (source, source_start, cnt);
248 if (cnt == max_framecnt) {
252 _capture_length += cnt;
259 MidiSource::mark_streaming_midi_write_started (NoteMode mode)
262 _model->set_note_mode (mode);
263 _model->start_write ();
270 MidiSource::mark_write_starting_now (framecnt_t position,
271 framecnt_t capture_length,
272 framecnt_t loop_length)
274 /* I'm not sure if this is the best way to approach this, but
275 _capture_length needs to be set up with the transport frame
276 when a record actually starts, as it is used by
277 SMFSource::write_unlocked to decide whether incoming notes
278 are within the correct time range.
279 mark_streaming_midi_write_started (perhaps a more logical
280 place to do this) is not called at exactly the time when
281 record starts, and I don't think it necessarily can be
282 because it is not RT-safe.
285 set_timeline_position(position);
286 _capture_length = capture_length;
287 _capture_loop_length = loop_length;
289 BeatsFramesConverter converter(_session.tempo_map(), position);
290 _length_beats = converter.from(capture_length);
294 MidiSource::mark_streaming_write_started ()
296 NoteMode note_mode = _model ? _model->note_mode() : Sustained;
297 mark_streaming_midi_write_started (note_mode);
301 MidiSource::mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::MusicalTime>::StuckNoteOption option,
302 Evoral::MusicalTime end)
305 _model->end_write (option, end);
312 MidiSource::mark_streaming_write_completed ()
314 mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::MusicalTime>::DeleteStuckNotes);
318 MidiSource::write_to (boost::shared_ptr<MidiSource> newsrc, Evoral::MusicalTime begin, Evoral::MusicalTime end)
320 newsrc->set_timeline_position (_timeline_position);
321 newsrc->copy_interpolation_from (this);
322 newsrc->copy_automation_state_from (this);
325 if (begin == Evoral::MinMusicalTime && end == Evoral::MaxMusicalTime) {
326 _model->write_to (newsrc);
328 _model->write_section_to (newsrc, begin, end);
331 error << string_compose (_("programming error: %1"), X_("no model for MidiSource during ::clone()"));
335 newsrc->flush_midi();
337 /* force a reload of the model if the range is partial */
339 if (begin != Evoral::MinMusicalTime || end != Evoral::MaxMusicalTime) {
340 newsrc->load_model (true, true);
342 newsrc->set_model (_model);
345 /* this file is not removable (but since it is MIDI, it is mutable) */
347 boost::dynamic_pointer_cast<FileSource> (newsrc)->prevent_deletion ();
353 MidiSource::session_saved()
355 /* this writes a copy of the data to disk.
356 XXX do we need to do this every time?
359 if (_model && _model->edited()) {
360 /* The model is edited, write its contents into the current source
361 file (overwiting previous contents). */
363 /* Temporarily drop our reference to the model so that as the model
364 pushes its current state to us, we don't try to update it. */
365 boost::shared_ptr<MidiModel> mm = _model;
368 /* Flush model contents to disk. */
369 mm->sync_to_source ();
371 /* Reacquire model. */
380 MidiSource::set_note_mode(NoteMode mode)
383 _model->set_note_mode(mode);
388 MidiSource::drop_model ()
391 ModelChanged (); /* EMIT SIGNAL */
395 MidiSource::set_model (boost::shared_ptr<MidiModel> m)
398 ModelChanged (); /* EMIT SIGNAL */
401 Evoral::ControlList::InterpolationStyle
402 MidiSource::interpolation_of (Evoral::Parameter p) const
404 InterpolationStyleMap::const_iterator i = _interpolation_style.find (p);
405 if (i == _interpolation_style.end()) {
406 return EventTypeMap::instance().interpolation_of (p);
413 MidiSource::automation_state_of (Evoral::Parameter p) const
415 AutomationStateMap::const_iterator i = _automation_state.find (p);
416 if (i == _automation_state.end()) {
417 /* default to `play', otherwise if MIDI is recorded /
418 imported with controllers etc. they are by default
419 not played back, which is a little surprising.
427 /** Set interpolation style to be used for a given parameter. This change will be
428 * propagated to anyone who needs to know.
431 MidiSource::set_interpolation_of (Evoral::Parameter p, Evoral::ControlList::InterpolationStyle s)
433 if (interpolation_of (p) == s) {
437 if (EventTypeMap::instance().interpolation_of (p) == s) {
438 /* interpolation type is being set to the default, so we don't need a note in our map */
439 _interpolation_style.erase (p);
441 _interpolation_style[p] = s;
444 InterpolationChanged (p, s); /* EMIT SIGNAL */
448 MidiSource::set_automation_state_of (Evoral::Parameter p, AutoState s)
450 if (automation_state_of (p) == s) {
455 /* automation state is being set to the default, so we don't need a note in our map */
456 _automation_state.erase (p);
458 _automation_state[p] = s;
461 AutomationStateChanged (p, s); /* EMIT SIGNAL */
465 MidiSource::copy_interpolation_from (boost::shared_ptr<MidiSource> s)
467 copy_interpolation_from (s.get ());
471 MidiSource::copy_automation_state_from (boost::shared_ptr<MidiSource> s)
473 copy_automation_state_from (s.get ());
477 MidiSource::copy_interpolation_from (MidiSource* s)
479 _interpolation_style = s->_interpolation_style;
481 /* XXX: should probably emit signals here */
485 MidiSource::copy_automation_state_from (MidiSource* s)
487 _automation_state = s->_automation_state;
489 /* XXX: should probably emit signals here */