Merged with trunk R999.
[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::MidiPlaylist (Session& session, const XMLNode& node, bool hidden)
44                 : Playlist (session, node, DataType::MIDI, hidden)
45 {
46         const XMLProperty* prop = node.property("type");
47         assert(prop && DataType(prop->value()) == DataType::MIDI);
48
49         in_set_state = true;
50         set_state (node);
51         in_set_state = false;
52
53         if (!hidden) {
54                 PlaylistCreated (this); /* EMIT SIGNAL */
55         }
56 }
57
58 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
59                 : Playlist (session, name, DataType::MIDI, hidden)
60 {
61         if (!hidden) {
62                 PlaylistCreated (this); /* EMIT SIGNAL */
63         }
64
65 }
66
67 MidiPlaylist::MidiPlaylist (const MidiPlaylist& other, string name, bool hidden)
68                 : Playlist (other, name, hidden)
69 {
70         throw; // nope
71
72         /*
73         list<Region*>::const_iterator in_o  = other.regions.begin();
74         list<Region*>::iterator in_n = regions.begin();
75
76         while (in_o != other.regions.end()) {
77                 MidiRegion *ar = dynamic_cast<MidiRegion *>( (*in_o) );
78
79                 for (list<Crossfade *>::const_iterator xfades = other._crossfades.begin(); xfades != other._crossfades.end(); ++xfades) {
80                         if ( &(*xfades)->in() == ar) {
81                                 // We found one! Now copy it!
82
83                                 list<Region*>::const_iterator out_o = other.regions.begin();
84                                 list<Region*>::const_iterator out_n = regions.begin();
85
86                                 while (out_o != other.regions.end()) {
87
88                                         MidiRegion *ar2 = dynamic_cast<MidiRegion *>( (*out_o) );
89
90                                         if ( &(*xfades)->out() == ar2) {
91                                                 MidiRegion *in  = dynamic_cast<MidiRegion*>( (*in_n) );
92                                                 MidiRegion *out = dynamic_cast<MidiRegion*>( (*out_n) );
93                                                 Crossfade *new_fade = new Crossfade( *(*xfades), in, out);
94                                                 add_crossfade(*new_fade);
95                                                 break;
96                                         }
97
98                                         out_o++;
99                                         out_n++;
100                                 }
101                                 //                              cerr << "HUH!? second region in the crossfade not found!" << endl;
102                         }
103                 }
104
105                 in_o++;
106                 in_n++;
107         }
108 */
109         if (!hidden) {
110                 PlaylistCreated (this); /* EMIT SIGNAL */
111         }
112 }
113
114 MidiPlaylist::MidiPlaylist (const MidiPlaylist& other, jack_nframes_t start, jack_nframes_t dur, string name, bool hidden)
115                 : Playlist (other, start, dur, name, hidden)
116 {
117         /* this constructor does NOT notify others (session) */
118 }
119
120 MidiPlaylist::~MidiPlaylist ()
121 {
122         GoingAway (); /* EMIT SIGNAL */
123 }
124
125 struct RegionSortByLayer {
126     bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
127             return a->layer() < b->layer();
128     }
129 };
130
131 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
132 jack_nframes_t
133 MidiPlaylist::read (MidiRingBuffer& dst, jack_nframes_t start,
134                      jack_nframes_t dur, unsigned chan_n)
135 {
136         /* this function is never called from a realtime thread, so
137            its OK to block (for short intervals).
138         */
139
140         Glib::Mutex::Lock rm (region_lock);
141
142         jack_nframes_t ret         = 0;
143         jack_nframes_t end         = start + dur - 1;
144         //jack_nframes_t read_frames = 0;
145         //jack_nframes_t skip_frames = 0;
146
147         //_read_data_count = 0;
148
149         // relevent regions overlapping start <--> end
150         vector<boost::shared_ptr<Region> > regs;
151
152         for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
153
154                 if ((*i)->coverage (start, end) != OverlapNone) {
155                         regs.push_back(*i);
156                 }
157         }
158
159         RegionSortByLayer layer_cmp;
160         sort(regs.begin(), regs.end(), layer_cmp);
161
162         for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
163                 // FIXME: ensure time is monotonic here
164                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
165                 mr->read_at (dst, start, dur, chan_n, 0, 0);// FIXME read_frames, skip_frames);
166                 ret += mr->read_data_count();
167         }
168
169         _read_data_count += ret;
170         
171         //return ret; FIXME?
172         return dur;
173 }
174
175
176 void
177 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
178 {
179 }
180
181
182 void
183 MidiPlaylist::flush_notifications ()
184 {
185         Playlist::flush_notifications();
186
187         if (in_flush) {
188                 return;
189         }
190
191         in_flush = true;
192
193         in_flush = false;
194 }
195
196 void
197 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> r)
198 {
199 }
200
201 void
202 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> original, boost::shared_ptr<Region> left, boost::shared_ptr<Region> right)
203 {
204         throw; // I don't wanna
205         /*
206         MidiRegion *orig  = dynamic_cast<MidiRegion*>(o);
207         MidiRegion *left  = dynamic_cast<MidiRegion*>(l);
208         MidiRegion *right = dynamic_cast<MidiRegion*>(r);
209
210         for (Crossfades::iterator x = _crossfades.begin(); x != _crossfades.end();) {
211                 Crossfades::iterator tmp;
212                 tmp = x;
213                 ++tmp;
214
215                 Crossfade *fade = 0;
216
217                 if ((*x)->_in == orig) {
218                         if (! (*x)->covers(right->position())) {
219                                 fade = new Crossfade( *(*x), left, (*x)->_out);
220                         } else {
221                                 // Overlap, the crossfade is copied on the left side of the right region instead
222                                 fade = new Crossfade( *(*x), right, (*x)->_out);
223                         }
224                 }
225
226                 if ((*x)->_out == orig) {
227                         if (! (*x)->covers(right->position())) {
228                                 fade = new Crossfade( *(*x), (*x)->_in, right);
229                         } else {
230                                 // Overlap, the crossfade is copied on the right side of the left region instead
231                                 fade = new Crossfade( *(*x), (*x)->_in, left);
232                         }
233                 }
234
235                 if (fade) {
236                         _crossfades.remove( (*x) );
237                         add_crossfade (*fade);
238                 }
239                 x = tmp;
240         }*/
241 }
242
243 void
244 MidiPlaylist::check_dependents (boost::shared_ptr<Region> r, bool norefresh)
245 {
246 }
247
248
249 int
250 MidiPlaylist::set_state (const XMLNode& node)
251 {
252         if (!in_set_state) {
253                 Playlist::set_state (node);
254         }
255
256         // Actually Charles, I don't much care for children
257         
258         /*
259         XMLNodeList nlist = node.children();
260
261         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
262
263                 XMLNode* const child = *niter;
264
265         }*/
266
267         return 0;
268 }
269
270 XMLNode&
271 MidiPlaylist::state (bool full_state)
272 {
273         XMLNode& node = Playlist::state (full_state);
274
275         return node;
276 }
277
278 void
279 MidiPlaylist::dump () const
280 {
281         boost::shared_ptr<Region> r;
282
283         cerr << "Playlist \"" << _name << "\" " << endl
284         << regions.size() << " regions "
285         << endl;
286
287         for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
288                 r = *i;
289                 cerr << "  " << r->name() << " @ " << r << " ["
290                 << r->start() << "+" << r->length()
291                 << "] at "
292                 << r->position()
293                 << " on layer "
294                 << r->layer ()
295                 << endl;
296         }
297 }
298
299 bool
300 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
301 {
302         boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
303         bool changed = false;
304
305         if (r == 0) {
306                 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
307                 << endmsg;
308                 /*NOTREACHED*/
309                 return false;
310         }
311
312         {
313                 RegionLock rlock (this);
314                 RegionList::iterator i;
315                 RegionList::iterator tmp;
316
317                 for (i = regions.begin(); i != regions.end(); ) {
318
319                         tmp = i;
320                         ++tmp;
321
322                         if ((*i) == region) {
323                                 regions.erase (i);
324                                 changed = true;
325                         }
326
327                         i = tmp;
328                 }
329         }
330
331
332         if (changed) {
333                 /* overload this, it normally means "removed", not destroyed */
334                 notify_region_removed (region);
335         }
336
337         return changed;
338 }
339
340 bool
341 MidiPlaylist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
342 {
343         if (in_flush || in_set_state) {
344                 return false;
345         }
346
347         // Feeling rather uninterested today, but thanks for the heads up anyway!
348         
349         Change our_interests = Change (/*MidiRegion::FadeInChanged|
350                                        MidiRegion::FadeOutChanged|
351                                        MidiRegion::FadeInActiveChanged|
352                                        MidiRegion::FadeOutActiveChanged|
353                                        MidiRegion::EnvelopeActiveChanged|
354                                        MidiRegion::ScaleAmplitudeChanged|
355                                        MidiRegion::EnvelopeChanged*/);
356         bool parent_wants_notify;
357
358         parent_wants_notify = Playlist::region_changed (what_changed, region);
359
360         if ((parent_wants_notify || (what_changed & our_interests))) {
361                 notify_modified ();
362         }
363
364         return true;
365 }
366