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>
35 #include <ardour/midi_region.h>
36 #include <ardour/session.h>
37 #include <ardour/gain.h>
38 #include <ardour/dB.h>
39 #include <ardour/playlist.h>
40 #include <ardour/midi_source.h>
41 #include <ardour/types.h>
42 #include <ardour/midi_ring_buffer.h>
48 using namespace ARDOUR;
50 /** Basic MidiRegion constructor (one channel) */
51 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, jack_nframes_t start, jack_nframes_t length)
52 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::MIDI, 0, Region::Flag(Region::DefaultFlags|Region::External))
54 assert(_name.find("/") == string::npos);
57 /* Basic MidiRegion constructor (one channel) */
58 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags)
59 : Region (src, start, length, name, DataType::MIDI, layer, flags)
61 assert(_name.find("/") == string::npos);
64 /* Basic MidiRegion constructor (many channels) */
65 MidiRegion::MidiRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags)
66 : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
68 assert(_name.find("/") == string::npos);
72 /** Create a new MidiRegion, that is part of an existing one */
73 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags)
74 : Region (other, offset, length, name, layer, flags)
76 assert(_name.find("/") == string::npos);
79 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other)
82 assert(_name.find("/") == string::npos);
85 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, const XMLNode& node)
88 if (set_state (node)) {
89 throw failed_constructor();
92 assert(_name.find("/") == string::npos);
93 assert(_type == DataType::MIDI);
96 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
99 if (set_state (node)) {
100 throw failed_constructor();
103 assert(_name.find("/") == string::npos);
104 assert(_type == DataType::MIDI);
107 MidiRegion::~MidiRegion ()
109 GoingAway (); /* EMIT SIGNAL */
113 MidiRegion::read_at (MidiRingBuffer& out, jack_nframes_t position,
115 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
117 return _read_at (_sources, out, position, dur, chan_n, read_frames, skip_frames);
121 MidiRegion::master_read_at (MidiRingBuffer& out, jack_nframes_t position,
122 jack_nframes_t dur, uint32_t chan_n) const
124 return _read_at (_master_sources, out, position, dur, chan_n, 0, 0);
128 MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst,
129 jack_nframes_t position, jack_nframes_t dur,
130 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
133 //cerr << _name << "._read_at(" << position << ") - " << _position << endl;
135 jack_nframes_t internal_offset = 0;
136 jack_nframes_t src_offset = 0;
137 jack_nframes_t to_read = 0;
139 /* precondition: caller has verified that we cover the desired section */
143 if (position < _position) {
145 src_offset = _position - position;
148 internal_offset = position - _position;
152 if (internal_offset >= _length) {
153 return 0; /* read nothing */
157 if ((to_read = min (dur, _length - internal_offset)) == 0) {
158 return 0; /* read nothing */
161 // FIXME: non-opaque MIDI regions not yet supported
165 return 0; /* read nothing */
168 _read_data_count = 0;
170 boost::shared_ptr<MidiSource> src = midi_source(chan_n);
171 if (src->read (dst, _start + internal_offset, to_read, _position) != to_read) {
172 return 0; /* "read nothing" */
175 _read_data_count += src->read_data_count(); // FIXME: semantics?
181 MidiRegion::state (bool full)
183 XMLNode& node (Region::state (full));
187 LocaleGuard lg (X_("POSIX"));
189 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
190 node.add_property ("flags", buf);
192 for (uint32_t n=0; n < _sources.size(); ++n) {
193 snprintf (buf2, sizeof(buf2), "source-%d", n);
194 _sources[n]->id().print (buf, sizeof(buf));
195 node.add_property (buf2, buf);
198 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
199 node.add_property ("channels", buf);
201 child = node.add_child ("Envelope");
204 child->add_property ("default", "yes");
207 if (full && _extra_xml) {
208 node.add_child_copy (*_extra_xml);
215 MidiRegion::set_state (const XMLNode& node)
217 const XMLNodeList& nlist = node.children();
218 const XMLProperty *prop;
219 LocaleGuard lg (X_("POSIX"));
221 Region::set_state (node);
223 if ((prop = node.property ("flags")) != 0) {
224 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
227 /* Now find child items */
228 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
235 /** Hello, children */
242 MidiRegion::recompute_at_end ()
244 /* our length has changed
245 * (non destructively) "chop" notes that pass the end boundary, to
246 * prevent stuck notes.
251 MidiRegion::recompute_at_start ()
253 /* as above, but the shift was from the front
254 * maybe bump currently active note's note-ons up so they sound here?
255 * that could be undesireable in certain situations though.. maybe
256 * remove the note entirely, including it's note off? something needs to
257 * be done to keep the played MIDI sane to avoid messing up voices of
258 * polyhonic things etc........
263 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
265 // Separate by MIDI channel? bit different from audio since this is separating based
266 // on the actual contained data and destructively modifies and creates new sources..
272 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
277 /* generate a new name */
279 if (session.region_name (new_name, _name)) {
283 /* create a copy with just one source */
285 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
289 // Actually, I would prefer not if that's alright
293 boost::shared_ptr<MidiSource>
294 MidiRegion::midi_source (uint32_t n) const
296 // Guaranteed to succeed (use a static cast?)
297 return boost::dynamic_pointer_cast<MidiSource>(source(n));