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