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