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)) {
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)) {
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::read_at (MidiRingBuffer& out, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
122 return _read_at (_sources, out, position, dur, chan_n, mode);
126 MidiRegion::master_read_at (MidiRingBuffer& out, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
128 return _read_at (_master_sources, out, position, dur, chan_n, mode);
132 MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
134 //cerr << _name << "._read_at(" << position << ") - " << _position << " duration: " << dur << endl;
136 nframes_t internal_offset = 0;
137 nframes_t src_offset = 0;
138 nframes_t to_read = 0;
140 /* precondition: caller has verified that we cover the desired section */
144 if (position < _position) {
146 src_offset = _position - position;
149 internal_offset = position - _position;
153 if (internal_offset >= _length) {
154 return 0; /* read nothing */
158 if ((to_read = min (dur, _length - internal_offset)) == 0) {
159 return 0; /* read nothing */
162 // FIXME: non-opaque MIDI regions not yet supported
166 return 0; /* read nothing */
169 _read_data_count = 0;
171 boost::shared_ptr<MidiSource> src = midi_source(chan_n);
172 src->set_note_mode(mode);
175 // the destination buffer
177 // where to start reading in the region
178 _start + internal_offset,
181 // the offset in the output buffer
184 return 0; /* "read nothing" */
187 _read_data_count += src->read_data_count();
193 MidiRegion::state (bool full)
195 XMLNode& node (Region::state (full));
198 LocaleGuard lg (X_("POSIX"));
200 node.add_property ("flags", enum_2_string (_flags));
202 // XXX these should move into Region
204 for (uint32_t n=0; n < _sources.size(); ++n) {
205 snprintf (buf2, sizeof(buf2), "source-%d", n);
206 _sources[n]->id().print (buf, sizeof(buf));
207 node.add_property (buf2, buf);
210 for (uint32_t n=0; n < _master_sources.size(); ++n) {
211 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
212 _master_sources[n]->id().print (buf, sizeof (buf));
213 node.add_property (buf2, buf);
216 if (full && _extra_xml) {
217 node.add_child_copy (*_extra_xml);
224 MidiRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
226 const XMLProperty *prop;
227 LocaleGuard lg (X_("POSIX"));
229 Region::set_live_state (node, what_changed, false);
231 uint32_t old_flags = _flags;
233 if ((prop = node.property ("flags")) != 0) {
234 _flags = Flag (string_2_enum (prop->value(), _flags));
236 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
238 _flags = Flag (_flags & ~Region::LeftOfSplit);
239 _flags = Flag (_flags & ~Region::RightOfSplit);
242 if ((old_flags ^ _flags) & Muted) {
243 what_changed = Change (what_changed|MuteChanged);
245 if ((old_flags ^ _flags) & Opaque) {
246 what_changed = Change (what_changed|OpacityChanged);
248 if ((old_flags ^ _flags) & Locked) {
249 what_changed = Change (what_changed|LockChanged);
253 send_change (what_changed);
260 MidiRegion::set_state (const XMLNode& node)
262 /* Region::set_state() calls the virtual set_live_state(),
263 which will get us back to AudioRegion::set_live_state()
264 to handle the relevant stuff.
267 return Region::set_state (node);
271 MidiRegion::recompute_at_end ()
273 /* our length has changed
274 * (non destructively) "chop" notes that pass the end boundary, to
275 * prevent stuck notes.
280 MidiRegion::recompute_at_start ()
282 /* as above, but the shift was from the front
283 * maybe bump currently active note's note-ons up so they sound here?
284 * that could be undesireable in certain situations though.. maybe
285 * remove the note entirely, including it's note off? something needs to
286 * be done to keep the played MIDI sane to avoid messing up voices of
287 * polyhonic things etc........
292 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
294 // Separate by MIDI channel? bit different from audio since this is separating based
295 // on the actual contained data and destructively modifies and creates new sources..
301 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
306 /* generate a new name */
308 if (session.region_name (new_name, _name)) {
312 /* create a copy with just one source */
314 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
318 // Actually, I would prefer not if that's alright
323 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
328 boost::shared_ptr<MidiSource>
329 MidiRegion::midi_source (uint32_t n) const
331 // Guaranteed to succeed (use a static cast?)
332 return boost::dynamic_pointer_cast<MidiSource>(source(n));
337 MidiRegion::switch_source(boost::shared_ptr<Source> src)
339 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
343 // MIDI regions have only one source
345 _sources.push_back(msrc);
347 set_name(msrc->name());