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