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/Beats.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, other->_length_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, const int32_t sub_num)
106 : Region (other, offset, sub_num)
107 , _start_beats (Properties::start_beats, Evoral::Beats())
108 , _length_beats (Properties::length_beats, other->_length_beats)
110 _start_beats = Evoral::Beats (_session.tempo_map().exact_beat_at_frame ((other->_position + offset), sub_num) - other->beat()) + other->_start_beats;
111 update_length_beats (sub_num);
112 register_properties ();
114 assert(_name.val().find("/") == string::npos);
115 midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
119 MidiRegion::~MidiRegion ()
123 /** Export the MIDI data of the MidiRegion to a new MIDI file (SMF).
126 MidiRegion::do_export (string path) const
128 boost::shared_ptr<MidiSource> newsrc;
130 /* caller must check for pre-existing file */
131 assert (!path.empty());
132 assert (!Glib::file_test (path, Glib::FILE_TEST_EXISTS));
133 newsrc = boost::dynamic_pointer_cast<MidiSource>(
134 SourceFactory::createWritable(DataType::MIDI, _session,
135 path, false, _session.frame_rate()));
137 BeatsFramesConverter bfc (_session.tempo_map(), _position);
138 Evoral::Beats const bbegin = bfc.from (_start);
139 Evoral::Beats const bend = bfc.from (_start + _length);
142 /* Lock our source since we'll be reading from it. write_to() will
143 take a lock on newsrc. */
144 Source::Lock lm (midi_source(0)->mutex());
145 if (midi_source(0)->export_write_to (lm, newsrc, bbegin, bend)) {
154 /** Create a new MidiRegion that has its own version of some/all of the Source used by another.
156 boost::shared_ptr<MidiRegion>
157 MidiRegion::clone (string path) const
159 boost::shared_ptr<MidiSource> newsrc;
161 /* caller must check for pre-existing file */
162 assert (!path.empty());
163 assert (!Glib::file_test (path, Glib::FILE_TEST_EXISTS));
164 newsrc = boost::dynamic_pointer_cast<MidiSource>(
165 SourceFactory::createWritable(DataType::MIDI, _session,
166 path, false, _session.frame_rate()));
167 return clone (newsrc);
170 boost::shared_ptr<MidiRegion>
171 MidiRegion::clone (boost::shared_ptr<MidiSource> newsrc) const
173 BeatsFramesConverter bfc (_session.tempo_map(), _position);
174 Evoral::Beats const bbegin = bfc.from (_start);
175 Evoral::Beats const bend = bfc.from (_start + _length);
178 /* Lock our source since we'll be reading from it. write_to() will
179 take a lock on newsrc. */
180 Source::Lock lm (midi_source(0)->mutex());
181 if (midi_source(0)->write_to (lm, newsrc, bbegin, bend)) {
182 return boost::shared_ptr<MidiRegion> ();
188 plist.add (Properties::name, PBD::basename_nosuffix (newsrc->name()));
189 plist.add (Properties::whole_file, true);
190 plist.add (Properties::start, _start);
191 plist.add (Properties::start_beats, _start_beats);
192 plist.add (Properties::length, _length);
193 plist.add (Properties::length_beats, _length_beats);
194 plist.add (Properties::layer, 0);
196 boost::shared_ptr<MidiRegion> ret (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (newsrc, plist, true)));
197 ret->set_beat (beat());
203 MidiRegion::post_set (const PropertyChange& pc)
205 Region::post_set (pc);
207 if (pc.contains (Properties::length) && !pc.contains (Properties::length_beats)) {
208 /* update non-musically */
209 update_length_beats (0);
210 } else if (pc.contains (Properties::start) && !pc.contains (Properties::start_beats)) {
211 set_start_beats_from_start_frames ();
216 MidiRegion::set_start_beats_from_start_frames ()
218 _start_beats = Evoral::Beats (beat() - _session.tempo_map().beat_at_frame (_position - _start));
222 MidiRegion::set_length_internal (framecnt_t len, const int32_t sub_num)
224 Region::set_length_internal (len, sub_num);
225 update_length_beats (sub_num);
229 MidiRegion::update_after_tempo_map_change (bool /* send */)
231 boost::shared_ptr<Playlist> pl (playlist());
233 if (!pl || position_lock_style() != MusicTime) {
237 const framepos_t old_pos = _position;
238 const framepos_t old_length = _length;
239 const framepos_t old_start = _start;
241 Region::update_after_tempo_map_change (false);
243 /* _start has now been updated. */
244 _length = _session.tempo_map().frame_at_beat (beat() + _length_beats.val().to_double()) - _position;
246 PropertyChange s_and_l;
247 if (old_start != _start) {
248 s_and_l.add (Properties::start);
250 if (old_length != _length) {
251 s_and_l.add (Properties::length);
253 if (old_pos != _position) {
254 s_and_l.add (Properties::position);
257 send_change (s_and_l);
261 MidiRegion::update_length_beats (const int32_t sub_num)
263 _length_beats = Evoral::Beats (_session.tempo_map().exact_beat_at_frame (_position + _length, sub_num) - beat());
267 MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute, const int32_t sub_num)
269 Region::set_position_internal (pos, allow_bbt_recompute, sub_num);
271 /* set _start to new position in tempo map */
272 _start = _position - _session.tempo_map().frame_at_beat (beat() - _start_beats.val().to_double());
274 /* in construction from src */
275 if (_length_beats == Evoral::Beats()) {
276 update_length_beats (sub_num);
279 /* leave _length_beats alone, and change _length to reflect the state of things
280 at the new position (tempo map may dictate a different number of frames).
282 Region::set_length_internal (_session.tempo_map().frame_at_beat (beat() + _length_beats.val().to_double()) - _position, sub_num);
286 MidiRegion::read_at (Evoral::EventSink<framepos_t>& out,
291 MidiStateTracker* tracker,
292 MidiChannelFilter* filter) const
294 return _read_at (_sources, out, position, dur, chan_n, mode, tracker, filter);
298 MidiRegion::master_read_at (MidiRingBuffer<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
300 return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
304 MidiRegion::_read_at (const SourceList& /*srcs*/,
305 Evoral::EventSink<framepos_t>& dst,
310 MidiStateTracker* tracker,
311 MidiChannelFilter* filter) const
313 frameoffset_t internal_offset = 0;
314 framecnt_t to_read = 0;
316 /* precondition: caller has verified that we cover the desired section */
321 return 0; /* read nothing */
324 if (position < _position) {
325 /* we are starting the read from before the start of the region */
327 dur -= _position - position;
329 /* we are starting the read from after the start of the region */
330 internal_offset = position - _position;
333 if (internal_offset >= _length) {
334 return 0; /* read nothing */
337 if ((to_read = min (dur, _length - internal_offset)) == 0) {
338 return 0; /* read nothing */
341 boost::shared_ptr<MidiSource> src = midi_source(chan_n);
343 Glib::Threads::Mutex::Lock lm(src->mutex());
345 src->set_note_mode(lm, mode);
348 cerr << "MR " << name () << " read @ " << position << " * " << to_read
349 << " _position = " << _position
350 << " _start = " << _start
351 << " intoffset = " << internal_offset
355 /* This call reads events from a source and writes them to `dst' timed in session frames */
359 dst, // destination buffer
360 _position - _start, // start position of the source in session frames
361 _start + internal_offset, // where to start reading in the source
362 to_read, // read duration in frames
365 _filtered_parameters,
367 _start_beats.val().to_double()
369 return 0; /* "read nothing" */
378 return Region::state ();
382 MidiRegion::set_state (const XMLNode& node, int version)
384 int ret = Region::set_state (node, version);
387 /* set length beats to the frame (non-musical) */
388 if (position_lock_style() == AudioTime) {
389 update_length_beats (0);
397 MidiRegion::recompute_at_end ()
399 /* our length has changed
400 * so what? stuck notes are dealt with via a note state tracker
405 MidiRegion::recompute_at_start ()
407 /* as above, but the shift was from the front
408 * maybe bump currently active note's note-ons up so they sound here?
409 * that could be undesireable in certain situations though.. maybe
410 * remove the note entirely, including it's note off? something needs to
411 * be done to keep the played MIDI sane to avoid messing up voices of
412 * polyhonic things etc........
417 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
423 boost::shared_ptr<Evoral::Control>
424 MidiRegion::control (const Evoral::Parameter& id, bool create)
426 return model()->control(id, create);
429 boost::shared_ptr<const Evoral::Control>
430 MidiRegion::control (const Evoral::Parameter& id) const
432 return model()->control(id);
435 boost::shared_ptr<MidiModel>
438 return midi_source()->model();
441 boost::shared_ptr<const MidiModel>
442 MidiRegion::model() const
444 return midi_source()->model();
447 boost::shared_ptr<MidiSource>
448 MidiRegion::midi_source (uint32_t n) const
450 // Guaranteed to succeed (use a static cast?)
451 return boost::dynamic_pointer_cast<MidiSource>(source(n));
455 MidiRegion::model_changed ()
461 /* build list of filtered Parameters, being those whose automation state is not `Play' */
463 _filtered_parameters.clear ();
465 Automatable::Controls const & c = model()->controls();
467 for (Automatable::Controls::const_iterator i = c.begin(); i != c.end(); ++i) {
468 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
470 if (ac->alist()->automation_state() != Play) {
471 _filtered_parameters.insert (ac->parameter ());
475 /* watch for changes to controls' AutoState */
476 midi_source()->AutomationStateChanged.connect_same_thread (
477 _model_connection, boost::bind (&MidiRegion::model_automation_state_changed, this, _1)
482 MidiRegion::model_automation_state_changed (Evoral::Parameter const & p)
484 /* Update our filtered parameters list after a change to a parameter's AutoState */
486 boost::shared_ptr<AutomationControl> ac = model()->automation_control (p);
487 if (!ac || ac->alist()->automation_state() == Play) {
488 /* It should be "impossible" for ac to be NULL, but if it is, don't
489 filter the parameter so events aren't lost. */
490 _filtered_parameters.erase (p);
492 _filtered_parameters.insert (p);
495 /* the source will have an iterator into the model, and that iterator will have been set up
496 for a given set of filtered_parameters, so now that we've changed that list we must invalidate
499 Glib::Threads::Mutex::Lock lm (midi_source(0)->mutex(), Glib::Threads::TRY_LOCK);
501 /* TODO: This is too aggressive, we need more fine-grained invalidation. */
502 midi_source(0)->invalidate (lm);
506 /** This is called when a trim drag has resulted in a -ve _start time for this region.
507 * Fix it up by adding some empty space to the source.
510 MidiRegion::fix_negative_start ()
512 BeatsFramesConverter c (_session.tempo_map(), _position);
514 model()->insert_silence_at_start (c.from (-_start));
516 _start_beats = Evoral::Beats();
520 MidiRegion::set_start_internal (framecnt_t s, const int32_t sub_num)
522 Region::set_start_internal (s, sub_num);
524 if (position_lock_style() == AudioTime) {
525 set_start_beats_from_start_frames ();
530 MidiRegion::trim_to_internal (framepos_t position, framecnt_t length, const int32_t sub_num)
532 framepos_t new_start;
538 PropertyChange what_changed;
540 /* beat has been set exactly by set_position_internal, but the source starts on a frame.
541 working in beats seems the correct thing to do, but reports of a missing first note
542 on playback suggest otherwise. for now, we work in exact beats.
544 const double pos_beat = _session.tempo_map().exact_beat_at_frame (position, sub_num);
545 const double beat_delta = pos_beat - beat();
547 /* Set position before length, otherwise for MIDI regions this bad thing happens:
548 * 1. we call set_length_internal; length in beats is computed using the region's current
549 * (soon-to-be old) position
550 * 2. we call set_position_internal; position is set and length in frames re-computed using
551 * length in beats from (1) but at the new position, which is wrong if the region
552 * straddles a tempo/meter change.
555 if (_position != position) {
556 set_position_internal (position, true, sub_num);
557 what_changed.add (Properties::position);
560 const double new_start_beat = _start_beats.val().to_double() + beat_delta;
561 new_start = _position - _session.tempo_map().frame_at_beat (beat() - new_start_beat);
563 if (!verify_start_and_length (new_start, length)) {
567 if (_start != new_start) {
568 _start_beats = Evoral::Beats (new_start_beat);
569 what_changed.add (Properties::start_beats);
571 set_start_internal (new_start, sub_num);
572 what_changed.add (Properties::start);
575 if (_length != length) {
576 set_length_internal (length, sub_num);
577 what_changed.add (Properties::length);
578 what_changed.add (Properties::length_beats);
581 set_whole_file (false);
583 PropertyChange start_and_length;
585 start_and_length.add (Properties::start);
586 start_and_length.add (Properties::length);
588 if (what_changed.contains (start_and_length)) {
592 if (!what_changed.empty()) {
593 send_change (what_changed);