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/thread.h>
29 #include "pbd/basename.h"
30 #include "pbd/xml++.h"
31 #include "pbd/enumwriter.h"
33 #include "ardour/midi_region.h"
34 #include "ardour/session.h"
35 #include "ardour/gain.h"
36 #include "ardour/dB.h"
37 #include "ardour/playlist.h"
38 #include "ardour/midi_source.h"
39 #include "ardour/region_factory.h"
40 #include "ardour/types.h"
41 #include "ardour/midi_ring_buffer.h"
47 using namespace ARDOUR;
51 namespace Properties {
52 PBD::PropertyDescriptor<void*> midi_data;
53 PBD::PropertyDescriptor<Evoral::MusicalTime> length_beats;
58 MidiRegion::make_property_quarks ()
60 Properties::midi_data.property_id = g_quark_from_static_string (X_("midi-data"));
61 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for midi-data = %1\n", Properties::midi_data.property_id));
62 Properties::length_beats.property_id = g_quark_from_static_string (X_("length-beats"));
63 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for length-beats = %1\n", Properties::length_beats.property_id));
67 MidiRegion::register_properties ()
69 add_property (_length_beats);
72 /* Basic MidiRegion constructor (many channels) */
73 MidiRegion::MidiRegion (const SourceList& srcs)
75 , _length_beats (Properties::length_beats, midi_source(0)->length_beats())
77 register_properties ();
79 midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
81 assert(_name.val().find("/") == string::npos);
82 assert(_type == DataType::MIDI);
85 /** Create a new MidiRegion, that is part of an existing one */
86 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, frameoffset_t offset, bool offset_relative)
87 : Region (other, offset, offset_relative)
88 , _length_beats (Properties::length_beats, (Evoral::MusicalTime) 0)
90 update_length_beats ();
91 register_properties ();
93 assert(_name.val().find("/") == string::npos);
94 midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
98 MidiRegion::~MidiRegion ()
102 /** Create a new MidiRegion that has its own version of some/all of the Source used by another.
104 boost::shared_ptr<MidiRegion>
107 BeatsFramesConverter bfc (_session.tempo_map(), _position);
108 Evoral::MusicalTime const bbegin = bfc.from (_start);
109 Evoral::MusicalTime const bend = bfc.from (_start + _length);
111 boost::shared_ptr<MidiSource> ms = midi_source(0)->clone (bbegin, bend);
115 plist.add (Properties::name, ms->name());
116 plist.add (Properties::whole_file, true);
117 plist.add (Properties::start, _start);
118 plist.add (Properties::length, _length);
119 plist.add (Properties::length_beats, _length_beats);
120 plist.add (Properties::layer, 0);
122 return boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (ms, plist, true));
126 MidiRegion::post_set (const PropertyChange& pc)
128 if (pc.contains (Properties::length) || pc.contains (Properties::position)) {
129 update_length_beats ();
134 MidiRegion::set_length_internal (framecnt_t len)
136 Region::set_length_internal (len);
137 update_length_beats ();
141 MidiRegion::update_length_beats ()
143 BeatsFramesConverter converter (_session.tempo_map(), _position);
144 _length_beats = converter.from (_length);
148 MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
150 Region::set_position_internal (pos, allow_bbt_recompute);
151 /* zero length regions don't exist - so if _length_beats is zero, this object
152 is under construction.
155 /* leave _length_beats alone, and change _length to reflect the state of things
156 at the new position (tempo map may dictate a different number of frames
158 BeatsFramesConverter converter (_session.tempo_map(), _position);
159 Region::set_length_internal (converter.to (_length_beats));
164 MidiRegion::read_at (Evoral::EventSink<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
166 return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
170 MidiRegion::master_read_at (MidiRingBuffer<framepos_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
172 return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
176 MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<framepos_t>& dst, framepos_t position, framecnt_t dur, uint32_t chan_n,
177 NoteMode mode, MidiStateTracker* tracker) const
179 frameoffset_t internal_offset = 0;
180 framecnt_t to_read = 0;
182 /* precondition: caller has verified that we cover the desired section */
187 return 0; /* read nothing */
190 if (position < _position) {
191 /* we are starting the read from before the start of the region */
193 dur -= _position - position;
195 /* we are starting the read from after the start of the region */
196 internal_offset = position - _position;
199 if (internal_offset >= _length) {
200 return 0; /* read nothing */
203 if ((to_read = min (dur, _length - internal_offset)) == 0) {
204 return 0; /* read nothing */
207 _read_data_count = 0;
209 boost::shared_ptr<MidiSource> src = midi_source(chan_n);
210 src->set_note_mode(mode);
213 cerr << "MR read @ " << position << " * " << to_read
214 << " _position = " << _position
215 << " _start = " << _start
216 << " intoffset = " << internal_offset
220 /* This call reads events from a source and writes them to `dst' timed in session frames */
223 dst, // destination buffer
224 _position - _start, // start position of the source in session frames
225 _start + internal_offset, // where to start reading in the source
226 to_read, // read duration in frames
230 return 0; /* "read nothing" */
233 _read_data_count += src->read_data_count();
241 return Region::state ();
245 MidiRegion::set_state (const XMLNode& node, int version)
247 int ret = Region::set_state (node, version);
250 update_length_beats ();
257 MidiRegion::recompute_at_end ()
259 /* our length has changed
260 * so what? stuck notes are dealt with via a note state tracker
265 MidiRegion::recompute_at_start ()
267 /* as above, but the shift was from the front
268 * maybe bump currently active note's note-ons up so they sound here?
269 * that could be undesireable in certain situations though.. maybe
270 * remove the note entirely, including it's note off? something needs to
271 * be done to keep the played MIDI sane to avoid messing up voices of
272 * polyhonic things etc........
277 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
284 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
289 boost::shared_ptr<MidiSource>
290 MidiRegion::midi_source (uint32_t n) const
292 // Guaranteed to succeed (use a static cast?)
293 return boost::dynamic_pointer_cast<MidiSource>(source(n));
297 MidiRegion::model_changed ()
303 /* build list of filtered Parameters, being those whose automation state is not `Play' */
305 _filtered_parameters.clear ();
307 Automatable::Controls const & c = model()->controls();
309 for (Automatable::Controls::const_iterator i = c.begin(); i != c.end(); ++i) {
310 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
312 if (ac->alist()->automation_state() != Play) {
313 _filtered_parameters.insert (ac->parameter ());
317 /* watch for changes to controls' AutoState */
318 midi_source()->AutomationStateChanged.connect_same_thread (
319 _model_connection, boost::bind (&MidiRegion::model_automation_state_changed, this, _1)
322 model()->ContentsChanged.connect_same_thread (
323 _model_contents_connection, boost::bind (&MidiRegion::model_contents_changed, this));
327 MidiRegion::model_contents_changed ()
329 send_change (PropertyChange (Properties::midi_data));
333 MidiRegion::model_automation_state_changed (Evoral::Parameter const & p)
335 /* Update our filtered parameters list after a change to a parameter's AutoState */
337 boost::shared_ptr<AutomationControl> ac = model()->automation_control (p);
340 if (ac->alist()->automation_state() == Play) {
341 _filtered_parameters.erase (p);
343 _filtered_parameters.insert (p);
346 /* the source will have an iterator into the model, and that iterator will have been set up
347 for a given set of filtered_parameters, so now that we've changed that list we must invalidate
350 Glib::Mutex::Lock lm (midi_source(0)->mutex());
351 midi_source(0)->invalidate ();
354 /** This is called when a trim drag has resulted in a -ve _start time for this region.
355 * Fix it up by adding some empty space to the source.
358 MidiRegion::fix_negative_start ()
360 BeatsFramesConverter c (_session.tempo_map(), _position);
362 model()->insert_silence_at_start (c.from (-_start));