More solid "fake" recording and serialization
[ardour.git] / libs / ardour / midi_region.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: midiregion.cc 746 2006-08-02 02:44:23Z drobilla $
19 */
20
21 #include <cmath>
22 #include <climits>
23 #include <cfloat>
24
25 #include <set>
26
27 #include <sigc++/bind.h>
28 #include <sigc++/class_slot.h>
29
30 #include <glibmm/thread.h>
31
32 #include <pbd/basename.h>
33 #include <pbd/xml++.h>
34
35 #include <ardour/midi_region.h>
36 #include <ardour/session.h>
37 #include <ardour/gain.h>
38 #include <ardour/dB.h>
39 #include <ardour/playlist.h>
40 #include <ardour/midi_source.h>
41 #include <ardour/types.h>
42
43 #include "i18n.h"
44 #include <locale.h>
45
46 using namespace std;
47 using namespace ARDOUR;
48
49 /** Basic MidiRegion constructor (one channel) */
50 MidiRegion::MidiRegion (MidiSource& src, jack_nframes_t start, jack_nframes_t length, bool announce)
51         : Region (src, start, length, PBD::basename_nosuffix(src.name()), DataType::MIDI, 0,  Region::Flag(Region::DefaultFlags|Region::External))
52 {
53         save_state ("initial state");
54
55         if (announce) {
56                  CheckNewRegion (this); /* EMIT SIGNAL */
57         }
58         
59         assert(_name.find("/") == string::npos);
60 }
61
62 /* Basic MidiRegion constructor (one channel) */
63 MidiRegion::MidiRegion (MidiSource& src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
64         : Region (src, start, length, name, DataType::MIDI, layer, flags)
65 {
66         save_state ("initial state");
67
68         if (announce) {
69                  CheckNewRegion (this); /* EMIT SIGNAL */
70         }
71
72         assert(_name.find("/") == string::npos);
73 }
74
75 /* Basic MidiRegion constructor (many channels) */
76 MidiRegion::MidiRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
77         : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
78 {
79         save_state ("initial state");
80
81         if (announce) {
82                  CheckNewRegion (this); /* EMIT SIGNAL */
83         }
84         
85         assert(_name.find("/") == string::npos);
86 }
87
88
89 /** Create a new MidiRegion, that is part of an existing one */
90 MidiRegion::MidiRegion (const MidiRegion& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
91         : Region (other, offset, length, name, layer, flags)
92 {
93         save_state ("initial state");
94
95         if (announce) {
96                 CheckNewRegion (this); /* EMIT SIGNAL */
97         }
98         
99         assert(_name.find("/") == string::npos);
100 }
101
102 MidiRegion::MidiRegion (const MidiRegion &other)
103         : Region (other)
104 {
105         save_state ("initial state");
106
107         /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
108         assert(_name.find("/") == string::npos);
109 }
110
111 MidiRegion::MidiRegion (MidiSource& src, const XMLNode& node)
112         : Region (src, node)
113 {
114         if (set_state (node)) {
115                 throw failed_constructor();
116         }
117
118         save_state ("initial state");
119
120         assert(_name.find("/") == string::npos);
121         assert(_type == DataType::MIDI);
122
123         CheckNewRegion (this); /* EMIT SIGNAL */
124 }
125
126 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
127         : Region (srcs, node)
128 {
129         if (set_state (node)) {
130                 throw failed_constructor();
131         }
132
133         save_state ("initial state");
134
135         assert(_name.find("/") == string::npos);
136         assert(_type == DataType::MIDI);
137
138         CheckNewRegion (this); /* EMIT SIGNAL */
139 }
140
141 MidiRegion::~MidiRegion ()
142 {
143         GoingAway (this);
144 }
145
146 StateManager::State*
147 MidiRegion::state_factory (std::string why) const
148 {
149         RegionState* state = new RegionState (why);
150
151         Region::store_state (*state);
152
153         return state;
154 }       
155
156 Change
157 MidiRegion::restore_state (StateManager::State& sstate) 
158 {
159         RegionState* state = dynamic_cast<RegionState*> (&sstate);
160         Change what_changed = Region::restore_and_return_flags (*state);
161         
162         if (_flags != Flag (state->_flags)) {
163                 
164                 //uint32_t old_flags = _flags;
165                 
166                 _flags = Flag (state->_flags);
167         }
168                 
169         what_changed = Change (what_changed);
170
171         return what_changed;
172 }
173
174 UndoAction
175 MidiRegion::get_memento() const
176 {
177         return sigc::bind (mem_fun (*(const_cast<MidiRegion *> (this)), &StateManager::use_state), _current_state_id);
178 }
179
180 jack_nframes_t
181 MidiRegion::read_at (MidiBuffer& out, jack_nframes_t position, 
182                       jack_nframes_t cnt, 
183                       uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
184 {
185         return _read_at (_sources, out, position, cnt, chan_n, read_frames, skip_frames);
186 }
187
188 jack_nframes_t
189 MidiRegion::master_read_at (MidiBuffer& out, jack_nframes_t position, 
190                              jack_nframes_t cnt, uint32_t chan_n) const
191 {
192         return _read_at (_master_sources, out, position, cnt, chan_n, 0, 0);
193 }
194
195 jack_nframes_t
196 MidiRegion::_read_at (const SourceList& srcs, MidiBuffer& buf, 
197                        jack_nframes_t position, jack_nframes_t cnt, 
198                        uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
199 {
200         jack_nframes_t internal_offset;
201         jack_nframes_t buf_offset;
202         jack_nframes_t to_read;
203         
204         /* precondition: caller has verified that we cover the desired section */
205
206         assert(chan_n == 0);
207         
208         if (position < _position) {
209                 internal_offset = 0;
210                 buf_offset = _position - position;
211                 cnt -= buf_offset;
212         } else {
213                 internal_offset = position - _position;
214                 buf_offset = 0;
215         }
216
217         if (internal_offset >= _length) {
218                 return 0; /* read nothing */
219         }
220         
221
222         if ((to_read = min (cnt, _length - internal_offset)) == 0) {
223                 return 0; /* read nothing */
224         }
225
226         // FIXME: non-opaque MIDI regions not yet supported
227         assert(opaque());
228
229         if (muted()) {
230                 return 0; /* read nothing */
231         }
232
233         _read_data_count = 0;
234
235         MidiSource& src = midi_source(chan_n);
236         if (src.read (buf, _start + internal_offset, to_read) != to_read) {
237                 return 0; /* "read nothing" */
238         }
239
240         _read_data_count += src.read_data_count();
241
242         return to_read;
243 }
244         
245 XMLNode&
246 MidiRegion::state (bool full)
247 {
248         XMLNode& node (Region::state (full));
249         XMLNode *child;
250         char buf[64];
251         char buf2[64];
252         LocaleGuard lg (X_("POSIX"));
253         
254         snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
255         node.add_property ("flags", buf);
256
257         for (uint32_t n=0; n < _sources.size(); ++n) {
258                 snprintf (buf2, sizeof(buf2), "source-%d", n);
259                 _sources[n]->id().print (buf);
260                 node.add_property (buf2, buf);
261         }
262
263         snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
264         node.add_property ("channels", buf);
265
266         child = node.add_child ("Envelope");
267
268         if ( ! full) {
269                 child->add_property ("default", "yes");
270         }
271
272         if (full && _extra_xml) {
273                 node.add_child_copy (*_extra_xml);
274         }
275
276         return node;
277 }
278
279 int
280 MidiRegion::set_state (const XMLNode& node)
281 {
282         const XMLNodeList& nlist = node.children();
283         const XMLProperty *prop;
284         LocaleGuard lg (X_("POSIX"));
285
286         Region::set_state (node);
287
288         if ((prop = node.property ("flags")) != 0) {
289                 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
290         }
291
292         /* Now find child items */
293         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
294                 
295                 XMLNode *child;
296                 //XMLProperty *prop;
297                 
298                 child = (*niter);
299                 
300                 /** Hello, children */
301         }
302
303         return 0;
304 }
305
306 void
307 MidiRegion::recompute_at_end ()
308 {
309         /* our length has changed
310          * (non destructively) "chop" notes that pass the end boundary, to
311          * prevent stuck notes.
312          */
313 }       
314
315 void
316 MidiRegion::recompute_at_start ()
317 {
318         /* as above, but the shift was from the front
319          * maybe bump currently active note's note-ons up so they sound here?
320          * that could be undesireable in certain situations though.. maybe
321          * remove the note entirely, including it's note off?  something needs to
322          * be done to keep the played MIDI sane to avoid messing up voices of
323          * polyhonic things etc........
324          */
325 }
326
327 int
328 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
329 {
330 #if 0
331         SourceList srcs;
332         string new_name;
333
334         for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
335
336                 srcs.clear ();
337                 srcs.push_back (*i);
338
339                 /* generate a new name */
340                 
341                 if (session.region_name (new_name, _name)) {
342                         return -1;
343                 }
344
345                 /* create a copy with just one source */
346
347                 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
348         }
349 #endif
350
351         // Actually, I would prefer not if that's alright
352         return -1;
353 }
354
355 MidiSource&
356 MidiRegion::midi_source (uint32_t n) const
357 {
358         // Guaranteed to succeed (use a static cast?)
359         return dynamic_cast<MidiSource&>(source(n));
360 }
361