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