(FULL commit) start of a nominal debug tracing system, with 64 bits available for...
[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, Stateful::loading_state_version)) {
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, Stateful::loading_state_version)) {
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 void
120 MidiRegion::set_position_internal (nframes_t pos, bool allow_bbt_recompute)
121 {
122         BeatsFramesConverter old_converter(_session, _position - _start);
123         double length_beats = old_converter.from(_length);
124
125         Region::set_position_internal(pos, allow_bbt_recompute);
126
127         BeatsFramesConverter new_converter(_session, pos - _start);
128
129         set_length(new_converter.to(length_beats), 0);
130 }
131
132 nframes_t
133 MidiRegion::read_at (Evoral::EventSink<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
134 {
135         return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
136 }
137
138 nframes_t
139 MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
140 {
141         return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
142 }
143
144 nframes_t
145 MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>& dst, sframes_t position, nframes_t dur, uint32_t chan_n, 
146                       NoteMode mode, MidiStateTracker* tracker) const
147 {
148         nframes_t internal_offset = 0;
149         nframes_t src_offset      = 0;
150         nframes_t to_read         = 0;
151
152         /* precondition: caller has verified that we cover the desired section */
153
154         assert(chan_n == 0);
155
156         if (position < _position) {
157                 internal_offset = 0;
158                 src_offset = _position - position;
159                 dur -= src_offset;
160         } else {
161                 internal_offset = position - _position;
162                 src_offset = 0;
163         }
164
165         if (internal_offset >= _length) {
166                 return 0; /* read nothing */
167         }
168
169         if ((to_read = min (dur, _length - internal_offset)) == 0) {
170                 return 0; /* read nothing */
171         }
172
173         // FIXME: non-opaque MIDI regions not yet supported
174         assert(opaque());
175
176         if (muted()) {
177                 return 0; /* read nothing */
178         }
179
180         _read_data_count = 0;
181
182         boost::shared_ptr<MidiSource> src = midi_source(chan_n);
183         src->set_note_mode(mode);
184
185         nframes_t output_buffer_position = 0;
186         nframes_t negative_output_buffer_position = 0;
187         if (_position >= _start) {
188                 // handle resizing of beginnings of regions correctly
189                 output_buffer_position = _position - _start;
190         } else {
191                 // when _start is greater than _position, we have to subtract
192                 // _start from the note times in the midi source
193                 negative_output_buffer_position = _start;
194         }
195
196         /*cerr << "MR read @ " << position << " * " << to_read
197                 << " _position = " << _position
198             << " _start = " << _start
199             << " offset = " << output_buffer_position
200             << " negoffset = " << negative_output_buffer_position
201             << " intoffset = " << internal_offset
202             << endl;*/
203
204         if (src->midi_read (
205                         dst, // destination buffer
206                         _position - _start, // start position of the source in this read context
207                         _start + internal_offset, // where to start reading in the source
208                         to_read, // read duration in frames
209                         output_buffer_position, // the offset in the output buffer
210                         negative_output_buffer_position, // amount to substract from note times
211                         tracker
212                     ) != to_read) {
213                 return 0; /* "read nothing" */
214         }
215
216         _read_data_count += src->read_data_count();
217
218         return to_read;
219 }
220
221 XMLNode&
222 MidiRegion::state (bool full)
223 {
224         XMLNode& node (Region::state (full));
225         char buf[64];
226         char buf2[64];
227         LocaleGuard lg (X_("POSIX"));
228
229         node.add_property ("flags", enum_2_string (_flags));
230
231         // XXX these should move into Region
232
233         for (uint32_t n=0; n < _sources.size(); ++n) {
234                 snprintf (buf2, sizeof(buf2), "source-%d", n);
235                 _sources[n]->id().print (buf, sizeof(buf));
236                 node.add_property (buf2, buf);
237         }
238
239         for (uint32_t n=0; n < _master_sources.size(); ++n) {
240                 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
241                 _master_sources[n]->id().print (buf, sizeof (buf));
242                 node.add_property (buf2, buf);
243         }
244
245         if (full && _extra_xml) {
246                 node.add_child_copy (*_extra_xml);
247         }
248
249         return node;
250 }
251
252 int
253 MidiRegion::set_live_state (const XMLNode& node, int version, Change& what_changed, bool send)
254 {
255         const XMLProperty *prop;
256         LocaleGuard lg (X_("POSIX"));
257
258         Region::set_live_state (node, version, what_changed, false);
259
260         uint32_t old_flags = _flags;
261
262         if ((prop = node.property ("flags")) != 0) {
263                 _flags = Flag (string_2_enum (prop->value(), _flags));
264
265                 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
266
267                 _flags = Flag (_flags & ~Region::LeftOfSplit);
268                 _flags = Flag (_flags & ~Region::RightOfSplit);
269         }
270
271         if ((old_flags ^ _flags) & Muted) {
272                 what_changed = Change (what_changed|MuteChanged);
273         }
274         if ((old_flags ^ _flags) & Opaque) {
275                 what_changed = Change (what_changed|OpacityChanged);
276         }
277         if ((old_flags ^ _flags) & Locked) {
278                 what_changed = Change (what_changed|LockChanged);
279         }
280
281         if (send) {
282                 send_change (what_changed);
283         }
284
285         return 0;
286 }
287
288 int
289 MidiRegion::set_state (const XMLNode& node, int version)
290 {
291         /* Region::set_state() calls the virtual set_live_state(),
292            which will get us back to AudioRegion::set_live_state()
293            to handle the relevant stuff.
294         */
295
296         return Region::set_state (node, version);
297 }
298
299 void
300 MidiRegion::recompute_at_end ()
301 {
302         /* our length has changed
303          * (non destructively) "chop" notes that pass the end boundary, to
304          * prevent stuck notes.
305          */
306 }
307
308 void
309 MidiRegion::recompute_at_start ()
310 {
311         /* as above, but the shift was from the front
312          * maybe bump currently active note's note-ons up so they sound here?
313          * that could be undesireable in certain situations though.. maybe
314          * remove the note entirely, including it's note off?  something needs to
315          * be done to keep the played MIDI sane to avoid messing up voices of
316          * polyhonic things etc........
317          */
318 }
319
320 int
321 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
322 {
323         // TODO
324         return -1;
325 }
326
327 int
328 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
329 {
330         return -1;
331 }
332
333 boost::shared_ptr<MidiSource>
334 MidiRegion::midi_source (uint32_t n) const
335 {
336         // Guaranteed to succeed (use a static cast?)
337         return boost::dynamic_pointer_cast<MidiSource>(source(n));
338 }
339
340
341 void
342 MidiRegion::switch_source(boost::shared_ptr<Source> src)
343 {
344         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
345         if (!msrc)
346                 return;
347
348         // MIDI regions have only one source
349         _sources.clear();
350         _sources.push_back(msrc);
351
352         set_name(msrc->name());
353 }
354