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 <sigc++/bind.h>
28 #include <sigc++/class_slot.h>
30 #include <glibmm/thread.h>
32 #include "pbd/basename.h"
33 #include "pbd/xml++.h"
34 #include "pbd/enumwriter.h"
36 #include "ardour/midi_region.h"
37 #include "ardour/session.h"
38 #include "ardour/gain.h"
39 #include "ardour/dB.h"
40 #include "ardour/playlist.h"
41 #include "ardour/midi_source.h"
42 #include "ardour/types.h"
43 #include "ardour/midi_ring_buffer.h"
49 using namespace ARDOUR;
51 /** Basic MidiRegion constructor (one channel) */
52 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length)
53 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::MIDI, 0, Region::Flag(Region::DefaultFlags|Region::External))
55 assert(_name.find("/") == string::npos);
56 midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
59 /* Basic MidiRegion constructor (one channel) */
60 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
61 : Region (src, start, length, name, DataType::MIDI, layer, flags)
63 assert(_name.find("/") == string::npos);
64 midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
67 /* Basic MidiRegion constructor (many channels) */
68 MidiRegion::MidiRegion (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
69 : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
71 assert(_name.find("/") == string::npos);
72 midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
76 /** Create a new MidiRegion, that is part of an existing one */
77 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
78 : Region (other, offset, length, name, layer, flags)
80 assert(_name.find("/") == string::npos);
81 midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
84 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other)
87 assert(_name.find("/") == string::npos);
88 midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
91 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, const XMLNode& node)
94 if (set_state (node, Stateful::loading_state_version)) {
95 throw failed_constructor();
98 midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
99 assert(_name.find("/") == string::npos);
100 assert(_type == DataType::MIDI);
103 MidiRegion::MidiRegion (const SourceList& srcs, const XMLNode& node)
104 : Region (srcs, node)
106 if (set_state (node, Stateful::loading_state_version)) {
107 throw failed_constructor();
110 midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegion::switch_source));
111 assert(_name.find("/") == string::npos);
112 assert(_type == DataType::MIDI);
115 MidiRegion::~MidiRegion ()
120 MidiRegion::set_position_internal (nframes_t pos, bool allow_bbt_recompute)
122 BeatsFramesConverter old_converter(_session, _position - _start);
123 double length_beats = old_converter.from(_length);
125 Region::set_position_internal(pos, allow_bbt_recompute);
127 BeatsFramesConverter new_converter(_session, pos - _start);
129 set_length(new_converter.to(length_beats), 0);
133 MidiRegion::read_at (MidiRingBuffer<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
135 return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
139 MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
141 return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
145 MidiRegion::_read_at (const SourceList& /*srcs*/,
146 MidiRingBuffer<nframes_t>& dst, sframes_t position, nframes_t dur, uint32_t chan_n,
147 NoteMode mode, MidiStateTracker* tracker) const
149 nframes_t internal_offset = 0;
150 nframes_t src_offset = 0;
151 nframes_t to_read = 0;
153 /* precondition: caller has verified that we cover the desired section */
157 if (position < _position) {
159 src_offset = _position - position;
162 internal_offset = position - _position;
166 if (internal_offset >= _length) {
167 return 0; /* read nothing */
170 if ((to_read = min (dur, _length - internal_offset)) == 0) {
171 return 0; /* read nothing */
174 // FIXME: non-opaque MIDI regions not yet supported
178 return 0; /* read nothing */
181 _read_data_count = 0;
183 boost::shared_ptr<MidiSource> src = midi_source(chan_n);
184 src->set_note_mode(mode);
186 nframes_t output_buffer_position = 0;
187 nframes_t negative_output_buffer_position = 0;
188 if (_position >= _start) {
189 // handle resizing of beginnings of regions correctly
190 output_buffer_position = _position - _start;
192 // when _start is greater than _position, we have to subtract
193 // _start from the note times in the midi source
194 negative_output_buffer_position = _start;
197 /*cerr << "MR read @ " << position << " * " << to_read
198 << " _position = " << _position
199 << " _start = " << _start
200 << " offset = " << output_buffer_position
201 << " negoffset = " << negative_output_buffer_position
202 << " intoffset = " << internal_offset
206 dst, // destination buffer
207 _position - _start, // start position of the source in this read context
208 _start + internal_offset, // where to start reading in the source
209 to_read, // read duration in frames
210 output_buffer_position, // the offset in the output buffer
211 negative_output_buffer_position, // amount to substract from note times
214 return 0; /* "read nothing" */
217 _read_data_count += src->read_data_count();
223 MidiRegion::state (bool full)
225 XMLNode& node (Region::state (full));
228 LocaleGuard lg (X_("POSIX"));
230 node.add_property ("flags", enum_2_string (_flags));
232 // XXX these should move into Region
234 for (uint32_t n=0; n < _sources.size(); ++n) {
235 snprintf (buf2, sizeof(buf2), "source-%d", n);
236 _sources[n]->id().print (buf, sizeof(buf));
237 node.add_property (buf2, buf);
240 for (uint32_t n=0; n < _master_sources.size(); ++n) {
241 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
242 _master_sources[n]->id().print (buf, sizeof (buf));
243 node.add_property (buf2, buf);
246 if (full && _extra_xml) {
247 node.add_child_copy (*_extra_xml);
254 MidiRegion::set_live_state (const XMLNode& node, int version, Change& what_changed, bool send)
256 const XMLProperty *prop;
257 LocaleGuard lg (X_("POSIX"));
259 Region::set_live_state (node, version, what_changed, false);
261 uint32_t old_flags = _flags;
263 if ((prop = node.property ("flags")) != 0) {
264 _flags = Flag (string_2_enum (prop->value(), _flags));
266 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
268 _flags = Flag (_flags & ~Region::LeftOfSplit);
269 _flags = Flag (_flags & ~Region::RightOfSplit);
272 if ((old_flags ^ _flags) & Muted) {
273 what_changed = Change (what_changed|MuteChanged);
275 if ((old_flags ^ _flags) & Opaque) {
276 what_changed = Change (what_changed|OpacityChanged);
278 if ((old_flags ^ _flags) & Locked) {
279 what_changed = Change (what_changed|LockChanged);
283 send_change (what_changed);
290 MidiRegion::set_state (const XMLNode& node, int version)
292 /* Region::set_state() calls the virtual set_live_state(),
293 which will get us back to AudioRegion::set_live_state()
294 to handle the relevant stuff.
297 return Region::set_state (node, version);
301 MidiRegion::recompute_at_end ()
303 /* our length has changed
304 * (non destructively) "chop" notes that pass the end boundary, to
305 * prevent stuck notes.
310 MidiRegion::recompute_at_start ()
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........
322 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
329 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
334 boost::shared_ptr<MidiSource>
335 MidiRegion::midi_source (uint32_t n) const
337 // Guaranteed to succeed (use a static cast?)
338 return boost::dynamic_pointer_cast<MidiSource>(source(n));
343 MidiRegion::switch_source(boost::shared_ptr<Source> src)
345 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
349 // MIDI regions have only one source
351 _sources.push_back(msrc);
353 set_name(msrc->name());