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.
27 #include "pbd/error.h"
29 #include "evoral/EventList.hpp"
31 #include "ardour/configuration.h"
32 #include "ardour/debug.h"
33 #include "ardour/midi_model.h"
34 #include "ardour/midi_playlist.h"
35 #include "ardour/midi_region.h"
36 #include "ardour/midi_ring_buffer.h"
37 #include "ardour/session.h"
38 #include "ardour/types.h"
42 using namespace ARDOUR;
46 MidiPlaylist::MidiPlaylist (Session& session, const XMLNode& node, bool hidden)
47 : Playlist (session, node, DataType::MIDI, hidden)
48 , _note_mode(Sustained)
51 const XMLProperty* prop = node.property("type");
52 assert(prop && DataType(prop->value()) == DataType::MIDI);
56 if (set_state (node, Stateful::loading_state_version)) {
57 throw failed_constructor ();
62 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
63 : Playlist (session, name, DataType::MIDI, hidden)
64 , _note_mode(Sustained)
68 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, string name, bool hidden)
69 : Playlist (other, name, hidden)
70 , _note_mode(other->_note_mode)
74 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, framepos_t start, framecnt_t dur, string name, bool hidden)
75 : Playlist (other, start, dur, name, hidden)
76 , _note_mode(other->_note_mode)
78 /* this constructor does NOT notify others (session) */
81 MidiPlaylist::~MidiPlaylist ()
85 template<typename Time>
86 struct EventsSortByTimeAndType {
87 bool operator() (Evoral::Event<Time>* a, Evoral::Event<Time>* b) {
88 if (a->time() == b->time()) {
89 if (EventTypeMap::instance().type_is_midi (a->event_type()) && EventTypeMap::instance().type_is_midi (b->event_type())) {
90 /* negate return value since we must return whether
91 * or not a should sort before b, not b before a
93 return !MidiBuffer::second_simultaneous_midi_byte_is_first (a->buffer()[0], b->buffer()[0]);
96 return a->time() < b->time();
100 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
102 MidiPlaylist::read (Evoral::EventSink<framepos_t>& dst, framepos_t start, framecnt_t dur, unsigned chan_n)
104 /* this function is never called from a realtime thread, so
105 its OK to block (for short intervals).
108 Glib::RecMutex::Lock rm (region_lock);
109 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("++++++ %1 .. %2 +++++++ %3 trackers +++++++++++++++++\n",
110 start, start + dur, _note_trackers.size()));
112 framepos_t end = start + dur - 1;
114 // relevent regions overlapping start <--> end
115 vector< boost::shared_ptr<Region> > regs;
116 vector< boost::shared_ptr<Region> > ended;
117 typedef pair<MidiStateTracker*,framepos_t> TrackerInfo;
118 vector<TrackerInfo> tracker_info;
119 NoteTrackers::iterator t;
121 for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
123 /* in this call to coverage, the return value indicates the
124 * overlap status of the read range (start...end) WRT to
128 switch ((*i)->coverage (start, end)) {
130 case OverlapInternal:
131 case OverlapExternal:
136 /* this region ends within the read range */
138 ended.push_back (*i);
146 if (regs.size() == 1 &&
147 (ended.empty() || (ended.size() == 1 && ended.front() == regs.front()))) {
149 /* just a single region - read directly into dst */
151 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("Single region (%1) read, ended during this read %2\n", regs.front()->name(),
154 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(regs.front());
158 NoteTrackers::iterator t = _note_trackers.find (mr.get());
159 MidiStateTracker* tracker;
160 bool new_tracker = false;
162 if (t == _note_trackers.end()) {
163 tracker = new MidiStateTracker;
165 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tBEFORE: new tracker\n");
168 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tBEFORE: tracker says there are %1 on notes\n", tracker->on()));
171 mr->read_at (dst, start, dur, chan_n, _note_mode, tracker);
172 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tAFTER: tracker says there are %1 on notes\n", tracker->on()));
174 if (!ended.empty()) {
175 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1 ended in this read, resolve notes and delete (%2) tracker\n",
176 mr->name(), ((new_tracker) ? "new" : "old")));
177 tracker->resolve_notes (dst, mr->last_frame());
180 _note_trackers.erase (t);
184 pair<Region*,MidiStateTracker*> newpair;
185 newpair.first = mr.get();
186 newpair.second = tracker;
187 _note_trackers.insert (newpair);
188 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tadded tracker to trackers\n");
195 /* multiple regions and/or note resolution: sort by layer, read into a temporary non-monotonically
196 sorted EventSink, sort and then insert into dst.
199 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("%1 regions to read, plus %2 trackers\n", regs.size(), tracker_info.size()));
201 Evoral::EventList<framepos_t> evlist;
203 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("for %1 .. %2 we have %3 to consider\n", start, start+dur-1, regs.size()));
205 for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
207 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
213 NoteTrackers::iterator t = _note_trackers.find (mr.get());
214 MidiStateTracker* tracker;
215 bool new_tracker = false;
217 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("Before %1 (%2 .. %3) we now have %4 events\n", mr->name(), mr->position(), mr->last_frame(), evlist.size()));
219 if (t == _note_trackers.end()) {
220 tracker = new MidiStateTracker;
222 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tBEFORE: new tracker\n");
225 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tBEFORE: tracker says there are %1 on notes\n", tracker->on()));
229 mr->read_at (evlist, start, dur, chan_n, _note_mode, tracker);
232 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("After %1 (%2 .. %3) we now have %4\n", mr->name(), mr->position(), mr->last_frame(), evlist.size()));
233 for (Evoral::EventList<framepos_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
234 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1\n", **x));
236 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tAFTER: tracker says there are %1 on notes\n", tracker->on()));
238 if (find (ended.begin(), ended.end(), *i) != ended.end()) {
240 /* the region ended within the read range, so
241 * resolve any dangling notes (i.e. notes whose
242 * end is beyond the end of the region).
245 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1 ended in this read, resolve notes and delete (%2) tracker\n",
246 mr->name(), ((new_tracker) ? "new" : "old")));
248 tracker->resolve_notes (evlist, (*i)->last_frame());
251 _note_trackers.erase (t);
257 pair<Region*,MidiStateTracker*> newpair;
258 newpair.first = mr.get();
259 newpair.second = tracker;
260 _note_trackers.insert (newpair).first;
261 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tadded tracker to trackers\n");
266 if (!evlist.empty()) {
268 /* sort the event list */
269 EventsSortByTimeAndType<framepos_t> cmp;
273 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("Final we now have %1 events\n", evlist.size()));
274 for (Evoral::EventList<framepos_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
275 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1\n", **x));
279 for (Evoral::EventList<framepos_t>::iterator e = evlist.begin(); e != evlist.end(); ++e) {
280 Evoral::Event<framepos_t>* ev (*e);
281 dst.write (ev->time(), ev->event_type(), ev->size(), ev->buffer());
288 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "-------------------------------------------------------------\n");
293 MidiPlaylist::clear_note_trackers ()
295 Glib::RecMutex::Lock rm (region_lock);
296 for (NoteTrackers::iterator n = _note_trackers.begin(); n != _note_trackers.end(); ++n) {
299 DEBUG_TRACE (DEBUG::MidiTrackers, string_compose ("%1 clears all note trackers\n", name()));
300 _note_trackers.clear ();
304 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
306 /* MIDI regions have no dependents (crossfades) but we might be tracking notes */
307 NoteTrackers::iterator t = _note_trackers.find (region.get());
309 /* GACK! THREAD SAFETY! */
311 if (t != _note_trackers.end()) {
313 _note_trackers.erase (t);
319 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> /*r*/)
321 /* MIDI regions have no dependents (crossfades) */
325 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> /*original*/, boost::shared_ptr<Region> /*left*/, boost::shared_ptr<Region> /*right*/)
327 /* No MIDI crossfading (yet?), so nothing to do here */
331 MidiPlaylist::check_dependents (boost::shared_ptr<Region> /*r*/, bool /*norefresh*/)
333 /* MIDI regions have no dependents (crossfades) */
338 MidiPlaylist::set_state (const XMLNode& node, int version)
343 if (Playlist::set_state (node, version)) {
354 MidiPlaylist::dump () const
356 boost::shared_ptr<Region> r;
358 cerr << "Playlist \"" << _name << "\" " << endl
359 << regions.size() << " regions "
362 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
364 cerr << " " << r->name() << " @ " << r << " ["
365 << r->start() << "+" << r->length()
375 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
377 boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
383 bool changed = false;
386 RegionLock rlock (this);
387 RegionList::iterator i;
388 RegionList::iterator tmp;
390 for (i = regions.begin(); i != regions.end(); ) {
395 if ((*i) == region) {
406 /* overload this, it normally means "removed", not destroyed */
407 notify_region_removed (region);
413 set<Evoral::Parameter>
414 MidiPlaylist::contained_automation()
416 /* this function is never called from a realtime thread, so
417 its OK to block (for short intervals).
420 Glib::RecMutex::Lock rm (region_lock);
422 set<Evoral::Parameter> ret;
424 for (RegionList::const_iterator r = regions.begin(); r != regions.end(); ++r) {
425 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*r);
427 for (Automatable::Controls::iterator c = mr->model()->controls().begin();
428 c != mr->model()->controls().end(); ++c) {
429 ret.insert(c->first);
438 MidiPlaylist::region_changed (const PBD::PropertyChange& what_changed, boost::shared_ptr<Region> region)
440 if (in_flush || in_set_state) {
444 PBD::PropertyChange our_interests;
445 our_interests.add (Properties::midi_data);
447 bool parent_wants_notify = Playlist::region_changed (what_changed, region);
449 if (parent_wants_notify || what_changed.contains (our_interests)) {
450 notify_contents_changed ();