Adding sends/port inserts to MIDI tracks (not actually working yet though)
[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 #include <ardour/midi_ring_buffer.h>
34
35 #include <pbd/error.h>
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace sigc;
41 using namespace std;
42
43 MidiPlaylist::State::~State ()
44 {}
45
46 MidiPlaylist::MidiPlaylist (Session& session, const XMLNode& node, bool hidden)
47                 : Playlist (session, node, DataType::MIDI, hidden)
48 {
49         const XMLProperty* prop = node.property("type");
50         assert(prop && DataType(prop->value()) == DataType::MIDI);
51
52         in_set_state = true;
53         set_state (node);
54         in_set_state = false;
55
56         save_state (_("initial state"));
57
58         if (!hidden) {
59                 PlaylistCreated (this); /* EMIT SIGNAL */
60         }
61 }
62
63 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
64                 : Playlist (session, name, DataType::MIDI, hidden)
65 {
66         save_state (_("initial state"));
67
68         if (!hidden) {
69                 PlaylistCreated (this); /* EMIT SIGNAL */
70         }
71
72 }
73
74 MidiPlaylist::MidiPlaylist (const MidiPlaylist& other, string name, bool hidden)
75                 : Playlist (other, name, hidden)
76 {
77         throw; // nope
78         save_state (_("initial state"));
79
80         /*
81         list<Region*>::const_iterator in_o  = other.regions.begin();
82         list<Region*>::iterator in_n = regions.begin();
83
84         while (in_o != other.regions.end()) {
85                 MidiRegion *ar = dynamic_cast<MidiRegion *>( (*in_o) );
86
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!
90
91                                 list<Region*>::const_iterator out_o = other.regions.begin();
92                                 list<Region*>::const_iterator out_n = regions.begin();
93
94                                 while (out_o != other.regions.end()) {
95
96                                         MidiRegion *ar2 = dynamic_cast<MidiRegion *>( (*out_o) );
97
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);
103                                                 break;
104                                         }
105
106                                         out_o++;
107                                         out_n++;
108                                 }
109                                 //                              cerr << "HUH!? second region in the crossfade not found!" << endl;
110                         }
111                 }
112
113                 in_o++;
114                 in_n++;
115         }
116 */
117         if (!hidden) {
118                 PlaylistCreated (this); /* EMIT SIGNAL */
119         }
120 }
121
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)
124 {
125         save_state (_("initial state"));
126
127         /* this constructor does NOT notify others (session) */
128 }
129
130 MidiPlaylist::~MidiPlaylist ()
131 {
132         GoingAway (); /* EMIT SIGNAL */
133
134         for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
135
136                 MidiPlaylist::State* apstate = dynamic_cast<MidiPlaylist::State*> (*i);
137                 delete apstate;
138         }
139 }
140
141 struct RegionSortByLayer {
142     bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
143             return a->layer() < b->layer();
144     }
145 };
146
147 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
148 jack_nframes_t
149 MidiPlaylist::read (MidiRingBuffer& dst, jack_nframes_t start,
150                      jack_nframes_t dur, unsigned chan_n)
151 {
152         /* this function is never called from a realtime thread, so
153            its OK to block (for short intervals).
154         */
155
156         Glib::Mutex::Lock rm (region_lock);
157
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;
162
163         //_read_data_count = 0;
164
165         // relevent regions overlapping start <--> end
166         vector<boost::shared_ptr<Region> > regs;
167
168         for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
169
170                 if ((*i)->coverage (start, end) != OverlapNone) {
171                         regs.push_back(*i);
172                 }
173         }
174
175         RegionSortByLayer layer_cmp;
176         sort(regs.begin(), regs.end(), layer_cmp);
177
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();
183         }
184
185         _read_data_count += ret;
186         
187         //return ret; FIXME?
188         return dur;
189 }
190
191
192 void
193 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
194 {
195 }
196
197
198 void
199 MidiPlaylist::flush_notifications ()
200 {
201         Playlist::flush_notifications();
202
203         if (in_flush) {
204                 return;
205         }
206
207         in_flush = true;
208
209         in_flush = false;
210 }
211
212 void
213 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> r)
214 {
215 }
216
217 void
218 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> original, boost::shared_ptr<Region> left, boost::shared_ptr<Region> right)
219 {
220         throw; // I don't wanna
221         /*
222         MidiRegion *orig  = dynamic_cast<MidiRegion*>(o);
223         MidiRegion *left  = dynamic_cast<MidiRegion*>(l);
224         MidiRegion *right = dynamic_cast<MidiRegion*>(r);
225
226         for (Crossfades::iterator x = _crossfades.begin(); x != _crossfades.end();) {
227                 Crossfades::iterator tmp;
228                 tmp = x;
229                 ++tmp;
230
231                 Crossfade *fade = 0;
232
233                 if ((*x)->_in == orig) {
234                         if (! (*x)->covers(right->position())) {
235                                 fade = new Crossfade( *(*x), left, (*x)->_out);
236                         } else {
237                                 // Overlap, the crossfade is copied on the left side of the right region instead
238                                 fade = new Crossfade( *(*x), right, (*x)->_out);
239                         }
240                 }
241
242                 if ((*x)->_out == orig) {
243                         if (! (*x)->covers(right->position())) {
244                                 fade = new Crossfade( *(*x), (*x)->_in, right);
245                         } else {
246                                 // Overlap, the crossfade is copied on the right side of the left region instead
247                                 fade = new Crossfade( *(*x), (*x)->_in, left);
248                         }
249                 }
250
251                 if (fade) {
252                         _crossfades.remove( (*x) );
253                         add_crossfade (*fade);
254                 }
255                 x = tmp;
256         }*/
257 }
258
259 void
260 MidiPlaylist::check_dependents (boost::shared_ptr<Region> r, bool norefresh)
261 {
262 }
263
264
265 int
266 MidiPlaylist::set_state (const XMLNode& node)
267 {
268         if (!in_set_state) {
269                 Playlist::set_state (node);
270         }
271
272         // Actually Charles, I don't much care for children
273         
274         /*
275         XMLNodeList nlist = node.children();
276
277         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
278
279                 XMLNode* const child = *niter;
280
281         }*/
282
283         return 0;
284 }
285
286 void
287 MidiPlaylist::drop_all_states ()
288 {
289         set<boost::shared_ptr<Region> > all_regions;
290
291         /* find every region we've ever used, and add it to the set of 
292            all regions. same for xfades;
293         */
294
295         for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
296                 
297                 MidiPlaylist::State* apstate = dynamic_cast<MidiPlaylist::State*> (*i);
298
299                 for (RegionList::iterator r = apstate->regions.begin(); r != apstate->regions.end(); ++r) {
300                         all_regions.insert (*r);
301                 }
302         }
303
304         /* now remove from the "all" lists every region that is in the current list. */
305
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);
310                 }
311         }
312         
313         /* Now do the generic thing ... */
314
315         StateManager::drop_all_states ();
316 }
317
318 StateManager::State*
319 MidiPlaylist::state_factory (std::string why) const
320 {
321         State* state = new State (why);
322
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());
327         }
328
329         return state;
330 }
331
332 Change
333 MidiPlaylist::restore_state (StateManager::State& state)
334 {
335         {
336                 RegionLock rlock (this);
337                 State* apstate = dynamic_cast<State*> (&state);
338
339                 in_set_state = true;
340
341                 regions = apstate->regions;
342
343                 for (list<UndoAction>::iterator s = apstate->
344                                                     region_states.begin();
345                         s != apstate->region_states.end();
346                         ++s) {
347                         (*s) ();
348                 }
349
350                 in_set_state = false;
351         }
352
353         notify_length_changed ();
354         return Change (~0);
355 }
356
357 UndoAction
358 MidiPlaylist::get_memento () const
359 {
360         return sigc::bind (mem_fun (*(const_cast<MidiPlaylist*> (this)), &StateManager::use_state), _current_state_id);
361 }
362
363
364 XMLNode&
365 MidiPlaylist::state (bool full_state)
366 {
367         XMLNode& node = Playlist::state (full_state);
368
369         return node;
370 }
371
372 void
373 MidiPlaylist::dump () const
374 {
375         boost::shared_ptr<Region> r;
376
377         cerr << "Playlist \"" << _name << "\" " << endl
378         << regions.size() << " regions "
379         << endl;
380
381         for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
382                 r = *i;
383                 cerr << "  " << r->name() << " @ " << r << " ["
384                 << r->start() << "+" << r->length()
385                 << "] at "
386                 << r->position()
387                 << " on layer "
388                 << r->layer ()
389                 << endl;
390         }
391 }
392
393 bool
394 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
395 {
396         boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
397         bool changed = false;
398
399         if (r == 0) {
400                 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
401                 << endmsg;
402                 /*NOTREACHED*/
403                 return false;
404         }
405
406         {
407                 RegionLock rlock (this);
408                 RegionList::iterator i;
409                 RegionList::iterator tmp;
410
411                 for (i = regions.begin(); i != regions.end(); ) {
412
413                         tmp = i;
414                         ++tmp;
415
416                         if ((*i) == region) {
417                                 regions.erase (i);
418                                 changed = true;
419                         }
420
421                         i = tmp;
422                 }
423         }
424
425         for (StateMap::iterator s = states.begin(); s != states.end(); ) {
426                 StateMap::iterator tmp;
427
428                 tmp = s;
429                 ++tmp;
430
431                 State* astate = dynamic_cast<State*> (*s);
432
433                 list<UndoAction>::iterator rsi, rsitmp;
434                 RegionList::iterator ri, ritmp;
435
436                 for (ri = astate->regions.begin(), rsi = astate->region_states.begin();
437                         ri != astate->regions.end() && rsi != astate->region_states.end();) {
438
439
440                         ritmp = ri;
441                         ++ritmp;
442
443                         rsitmp = rsi;
444                         ++rsitmp;
445
446                         if (region == (*ri)) {
447                                 astate->regions.erase (ri);
448                                 astate->region_states.erase (rsi);
449                         }
450
451                         ri = ritmp;
452                         rsi = rsitmp;
453                 }
454
455                 s = tmp;
456         }
457
458
459         if (changed) {
460                 /* overload this, it normally means "removed", not destroyed */
461                 notify_region_removed (region);
462         }
463
464         return changed;
465 }
466
467 bool
468 MidiPlaylist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
469 {
470         if (in_flush || in_set_state) {
471                 return false;
472         }
473
474         // Feeling rather uninterested today, but thanks for the heads up anyway!
475         
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;
484
485         parent_wants_notify = Playlist::region_changed (what_changed, region);
486
487         maybe_save_state (_("region modified"));
488
489         if ((parent_wants_notify || (what_changed & our_interests))) {
490                 notify_modified ();
491         }
492
493         return true;
494 }
495