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.
28 #include "evoral/EventList.hpp"
30 #include "ardour/debug.h"
31 #include "ardour/midi_model.h"
32 #include "ardour/midi_playlist.h"
33 #include "ardour/midi_region.h"
34 #include "ardour/midi_state_tracker.h"
35 #include "ardour/types.h"
39 using namespace ARDOUR;
43 MidiPlaylist::MidiPlaylist (Session& session, const XMLNode& node, bool hidden)
44 : Playlist (session, node, DataType::MIDI, hidden)
45 , _note_mode(Sustained)
48 const XMLProperty* prop = node.property("type");
49 assert(prop && DataType(prop->value()) == DataType::MIDI);
53 if (set_state (node, Stateful::loading_state_version)) {
54 throw failed_constructor ();
61 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
62 : Playlist (session, name, DataType::MIDI, hidden)
63 , _note_mode(Sustained)
67 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, string name, bool hidden)
68 : Playlist (other, name, hidden)
69 , _note_mode(other->_note_mode)
73 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other,
78 : Playlist (other, start, dur, name, hidden)
79 , _note_mode(other->_note_mode)
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 (parameter_is_midi ((AutomationType)a->event_type()) &&
92 parameter_is_midi ((AutomationType)b->event_type())) {
93 /* negate return value since we must return whether
94 * or not a should sort before b, not b before a
96 return !MidiBuffer::second_simultaneous_midi_byte_is_first (a->buffer()[0], b->buffer()[0]);
99 return a->time() < b->time();
104 MidiPlaylist::read (Evoral::EventSink<framepos_t>& dst, framepos_t start, framecnt_t dur, unsigned chan_n)
106 Playlist::RegionReadLock rl (this);
108 DEBUG_TRACE (DEBUG::MidiPlaylistIO,
109 string_compose ("---- MidiPlaylist::read %1 .. %2 (%3 trackers) ----\n",
110 start, start + dur, _note_trackers.size()));
112 typedef pair<MidiStateTracker*,framepos_t> TrackerInfo;
114 /* Find relevant regions that overlap [start..end] */
115 const framepos_t end = start + dur - 1;
116 std::vector< boost::shared_ptr<Region> > regs;
117 std::vector< boost::shared_ptr<Region> > ended;
118 for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
119 switch ((*i)->coverage (start, end)) {
120 case Evoral::OverlapStart:
121 case Evoral::OverlapInternal:
125 case Evoral::OverlapExternal:
126 /* this region is entirely contained in the read range */
128 ended.push_back (*i);
131 case Evoral::OverlapEnd:
132 /* this region ends within the read range */
134 ended.push_back (*i);
143 /* If we are reading from a single region, we can read directly into dst. Otherwise,
144 we read into a temporarily list, sort it, then write that to dst. */
145 const bool direct_read = regs.size() == 1 &&
146 (ended.empty() || (ended.size() == 1 && ended.front() == regs.front()));
148 Evoral::EventList<framepos_t> evlist;
149 Evoral::EventSink<framepos_t>& tgt = direct_read ? dst : evlist;
151 DEBUG_TRACE (DEBUG::MidiPlaylistIO,
152 string_compose ("\t%1 regions to read, direct: %2\n", regs.size(), direct_read));
154 for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
155 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
160 /* Get the existing note tracker for this region, or create a new one. */
161 NoteTrackers::iterator t = _note_trackers.find (mr.get());
162 MidiStateTracker* tracker = NULL;
163 bool new_tracker = false;
164 if (t == _note_trackers.end()) {
165 tracker = new MidiStateTracker;
167 DEBUG_TRACE (DEBUG::MidiPlaylistIO,
168 string_compose ("\tPre-read %1 (%2 .. %3): new tracker\n",
169 mr->name(), mr->position(), mr->last_frame()));
172 DEBUG_TRACE (DEBUG::MidiPlaylistIO,
173 string_compose ("\tPre-read %1 (%2 .. %3): %4 active notes\n",
174 mr->name(), mr->position(), mr->last_frame(), tracker->on()));
177 /** Read from region into target. */
178 mr->read_at (tgt, start, dur, chan_n, _note_mode, tracker);
179 DEBUG_TRACE (DEBUG::MidiPlaylistIO,
180 string_compose ("\tPost-read: %1 active notes\n", tracker->on()));
182 if (find (ended.begin(), ended.end(), *i) != ended.end()) {
183 /* Region ended within the read range, so resolve any active notes
184 (either stuck notes in the data, or notes that end after the end
186 DEBUG_TRACE (DEBUG::MidiPlaylistIO,
187 string_compose ("\t%1 ended, resolve notes and delete (%2) tracker\n",
188 mr->name(), ((new_tracker) ? "new" : "old")));
190 tracker->resolve_notes (tgt, (*i)->last_frame());
193 _note_trackers.erase (t);
199 _note_trackers.insert (make_pair (mr.get(), tracker));
200 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "\tadded tracker to trackers\n");
205 if (!direct_read && !evlist.empty()) {
206 /* We've read from multiple regions, sort the event list by time. */
207 EventsSortByTimeAndType<framepos_t> cmp;
210 /* Copy ordered events from event list to dst. */
211 for (Evoral::EventList<framepos_t>::iterator e = evlist.begin(); e != evlist.end(); ++e) {
212 Evoral::Event<framepos_t>* ev (*e);
213 dst.write (ev->time(), ev->event_type(), ev->size(), ev->buffer());
218 DEBUG_TRACE (DEBUG::MidiPlaylistIO, "---- End MidiPlaylist::read ----\n");
223 MidiPlaylist::reset_note_trackers ()
225 Playlist::RegionWriteLock rl (this, false);
227 for (NoteTrackers::iterator n = _note_trackers.begin(); n != _note_trackers.end(); ++n) {
230 DEBUG_TRACE (DEBUG::MidiTrackers, string_compose ("%1 reset all note trackers\n", name()));
231 _note_trackers.clear ();
235 MidiPlaylist::resolve_note_trackers (Evoral::EventSink<framepos_t>& dst, framepos_t time)
237 Playlist::RegionWriteLock rl (this, false);
239 for (NoteTrackers::iterator n = _note_trackers.begin(); n != _note_trackers.end(); ++n) {
240 n->second->resolve_notes(dst, time);
243 DEBUG_TRACE (DEBUG::MidiTrackers, string_compose ("%1 resolve all note trackers\n", name()));
244 _note_trackers.clear ();
248 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
250 /* MIDI regions have no dependents (crossfades) but we might be tracking notes */
251 NoteTrackers::iterator t = _note_trackers.find (region.get());
253 /* GACK! THREAD SAFETY! */
255 if (t != _note_trackers.end()) {
257 _note_trackers.erase (t);
262 MidiPlaylist::set_state (const XMLNode& node, int version)
267 if (Playlist::set_state (node, version)) {
278 MidiPlaylist::dump () const
280 boost::shared_ptr<Region> r;
282 cerr << "Playlist \"" << _name << "\" " << endl
283 << regions.size() << " regions "
286 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
288 cerr << " " << r->name() << " @ " << r << " ["
289 << r->start() << "+" << r->length()
299 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
301 boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
307 bool changed = false;
310 RegionWriteLock rlock (this);
311 RegionList::iterator i;
312 RegionList::iterator tmp;
314 for (i = regions.begin(); i != regions.end(); ) {
319 if ((*i) == region) {
330 /* overload this, it normally means "removed", not destroyed */
331 notify_region_removed (region);
337 set<Evoral::Parameter>
338 MidiPlaylist::contained_automation()
340 /* this function is never called from a realtime thread, so
341 its OK to block (for short intervals).
344 Playlist::RegionReadLock rl (this);
345 set<Evoral::Parameter> ret;
347 for (RegionList::const_iterator r = regions.begin(); r != regions.end(); ++r) {
348 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*r);
350 for (Automatable::Controls::iterator c = mr->model()->controls().begin();
351 c != mr->model()->controls().end(); ++c) {
352 ret.insert(c->first);
361 MidiPlaylist::region_changed (const PBD::PropertyChange& what_changed, boost::shared_ptr<Region> region)
363 if (in_flush || in_set_state) {
367 PBD::PropertyChange our_interests;
368 our_interests.add (Properties::midi_data);
370 bool parent_wants_notify = Playlist::region_changed (what_changed, region);
372 if (parent_wants_notify || what_changed.contains (our_interests)) {
373 notify_contents_changed ();