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::State::~State ()
46 MidiPlaylist::MidiPlaylist (Session& session, const XMLNode& node, bool hidden)
47 : Playlist (session, node, DataType::MIDI, hidden)
49 const XMLProperty* prop = node.property("type");
50 assert(prop && DataType(prop->value()) == DataType::MIDI);
56 save_state (_("initial state"));
59 PlaylistCreated (this); /* EMIT SIGNAL */
63 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
64 : Playlist (session, name, DataType::MIDI, hidden)
66 save_state (_("initial state"));
69 PlaylistCreated (this); /* EMIT SIGNAL */
74 MidiPlaylist::MidiPlaylist (const MidiPlaylist& other, string name, bool hidden)
75 : Playlist (other, name, hidden)
78 save_state (_("initial state"));
81 list<Region*>::const_iterator in_o = other.regions.begin();
82 list<Region*>::iterator in_n = regions.begin();
84 while (in_o != other.regions.end()) {
85 MidiRegion *ar = dynamic_cast<MidiRegion *>( (*in_o) );
87 for (list<Crossfade *>::const_iterator xfades = other._crossfades.begin(); xfades != other._crossfades.end(); ++xfades) {
88 if ( &(*xfades)->in() == ar) {
89 // We found one! Now copy it!
91 list<Region*>::const_iterator out_o = other.regions.begin();
92 list<Region*>::const_iterator out_n = regions.begin();
94 while (out_o != other.regions.end()) {
96 MidiRegion *ar2 = dynamic_cast<MidiRegion *>( (*out_o) );
98 if ( &(*xfades)->out() == ar2) {
99 MidiRegion *in = dynamic_cast<MidiRegion*>( (*in_n) );
100 MidiRegion *out = dynamic_cast<MidiRegion*>( (*out_n) );
101 Crossfade *new_fade = new Crossfade( *(*xfades), in, out);
102 add_crossfade(*new_fade);
109 // cerr << "HUH!? second region in the crossfade not found!" << endl;
118 PlaylistCreated (this); /* EMIT SIGNAL */
122 MidiPlaylist::MidiPlaylist (const MidiPlaylist& other, jack_nframes_t start, jack_nframes_t dur, string name, bool hidden)
123 : Playlist (other, start, dur, name, hidden)
125 save_state (_("initial state"));
127 /* this constructor does NOT notify others (session) */
130 MidiPlaylist::~MidiPlaylist ()
132 GoingAway (); /* EMIT SIGNAL */
134 for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
136 MidiPlaylist::State* apstate = dynamic_cast<MidiPlaylist::State*> (*i);
141 struct RegionSortByLayer {
142 bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
143 return a->layer() < b->layer();
147 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
149 MidiPlaylist::read (MidiRingBuffer& dst, jack_nframes_t start,
150 jack_nframes_t dur, unsigned chan_n)
152 /* this function is never called from a realtime thread, so
153 its OK to block (for short intervals).
156 Glib::Mutex::Lock rm (region_lock);
158 jack_nframes_t ret = 0;
159 jack_nframes_t end = start + dur - 1;
160 //jack_nframes_t read_frames = 0;
161 //jack_nframes_t skip_frames = 0;
163 //_read_data_count = 0;
165 // relevent regions overlapping start <--> end
166 vector<boost::shared_ptr<Region> > regs;
168 for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
170 if ((*i)->coverage (start, end) != OverlapNone) {
175 RegionSortByLayer layer_cmp;
176 sort(regs.begin(), regs.end(), layer_cmp);
178 for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
179 // FIXME: ensure time is monotonic here
180 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
181 mr->read_at (dst, start, dur, chan_n, 0, 0);// FIXME read_frames, skip_frames);
182 ret += mr->read_data_count();
185 _read_data_count += ret;
193 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
199 MidiPlaylist::flush_notifications ()
201 Playlist::flush_notifications();
213 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> r)
218 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> original, boost::shared_ptr<Region> left, boost::shared_ptr<Region> right)
220 throw; // I don't wanna
222 MidiRegion *orig = dynamic_cast<MidiRegion*>(o);
223 MidiRegion *left = dynamic_cast<MidiRegion*>(l);
224 MidiRegion *right = dynamic_cast<MidiRegion*>(r);
226 for (Crossfades::iterator x = _crossfades.begin(); x != _crossfades.end();) {
227 Crossfades::iterator tmp;
233 if ((*x)->_in == orig) {
234 if (! (*x)->covers(right->position())) {
235 fade = new Crossfade( *(*x), left, (*x)->_out);
237 // Overlap, the crossfade is copied on the left side of the right region instead
238 fade = new Crossfade( *(*x), right, (*x)->_out);
242 if ((*x)->_out == orig) {
243 if (! (*x)->covers(right->position())) {
244 fade = new Crossfade( *(*x), (*x)->_in, right);
246 // Overlap, the crossfade is copied on the right side of the left region instead
247 fade = new Crossfade( *(*x), (*x)->_in, left);
252 _crossfades.remove( (*x) );
253 add_crossfade (*fade);
260 MidiPlaylist::check_dependents (boost::shared_ptr<Region> r, bool norefresh)
266 MidiPlaylist::set_state (const XMLNode& node)
269 Playlist::set_state (node);
272 // Actually Charles, I don't much care for children
275 XMLNodeList nlist = node.children();
277 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
279 XMLNode* const child = *niter;
287 MidiPlaylist::drop_all_states ()
289 set<boost::shared_ptr<Region> > all_regions;
291 /* find every region we've ever used, and add it to the set of
292 all regions. same for xfades;
295 for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
297 MidiPlaylist::State* apstate = dynamic_cast<MidiPlaylist::State*> (*i);
299 for (RegionList::iterator r = apstate->regions.begin(); r != apstate->regions.end(); ++r) {
300 all_regions.insert (*r);
304 /* now remove from the "all" lists every region that is in the current list. */
306 for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
307 set<boost::shared_ptr<Region> >::iterator x = all_regions.find (*i);
308 if (x != all_regions.end()) {
309 all_regions.erase (x);
313 /* Now do the generic thing ... */
315 StateManager::drop_all_states ();
319 MidiPlaylist::state_factory (std::string why) const
321 State* state = new State (why);
323 state->regions = regions;
324 state->region_states.clear ();
325 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
326 state->region_states.push_back ((*i)->get_memento());
333 MidiPlaylist::restore_state (StateManager::State& state)
336 RegionLock rlock (this);
337 State* apstate = dynamic_cast<State*> (&state);
341 regions = apstate->regions;
343 for (list<UndoAction>::iterator s = apstate->
344 region_states.begin();
345 s != apstate->region_states.end();
350 in_set_state = false;
353 notify_length_changed ();
358 MidiPlaylist::get_memento () const
360 return sigc::bind (mem_fun (*(const_cast<MidiPlaylist*> (this)), &StateManager::use_state), _current_state_id);
365 MidiPlaylist::state (bool full_state)
367 XMLNode& node = Playlist::state (full_state);
373 MidiPlaylist::dump () const
375 boost::shared_ptr<Region> r;
377 cerr << "Playlist \"" << _name << "\" " << endl
378 << regions.size() << " regions "
381 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
383 cerr << " " << r->name() << " @ " << r << " ["
384 << r->start() << "+" << r->length()
394 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
396 boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
397 bool changed = false;
400 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
407 RegionLock rlock (this);
408 RegionList::iterator i;
409 RegionList::iterator tmp;
411 for (i = regions.begin(); i != regions.end(); ) {
416 if ((*i) == region) {
425 for (StateMap::iterator s = states.begin(); s != states.end(); ) {
426 StateMap::iterator tmp;
431 State* astate = dynamic_cast<State*> (*s);
433 list<UndoAction>::iterator rsi, rsitmp;
434 RegionList::iterator ri, ritmp;
436 for (ri = astate->regions.begin(), rsi = astate->region_states.begin();
437 ri != astate->regions.end() && rsi != astate->region_states.end();) {
446 if (region == (*ri)) {
447 astate->regions.erase (ri);
448 astate->region_states.erase (rsi);
460 /* overload this, it normally means "removed", not destroyed */
461 notify_region_removed (region);
468 MidiPlaylist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
470 if (in_flush || in_set_state) {
474 // Feeling rather uninterested today, but thanks for the heads up anyway!
476 Change our_interests = Change (/*MidiRegion::FadeInChanged|
477 MidiRegion::FadeOutChanged|
478 MidiRegion::FadeInActiveChanged|
479 MidiRegion::FadeOutActiveChanged|
480 MidiRegion::EnvelopeActiveChanged|
481 MidiRegion::ScaleAmplitudeChanged|
482 MidiRegion::EnvelopeChanged*/);
483 bool parent_wants_notify;
485 parent_wants_notify = Playlist::region_changed (what_changed, region);
487 maybe_save_state (_("region modified"));
489 if ((parent_wants_notify || (what_changed & our_interests))) {