- Changed IO's vector<Port*>'s to PortList
[ardour.git] / libs / ardour / midi_playlist.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3         Written by Dave Robillard, 2006
4
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.
9  
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.
14  
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.
18 */
19
20 #include <cassert>
21
22 #include <algorithm>
23
24 #include <stdlib.h>
25
26 #include <sigc++/bind.h>
27
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
34 #include <pbd/error.h>
35
36 #include "i18n.h"
37
38 using namespace ARDOUR;
39 using namespace sigc;
40 using namespace std;
41
42 MidiPlaylist::State::~State ()
43 {}
44
45 MidiPlaylist::MidiPlaylist (Session& session, const XMLNode& node, bool hidden)
46                 : Playlist (session, node, hidden)
47 {
48         in_set_state = true;
49         set_state (node);
50         in_set_state = false;
51
52         save_state (_("initial state"));
53
54         if (!hidden) {
55                 PlaylistCreated (this); /* EMIT SIGNAL */
56         }
57 }
58
59 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
60                 : Playlist (session, name, hidden)
61 {
62         save_state (_("initial state"));
63
64         if (!hidden) {
65                 PlaylistCreated (this); /* EMIT SIGNAL */
66         }
67
68 }
69
70 MidiPlaylist::MidiPlaylist (const MidiPlaylist& other, string name, bool hidden)
71                 : Playlist (other, name, hidden)
72 {
73         save_state (_("initial state"));
74
75         /*
76         list<Region*>::const_iterator in_o  = other.regions.begin();
77         list<Region*>::iterator in_n = regions.begin();
78
79         while (in_o != other.regions.end()) {
80                 MidiRegion *ar = dynamic_cast<MidiRegion *>( (*in_o) );
81
82                 for (list<Crossfade *>::const_iterator xfades = other._crossfades.begin(); xfades != other._crossfades.end(); ++xfades) {
83                         if ( &(*xfades)->in() == ar) {
84                                 // We found one! Now copy it!
85
86                                 list<Region*>::const_iterator out_o = other.regions.begin();
87                                 list<Region*>::const_iterator out_n = regions.begin();
88
89                                 while (out_o != other.regions.end()) {
90
91                                         MidiRegion *ar2 = dynamic_cast<MidiRegion *>( (*out_o) );
92
93                                         if ( &(*xfades)->out() == ar2) {
94                                                 MidiRegion *in  = dynamic_cast<MidiRegion*>( (*in_n) );
95                                                 MidiRegion *out = dynamic_cast<MidiRegion*>( (*out_n) );
96                                                 Crossfade *new_fade = new Crossfade( *(*xfades), in, out);
97                                                 add_crossfade(*new_fade);
98                                                 break;
99                                         }
100
101                                         out_o++;
102                                         out_n++;
103                                 }
104                                 //                              cerr << "HUH!? second region in the crossfade not found!" << endl;
105                         }
106                 }
107
108                 in_o++;
109                 in_n++;
110         }
111 */
112         if (!hidden) {
113                 PlaylistCreated (this); /* EMIT SIGNAL */
114         }
115 }
116
117 MidiPlaylist::MidiPlaylist (const MidiPlaylist& other, jack_nframes_t start, jack_nframes_t cnt, string name, bool hidden)
118                 : Playlist (other, start, cnt, name, hidden)
119 {
120         save_state (_("initial state"));
121
122         /* this constructor does NOT notify others (session) */
123 }
124
125 MidiPlaylist::~MidiPlaylist ()
126 {
127         set <Region*> all_regions;
128
129         GoingAway (this);
130
131         /* find every region we've ever used, and add it to the set of
132            all regions.
133         */
134
135         for (RegionList::iterator x = regions.begin(); x != regions.end(); ++x) {
136                 all_regions.insert (*x);
137         }
138
139         for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
140
141                 MidiPlaylist::State* apstate = dynamic_cast<MidiPlaylist::State*> (*i);
142
143                 for (RegionList::iterator r = apstate->regions.begin(); r != apstate->regions.end(); ++r) {
144                         all_regions.insert (*r);
145                 }
146
147                 delete apstate;
148         }
149
150         /* delete every region */
151
152         for (set<Region *>::iterator ar = all_regions.begin(); ar != all_regions.end(); ++ar) {
153                 (*ar)->unlock_sources ();
154                 delete *ar;
155         }
156
157 }
158
159 struct RegionSortByLayer
160 {
161         bool operator() (Region *a, Region *b)
162         {
163                 return a->layer() < b->layer();
164         }
165 };
166
167 /** FIXME: semantics of return value? */
168 jack_nframes_t
169 MidiPlaylist::read (RawMidi *buf, RawMidi *mixdown_buffer, jack_nframes_t start,
170                      jack_nframes_t cnt, unsigned chan_n)
171 {
172         /* this function is never called from a realtime thread, so
173            its OK to block (for short intervals).
174         */
175
176         Glib::Mutex::Lock rm (region_lock);
177
178         jack_nframes_t ret         = 0;
179         jack_nframes_t end         =  start + cnt - 1;
180         jack_nframes_t read_frames = 0;
181         jack_nframes_t skip_frames = 0;
182
183         _read_data_count = 0;
184
185         vector<MidiRegion*> regs; // relevent regions overlapping start <--> end
186
187         for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
188                 MidiRegion* const mr = dynamic_cast<MidiRegion*>(*i);
189                 if (mr && mr->coverage (start, end) != OverlapNone) {
190                         regs.push_back(mr);
191                 }
192         }
193
194         RegionSortByLayer layer_cmp;
195         sort(regs.begin(), regs.end(), layer_cmp);
196
197         for (vector<MidiRegion*>::iterator i = regs.begin(); i != regs.end(); ++i) {
198                         (*i)->read_at (buf, mixdown_buffer, start, cnt, chan_n, read_frames, skip_frames);
199                         ret += (*i)->read_data_count();
200         }
201
202         _read_data_count += ret;
203         
204         return ret;
205 }
206
207
208 void
209 MidiPlaylist::remove_dependents (Region& region)
210 {
211         MidiRegion* r = dynamic_cast<MidiRegion*> (&region);
212
213         if (r == 0) {
214                 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
215                 << endmsg;
216                 return;
217         }
218
219 }
220
221
222 void
223 MidiPlaylist::flush_notifications ()
224 {
225         Playlist::flush_notifications();
226
227         if (in_flush) {
228                 return;
229         }
230
231         in_flush = true;
232
233         in_flush = false;
234 }
235
236 void
237 MidiPlaylist::refresh_dependents (Region& r)
238 {
239         MidiRegion* ar = dynamic_cast<MidiRegion*>(&r);
240
241         if (ar == 0) {
242                 return;
243         }
244 }
245
246 void
247 MidiPlaylist::finalize_split_region (Region *o, Region *l, Region *r)
248 {
249         /*
250         MidiRegion *orig  = dynamic_cast<MidiRegion*>(o);
251         MidiRegion *left  = dynamic_cast<MidiRegion*>(l);
252         MidiRegion *right = dynamic_cast<MidiRegion*>(r);
253
254         for (Crossfades::iterator x = _crossfades.begin(); x != _crossfades.end();) {
255                 Crossfades::iterator tmp;
256                 tmp = x;
257                 ++tmp;
258
259                 Crossfade *fade = 0;
260
261                 if ((*x)->_in == orig) {
262                         if (! (*x)->covers(right->position())) {
263                                 fade = new Crossfade( *(*x), left, (*x)->_out);
264                         } else {
265                                 // Overlap, the crossfade is copied on the left side of the right region instead
266                                 fade = new Crossfade( *(*x), right, (*x)->_out);
267                         }
268                 }
269
270                 if ((*x)->_out == orig) {
271                         if (! (*x)->covers(right->position())) {
272                                 fade = new Crossfade( *(*x), (*x)->_in, right);
273                         } else {
274                                 // Overlap, the crossfade is copied on the right side of the left region instead
275                                 fade = new Crossfade( *(*x), (*x)->_in, left);
276                         }
277                 }
278
279                 if (fade) {
280                         _crossfades.remove( (*x) );
281                         add_crossfade (*fade);
282                 }
283                 x = tmp;
284         }*/
285 }
286
287 void
288 MidiPlaylist::check_dependents (Region& r, bool norefresh)
289 {
290         MidiRegion* other;
291         MidiRegion* region;
292         MidiRegion* top;
293         MidiRegion* bottom;
294
295         if (in_set_state || in_partition) {
296                 return;
297         }
298
299         if ((region = dynamic_cast<MidiRegion*> (&r)) == 0) {
300                 PBD::fatal << _("programming error: non-midi Region tested for overlap in midi playlist")
301                 << endmsg;
302                 return;
303         }
304
305         if (!norefresh) {
306                 refresh_dependents (r);
307         }
308
309         for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
310
311                 other = dynamic_cast<MidiRegion*> (*i);
312
313                 if (other == region) {
314                         continue;
315                 }
316
317                 if (other->muted() || region->muted()) {
318                         continue;
319                 }
320
321                 if (other->layer() < region->layer()) {
322                         top = region;
323                         bottom = other;
324                 } else {
325                         top = other;
326                         bottom = region;
327                 }
328
329         }
330 }
331
332
333 int
334 MidiPlaylist::set_state (const XMLNode& node)
335 {
336         /*
337         XMLNode *child;
338         XMLNodeList nlist;
339         XMLNodeConstIterator niter;
340
341         if (!in_set_state) {
342                 Playlist::set_state (node);
343         }
344
345         nlist = node.children();
346
347         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
348
349                 child = *niter;
350
351         }*/
352
353         return 0;
354 }
355
356 void
357 MidiPlaylist::drop_all_states ()
358 {
359         set<Region*> all_regions;
360
361         /* find every region we've ever used, and add it to the set of
362            all regions. same for xfades;
363         */
364
365         for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
366
367                 MidiPlaylist::State* apstate = dynamic_cast<MidiPlaylist::State*> (*i);
368
369                 for (RegionList::iterator r = apstate->regions.begin(); r != apstate->regions.end(); ++r) {
370                         all_regions.insert (*r);
371                 }
372         }
373
374         /* now remove from the "all" lists every region that is in the current list. */
375
376         for (list<Region*>::iterator i = regions.begin(); i != regions.end(); ++i) {
377                 set
378                         <Region*>::iterator x = all_regions.find (*i);
379                 if (x != all_regions.end()) {
380                         all_regions.erase (x);
381                 }
382         }
383
384         /* delete every region that is left - these are all things that are part of our "history" */
385
386         for (set
387                 <Region *>::iterator ar = all_regions.begin(); ar != all_regions.end(); ++ar) {
388                 (*ar)->unlock_sources ();
389                 delete *ar;
390         }
391
392         /* Now do the generic thing ... */
393
394         StateManager::drop_all_states ();
395 }
396
397 StateManager::State*
398 MidiPlaylist::state_factory (std::string why) const
399 {
400         State* state = new State (why);
401
402         state->regions = regions;
403         state->region_states.clear ();
404         for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
405                 state->region_states.push_back ((*i)->get_memento());
406         }
407
408         return state;
409 }
410
411 Change
412 MidiPlaylist::restore_state (StateManager::State& state)
413 {
414         {
415                 RegionLock rlock (this);
416                 State* apstate = dynamic_cast<State*> (&state);
417
418                 in_set_state = true;
419
420                 regions = apstate->regions;
421
422                 for (list<UndoAction>::iterator s = apstate->
423                                                     region_states.begin();
424                         s != apstate->region_states.end();
425                         ++s) {
426                         (*s) ();
427                 }
428
429                 in_set_state = false;
430         }
431
432         notify_length_changed ();
433         return Change (~0);
434 }
435
436 UndoAction
437 MidiPlaylist::get_memento () const
438 {
439         return sigc::bind (mem_fun (*(const_cast<MidiPlaylist*> (this)), &StateManager::use_state), _current_state_id);
440 }
441
442
443 XMLNode&
444 MidiPlaylist::state (bool full_state)
445 {
446         XMLNode& node = Playlist::state (full_state);
447
448         return node;
449 }
450
451 void
452 MidiPlaylist::dump () const
453 {
454         Region *r;
455
456         cerr << "Playlist \"" << _name << "\" " << endl
457         << regions.size() << " regions "
458         << endl;
459
460         for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
461                 r = *i;
462                 cerr << "  " << r->name() << " @ " << r << " ["
463                 << r->start() << "+" << r->length()
464                 << "] at "
465                 << r->position()
466                 << " on layer "
467                 << r->layer ()
468                 << endl;
469         }
470 }
471
472 bool
473 MidiPlaylist::destroy_region (Region* region)
474 {
475         MidiRegion* r = dynamic_cast<MidiRegion*> (region);
476         bool changed = false;
477
478         if (r == 0) {
479                 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
480                 << endmsg;
481                 /*NOTREACHED*/
482                 return false;
483         }
484
485         {
486                 RegionLock rlock (this);
487                 RegionList::iterator i;
488                 RegionList::iterator tmp;
489
490                 for (i = regions.begin(); i != regions.end(); ) {
491
492                         tmp = i;
493                         ++tmp;
494
495                         if ((*i) == region) {
496                                 (*i)->unlock_sources ();
497                                 regions.erase (i);
498                                 changed = true;
499                         }
500
501                         i = tmp;
502                 }
503         }
504
505         for (StateMap::iterator s = states.begin(); s != states.end(); ) {
506                 StateMap::iterator tmp;
507
508                 tmp = s;
509                 ++tmp;
510
511                 State* astate = dynamic_cast<State*> (*s);
512
513                 list<UndoAction>::iterator rsi, rsitmp;
514                 RegionList::iterator ri, ritmp;
515
516                 for (ri = astate->regions.begin(), rsi = astate->region_states.begin();
517                         ri != astate->regions.end() && rsi != astate->region_states.end();) {
518
519
520                         ritmp = ri;
521                         ++ritmp;
522
523                         rsitmp = rsi;
524                         ++rsitmp;
525
526                         if (region == (*ri)) {
527                                 astate->regions.erase (ri);
528                                 astate->region_states.erase (rsi);
529                         }
530
531                         ri = ritmp;
532                         rsi = rsitmp;
533                 }
534
535                 s = tmp;
536         }
537
538
539         if (changed) {
540                 /* overload this, it normally means "removed", not destroyed */
541                 notify_region_removed (region);
542         }
543
544         return changed;
545 }
546
547
548 void
549 MidiPlaylist::get_equivalent_regions (const MidiRegion& other, vector<MidiRegion*>& results)
550 {
551         for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
552
553                 MidiRegion* ar = dynamic_cast<MidiRegion*> (*i);
554
555                 if (ar) {
556                         if (Config->get_use_overlap_equivalency()) {
557                                 if (ar->overlap_equivalent (other)) {
558                                         results.push_back (ar);
559                                 } else if (ar->equivalent (other)) {
560                                         results.push_back (ar);
561                                 }
562                         }
563                 }
564         }
565 }
566
567 void
568 MidiPlaylist::get_region_list_equivalent_regions (const MidiRegion& other, vector<MidiRegion*>& results)
569 {
570         for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
571
572                 MidiRegion* ar = dynamic_cast<MidiRegion*> (*i);
573
574                 if (ar && ar->region_list_equivalent (other)) {
575                         results.push_back (ar);
576                 }
577         }
578 }
579
580 bool
581 MidiPlaylist::region_changed (Change what_changed, Region* region)
582 {
583         if (in_flush || in_set_state) {
584                 return false;
585         }
586
587         Change our_interests = Change (/*MidiRegion::FadeInChanged|
588                                        MidiRegion::FadeOutChanged|
589                                        MidiRegion::FadeInActiveChanged|
590                                        MidiRegion::FadeOutActiveChanged|
591                                        MidiRegion::EnvelopeActiveChanged|
592                                        MidiRegion::ScaleAmplitudeChanged|
593                                        MidiRegion::EnvelopeChanged*/);
594         bool parent_wants_notify;
595
596         parent_wants_notify = Playlist::region_changed (what_changed, region);
597
598         maybe_save_state (_("region modified"));
599
600         if ((parent_wants_notify || (what_changed & our_interests))) {
601                 notify_modified ();
602         }
603
604         return true;
605 }
606