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>
47 using namespace ARDOUR;
49 /** Basic MidiRegion constructor (one channel) */
50 MidiRegion::MidiRegion (MidiSource& src, jack_nframes_t start, jack_nframes_t length, bool announce)
51 : Region (src, start, length, PBD::basename_nosuffix(src.name()), DataType::MIDI, 0, Region::Flag(Region::DefaultFlags|Region::External))
53 save_state ("initial state");
56 CheckNewRegion (this); /* EMIT SIGNAL */
59 assert(_name.find("/") == string::npos);
62 /* Basic MidiRegion constructor (one channel) */
63 MidiRegion::MidiRegion (MidiSource& src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
64 : Region (src, start, length, name, DataType::MIDI, layer, flags)
66 save_state ("initial state");
69 CheckNewRegion (this); /* EMIT SIGNAL */
72 assert(_name.find("/") == string::npos);
75 /* Basic MidiRegion constructor (many channels) */
76 MidiRegion::MidiRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
77 : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
79 save_state ("initial state");
82 CheckNewRegion (this); /* EMIT SIGNAL */
85 assert(_name.find("/") == string::npos);
89 /** Create a new MidiRegion, that is part of an existing one */
90 MidiRegion::MidiRegion (const MidiRegion& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
91 : Region (other, offset, length, name, layer, flags)
93 save_state ("initial state");
96 CheckNewRegion (this); /* EMIT SIGNAL */
99 assert(_name.find("/") == string::npos);
102 MidiRegion::MidiRegion (const MidiRegion &other)
105 save_state ("initial state");
107 /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
108 assert(_name.find("/") == string::npos);
111 MidiRegion::MidiRegion (MidiSource& src, const XMLNode& node)
114 if (set_state (node)) {
115 throw failed_constructor();
118 save_state ("initial state");
120 assert(_name.find("/") == string::npos);
121 assert(_type == DataType::MIDI);
123 CheckNewRegion (this); /* EMIT SIGNAL */
126 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
127 : Region (srcs, node)
129 if (set_state (node)) {
130 throw failed_constructor();
133 save_state ("initial state");
135 assert(_name.find("/") == string::npos);
136 assert(_type == DataType::MIDI);
138 CheckNewRegion (this); /* EMIT SIGNAL */
141 MidiRegion::~MidiRegion ()
147 MidiRegion::state_factory (std::string why) const
149 RegionState* state = new RegionState (why);
151 Region::store_state (*state);
157 MidiRegion::restore_state (StateManager::State& sstate)
159 RegionState* state = dynamic_cast<RegionState*> (&sstate);
160 Change what_changed = Region::restore_and_return_flags (*state);
162 if (_flags != Flag (state->_flags)) {
164 //uint32_t old_flags = _flags;
166 _flags = Flag (state->_flags);
169 what_changed = Change (what_changed);
175 MidiRegion::get_memento() const
177 return sigc::bind (mem_fun (*(const_cast<MidiRegion *> (this)), &StateManager::use_state), _current_state_id);
181 MidiRegion::read_at (MidiBuffer& out, jack_nframes_t position,
183 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
185 return _read_at (_sources, out, position, cnt, chan_n, read_frames, skip_frames);
189 MidiRegion::master_read_at (MidiBuffer& out, jack_nframes_t position,
190 jack_nframes_t cnt, uint32_t chan_n) const
192 return _read_at (_master_sources, out, position, cnt, chan_n, 0, 0);
196 MidiRegion::_read_at (const SourceList& srcs, MidiBuffer& buf,
197 jack_nframes_t position, jack_nframes_t cnt,
198 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
200 jack_nframes_t internal_offset;
201 jack_nframes_t buf_offset;
202 jack_nframes_t to_read;
204 /* precondition: caller has verified that we cover the desired section */
208 if (position < _position) {
210 buf_offset = _position - position;
213 internal_offset = position - _position;
217 if (internal_offset >= _length) {
218 return 0; /* read nothing */
222 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
223 return 0; /* read nothing */
226 // FIXME: non-opaque MIDI regions not yet supported
230 return 0; /* read nothing */
233 _read_data_count = 0;
235 MidiSource& src = midi_source(chan_n);
236 if (src.read (buf, _start + internal_offset, to_read) != to_read) {
237 return 0; /* "read nothing" */
240 _read_data_count += src.read_data_count();
246 MidiRegion::state (bool full)
248 XMLNode& node (Region::state (full));
252 LocaleGuard lg (X_("POSIX"));
254 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
255 node.add_property ("flags", buf);
257 for (uint32_t n=0; n < _sources.size(); ++n) {
258 snprintf (buf2, sizeof(buf2), "source-%d", n);
259 _sources[n]->id().print (buf);
260 node.add_property (buf2, buf);
263 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
264 node.add_property ("channels", buf);
266 child = node.add_child ("Envelope");
269 child->add_property ("default", "yes");
272 if (full && _extra_xml) {
273 node.add_child_copy (*_extra_xml);
280 MidiRegion::set_state (const XMLNode& node)
282 const XMLNodeList& nlist = node.children();
283 const XMLProperty *prop;
284 LocaleGuard lg (X_("POSIX"));
286 Region::set_state (node);
288 if ((prop = node.property ("flags")) != 0) {
289 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
292 /* Now find child items */
293 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
300 /** Hello, children */
307 MidiRegion::recompute_at_end ()
309 /* our length has changed
310 * (non destructively) "chop" notes that pass the end boundary, to
311 * prevent stuck notes.
316 MidiRegion::recompute_at_start ()
318 /* as above, but the shift was from the front
319 * maybe bump currently active note's note-ons up so they sound here?
320 * that could be undesireable in certain situations though.. maybe
321 * remove the note entirely, including it's note off? something needs to
322 * be done to keep the played MIDI sane to avoid messing up voices of
323 * polyhonic things etc........
328 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
334 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
339 /* generate a new name */
341 if (session.region_name (new_name, _name)) {
345 /* create a copy with just one source */
347 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
351 // Actually, I would prefer not if that's alright
356 MidiRegion::midi_source (uint32_t n) const
358 // Guaranteed to succeed (use a static cast?)
359 return dynamic_cast<MidiSource&>(source(n));