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)
46 const XMLProperty* prop = node.property("type");
47 assert(prop && DataType(prop->value()) == DataType::MIDI);
54 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
55 : Playlist (session, name, DataType::MIDI, hidden)
59 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, string name, bool hidden)
60 : Playlist (other, name, hidden)
65 list<Region*>::const_iterator in_o = other.regions.begin();
66 list<Region*>::iterator in_n = regions.begin();
68 while (in_o != other.regions.end()) {
69 MidiRegion *ar = dynamic_cast<MidiRegion *>( (*in_o) );
71 for (list<Crossfade *>::const_iterator xfades = other._crossfades.begin(); xfades != other._crossfades.end(); ++xfades) {
72 if ( &(*xfades)->in() == ar) {
73 // We found one! Now copy it!
75 list<Region*>::const_iterator out_o = other.regions.begin();
76 list<Region*>::const_iterator out_n = regions.begin();
78 while (out_o != other.regions.end()) {
80 MidiRegion *ar2 = dynamic_cast<MidiRegion *>( (*out_o) );
82 if ( &(*xfades)->out() == ar2) {
83 MidiRegion *in = dynamic_cast<MidiRegion*>( (*in_n) );
84 MidiRegion *out = dynamic_cast<MidiRegion*>( (*out_n) );
85 Crossfade *new_fade = new Crossfade( *(*xfades), in, out);
86 add_crossfade(*new_fade);
93 // cerr << "HUH!? second region in the crossfade not found!" << endl;
103 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, nframes_t start, nframes_t dur, string name, bool hidden)
104 : Playlist (other, start, dur, name, hidden)
106 /* this constructor does NOT notify others (session) */
109 MidiPlaylist::~MidiPlaylist ()
111 GoingAway (); /* EMIT SIGNAL */
114 struct RegionSortByLayer {
115 bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
116 return a->layer() < b->layer();
120 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
122 MidiPlaylist::read (MidiRingBuffer& dst, nframes_t start,
123 nframes_t dur, unsigned chan_n)
125 /* this function is never called from a realtime thread, so
126 its OK to block (for short intervals).
129 Glib::Mutex::Lock rm (region_lock);
132 nframes_t end = start + dur - 1;
134 //_read_data_count = 0;
136 // relevent regions overlapping start <--> end
137 vector<boost::shared_ptr<Region> > regs;
139 for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
141 if ((*i)->coverage (start, end) != OverlapNone) {
146 RegionSortByLayer layer_cmp;
147 sort(regs.begin(), regs.end(), layer_cmp);
149 for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
150 // FIXME: ensure time is monotonic here
151 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
152 mr->read_at (dst, start, dur, chan_n);
153 ret += mr->read_data_count();
156 _read_data_count += ret;
164 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
170 MidiPlaylist::flush_notifications ()
172 Playlist::flush_notifications();
184 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> r)
189 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> original, boost::shared_ptr<Region> left, boost::shared_ptr<Region> right)
191 throw; // I don't wanna
193 MidiRegion *orig = dynamic_cast<MidiRegion*>(o);
194 MidiRegion *left = dynamic_cast<MidiRegion*>(l);
195 MidiRegion *right = dynamic_cast<MidiRegion*>(r);
197 for (Crossfades::iterator x = _crossfades.begin(); x != _crossfades.end();) {
198 Crossfades::iterator tmp;
204 if ((*x)->_in == orig) {
205 if (! (*x)->covers(right->position())) {
206 fade = new Crossfade( *(*x), left, (*x)->_out);
208 // Overlap, the crossfade is copied on the left side of the right region instead
209 fade = new Crossfade( *(*x), right, (*x)->_out);
213 if ((*x)->_out == orig) {
214 if (! (*x)->covers(right->position())) {
215 fade = new Crossfade( *(*x), (*x)->_in, right);
217 // Overlap, the crossfade is copied on the right side of the left region instead
218 fade = new Crossfade( *(*x), (*x)->_in, left);
223 _crossfades.remove( (*x) );
224 add_crossfade (*fade);
231 MidiPlaylist::check_dependents (boost::shared_ptr<Region> r, bool norefresh)
237 MidiPlaylist::set_state (const XMLNode& node)
240 Playlist::set_state (node);
243 // Actually Charles, I don't much care for children
246 XMLNodeList nlist = node.children();
248 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
250 XMLNode* const child = *niter;
258 MidiPlaylist::state (bool full_state)
260 XMLNode& node = Playlist::state (full_state);
266 MidiPlaylist::dump () const
268 boost::shared_ptr<Region> r;
270 cerr << "Playlist \"" << _name << "\" " << endl
271 << regions.size() << " regions "
274 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
276 cerr << " " << r->name() << " @ " << r << " ["
277 << r->start() << "+" << r->length()
287 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
289 boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
290 bool changed = false;
293 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
300 RegionLock rlock (this);
301 RegionList::iterator i;
302 RegionList::iterator tmp;
304 for (i = regions.begin(); i != regions.end(); ) {
309 if ((*i) == region) {
320 /* overload this, it normally means "removed", not destroyed */
321 notify_region_removed (region);
328 MidiPlaylist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
330 if (in_flush || in_set_state) {
334 // Feeling rather uninterested today, but thanks for the heads up anyway!
336 Change our_interests = Change (/*MidiRegion::FadeInChanged|
337 MidiRegion::FadeOutChanged|
338 MidiRegion::FadeInActiveChanged|
339 MidiRegion::FadeOutActiveChanged|
340 MidiRegion::EnvelopeActiveChanged|
341 MidiRegion::ScaleAmplitudeChanged|
342 MidiRegion::EnvelopeChanged*/);
343 bool parent_wants_notify;
345 parent_wants_notify = Playlist::region_changed (what_changed, region);
347 if ((parent_wants_notify || (what_changed & our_interests))) {