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 (MidiSource& src, jack_nframes_t start, jack_nframes_t length, bool announce)
52 : Region (src, start, length, PBD::basename_nosuffix(src.name()), DataType::MIDI, 0, Region::Flag(Region::DefaultFlags|Region::External))
54 save_state ("initial state");
57 CheckNewRegion (this); /* EMIT SIGNAL */
60 assert(_name.find("/") == string::npos);
63 /* Basic MidiRegion constructor (one channel) */
64 MidiRegion::MidiRegion (MidiSource& src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
65 : Region (src, start, length, name, DataType::MIDI, layer, flags)
67 save_state ("initial state");
70 CheckNewRegion (this); /* EMIT SIGNAL */
73 assert(_name.find("/") == string::npos);
76 /* Basic MidiRegion constructor (many channels) */
77 MidiRegion::MidiRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
78 : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
80 save_state ("initial state");
83 CheckNewRegion (this); /* EMIT SIGNAL */
86 assert(_name.find("/") == string::npos);
90 /** Create a new MidiRegion, that is part of an existing one */
91 MidiRegion::MidiRegion (const MidiRegion& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
92 : Region (other, offset, length, name, layer, flags)
94 save_state ("initial state");
97 CheckNewRegion (this); /* EMIT SIGNAL */
100 assert(_name.find("/") == string::npos);
103 MidiRegion::MidiRegion (const MidiRegion &other)
106 save_state ("initial state");
108 /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
109 assert(_name.find("/") == string::npos);
112 MidiRegion::MidiRegion (MidiSource& src, const XMLNode& node)
115 if (set_state (node)) {
116 throw failed_constructor();
119 save_state ("initial state");
121 assert(_name.find("/") == string::npos);
122 assert(_type == DataType::MIDI);
124 CheckNewRegion (this); /* EMIT SIGNAL */
127 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
128 : Region (srcs, node)
130 if (set_state (node)) {
131 throw failed_constructor();
134 save_state ("initial state");
136 assert(_name.find("/") == string::npos);
137 assert(_type == DataType::MIDI);
139 CheckNewRegion (this); /* EMIT SIGNAL */
142 MidiRegion::~MidiRegion ()
148 MidiRegion::state_factory (std::string why) const
150 RegionState* state = new RegionState (why);
152 Region::store_state (*state);
158 MidiRegion::restore_state (StateManager::State& sstate)
160 RegionState* state = dynamic_cast<RegionState*> (&sstate);
161 Change what_changed = Region::restore_and_return_flags (*state);
163 if (_flags != Flag (state->_flags)) {
165 //uint32_t old_flags = _flags;
167 _flags = Flag (state->_flags);
170 what_changed = Change (what_changed);
176 MidiRegion::get_memento() const
178 return sigc::bind (mem_fun (*(const_cast<MidiRegion *> (this)), &StateManager::use_state), _current_state_id);
182 MidiRegion::read_at (MidiRingBuffer& out, jack_nframes_t position,
184 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
186 return _read_at (_sources, out, position, dur, chan_n, read_frames, skip_frames);
190 MidiRegion::master_read_at (MidiRingBuffer& out, jack_nframes_t position,
191 jack_nframes_t dur, uint32_t chan_n) const
193 return _read_at (_master_sources, out, position, dur, chan_n, 0, 0);
197 MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst,
198 jack_nframes_t position, jack_nframes_t dur,
199 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
205 const char note = rand()%30 + 30;
216 ev.time = (jack_nframes_t)(position + (9/10.0 * dur));
217 assert(ev.time < position + dur);
224 _read_data_count += dur;
228 jack_nframes_t internal_offset;
229 jack_nframes_t buf_offset;
230 jack_nframes_t to_read;
232 /* precondition: caller has verified that we cover the desired section */
236 if (position < _position) {
238 //buf_offset = _position - position;
241 internal_offset = position - _position;
245 if (internal_offset >= _length) {
246 return 0; /* read nothing */
250 if ((to_read = min (dur, _length - internal_offset)) == 0) {
251 return 0; /* read nothing */
254 // FIXME: non-opaque MIDI regions not yet supported
258 return 0; /* read nothing */
261 _read_data_count = 0;
263 MidiSource& src = midi_source(chan_n);
264 if (src.read (dst, _start + internal_offset, to_read) != to_read) {
265 return 0; /* "read nothing" */
268 _read_data_count += src.read_data_count();
274 MidiRegion::state (bool full)
276 XMLNode& node (Region::state (full));
280 LocaleGuard lg (X_("POSIX"));
282 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
283 node.add_property ("flags", buf);
285 for (uint32_t n=0; n < _sources.size(); ++n) {
286 snprintf (buf2, sizeof(buf2), "source-%d", n);
287 _sources[n]->id().print (buf);
288 node.add_property (buf2, buf);
291 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
292 node.add_property ("channels", buf);
294 child = node.add_child ("Envelope");
297 child->add_property ("default", "yes");
300 if (full && _extra_xml) {
301 node.add_child_copy (*_extra_xml);
308 MidiRegion::set_state (const XMLNode& node)
310 const XMLNodeList& nlist = node.children();
311 const XMLProperty *prop;
312 LocaleGuard lg (X_("POSIX"));
314 Region::set_state (node);
316 if ((prop = node.property ("flags")) != 0) {
317 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
320 /* Now find child items */
321 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
328 /** Hello, children */
335 MidiRegion::recompute_at_end ()
337 /* our length has changed
338 * (non destructively) "chop" notes that pass the end boundary, to
339 * prevent stuck notes.
344 MidiRegion::recompute_at_start ()
346 /* as above, but the shift was from the front
347 * maybe bump currently active note's note-ons up so they sound here?
348 * that could be undesireable in certain situations though.. maybe
349 * remove the note entirely, including it's note off? something needs to
350 * be done to keep the played MIDI sane to avoid messing up voices of
351 * polyhonic things etc........
356 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
362 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
367 /* generate a new name */
369 if (session.region_name (new_name, _name)) {
373 /* create a copy with just one source */
375 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
379 // Actually, I would prefer not if that's alright
384 MidiRegion::midi_source (uint32_t n) const
386 // Guaranteed to succeed (use a static cast?)
387 return dynamic_cast<MidiSource&>(source(n));