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
;
50 /* Basic MidiRegion constructor (many channels) */
51 MidiRegion::MidiRegion (const SourceList
& srcs
)
54 // midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
55 midi_source(0)->ModelChanged
.connect_same_thread (_source_connection
, boost::bind (&MidiRegion::model_changed
, this));
57 assert(_name
.val().find("/") == string::npos
);
58 assert(_type
== DataType::MIDI
);
61 /** Create a new MidiRegion, that is part of an existing one */
62 MidiRegion::MidiRegion (boost::shared_ptr
<const MidiRegion
> other
, frameoffset_t offset
, bool offset_relative
)
63 : Region (other
, offset
, offset_relative
)
65 assert(_name
.val().find("/") == string::npos
);
66 // midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
67 midi_source(0)->ModelChanged
.connect_same_thread (_source_connection
, boost::bind (&MidiRegion::model_changed
, this));
71 MidiRegion::~MidiRegion ()
75 /** Create a new MidiRegion that has its own version of some/all of the Source used by another.
77 boost::shared_ptr
<MidiRegion
>
80 BeatsFramesConverter
bfc (_session
.tempo_map(), _position
);
81 double bbegin
= bfc
.from (_position
);
82 double bend
= bfc
.from (last_frame() + 1);
84 boost::shared_ptr
<MidiSource
> ms
= midi_source(0)->clone (bbegin
, bend
);
88 plist
.add (Properties::name
, ms
->name());
89 plist
.add (Properties::whole_file
, true);
90 plist
.add (Properties::start
, 0);
91 plist
.add (Properties::length
, _length
);
92 plist
.add (Properties::layer
, 0);
94 return boost::dynamic_pointer_cast
<MidiRegion
> (RegionFactory::create (ms
, plist
, true));
98 MidiRegion::set_position_internal (framepos_t pos
, bool allow_bbt_recompute
)
100 BeatsFramesConverter
old_converter(_session
.tempo_map(), _position
- _start
);
101 double length_beats
= old_converter
.from(_length
);
103 Region::set_position_internal(pos
, allow_bbt_recompute
);
105 BeatsFramesConverter
new_converter(_session
.tempo_map(), pos
- _start
);
107 set_length(new_converter
.to(length_beats
), 0);
111 MidiRegion::read_at (Evoral::EventSink
<nframes_t
>& out
, framepos_t position
, framecnt_t dur
, uint32_t chan_n
, NoteMode mode
, MidiStateTracker
* tracker
) const
113 return _read_at (_sources
, out
, position
, dur
, chan_n
, mode
, tracker
);
117 MidiRegion::master_read_at (MidiRingBuffer
<nframes_t
>& out
, framepos_t position
, framecnt_t dur
, uint32_t chan_n
, NoteMode mode
) const
119 return _read_at (_master_sources
, out
, position
, dur
, chan_n
, mode
); /* no tracker */
123 MidiRegion::_read_at (const SourceList
& /*srcs*/, Evoral::EventSink
<nframes_t
>& dst
, framepos_t position
, framecnt_t dur
, uint32_t chan_n
,
124 NoteMode mode
, MidiStateTracker
* tracker
) const
126 frameoffset_t internal_offset
= 0;
127 frameoffset_t src_offset
= 0;
128 framecnt_t to_read
= 0;
130 /* precondition: caller has verified that we cover the desired section */
135 return 0; /* read nothing */
138 if (position
< _position
) {
140 src_offset
= _position
- position
;
143 internal_offset
= position
- _position
;
147 if (internal_offset
>= _length
) {
148 return 0; /* read nothing */
151 if ((to_read
= min (dur
, _length
- internal_offset
)) == 0) {
152 return 0; /* read nothing */
155 _read_data_count
= 0;
157 boost::shared_ptr
<MidiSource
> src
= midi_source(chan_n
);
158 src
->set_note_mode(mode
);
160 framepos_t output_buffer_position
= 0;
161 framepos_t negative_output_buffer_position
= 0;
162 if (_position
>= _start
) {
163 // handle resizing of beginnings of regions correctly
164 output_buffer_position
= _position
- _start
;
166 // when _start is greater than _position, we have to subtract
167 // _start from the note times in the midi source
168 negative_output_buffer_position
= _start
;
171 /*cerr << "MR read @ " << position << " * " << to_read
172 << " _position = " << _position
173 << " _start = " << _start
174 << " offset = " << output_buffer_position
175 << " negoffset = " << negative_output_buffer_position
176 << " intoffset = " << internal_offset
180 dst
, // destination buffer
181 _position
- _start
, // start position of the source in this read context
182 _start
+ internal_offset
, // where to start reading in the source
183 to_read
, // read duration in frames
184 output_buffer_position
, // the offset in the output buffer
185 negative_output_buffer_position
, // amount to substract from note times
189 return 0; /* "read nothing" */
192 _read_data_count
+= src
->read_data_count();
198 MidiRegion::state (bool full
)
200 return Region::state (full
);
204 MidiRegion::set_state (const XMLNode
& node
, int version
)
206 return Region::set_state (node
, version
);
210 MidiRegion::recompute_at_end ()
212 /* our length has changed
213 * so what? stuck notes are dealt with via a note state tracker
218 MidiRegion::recompute_at_start ()
220 /* as above, but the shift was from the front
221 * maybe bump currently active note's note-ons up so they sound here?
222 * that could be undesireable in certain situations though.. maybe
223 * remove the note entirely, including it's note off? something needs to
224 * be done to keep the played MIDI sane to avoid messing up voices of
225 * polyhonic things etc........
230 MidiRegion::separate_by_channel (ARDOUR::Session
&, vector
< boost::shared_ptr
<Region
> >&) const
237 MidiRegion::exportme (ARDOUR::Session
&, ARDOUR::ExportSpecification
&)
242 boost::shared_ptr
<MidiSource
>
243 MidiRegion::midi_source (uint32_t n
) const
245 // Guaranteed to succeed (use a static cast?)
246 return boost::dynamic_pointer_cast
<MidiSource
>(source(n
));
251 MidiRegion::switch_source(boost::shared_ptr
<Source
> src
)
253 _source_connection
.disconnect ();
255 boost::shared_ptr
<MidiSource
> msrc
= boost::dynamic_pointer_cast
<MidiSource
>(src
);
260 // MIDI regions have only one source
262 srcs
.push_back (msrc
);
267 set_name (msrc
->name());
269 msrc
->ModelChanged
.connect_same_thread (_source_connection
, boost::bind (&MidiRegion::model_changed
, this));
273 MidiRegion::model_changed ()
275 /* build list of filtered Parameters, being those whose automation state is not `Play' */
277 _filtered_parameters
.clear ();
279 Automatable::Controls
const & c
= model()->controls();
281 for (Automatable::Controls::const_iterator i
= c
.begin(); i
!= c
.end(); ++i
) {
282 boost::shared_ptr
<AutomationControl
> ac
= boost::dynamic_pointer_cast
<AutomationControl
> (i
->second
);
284 if (ac
->alist()->automation_state() != Play
) {
285 _filtered_parameters
.insert (ac
->parameter ());
289 /* watch for changes to controls' AutoState */
290 model()->AutomationStateChanged
.connect_same_thread (
291 _model_connection
, boost::bind (&MidiRegion::model_automation_state_changed
, this, _1
)
296 MidiRegion::model_automation_state_changed (Evoral::Parameter
const & p
)
298 /* Update our filtered parameters list after a change to a parameter's AutoState */
300 boost::shared_ptr
<AutomationControl
> ac
= model()->automation_control (p
);
303 if (ac
->alist()->automation_state() == Play
) {
304 _filtered_parameters
.erase (p
);
306 _filtered_parameters
.insert (p
);
309 /* the source will have an iterator into the model, and that iterator will have been set up
310 for a given set of filtered_paramters, so now that we've changed that list we must invalidate
313 Glib::Mutex::Lock
lm (midi_source(0)->mutex());
314 midi_source(0)->invalidate ();