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 ();
64 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
65 : Playlist (session, name, DataType::MIDI, hidden)
66 , _note_mode(Sustained)
70 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, string name, bool hidden)
71 : Playlist (other, name, hidden)
72 , _note_mode(other->_note_mode)
76 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, framepos_t start, framecnt_t dur, string name, bool hidden)
77 : Playlist (other, start, dur, name, hidden)
78 , _note_mode(other->_note_mode)
80 /* this constructor does NOT notify others (session) */
83 MidiPlaylist::~MidiPlaylist ()
87 template<typename Time>
88 struct EventsSortByTimeAndType {
89 bool operator() (Evoral::Event<Time>* a, Evoral::Event<Time>* b) {
90 if (a->time() == b->time()) {
91 if (EventTypeMap::instance().type_is_midi (a->event_type()) && EventTypeMap::instance().type_is_midi (b->event_type())) {
92 /* negate return value since we must return whether
93 * or not a should sort before b, not b before a
95 return !MidiBuffer::second_simultaneous_midi_byte_is_first (a->buffer()[0], b->buffer()[0]);
98 return a->time() < b->time();
102 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
104 MidiPlaylist::read (Evoral::EventSink<framepos_t>& dst, framepos_t start, framecnt_t dur, unsigned chan_n)
106 /* this function is never called from a realtime thread, so
107 its OK to block (for short intervals).
110 Playlist::RegionReadLock rl (this);
112 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("++++++ %1 .. %2 +++++++ %3 trackers +++++++++++++++++\n",
113 start, start + dur, _note_trackers.size()));
115 framepos_t end = start + dur - 1;
117 // relevent regions overlapping start <--> end
118 vector< boost::shared_ptr<Region> > regs;
119 vector< boost::shared_ptr<Region> > ended;
120 typedef pair<MidiStateTracker*,framepos_t> TrackerInfo;
121 vector<TrackerInfo> tracker_info;
122 NoteTrackers::iterator t;
124 for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
126 /* in this call to coverage, the return value indicates the
127 * overlap status of the read range (start...end) WRT to
131 switch ((*i)->coverage (start, end)) {
132 case Evoral::OverlapStart:
133 case Evoral::OverlapInternal:
134 case Evoral::OverlapExternal:
138 case Evoral::OverlapEnd:
139 /* this region ends within the read range */
141 ended.push_back (*i);
149 if (regs.size() == 1 &&
150 (ended.empty() || (ended.size() == 1 && ended.front() == regs.front()))) {
152 /* just a single region - read directly into dst */
154 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("Single region (%1) read, ended during this read %2\n", regs.front()->name(),
157 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(regs.front());
161 NoteTrackers::iterator t = _note_trackers.find (mr.get());
162 MidiStateTracker* tracker;
163 bool new_tracker = false;
165 if (t == _note_trackers.end()) {
166 tracker = new MidiStateTracker;
168 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tBEFORE: new tracker\n");
171 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tBEFORE: tracker says there are %1 on notes\n", tracker->on()));
174 mr->read_at (dst, start, dur, chan_n, _note_mode, tracker);
175 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tAFTER: tracker says there are %1 on notes\n", tracker->on()));
177 if (!ended.empty()) {
178 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1 ended in this read, resolve notes and delete (%2) tracker\n",
179 mr->name(), ((new_tracker) ? "new" : "old")));
180 tracker->resolve_notes (dst, mr->last_frame());
183 _note_trackers.erase (t);
187 pair<Region*,MidiStateTracker*> newpair;
188 newpair.first = mr.get();
189 newpair.second = tracker;
190 _note_trackers.insert (newpair);
191 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tadded tracker to trackers\n");
198 /* multiple regions and/or note resolution: sort by layer, read into a temporary non-monotonically
199 sorted EventSink, sort and then insert into dst.
202 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("%1 regions to read, plus %2 trackers\n", regs.size(), tracker_info.size()));
204 Evoral::EventList<framepos_t> evlist;
206 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("for %1 .. %2 we have %3 to consider\n", start, start+dur-1, regs.size()));
208 for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
210 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
216 NoteTrackers::iterator t = _note_trackers.find (mr.get());
217 MidiStateTracker* tracker;
218 bool new_tracker = false;
220 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()));
222 if (t == _note_trackers.end()) {
223 tracker = new MidiStateTracker;
225 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tBEFORE: new tracker\n");
228 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tBEFORE: tracker says there are %1 on notes\n", tracker->on()));
232 mr->read_at (evlist, start, dur, chan_n, _note_mode, tracker);
235 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("After %1 (%2 .. %3) we now have %4\n", mr->name(), mr->position(), mr->last_frame(), evlist.size()));
236 for (Evoral::EventList<framepos_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
237 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1\n", **x));
239 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\tAFTER: tracker says there are %1 on notes\n", tracker->on()));
241 if (find (ended.begin(), ended.end(), *i) != ended.end()) {
243 /* the region ended within the read range, so
244 * resolve any dangling notes (i.e. notes whose
245 * end is beyond the end of the region).
248 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1 ended in this read, resolve notes and delete (%2) tracker\n",
249 mr->name(), ((new_tracker) ? "new" : "old")));
251 tracker->resolve_notes (evlist, (*i)->last_frame());
254 _note_trackers.erase (t);
260 pair<Region*,MidiStateTracker*> newpair;
261 newpair.first = mr.get();
262 newpair.second = tracker;
263 _note_trackers.insert (newpair).first;
264 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tadded tracker to trackers\n");
269 if (!evlist.empty()) {
271 /* sort the event list */
272 EventsSortByTimeAndType<framepos_t> cmp;
276 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("Final we now have %1 events\n", evlist.size()));
277 for (Evoral::EventList<framepos_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
278 DEBUG_TRACE (DEBUG::MidiPlaylistIO, string_compose ("\t%1\n", **x));
282 for (Evoral::EventList<framepos_t>::iterator e = evlist.begin(); e != evlist.end(); ++e) {
283 Evoral::Event<framepos_t>* ev (*e);
284 dst.write (ev->time(), ev->event_type(), ev->size(), ev->buffer());
291 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "-------------------------------------------------------------\n");
296 MidiPlaylist::clear_note_trackers ()
298 Playlist::RegionWriteLock rl (this, false);
300 for (NoteTrackers::iterator n = _note_trackers.begin(); n != _note_trackers.end(); ++n) {
303 DEBUG_TRACE (DEBUG::MidiTrackers, string_compose ("%1 clears all note trackers\n", name()));
304 _note_trackers.clear ();
308 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
310 /* MIDI regions have no dependents (crossfades) but we might be tracking notes */
311 NoteTrackers::iterator t = _note_trackers.find (region.get());
313 /* GACK! THREAD SAFETY! */
315 if (t != _note_trackers.end()) {
317 _note_trackers.erase (t);
322 MidiPlaylist::set_state (const XMLNode& node, int version)
327 if (Playlist::set_state (node, version)) {
338 MidiPlaylist::dump () const
340 boost::shared_ptr<Region> r;
342 cerr << "Playlist \"" << _name << "\" " << endl
343 << regions.size() << " regions "
346 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
348 cerr << " " << r->name() << " @ " << r << " ["
349 << r->start() << "+" << r->length()
359 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
361 boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
367 bool changed = false;
370 RegionWriteLock rlock (this);
371 RegionList::iterator i;
372 RegionList::iterator tmp;
374 for (i = regions.begin(); i != regions.end(); ) {
379 if ((*i) == region) {
390 /* overload this, it normally means "removed", not destroyed */
391 notify_region_removed (region);
397 set<Evoral::Parameter>
398 MidiPlaylist::contained_automation()
400 /* this function is never called from a realtime thread, so
401 its OK to block (for short intervals).
404 Playlist::RegionReadLock rl (this);
405 set<Evoral::Parameter> ret;
407 for (RegionList::const_iterator r = regions.begin(); r != regions.end(); ++r) {
408 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*r);
410 for (Automatable::Controls::iterator c = mr->model()->controls().begin();
411 c != mr->model()->controls().end(); ++c) {
412 ret.insert(c->first);
421 MidiPlaylist::region_changed (const PBD::PropertyChange& what_changed, boost::shared_ptr<Region> region)
423 if (in_flush || in_set_state) {
427 PBD::PropertyChange our_interests;
428 our_interests.add (Properties::midi_data);
430 bool parent_wants_notify = Playlist::region_changed (what_changed, region);
432 if (parent_wants_notify || what_changed.contains (our_interests)) {
433 notify_contents_changed ();