2 Copyright (C) 2006 Paul Davis
3 Written by Dave Robillard, 2006
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.
26 #include <sigc++/bind.h>
28 #include "ardour/types.h"
29 #include "ardour/configuration.h"
30 #include "ardour/midi_playlist.h"
31 #include "ardour/midi_region.h"
32 #include "ardour/session.h"
33 #include "ardour/midi_ring_buffer.h"
35 #include "pbd/error.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)
47 const XMLProperty* prop = node.property("type");
48 assert(prop && DataType(prop->value()) == DataType::MIDI);
51 set_state (node, Stateful::loading_state_version);
55 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
56 : Playlist (session, name, DataType::MIDI, hidden)
60 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, string name, bool hidden)
61 : Playlist (other, name, hidden)
66 list<Region*>::const_iterator in_o = other.regions.begin();
67 list<Region*>::iterator in_n = regions.begin();
69 while (in_o != other.regions.end()) {
70 MidiRegion *ar = dynamic_cast<MidiRegion *>( (*in_o) );
72 for (list<Crossfade *>::const_iterator xfades = other._crossfades.begin(); xfades != other._crossfades.end(); ++xfades) {
73 if ( &(*xfades)->in() == ar) {
74 // We found one! Now copy it!
76 list<Region*>::const_iterator out_o = other.regions.begin();
77 list<Region*>::const_iterator out_n = regions.begin();
79 while (out_o != other.regions.end()) {
81 MidiRegion *ar2 = dynamic_cast<MidiRegion *>( (*out_o) );
83 if ( &(*xfades)->out() == ar2) {
84 MidiRegion *in = dynamic_cast<MidiRegion*>( (*in_n) );
85 MidiRegion *out = dynamic_cast<MidiRegion*>( (*out_n) );
86 Crossfade *new_fade = new Crossfade( *(*xfades), in, out);
87 add_crossfade(*new_fade);
94 // cerr << "HUH!? second region in the crossfade not found!" << endl;
104 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, nframes_t start, nframes_t dur, string name, bool hidden)
105 : Playlist (other, start, dur, name, hidden)
107 /* this constructor does NOT notify others (session) */
110 MidiPlaylist::~MidiPlaylist ()
112 GoingAway (); /* EMIT SIGNAL */
114 /* drop connections to signals */
119 struct RegionSortByLayer {
120 bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
121 return a->layer() < b->layer();
125 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
127 MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t dur, unsigned chan_n)
129 /* this function is never called from a realtime thread, so
130 its OK to block (for short intervals).
133 Glib::RecMutex::Lock rm (region_lock);
135 nframes_t end = start + dur - 1;
137 _read_data_count = 0;
139 // relevent regions overlapping start <--> end
140 vector<boost::shared_ptr<Region> > regs;
142 for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
143 if ((*i)->coverage (start, end) != OverlapNone) {
146 /* region does not cover the current read boundaries, so make
147 sure that we silence any notes that it had turned on
149 NoteTrackers::iterator t = _note_trackers.find ((*i).get());
150 if (t != _note_trackers.end()) {
151 t->second->resolve_notes (dst, (*i)->last_frame());
153 _note_trackers.erase (t);
158 RegionSortByLayer layer_cmp;
159 sort(regs.begin(), regs.end(), layer_cmp);
161 for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
162 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
165 NoteTrackers::iterator t = _note_trackers.find ((*i).get());
166 MidiStateTracker* tracker;
168 if (t == _note_trackers.end()) {
169 pair<Region*,MidiStateTracker*> newpair;
170 newpair.first = (*i).get();
171 tracker = newpair.second = new MidiStateTracker;
172 _note_trackers.insert (newpair);
177 mr->read_at (dst, start, dur, chan_n, _note_mode, tracker);
178 _read_data_count += mr->read_data_count();
186 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
188 /* MIDI regions have no dependents (crossfades) but we might be tracking notes */
189 NoteTrackers::iterator t = _note_trackers.find (region.get());
191 /* GACK! THREAD SAFETY! */
193 if (t != _note_trackers.end()) {
195 _note_trackers.erase (t);
201 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> /*r*/)
203 /* MIDI regions have no dependents (crossfades) */
207 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> /*original*/, boost::shared_ptr<Region> /*left*/, boost::shared_ptr<Region> /*right*/)
209 /* No MIDI crossfading (yet?), so nothing to do here */
213 MidiPlaylist::check_dependents (boost::shared_ptr<Region> /*r*/, bool /*norefresh*/)
215 /* MIDI regions have no dependents (crossfades) */
220 MidiPlaylist::set_state (const XMLNode& node, int version)
225 Playlist::set_state (node, version);
234 MidiPlaylist::dump () const
236 boost::shared_ptr<Region> r;
238 cerr << "Playlist \"" << _name << "\" " << endl
239 << regions.size() << " regions "
242 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
244 cerr << " " << r->name() << " @ " << r << " ["
245 << r->start() << "+" << r->length()
255 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
257 boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
258 bool changed = false;
261 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
268 RegionLock rlock (this);
269 RegionList::iterator i;
270 RegionList::iterator tmp;
272 for (i = regions.begin(); i != regions.end(); ) {
277 if ((*i) == region) {
288 /* overload this, it normally means "removed", not destroyed */
289 notify_region_removed (region);
295 set<Evoral::Parameter>
296 MidiPlaylist::contained_automation()
298 /* this function is never called from a realtime thread, so
299 its OK to block (for short intervals).
302 Glib::RecMutex::Lock rm (region_lock);
304 set<Evoral::Parameter> ret;
306 for (RegionList::const_iterator r = regions.begin(); r != regions.end(); ++r) {
307 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*r);
309 for (Automatable::Controls::iterator c = mr->model()->controls().begin();
310 c != mr->model()->controls().end(); ++c) {
311 ret.insert(c->first);
320 MidiPlaylist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
322 if (in_flush || in_set_state) {
326 // Feeling rather uninterested today, but thanks for the heads up anyway!
328 Change our_interests = Change (/*MidiRegion::FadeInChanged|
329 MidiRegion::FadeOutChanged|
330 MidiRegion::FadeInActiveChanged|
331 MidiRegion::FadeOutActiveChanged|
332 MidiRegion::EnvelopeActiveChanged|
333 MidiRegion::ScaleAmplitudeChanged|
334 MidiRegion::EnvelopeChanged*/);
335 bool parent_wants_notify;
337 parent_wants_notify = Playlist::region_changed (what_changed, region);
339 if ((parent_wants_notify || (what_changed & our_interests))) {