2 Copyright (C) 2000-2006 Paul Davis
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.
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.
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.
18 $Id: midiregion.cc 746 2006-08-02 02:44:23Z drobilla $
27 #include <glibmm/threads.h>
28 #include <glibmm/fileutils.h>
29 #include <glibmm/miscutils.h>
31 #include "evoral/types.hpp"
33 #include "pbd/xml++.h"
34 #include "pbd/basename.h"
36 #include "ardour/automation_control.h"
37 #include "ardour/midi_model.h"
38 #include "ardour/midi_region.h"
39 #include "ardour/midi_ring_buffer.h"
40 #include "ardour/midi_source.h"
41 #include "ardour/region_factory.h"
42 #include "ardour/session.h"
43 #include "ardour/source_factory.h"
44 #include "ardour/tempo.h"
45 #include "ardour/types.h"
51 using namespace ARDOUR;
55 namespace Properties {
56 PBD::PropertyDescriptor<Evoral::Beats> start_beats;
57 PBD::PropertyDescriptor<Evoral::Beats> length_beats;
62 MidiRegion::make_property_quarks ()
64 Properties::start_beats.property_id = g_quark_from_static_string (X_("start-beats"));
65 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for start-beats = %1\n", Properties::start_beats.property_id));
66 Properties::length_beats.property_id = g_quark_from_static_string (X_("length-beats"));
67 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for length-beats = %1\n", Properties::length_beats.property_id));
71 MidiRegion::register_properties ()
73 add_property (_start_beats);
74 add_property (_length_beats);
77 /* Basic MidiRegion constructor (many channels) */
78 MidiRegion::MidiRegion (const SourceList& srcs)
80 , _start_beats (Properties::start_beats, Evoral::Beats())
81 , _length_beats (Properties::length_beats, midi_source(0)->length_beats())
83 register_properties ();
85 midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
87 assert(_name.val().find("/") == string::npos);
88 assert(_type == DataType::MIDI);
91 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other)
93 , _start_beats (Properties::start_beats, other->_start_beats)
94 , _length_beats (Properties::length_beats, Evoral::Beats())
96 update_length_beats ();
97 register_properties ();
99 assert(_name.val().find("/") == string::npos);
100 midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
104 /** Create a new MidiRegion that is part of an existing one */
105 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, frameoffset_t offset)
106 : Region (other, offset)
107 , _start_beats (Properties::start_beats, Evoral::Beats())
108 , _length_beats (Properties::length_beats, Evoral::Beats())
110 BeatsFramesConverter bfc (_session.tempo_map(), _position);
111 Evoral::Beats const offset_beats = bfc.from (offset);
113 _start_beats = other->_start_beats.val() + offset_beats;
114 _length_beats = other->_length_beats.val() - offset_beats;
116 register_properties ();
118 assert(_name.val().find("/") == string::npos);
119 midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
123 MidiRegion::~MidiRegion ()
127 /** Create a new MidiRegion that has its own version of some/all of the Source used by another.
129 boost::shared_ptr<MidiRegion>
130 MidiRegion::clone (string path) const
132 boost::shared_ptr<MidiSource> newsrc;
134 /* caller must check for pre-existing file */
135 assert (!path.empty());
136 assert (!Glib::file_test (path, Glib::FILE_TEST_EXISTS));
137 newsrc = boost::dynamic_pointer_cast<MidiSource>(
138 SourceFactory::createWritable(DataType::MIDI, _session,
139 path, false, _session.frame_rate()));
140 return clone (newsrc);
143 boost::shared_ptr<MidiRegion>
144 MidiRegion::clone (boost::shared_ptr<MidiSource> newsrc) const
146 BeatsFramesConverter bfc (_session.tempo_map(), _position);
147 Evoral::Beats const bbegin = bfc.from (_start);
148 Evoral::Beats const bend = bfc.from (_start + _length);
151 /* Lock our source since we'll be reading from it. write_to() will
152 take a lock on newsrc. */
153 Source::Lock lm (midi_source(0)->mutex());
154 if (midi_source(0)->write_to (lm, newsrc, bbegin, bend)) {
155 return boost::shared_ptr<MidiRegion> ();
161 plist.add (Properties::name, PBD::basename_nosuffix (newsrc->name()));
162 plist.add (Properties::whole_file, true);
163 plist.add (Properties::start, _start);
164 plist.add (Properties::start_beats, _start_beats);
165 plist.add (Properties::length, _length);
166 plist.add (Properties::length_beats, _length_beats);
167 plist.add (Properties::layer, 0);
169 return boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (newsrc, plist, true));
173 MidiRegion::post_set (const PropertyChange& pc)
175 Region::post_set (pc);
177 if (pc.contains (Properties::length) && !pc.contains (Properties::length_beats)) {
178 update_length_beats ();
179 } else if (pc.contains (Properties::start) && !pc.contains (Properties::start_beats)) {
180 set_start_beats_from_start_frames ();
185 MidiRegion::set_start_beats_from_start_frames ()
187 BeatsFramesConverter c (_session.tempo_map(), _position - _start);
188 _start_beats = c.from (_start);
192 MidiRegion::set_length_internal (framecnt_t len)
194 Region::set_length_internal (len);
195 update_length_beats ();
199 MidiRegion::update_after_tempo_map_change ()
201 Region::update_after_tempo_map_change ();
203 /* _position has now been updated for the new tempo map */
204 _start = _position - _session.tempo_map().framepos_minus_beats (_position, _start_beats);
206 send_change (Properties::start);
210 MidiRegion::update_length_beats ()
212 BeatsFramesConverter converter (_session.tempo_map(), _position);
213 _length_beats = converter.from (_length);
217 MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
219 Region::set_position_internal (pos, allow_bbt_recompute);
220 /* zero length regions don't exist - so if _length_beats is zero, this object
221 is under construction.
223 if (_length_beats.val() == Evoral::Beats()) {
224 /* leave _length_beats alone, and change _length to reflect the state of things
225 at the new position (tempo map may dictate a different number of frames
227 BeatsFramesConverter converter (_session.tempo_map(), _position);
228 Region::set_length_internal (converter.to (_length_beats));
233 MidiRegion::read_at (Evoral::EventSink<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
235 return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
239 MidiRegion::master_read_at (MidiRingBuffer<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
241 return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
245 MidiRegion::_read_at (const SourceList& /*srcs*/,
246 Evoral::EventSink<framepos_t>& dst,
251 MidiStateTracker* tracker) const
253 frameoffset_t internal_offset = 0;
254 framecnt_t to_read = 0;
256 /* precondition: caller has verified that we cover the desired section */
261 return 0; /* read nothing */
264 if (position < _position) {
265 /* we are starting the read from before the start of the region */
267 dur -= _position - position;
269 /* we are starting the read from after the start of the region */
270 internal_offset = position - _position;
273 if (internal_offset >= _length) {
274 return 0; /* read nothing */
277 if ((to_read = min (dur, _length - internal_offset)) == 0) {
278 return 0; /* read nothing */
281 boost::shared_ptr<MidiSource> src = midi_source(chan_n);
283 Glib::Threads::Mutex::Lock lm(src->mutex());
285 src->set_note_mode(lm, mode);
288 cerr << "MR " << name () << " read @ " << position << " * " << to_read
289 << " _position = " << _position
290 << " _start = " << _start
291 << " intoffset = " << internal_offset
295 /* This call reads events from a source and writes them to `dst' timed in session frames */
299 dst, // destination buffer
300 _position - _start, // start position of the source in session frames
301 _start + internal_offset, // where to start reading in the source
302 to_read, // read duration in frames
306 return 0; /* "read nothing" */
315 return Region::state ();
319 MidiRegion::set_state (const XMLNode& node, int version)
321 int ret = Region::set_state (node, version);
324 update_length_beats ();
331 MidiRegion::recompute_at_end ()
333 /* our length has changed
334 * so what? stuck notes are dealt with via a note state tracker
339 MidiRegion::recompute_at_start ()
341 /* as above, but the shift was from the front
342 * maybe bump currently active note's note-ons up so they sound here?
343 * that could be undesireable in certain situations though.. maybe
344 * remove the note entirely, including it's note off? something needs to
345 * be done to keep the played MIDI sane to avoid messing up voices of
346 * polyhonic things etc........
351 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
357 boost::shared_ptr<Evoral::Control>
358 MidiRegion::control (const Evoral::Parameter& id, bool create)
360 return model()->control(id, create);
363 boost::shared_ptr<const Evoral::Control>
364 MidiRegion::control (const Evoral::Parameter& id) const
366 return model()->control(id);
369 boost::shared_ptr<MidiModel>
372 return midi_source()->model();
375 boost::shared_ptr<const MidiModel>
376 MidiRegion::model() const
378 return midi_source()->model();
381 boost::shared_ptr<MidiSource>
382 MidiRegion::midi_source (uint32_t n) const
384 // Guaranteed to succeed (use a static cast?)
385 return boost::dynamic_pointer_cast<MidiSource>(source(n));
389 MidiRegion::model_changed ()
395 /* build list of filtered Parameters, being those whose automation state is not `Play' */
397 _filtered_parameters.clear ();
399 Automatable::Controls const & c = model()->controls();
401 for (Automatable::Controls::const_iterator i = c.begin(); i != c.end(); ++i) {
402 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
404 if (ac->alist()->automation_state() != Play) {
405 _filtered_parameters.insert (ac->parameter ());
409 /* watch for changes to controls' AutoState */
410 midi_source()->AutomationStateChanged.connect_same_thread (
411 _model_connection, boost::bind (&MidiRegion::model_automation_state_changed, this, _1)
416 MidiRegion::model_automation_state_changed (Evoral::Parameter const & p)
418 /* Update our filtered parameters list after a change to a parameter's AutoState */
420 boost::shared_ptr<AutomationControl> ac = model()->automation_control (p);
421 if (!ac || ac->alist()->automation_state() == Play) {
422 /* It should be "impossible" for ac to be NULL, but if it is, don't
423 filter the parameter so events aren't lost. */
424 _filtered_parameters.erase (p);
426 _filtered_parameters.insert (p);
429 /* the source will have an iterator into the model, and that iterator will have been set up
430 for a given set of filtered_parameters, so now that we've changed that list we must invalidate
433 Glib::Threads::Mutex::Lock lm (midi_source(0)->mutex(), Glib::Threads::TRY_LOCK);
435 /* TODO: This is too aggressive, we need more fine-grained invalidation. */
436 midi_source(0)->invalidate (lm);
440 /** This is called when a trim drag has resulted in a -ve _start time for this region.
441 * Fix it up by adding some empty space to the source.
444 MidiRegion::fix_negative_start ()
446 BeatsFramesConverter c (_session.tempo_map(), _position);
448 model()->insert_silence_at_start (c.from (-_start));
450 _start_beats = Evoral::Beats();
453 /** Transpose the notes in this region by a given number of semitones */
455 MidiRegion::transpose (int semitones)
457 BeatsFramesConverter c (_session.tempo_map(), _start);
458 model()->transpose (c.from (_start), c.from (_start + _length), semitones);
462 MidiRegion::set_start_internal (framecnt_t s)
464 Region::set_start_internal (s);
465 set_start_beats_from_start_frames ();