Trim the include tree.
[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 <glibmm/thread.h>
28
29 #include "pbd/basename.h"
30 #include "pbd/xml++.h"
31 #include "pbd/enumwriter.h"
32
33 #include "ardour/automation_control.h"
34 #include "ardour/dB.h"
35 #include "ardour/gain.h"
36 #include "ardour/midi_region.h"
37 #include "ardour/midi_ring_buffer.h"
38 #include "ardour/midi_source.h"
39 #include "ardour/playlist.h"
40 #include "ardour/region_factory.h"
41 #include "ardour/session.h"
42 #include "ardour/types.h"
43
44 #include "i18n.h"
45 #include <locale.h>
46
47 using namespace std;
48 using namespace ARDOUR;
49 using namespace PBD;
50
51 namespace ARDOUR {
52         namespace Properties {
53                 PBD::PropertyDescriptor<void*>                midi_data;
54                 PBD::PropertyDescriptor<Evoral::MusicalTime>  length_beats;
55         }
56 }
57
58 void
59 MidiRegion::make_property_quarks ()
60 {
61         Properties::midi_data.property_id = g_quark_from_static_string (X_("midi-data"));
62         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for midi-data = %1\n", Properties::midi_data.property_id));
63         Properties::length_beats.property_id = g_quark_from_static_string (X_("length-beats"));
64         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for length-beats = %1\n", Properties::length_beats.property_id));
65 }
66
67 void
68 MidiRegion::register_properties ()
69 {
70         add_property (_length_beats);
71 }
72
73 /* Basic MidiRegion constructor (many channels) */
74 MidiRegion::MidiRegion (const SourceList& srcs)
75         : Region (srcs)
76         , _length_beats (Properties::length_beats, midi_source(0)->length_beats())
77 {
78         register_properties ();
79
80         midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
81         model_changed ();
82         assert(_name.val().find("/") == string::npos);
83         assert(_type == DataType::MIDI);
84 }
85
86 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other)
87         : Region (other)
88         , _length_beats (Properties::length_beats, (Evoral::MusicalTime) 0)
89 {
90         update_length_beats ();
91         register_properties ();
92
93         assert(_name.val().find("/") == string::npos);
94         midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
95         model_changed ();
96 }
97
98 /** Create a new MidiRegion, that is part of an existing one */
99 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, frameoffset_t offset)
100         : Region (other, offset)
101         , _length_beats (Properties::length_beats, (Evoral::MusicalTime) 0)
102 {
103         BeatsFramesConverter bfc (_session.tempo_map(), _position);
104         Evoral::MusicalTime const offset_beats = bfc.from (offset);
105
106         _length_beats = other->_length_beats - offset_beats;
107
108         register_properties ();
109
110         assert(_name.val().find("/") == string::npos);
111         midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
112         model_changed ();
113 }
114
115 MidiRegion::~MidiRegion ()
116 {
117 }
118
119 /** Create a new MidiRegion that has its own version of some/all of the Source used by another.
120  */
121 boost::shared_ptr<MidiRegion>
122 MidiRegion::clone () const
123 {
124         BeatsFramesConverter bfc (_session.tempo_map(), _position);
125         Evoral::MusicalTime const bbegin = bfc.from (_start);
126         Evoral::MusicalTime const bend = bfc.from (_start + _length);
127
128         boost::shared_ptr<MidiSource> ms = midi_source(0)->clone (bbegin, bend);
129
130         PropertyList plist;
131
132         plist.add (Properties::name, ms->name());
133         plist.add (Properties::whole_file, true);
134         plist.add (Properties::start, _start);
135         plist.add (Properties::length, _length);
136         plist.add (Properties::length_beats, _length_beats);
137         plist.add (Properties::layer, 0);
138
139         return boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (ms, plist, true));
140 }
141
142 void
143 MidiRegion::post_set (const PropertyChange& pc)
144 {
145         if (pc.contains (Properties::length) && !pc.contains (Properties::length_beats)) {
146                 update_length_beats ();
147         }
148 }
149
150 void
151 MidiRegion::set_length_internal (framecnt_t len)
152 {
153         Region::set_length_internal (len);
154         update_length_beats ();
155 }
156
157 void
158 MidiRegion::update_length_beats ()
159 {
160         BeatsFramesConverter converter (_session.tempo_map(), _position);
161         _length_beats = converter.from (_length);
162 }
163
164 void
165 MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
166 {
167         Region::set_position_internal (pos, allow_bbt_recompute);
168         /* zero length regions don't exist - so if _length_beats is zero, this object
169            is under construction.
170         */
171         if (_length_beats) {
172                 /* leave _length_beats alone, and change _length to reflect the state of things
173                    at the new position (tempo map may dictate a different number of frames
174                 */
175                 BeatsFramesConverter converter (_session.tempo_map(), _position);
176                 Region::set_length_internal (converter.to (_length_beats));
177         }
178 }
179
180 framecnt_t
181 MidiRegion::read_at (Evoral::EventSink<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
182 {
183         return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
184 }
185
186 framecnt_t
187 MidiRegion::master_read_at (MidiRingBuffer<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
188 {
189         return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
190 }
191
192 framecnt_t
193 MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<framepos_t>& dst, framepos_t position, framecnt_t dur, uint32_t chan_n,
194                       NoteMode mode, MidiStateTracker* tracker) const
195 {
196         frameoffset_t internal_offset = 0;
197         framecnt_t to_read         = 0;
198
199         /* precondition: caller has verified that we cover the desired section */
200
201         assert(chan_n == 0);
202
203         if (muted()) {
204                 return 0; /* read nothing */
205         }
206
207         if (position < _position) {
208                 /* we are starting the read from before the start of the region */
209                 internal_offset = 0;
210                 dur -= _position - position;
211         } else {
212                 /* we are starting the read from after the start of the region */
213                 internal_offset = position - _position;
214         }
215
216         if (internal_offset >= _length) {
217                 return 0; /* read nothing */
218         }
219
220         if ((to_read = min (dur, _length - internal_offset)) == 0) {
221                 return 0; /* read nothing */
222         }
223
224         _read_data_count = 0;
225
226         boost::shared_ptr<MidiSource> src = midi_source(chan_n);
227         src->set_note_mode(mode);
228
229         /*
230           cerr << "MR read @ " << position << " * " << to_read
231           << " _position = " << _position
232           << " _start = " << _start
233           << " intoffset = " << internal_offset
234           << endl;
235         */
236
237         /* This call reads events from a source and writes them to `dst' timed in session frames */
238
239         if (src->midi_read (
240                         dst, // destination buffer
241                         _position - _start, // start position of the source in session frames
242                         _start + internal_offset, // where to start reading in the source
243                         to_read, // read duration in frames
244                         tracker,
245                         _filtered_parameters
246                     ) != to_read) {
247                 return 0; /* "read nothing" */
248         }
249
250         _read_data_count += src->read_data_count();
251
252         return to_read;
253 }
254
255 XMLNode&
256 MidiRegion::state ()
257 {
258         return Region::state ();
259 }
260
261 int
262 MidiRegion::set_state (const XMLNode& node, int version)
263 {
264         int ret = Region::set_state (node, version);
265
266         if (ret == 0) {
267                 update_length_beats ();
268         }
269
270         return ret;
271 }
272
273 void
274 MidiRegion::recompute_at_end ()
275 {
276         /* our length has changed
277          * so what? stuck notes are dealt with via a note state tracker
278          */
279 }
280
281 void
282 MidiRegion::recompute_at_start ()
283 {
284         /* as above, but the shift was from the front
285          * maybe bump currently active note's note-ons up so they sound here?
286          * that could be undesireable in certain situations though.. maybe
287          * remove the note entirely, including it's note off?  something needs to
288          * be done to keep the played MIDI sane to avoid messing up voices of
289          * polyhonic things etc........
290          */
291 }
292
293 int
294 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
295 {
296         // TODO
297         return -1;
298 }
299
300 int
301 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
302 {
303         return -1;
304 }
305
306 boost::shared_ptr<MidiSource>
307 MidiRegion::midi_source (uint32_t n) const
308 {
309         // Guaranteed to succeed (use a static cast?)
310         return boost::dynamic_pointer_cast<MidiSource>(source(n));
311 }
312
313 void
314 MidiRegion::model_changed ()
315 {
316         if (!model()) {
317                 return;
318         }
319
320         /* build list of filtered Parameters, being those whose automation state is not `Play' */
321
322         _filtered_parameters.clear ();
323
324         Automatable::Controls const & c = model()->controls();
325
326         for (Automatable::Controls::const_iterator i = c.begin(); i != c.end(); ++i) {
327                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
328                 assert (ac);
329                 if (ac->alist()->automation_state() != Play) {
330                         _filtered_parameters.insert (ac->parameter ());
331                 }
332         }
333
334         /* watch for changes to controls' AutoState */
335         midi_source()->AutomationStateChanged.connect_same_thread (
336                 _model_connection, boost::bind (&MidiRegion::model_automation_state_changed, this, _1)
337                 );
338
339         model()->ContentsChanged.connect_same_thread (
340                 _model_contents_connection, boost::bind (&MidiRegion::model_contents_changed, this));
341 }
342
343 void
344 MidiRegion::model_contents_changed ()
345 {
346         send_change (PropertyChange (Properties::midi_data));
347 }
348
349 void
350 MidiRegion::model_automation_state_changed (Evoral::Parameter const & p)
351 {
352         /* Update our filtered parameters list after a change to a parameter's AutoState */
353
354         boost::shared_ptr<AutomationControl> ac = model()->automation_control (p);
355         assert (ac);
356
357         if (ac->alist()->automation_state() == Play) {
358                 _filtered_parameters.erase (p);
359         } else {
360                 _filtered_parameters.insert (p);
361         }
362
363         /* the source will have an iterator into the model, and that iterator will have been set up
364            for a given set of filtered_parameters, so now that we've changed that list we must invalidate
365            the iterator.
366         */
367         Glib::Mutex::Lock lm (midi_source(0)->mutex());
368         midi_source(0)->invalidate ();
369 }
370
371 /** This is called when a trim drag has resulted in a -ve _start time for this region.
372  *  Fix it up by adding some empty space to the source.
373  */
374 void
375 MidiRegion::fix_negative_start ()
376 {
377         BeatsFramesConverter c (_session.tempo_map(), _position);
378
379         model()->insert_silence_at_start (c.from (-_start));
380         _start = 0;
381 }
382
383 /** Transpose the notes in this region by a given number of semitones */
384 void
385 MidiRegion::transpose (int semitones)
386 {
387         BeatsFramesConverter c (_session.tempo_map(), _start);
388         model()->transpose (c.from (_start), c.from (_start + _length), semitones);
389 }