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