9720384130701ca22cf55a22b331644cd011b7c2
[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++;
50         set_state (node);
51         in_set_state--;
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         /* drop connections to signals */
114         
115         notify_callbacks ();
116 }
117
118 struct RegionSortByLayer {
119     bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
120             return a->layer() < b->layer();
121     }
122 };
123
124 /** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
125 nframes_t
126 MidiPlaylist::read (MidiRingBuffer& dst, nframes_t start,
127                      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::Mutex::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
144                 if ((*i)->coverage (start, end) != OverlapNone) {
145                         regs.push_back(*i);
146                 }
147         }
148
149         RegionSortByLayer layer_cmp;
150         sort(regs.begin(), regs.end(), layer_cmp);
151
152         for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
153                 // FIXME: ensure time is monotonic here?
154                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
155                 if (mr) {
156                         mr->read_at (dst, start, dur, chan_n, _note_mode);
157                         _read_data_count += mr->read_data_count();
158                 }
159         }
160
161         return dur;
162 }
163
164
165 void
166 MidiPlaylist::remove_dependents (boost::shared_ptr<Region> region)
167 {
168         /* MIDI regions have no dependents (crossfades) */
169 }
170
171
172 void
173 MidiPlaylist::refresh_dependents (boost::shared_ptr<Region> r)
174 {
175         /* MIDI regions have no dependents (crossfades) */
176 }
177
178 void
179 MidiPlaylist::finalize_split_region (boost::shared_ptr<Region> original, boost::shared_ptr<Region> left, boost::shared_ptr<Region> right)
180 {
181         /* No MIDI crossfading (yet?), so nothing to do here */
182 }
183
184 void
185 MidiPlaylist::check_dependents (boost::shared_ptr<Region> r, bool norefresh)
186 {
187         /* MIDI regions have no dependents (crossfades) */
188 }
189
190
191 int
192 MidiPlaylist::set_state (const XMLNode& node)
193 {
194         in_set_state++;
195         freeze ();
196
197         Playlist::set_state (node);
198
199         thaw();
200         in_set_state--;
201
202         return 0;
203 }
204
205 void
206 MidiPlaylist::dump () const
207 {
208         boost::shared_ptr<Region> r;
209
210         cerr << "Playlist \"" << _name << "\" " << endl
211         << regions.size() << " regions "
212         << endl;
213
214         for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
215                 r = *i;
216                 cerr << "  " << r->name() << " @ " << r << " ["
217                 << r->start() << "+" << r->length()
218                 << "] at "
219                 << r->position()
220                 << " on layer "
221                 << r->layer ()
222                 << endl;
223         }
224 }
225
226 bool
227 MidiPlaylist::destroy_region (boost::shared_ptr<Region> region)
228 {
229         boost::shared_ptr<MidiRegion> r = boost::dynamic_pointer_cast<MidiRegion> (region);
230         bool changed = false;
231
232         if (r == 0) {
233                 PBD::fatal << _("programming error: non-midi Region passed to remove_overlap in midi playlist")
234                 << endmsg;
235                 /*NOTREACHED*/
236                 return false;
237         }
238
239         {
240                 RegionLock rlock (this);
241                 RegionList::iterator i;
242                 RegionList::iterator tmp;
243
244                 for (i = regions.begin(); i != regions.end(); ) {
245
246                         tmp = i;
247                         ++tmp;
248
249                         if ((*i) == region) {
250                                 regions.erase (i);
251                                 changed = true;
252                         }
253
254                         i = tmp;
255                 }
256         }
257
258
259         if (changed) {
260                 /* overload this, it normally means "removed", not destroyed */
261                 notify_region_removed (region);
262         }
263
264         return changed;
265 }
266
267 bool
268 MidiPlaylist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
269 {
270         if (in_flush || in_set_state) {
271                 return false;
272         }
273
274         // Feeling rather uninterested today, but thanks for the heads up anyway!
275         
276         Change our_interests = Change (/*MidiRegion::FadeInChanged|
277                                        MidiRegion::FadeOutChanged|
278                                        MidiRegion::FadeInActiveChanged|
279                                        MidiRegion::FadeOutActiveChanged|
280                                        MidiRegion::EnvelopeActiveChanged|
281                                        MidiRegion::ScaleAmplitudeChanged|
282                                        MidiRegion::EnvelopeChanged*/);
283         bool parent_wants_notify;
284
285         parent_wants_notify = Playlist::region_changed (what_changed, region);
286
287         if ((parent_wants_notify || (what_changed & our_interests))) {
288                 notify_modified ();
289         }
290
291         return true;
292 }
293