rework Stateful::set_state() patch to avoid default version argument
[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                 , _note_mode(Sustained)
46 {
47         const XMLProperty* prop = node.property("type");
48         assert(prop && DataType(prop->value()) == DataType::MIDI);
49
50         in_set_state++;
51         set_state (node, Stateful::loading_state_version);
52         in_set_state--;
53 }
54
55 MidiPlaylist::MidiPlaylist (Session& session, string name, bool hidden)
56                 : Playlist (session, name, DataType::MIDI, hidden)
57 {
58 }
59
60 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, string name, bool hidden)
61                 : Playlist (other, name, hidden)
62 {
63         throw; // nope
64
65         /*
66         list<Region*>::const_iterator in_o  = other.regions.begin();
67         list<Region*>::iterator in_n = regions.begin();
68
69         while (in_o != other.regions.end()) {
70                 MidiRegion *ar = dynamic_cast<MidiRegion *>( (*in_o) );
71
72                 for (list<Crossfade *>::const_iterator xfades = other._crossfades.begin(); xfades != other._crossfades.end(); ++xfades) {
73                         if ( &(*xfades)->in() == ar) {
74                                 // We found one! Now copy it!
75
76                                 list<Region*>::const_iterator out_o = other.regions.begin();
77                                 list<Region*>::const_iterator out_n = regions.begin();
78
79                                 while (out_o != other.regions.end()) {
80
81                                         MidiRegion *ar2 = dynamic_cast<MidiRegion *>( (*out_o) );
82
83                                         if ( &(*xfades)->out() == ar2) {
84                                                 MidiRegion *in  = dynamic_cast<MidiRegion*>( (*in_n) );
85                                                 MidiRegion *out = dynamic_cast<MidiRegion*>( (*out_n) );
86                                                 Crossfade *new_fade = new Crossfade( *(*xfades), in, out);
87                                                 add_crossfade(*new_fade);
88                                                 break;
89                                         }
90
91                                         out_o++;
92                                         out_n++;
93                                 }
94                                 // cerr << "HUH!? second region in the crossfade not found!" << endl;
95                         }
96                 }
97
98                 in_o++;
99                 in_n++;
100         }
101 */
102 }
103
104 MidiPlaylist::MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, nframes_t start, nframes_t dur, string name, bool hidden)
105                 : Playlist (other, start, dur, name, hidden)
106 {
107         /* this constructor does NOT notify others (session) */
108 }
109
110 MidiPlaylist::~MidiPlaylist ()
111 {
112         GoingAway (); /* EMIT SIGNAL */
113
114         /* drop connections to signals */
115
116         notify_callbacks ();
117 }
118
119 struct RegionSortByLayer {
120     bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
121             return a->layer() < b->layer();
122     }
123 };
124
125 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
126 nframes_t
127 MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t dur, unsigned chan_n)
128 {
129         /* this function is never called from a realtime thread, so
130            its OK to block (for short intervals).
131         */
132
133         Glib::RecMutex::Lock rm (region_lock);
134
135         nframes_t end = start + dur - 1;
136
137         _read_data_count = 0;
138
139         // relevent regions overlapping start <--> end
140         vector<boost::shared_ptr<Region> > regs;
141
142         for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
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                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
153                 if (mr) {
154                         mr->read_at (dst, start, dur, chan_n, _note_mode);
155                         _read_data_count += mr->read_data_count();
156                 }
157         }
158
159         return dur;
160 }
161
162
163 void
164 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> /*region*/)
165 {
166         /* MIDI regions have no dependents (crossfades) */
167 }
168
169
170 void
171 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> /*r*/)
172 {
173         /* MIDI regions have no dependents (crossfades) */
174 }
175
176 void
177 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> /*original*/, boost::shared_ptr<Region> /*left*/, boost::shared_ptr<Region> /*right*/)
178 {
179         /* No MIDI crossfading (yet?), so nothing to do here */
180 }
181
182 void
183 MidiPlaylist::check_dependents (boost::shared_ptr<Region> /*r*/, bool /*norefresh*/)
184 {
185         /* MIDI regions have no dependents (crossfades) */
186 }
187
188
189 int
190 MidiPlaylist::set_state (const XMLNode& node, int version)
191 {
192         in_set_state++;
193         freeze ();
194
195         Playlist::set_state (node, version);
196
197         thaw();
198         in_set_state--;
199
200         return 0;
201 }
202
203 void
204 MidiPlaylist::dump () const
205 {
206         boost::shared_ptr<Region> r;
207
208         cerr << "Playlist \"" << _name << "\" " << endl
209         << regions.size() << " regions "
210         << endl;
211
212         for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
213                 r = *i;
214                 cerr << "  " << r->name() << " @ " << r << " ["
215                 << r->start() << "+" << r->length()
216                 << "] at "
217                 << r->position()
218                 << " on layer "
219                 << r->layer ()
220                 << endl;
221         }
222 }
223
224 bool
225 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
226 {
227         boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
228         bool changed = false;
229
230         if (r == 0) {
231                 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
232                 << endmsg;
233                 /*NOTREACHED*/
234                 return false;
235         }
236
237         {
238                 RegionLock rlock (this);
239                 RegionList::iterator i;
240                 RegionList::iterator tmp;
241
242                 for (i = regions.begin(); i != regions.end(); ) {
243
244                         tmp = i;
245                         ++tmp;
246
247                         if ((*i) == region) {
248                                 regions.erase (i);
249                                 changed = true;
250                         }
251
252                         i = tmp;
253                 }
254         }
255
256
257         if (changed) {
258                 /* overload this, it normally means "removed", not destroyed */
259                 notify_region_removed (region);
260         }
261
262         return changed;
263 }
264
265 set<Evoral::Parameter>
266 MidiPlaylist::contained_automation()
267 {
268         /* this function is never called from a realtime thread, so
269            its OK to block (for short intervals).
270         */
271
272         Glib::RecMutex::Lock rm (region_lock);
273
274         set<Evoral::Parameter> ret;
275
276         for (RegionList::const_iterator r = regions.begin(); r != regions.end(); ++r) {
277                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*r);
278
279                 for (Automatable::Controls::iterator c = mr->model()->controls().begin();
280                                 c != mr->model()->controls().end(); ++c) {
281                         ret.insert(c->first);
282                 }
283         }
284
285         return ret;
286 }
287
288
289 bool
290 MidiPlaylist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
291 {
292         if (in_flush || in_set_state) {
293                 return false;
294         }
295
296         // Feeling rather uninterested today, but thanks for the heads up anyway!
297
298         Change our_interests = Change (/*MidiRegion::FadeInChanged|
299                                        MidiRegion::FadeOutChanged|
300                                        MidiRegion::FadeInActiveChanged|
301                                        MidiRegion::FadeOutActiveChanged|
302                                        MidiRegion::EnvelopeActiveChanged|
303                                        MidiRegion::ScaleAmplitudeChanged|
304                                        MidiRegion::EnvelopeChanged*/);
305         bool parent_wants_notify;
306
307         parent_wants_notify = Playlist::region_changed (what_changed, region);
308
309         if ((parent_wants_notify || (what_changed & our_interests))) {
310                 notify_modified ();
311         }
312
313         return true;
314 }
315