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()), 0, Region::Flag(Region::DefaultFlags|Region::External))
53 save_state ("initial state");
56 CheckNewRegion (this); /* EMIT SIGNAL */
60 /* Basic MidiRegion constructor (one channel) */
61 MidiRegion::MidiRegion (MidiSource& src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
62 : Region (src, start, length, name, layer, flags)
64 save_state ("initial state");
67 CheckNewRegion (this); /* EMIT SIGNAL */
71 /* Basic MidiRegion constructor (many channels) */
72 MidiRegion::MidiRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
73 : Region (srcs, start, length, name, layer, flags)
75 save_state ("initial state");
78 CheckNewRegion (this); /* EMIT SIGNAL */
83 /** Create a new MidiRegion, that is part of an existing one */
84 MidiRegion::MidiRegion (const MidiRegion& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
85 : Region (other, offset, length, name, layer, flags)
87 save_state ("initial state");
90 CheckNewRegion (this); /* EMIT SIGNAL */
94 MidiRegion::MidiRegion (const MidiRegion &other)
97 save_state ("initial state");
99 /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
102 MidiRegion::MidiRegion (MidiSource& src, const XMLNode& node)
105 if (set_state (node)) {
106 throw failed_constructor();
109 save_state ("initial state");
111 CheckNewRegion (this); /* EMIT SIGNAL */
114 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
115 : Region (srcs, node)
117 if (set_state (node)) {
118 throw failed_constructor();
121 save_state ("initial state");
123 CheckNewRegion (this); /* EMIT SIGNAL */
126 MidiRegion::~MidiRegion ()
132 MidiRegion::state_factory (std::string why) const
134 RegionState* state = new RegionState (why);
136 Region::store_state (*state);
142 MidiRegion::restore_state (StateManager::State& sstate)
144 RegionState* state = dynamic_cast<RegionState*> (&sstate);
145 Change what_changed = Region::restore_and_return_flags (*state);
147 if (_flags != Flag (state->_flags)) {
149 //uint32_t old_flags = _flags;
151 _flags = Flag (state->_flags);
154 what_changed = Change (what_changed);
160 MidiRegion::get_memento() const
162 return sigc::bind (mem_fun (*(const_cast<MidiRegion *> (this)), &StateManager::use_state), _current_state_id);
166 MidiRegion::read_at (RawMidi *out, RawMidi* mix_buf, jack_nframes_t position,
168 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
170 return _read_at (_sources, out, position, cnt, chan_n, read_frames, skip_frames);
174 MidiRegion::master_read_at (RawMidi *out, RawMidi* mix_buf, jack_nframes_t position,
175 jack_nframes_t cnt, uint32_t chan_n) const
177 return _read_at (_master_sources, out, position, cnt, chan_n, 0, 0);
181 MidiRegion::_read_at (const SourceList& srcs, RawMidi *buf,
182 jack_nframes_t position, jack_nframes_t cnt,
183 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
185 jack_nframes_t internal_offset;
186 jack_nframes_t buf_offset;
187 jack_nframes_t to_read;
189 /* precondition: caller has verified that we cover the desired section */
193 if (position < _position) {
195 buf_offset = _position - position;
198 internal_offset = position - _position;
202 if (internal_offset >= _length) {
203 return 0; /* read nothing */
207 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
208 return 0; /* read nothing */
211 // FIXME: non-opaque MIDI regions not yet supported
215 return 0; /* read nothing */
218 _read_data_count = 0;
220 MidiSource& src = midi_source(chan_n);
221 if (src.read (buf, _start + internal_offset, to_read) != to_read) {
222 return 0; /* "read nothing" */
225 _read_data_count += src.read_data_count();
231 MidiRegion::state (bool full)
233 XMLNode& node (Region::state (full));
237 LocaleGuard lg (X_("POSIX"));
239 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
240 node.add_property ("flags", buf);
242 for (uint32_t n=0; n < _sources.size(); ++n) {
243 snprintf (buf2, sizeof(buf2), "source-%d", n);
244 _sources[n]->id().print (buf);
245 node.add_property (buf2, buf);
248 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
249 node.add_property ("channels", buf);
251 child = node.add_child ("Envelope");
254 child->add_property ("default", "yes");
257 if (full && _extra_xml) {
258 node.add_child_copy (*_extra_xml);
265 MidiRegion::set_state (const XMLNode& node)
267 const XMLNodeList& nlist = node.children();
268 const XMLProperty *prop;
269 LocaleGuard lg (X_("POSIX"));
271 Region::set_state (node);
273 if ((prop = node.property ("flags")) != 0) {
274 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
277 /* Now find child items */
278 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
285 /** Hello, children */
292 MidiRegion::recompute_at_end ()
294 /* our length has changed
295 * (non destructively) "chop" notes that pass the end boundary, to
296 * prevent stuck notes.
301 MidiRegion::recompute_at_start ()
303 /* as above, but the shift was from the front
304 * maybe bump currently active note's note-ons up so they sound here?
305 * that could be undesireable in certain situations though.. maybe
306 * remove the note entirely, including it's note off? something needs to
307 * be done to keep the played MIDI sane to avoid messing up voices of
308 * polyhonic things etc........
313 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
319 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
324 /* generate a new name */
326 if (session.region_name (new_name, _name)) {
330 /* create a copy with just one source */
332 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
339 MidiRegion::midi_source (uint32_t n) const
341 // Guaranteed to succeed (use a static cast?)
342 return dynamic_cast<MidiSource&>(source(n));