More tinkering with State<>. Use some StateDiffCommands instead of
[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
28 #include <glibmm/thread.h>
29
30 #include "pbd/basename.h"
31 #include "pbd/xml++.h"
32 #include "pbd/enumwriter.h"
33
34 #include "ardour/midi_region.h"
35 #include "ardour/session.h"
36 #include "ardour/gain.h"
37 #include "ardour/dB.h"
38 #include "ardour/playlist.h"
39 #include "ardour/midi_source.h"
40 #include "ardour/types.h"
41 #include "ardour/midi_ring_buffer.h"
42
43 #include "i18n.h"
44 #include <locale.h>
45
46 using namespace std;
47 using namespace ARDOUR;
48 using namespace PBD;
49
50 /** Basic MidiRegion constructor (one channel) */
51 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length)
52         : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::MIDI, 0,  Region::Flag(Region::DefaultFlags|Region::External))
53 {
54         assert(_name.get().find("/") == string::npos);
55         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
56 }
57
58 /* Basic MidiRegion constructor (one channel) */
59 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
60         : Region (src, start, length, name, DataType::MIDI, layer, flags)
61 {
62         assert(_name.get().find("/") == string::npos);
63         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
64 }
65
66 /* Basic MidiRegion constructor (many channels) */
67 MidiRegion::MidiRegion (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
68         : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
69 {
70         assert(_name.get().find("/") == string::npos);
71         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
72 }
73
74
75 /** Create a new MidiRegion, that is part of an existing one */
76 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
77         : Region (other, offset, length, name, layer, flags)
78 {
79         assert(_name.get().find("/") == string::npos);
80         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
81 }
82
83 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other)
84         : Region (other)
85 {
86         assert(_name.get().find("/") == string::npos);
87         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
88 }
89
90 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, const XMLNode& node)
91         : Region (src, node)
92 {
93         if (set_state (node, Stateful::loading_state_version)) {
94                 throw failed_constructor();
95         }
96
97         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
98         assert(_name.get().find("/") == string::npos);
99         assert(_type == DataType::MIDI);
100 }
101
102 MidiRegion::MidiRegion (const SourceList& srcs, const XMLNode& node)
103         : Region (srcs, node)
104 {
105         if (set_state (node, Stateful::loading_state_version)) {
106                 throw failed_constructor();
107         }
108
109         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
110         assert(_name.get().find("/") == string::npos);
111         assert(_type == DataType::MIDI);
112 }
113
114 MidiRegion::~MidiRegion ()
115 {
116 }
117
118 void
119 MidiRegion::set_position_internal (nframes_t pos, bool allow_bbt_recompute)
120 {
121         BeatsFramesConverter old_converter(_session.tempo_map(), _position - _start);
122         double length_beats = old_converter.from(_length);
123
124         Region::set_position_internal(pos, allow_bbt_recompute);
125
126         BeatsFramesConverter new_converter(_session.tempo_map(), pos - _start);
127
128         set_length(new_converter.to(length_beats), 0);
129 }
130
131 nframes_t
132 MidiRegion::read_at (Evoral::EventSink<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
133 {
134         return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
135 }
136
137 nframes_t
138 MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
139 {
140         return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
141 }
142
143 nframes_t
144 MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>& dst, sframes_t position, nframes_t dur, uint32_t chan_n, 
145                       NoteMode mode, MidiStateTracker* tracker) const
146 {
147         nframes_t internal_offset = 0;
148         nframes_t src_offset      = 0;
149         nframes_t to_read         = 0;
150
151         /* precondition: caller has verified that we cover the desired section */
152
153         assert(chan_n == 0);
154
155         if (muted()) {
156                 return 0; /* read nothing */
157         }
158
159         if (position < _position) {
160                 internal_offset = 0;
161                 src_offset = _position - position;
162                 dur -= src_offset;
163         } else {
164                 internal_offset = position - _position;
165                 src_offset = 0;
166         }
167
168         if (internal_offset >= _length) {
169                 return 0; /* read nothing */
170         }
171
172         if ((to_read = min (dur, _length - internal_offset)) == 0) {
173                 return 0; /* read nothing */
174         }
175
176         _read_data_count = 0;
177
178         boost::shared_ptr<MidiSource> src = midi_source(chan_n);
179         src->set_note_mode(mode);
180
181         nframes_t output_buffer_position = 0;
182         nframes_t negative_output_buffer_position = 0;
183         if (_position >= _start) {
184                 // handle resizing of beginnings of regions correctly
185                 output_buffer_position = _position - _start;
186         } else {
187                 // when _start is greater than _position, we have to subtract
188                 // _start from the note times in the midi source
189                 negative_output_buffer_position = _start;
190         }
191
192         /*cerr << "MR read @ " << position << " * " << to_read
193                 << " _position = " << _position
194             << " _start = " << _start
195             << " offset = " << output_buffer_position
196             << " negoffset = " << negative_output_buffer_position
197             << " intoffset = " << internal_offset
198             << endl;*/
199
200         if (src->midi_read (
201                         dst, // destination buffer
202                         _position - _start, // start position of the source in this read context
203                         _start + internal_offset, // where to start reading in the source
204                         to_read, // read duration in frames
205                         output_buffer_position, // the offset in the output buffer
206                         negative_output_buffer_position, // amount to substract from note times
207                         tracker
208                     ) != to_read) {
209                 return 0; /* "read nothing" */
210         }
211
212         _read_data_count += src->read_data_count();
213
214         return to_read;
215 }
216
217 XMLNode&
218 MidiRegion::state (bool full)
219 {
220         XMLNode& node (Region::state (full));
221         char buf[64];
222         char buf2[64];
223         LocaleGuard lg (X_("POSIX"));
224
225         node.add_property ("flags", enum_2_string (_flags));
226
227         // XXX these should move into Region
228
229         for (uint32_t n=0; n < _sources.size(); ++n) {
230                 snprintf (buf2, sizeof(buf2), "source-%d", n);
231                 _sources[n]->id().print (buf, sizeof(buf));
232                 node.add_property (buf2, buf);
233         }
234
235         for (uint32_t n=0; n < _master_sources.size(); ++n) {
236                 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
237                 _master_sources[n]->id().print (buf, sizeof (buf));
238                 node.add_property (buf2, buf);
239         }
240
241         if (full && _extra_xml) {
242                 node.add_child_copy (*_extra_xml);
243         }
244
245         return node;
246 }
247
248 int
249 MidiRegion::set_live_state (const XMLNode& node, int version, Change& what_changed, bool send)
250 {
251         const XMLProperty *prop;
252         LocaleGuard lg (X_("POSIX"));
253
254         Region::set_live_state (node, version, what_changed, false);
255
256         uint32_t old_flags = _flags;
257
258         if ((prop = node.property ("flags")) != 0) {
259                 _flags = Flag (string_2_enum (prop->value(), _flags));
260
261                 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
262
263                 _flags = Flag (_flags & ~Region::LeftOfSplit);
264                 _flags = Flag (_flags & ~Region::RightOfSplit);
265         }
266
267         if ((old_flags ^ _flags) & Muted) {
268                 what_changed = Change (what_changed|MuteChanged);
269         }
270         if ((old_flags ^ _flags) & Opaque) {
271                 what_changed = Change (what_changed|OpacityChanged);
272         }
273         if ((old_flags ^ _flags) & Locked) {
274                 what_changed = Change (what_changed|LockChanged);
275         }
276
277         if (send) {
278                 send_change (what_changed);
279         }
280
281         return 0;
282 }
283
284 int
285 MidiRegion::set_state (const XMLNode& node, int version)
286 {
287         /* Region::set_state() calls the virtual set_live_state(),
288            which will get us back to AudioRegion::set_live_state()
289            to handle the relevant stuff.
290         */
291
292         return Region::set_state (node, version);
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 (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
318 {
319         // TODO
320         return -1;
321 }
322
323 int
324 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
325 {
326         return -1;
327 }
328
329 boost::shared_ptr<MidiSource>
330 MidiRegion::midi_source (uint32_t n) const
331 {
332         // Guaranteed to succeed (use a static cast?)
333         return boost::dynamic_pointer_cast<MidiSource>(source(n));
334 }
335
336
337 void
338 MidiRegion::switch_source(boost::shared_ptr<Source> src)
339 {
340         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
341         if (!msrc)
342                 return;
343
344         // MIDI regions have only one source
345         _sources.clear();
346         _sources.push_back(msrc);
347
348         set_name(msrc->name());
349 }
350