- MIDI "recording" - rec region creation/drawing, actual MIDI region creation/view...
[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
60 /* Basic MidiRegion constructor (one channel) */
61 MidiRegion::MidiRegion (MidiSource& src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
62         : Region (src, start, length, name, DataType::MIDI, layer, flags)
63 {
64         save_state ("initial state");
65
66         if (announce) {
67                  CheckNewRegion (this); /* EMIT SIGNAL */
68         }
69 }
70
71 /* Basic MidiRegion constructor (many channels) */
72 MidiRegion::MidiRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
73         : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
74 {
75         save_state ("initial state");
76
77         if (announce) {
78                  CheckNewRegion (this); /* EMIT SIGNAL */
79         }
80 }
81
82
83 /** Create a new MidiRegion, that is part of an existing one */
84 MidiRegion::MidiRegion (const MidiRegion& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
85         : Region (other, offset, length, name, layer, flags)
86 {
87         save_state ("initial state");
88
89         if (announce) {
90                 CheckNewRegion (this); /* EMIT SIGNAL */
91         }
92 }
93
94 MidiRegion::MidiRegion (const MidiRegion &other)
95         : Region (other)
96 {
97         save_state ("initial state");
98
99         /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
100 }
101
102 MidiRegion::MidiRegion (MidiSource& src, const XMLNode& node)
103         : Region (src, node)
104 {
105         if (set_state (node)) {
106                 throw failed_constructor();
107         }
108
109         save_state ("initial state");
110
111         assert(_type == DataType::MIDI);
112
113         CheckNewRegion (this); /* EMIT SIGNAL */
114 }
115
116 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
117         : Region (srcs, node)
118 {
119         if (set_state (node)) {
120                 throw failed_constructor();
121         }
122
123         save_state ("initial state");
124
125         assert(_type == DataType::MIDI);
126
127         CheckNewRegion (this); /* EMIT SIGNAL */
128 }
129
130 MidiRegion::~MidiRegion ()
131 {
132         GoingAway (this);
133 }
134
135 StateManager::State*
136 MidiRegion::state_factory (std::string why) const
137 {
138         RegionState* state = new RegionState (why);
139
140         Region::store_state (*state);
141
142         return state;
143 }       
144
145 Change
146 MidiRegion::restore_state (StateManager::State& sstate) 
147 {
148         RegionState* state = dynamic_cast<RegionState*> (&sstate);
149         Change what_changed = Region::restore_and_return_flags (*state);
150         
151         if (_flags != Flag (state->_flags)) {
152                 
153                 //uint32_t old_flags = _flags;
154                 
155                 _flags = Flag (state->_flags);
156         }
157                 
158         what_changed = Change (what_changed);
159
160         return what_changed;
161 }
162
163 UndoAction
164 MidiRegion::get_memento() const
165 {
166         return sigc::bind (mem_fun (*(const_cast<MidiRegion *> (this)), &StateManager::use_state), _current_state_id);
167 }
168
169 jack_nframes_t
170 MidiRegion::read_at (RawMidi *out, RawMidi* mix_buf, jack_nframes_t position, 
171                       jack_nframes_t cnt, 
172                       uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
173 {
174         return _read_at (_sources, out, position, cnt, chan_n, read_frames, skip_frames);
175 }
176
177 jack_nframes_t
178 MidiRegion::master_read_at (RawMidi *out, RawMidi* mix_buf, jack_nframes_t position, 
179                              jack_nframes_t cnt, uint32_t chan_n) const
180 {
181         return _read_at (_master_sources, out, position, cnt, chan_n, 0, 0);
182 }
183
184 jack_nframes_t
185 MidiRegion::_read_at (const SourceList& srcs, RawMidi *buf, 
186                        jack_nframes_t position, jack_nframes_t cnt, 
187                        uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
188 {
189         jack_nframes_t internal_offset;
190         jack_nframes_t buf_offset;
191         jack_nframes_t to_read;
192         
193         /* precondition: caller has verified that we cover the desired section */
194
195         assert(chan_n == 0);
196         
197         if (position < _position) {
198                 internal_offset = 0;
199                 buf_offset = _position - position;
200                 cnt -= buf_offset;
201         } else {
202                 internal_offset = position - _position;
203                 buf_offset = 0;
204         }
205
206         if (internal_offset >= _length) {
207                 return 0; /* read nothing */
208         }
209         
210
211         if ((to_read = min (cnt, _length - internal_offset)) == 0) {
212                 return 0; /* read nothing */
213         }
214
215         // FIXME: non-opaque MIDI regions not yet supported
216         assert(opaque());
217
218         if (muted()) {
219                 return 0; /* read nothing */
220         }
221
222         _read_data_count = 0;
223
224         MidiSource& src = midi_source(chan_n);
225         if (src.read (buf, _start + internal_offset, to_read) != to_read) {
226                 return 0; /* "read nothing" */
227         }
228
229         _read_data_count += src.read_data_count();
230
231         return to_read;
232 }
233         
234 XMLNode&
235 MidiRegion::state (bool full)
236 {
237         XMLNode& node (Region::state (full));
238         XMLNode *child;
239         char buf[64];
240         char buf2[64];
241         LocaleGuard lg (X_("POSIX"));
242         
243         snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
244         node.add_property ("flags", buf);
245
246         for (uint32_t n=0; n < _sources.size(); ++n) {
247                 snprintf (buf2, sizeof(buf2), "source-%d", n);
248                 _sources[n]->id().print (buf);
249                 node.add_property (buf2, buf);
250         }
251
252         snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
253         node.add_property ("channels", buf);
254
255         child = node.add_child ("Envelope");
256
257         if ( ! full) {
258                 child->add_property ("default", "yes");
259         }
260
261         if (full && _extra_xml) {
262                 node.add_child_copy (*_extra_xml);
263         }
264
265         return node;
266 }
267
268 int
269 MidiRegion::set_state (const XMLNode& node)
270 {
271         const XMLNodeList& nlist = node.children();
272         const XMLProperty *prop;
273         LocaleGuard lg (X_("POSIX"));
274
275         Region::set_state (node);
276
277         if ((prop = node.property ("flags")) != 0) {
278                 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
279         }
280
281         /* Now find child items */
282         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
283                 
284                 XMLNode *child;
285                 //XMLProperty *prop;
286                 
287                 child = (*niter);
288                 
289                 /** Hello, children */
290         }
291
292         return 0;
293 }
294
295 void
296 MidiRegion::recompute_at_end ()
297 {
298         /* our length has changed
299          * (non destructively) "chop" notes that pass the end boundary, to
300          * prevent stuck notes.
301          */
302 }       
303
304 void
305 MidiRegion::recompute_at_start ()
306 {
307         /* as above, but the shift was from the front
308          * maybe bump currently active note's note-ons up so they sound here?
309          * that could be undesireable in certain situations though.. maybe
310          * remove the note entirely, including it's note off?  something needs to
311          * be done to keep the played MIDI sane to avoid messing up voices of
312          * polyhonic things etc........
313          */
314 }
315
316 int
317 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
318 {
319 #if 0
320         SourceList srcs;
321         string new_name;
322
323         for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
324
325                 srcs.clear ();
326                 srcs.push_back (*i);
327
328                 /* generate a new name */
329                 
330                 if (session.region_name (new_name, _name)) {
331                         return -1;
332                 }
333
334                 /* create a copy with just one source */
335
336                 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
337         }
338 #endif
339
340         // Actually, I would prefer not if that's alright
341         return -1;
342 }
343
344 MidiSource&
345 MidiRegion::midi_source (uint32_t n) const
346 {
347         // Guaranteed to succeed (use a static cast?)
348         return dynamic_cast<MidiSource&>(source(n));
349 }
350