* Add SysEx Support to MidiModel / SMF
[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 #include <pbd/enumwriter.h>
35
36 #include <ardour/midi_region.h>
37 #include <ardour/session.h>
38 #include <ardour/gain.h>
39 #include <ardour/dB.h>
40 #include <ardour/playlist.h>
41 #include <ardour/midi_source.h>
42 #include <ardour/types.h>
43 #include <ardour/midi_ring_buffer.h>
44
45 #include "i18n.h"
46 #include <locale.h>
47
48 using namespace std;
49 using namespace ARDOUR;
50
51 /** Basic MidiRegion constructor (one channel) */
52 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length)
53         : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::MIDI, 0,  Region::Flag(Region::DefaultFlags|Region::External))
54 {
55         assert(_name.find("/") == string::npos);
56         midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
57 }
58
59 /* Basic MidiRegion constructor (one channel) */
60 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
61         : Region (src, start, length, name, DataType::MIDI, layer, flags)
62 {
63         assert(_name.find("/") == string::npos);
64         midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
65 }
66
67 /* Basic MidiRegion constructor (many channels) */
68 MidiRegion::MidiRegion (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
69         : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
70 {
71         assert(_name.find("/") == string::npos);
72         midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
73 }
74
75
76 /** Create a new MidiRegion, that is part of an existing one */
77 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
78         : Region (other, offset, length, name, layer, flags)
79 {
80         assert(_name.find("/") == string::npos);
81         midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
82 }
83
84 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other)
85         : Region (other)
86 {
87         assert(_name.find("/") == string::npos);
88         midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
89 }
90
91 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, const XMLNode& node)
92         : Region (src, node)
93 {
94         if (set_state (node)) {
95                 throw failed_constructor();
96         }
97
98         midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
99         assert(_name.find("/") == string::npos);
100         assert(_type == DataType::MIDI);
101 }
102
103 MidiRegion::MidiRegion (const SourceList& srcs, const XMLNode& node)
104         : Region (srcs, node)
105 {
106         if (set_state (node)) {
107                 throw failed_constructor();
108         }
109
110         midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
111         assert(_name.find("/") == string::npos);
112         assert(_type == DataType::MIDI);
113 }
114
115 MidiRegion::~MidiRegion ()
116 {
117 }
118
119 nframes_t
120 MidiRegion::read_at (MidiRingBuffer<TimeType>& out, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
121 {
122         return _read_at (_sources, out, position, dur, chan_n, mode);
123 }
124
125 nframes_t
126 MidiRegion::master_read_at (MidiRingBuffer<TimeType>& out, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
127 {
128         return _read_at (_master_sources, out, position, dur, chan_n, mode);
129 }
130
131 nframes_t
132 MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer<TimeType>& dst, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
133 {
134         /*cerr << "MidiRegion " << _name << "._read_at(" << position << ") - "
135                 << position << " duration: " << dur << endl;*/
136
137         nframes_t internal_offset = 0;
138         nframes_t src_offset      = 0;
139         nframes_t to_read         = 0;
140         
141         /* precondition: caller has verified that we cover the desired section */
142
143         assert(chan_n == 0);
144         
145         if (position < _position) {
146                 internal_offset = 0;
147                 src_offset = _position - position;
148                 dur -= src_offset;
149         } else {
150                 internal_offset = position - _position;
151                 src_offset = 0;
152         }
153
154         if (internal_offset >= _length) {
155                 return 0; /* read nothing */
156         }
157         
158
159         if ((to_read = min (dur, _length - internal_offset)) == 0) {
160                 return 0; /* read nothing */
161         }
162
163         // FIXME: non-opaque MIDI regions not yet supported
164         assert(opaque());
165
166         if (muted()) {
167                 return 0; /* read nothing */
168         }
169
170         _read_data_count = 0;
171
172         boost::shared_ptr<MidiSource> src = midi_source(chan_n);
173         src->set_note_mode(mode);
174
175         nframes_t output_buffer_position = 0;
176         nframes_t negative_output_buffer_position = 0;
177         if (_position >= _start) {
178                 // handle resizing of beginnings of regions correctly
179                 output_buffer_position = _position - _start;
180         } else {
181                 // when _start is greater than _position, we have to subtract
182                 // _start from the note times in the midi source
183                 negative_output_buffer_position = _start; 
184         }
185         
186         if (src->midi_read (
187                         // the destination buffer
188                         dst,  
189                         // where to start reading in the region
190                         _start + internal_offset, 
191                         // how many bytes
192                         to_read, 
193                         // the offset in the output buffer
194                         output_buffer_position,
195                         // what to substract from note times written in the output buffer
196                         negative_output_buffer_position
197                 ) != to_read) {
198                 return 0; /* "read nothing" */
199         }
200
201         _read_data_count += src->read_data_count();
202
203         return to_read;
204 }
205         
206 XMLNode&
207 MidiRegion::state (bool full)
208 {
209         XMLNode& node (Region::state (full));
210         char buf[64];
211         char buf2[64];
212         LocaleGuard lg (X_("POSIX"));
213         
214         node.add_property ("flags", enum_2_string (_flags));
215
216         // XXX these should move into Region
217         
218         for (uint32_t n=0; n < _sources.size(); ++n) {
219                 snprintf (buf2, sizeof(buf2), "source-%d", n);
220                 _sources[n]->id().print (buf, sizeof(buf));
221                 node.add_property (buf2, buf);
222         }
223
224         for (uint32_t n=0; n < _master_sources.size(); ++n) {
225                 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
226                 _master_sources[n]->id().print (buf, sizeof (buf));
227                 node.add_property (buf2, buf);
228         }
229
230         if (full && _extra_xml) {
231                 node.add_child_copy (*_extra_xml);
232         }
233
234         return node;
235 }
236
237 int
238 MidiRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
239 {
240         const XMLProperty *prop;
241         LocaleGuard lg (X_("POSIX"));
242
243         Region::set_live_state (node, what_changed, false);
244
245         uint32_t old_flags = _flags;
246                 
247         if ((prop = node.property ("flags")) != 0) {
248                 _flags = Flag (string_2_enum (prop->value(), _flags));
249
250                 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
251
252                 _flags = Flag (_flags & ~Region::LeftOfSplit);
253                 _flags = Flag (_flags & ~Region::RightOfSplit);
254         }
255
256         if ((old_flags ^ _flags) & Muted) {
257                 what_changed = Change (what_changed|MuteChanged);
258         }
259         if ((old_flags ^ _flags) & Opaque) {
260                 what_changed = Change (what_changed|OpacityChanged);
261         }
262         if ((old_flags ^ _flags) & Locked) {
263                 what_changed = Change (what_changed|LockChanged);
264         }
265
266         if (send) {
267                 send_change (what_changed);
268         }
269
270         return 0;
271 }
272
273 int
274 MidiRegion::set_state (const XMLNode& node)
275 {
276         /* Region::set_state() calls the virtual set_live_state(),
277            which will get us back to AudioRegion::set_live_state()
278            to handle the relevant stuff.
279         */
280
281         return Region::set_state (node);
282 }
283
284 void
285 MidiRegion::recompute_at_end ()
286 {
287         /* our length has changed
288          * (non destructively) "chop" notes that pass the end boundary, to
289          * prevent stuck notes.
290          */
291 }       
292
293 void
294 MidiRegion::recompute_at_start ()
295 {
296         /* as above, but the shift was from the front
297          * maybe bump currently active note's note-ons up so they sound here?
298          * that could be undesireable in certain situations though.. maybe
299          * remove the note entirely, including it's note off?  something needs to
300          * be done to keep the played MIDI sane to avoid messing up voices of
301          * polyhonic things etc........
302          */
303 }
304
305 int
306 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
307 {
308         // Separate by MIDI channel?  bit different from audio since this is separating based
309         // on the actual contained data and destructively modifies and creates new sources..
310         
311 #if 0
312         SourceList srcs;
313         string new_name;
314
315         for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
316
317                 srcs.clear ();
318                 srcs.push_back (*i);
319
320                 /* generate a new name */
321                 
322                 if (session.region_name (new_name, _name)) {
323                         return -1;
324                 }
325
326                 /* create a copy with just one source */
327
328                 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
329         }
330 #endif
331
332         // Actually, I would prefer not if that's alright
333         return -1;
334 }
335
336 int
337 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
338 {
339         return -1;
340 }
341
342 boost::shared_ptr<MidiSource>
343 MidiRegion::midi_source (uint32_t n) const
344 {
345         // Guaranteed to succeed (use a static cast?)
346         return boost::dynamic_pointer_cast<MidiSource>(source(n));
347 }
348
349
350 void
351 MidiRegion::switch_source(boost::shared_ptr<Source> src)
352 {
353         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
354         if (!msrc)
355                 return;
356
357         // MIDI regions have only one source
358         _sources.clear();
359         _sources.push_back(msrc);
360
361         set_name(msrc->name());
362 }
363