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 $
28 #include <glibmm/thread.h>
30 #include "pbd/basename.h"
31 #include "pbd/xml++.h"
32 #include "pbd/enumwriter.h"
34 #include "ardour/midi_region.h"
35 #include "ardour/session.h"
36 #include "ardour/gain.h"
37 #include "ardour/dB.h"
38 #include "ardour/playlist.h"
39 #include "ardour/midi_source.h"
40 #include "ardour/types.h"
41 #include "ardour/midi_ring_buffer.h"
47 using namespace ARDOUR;
50 /** Basic MidiRegion constructor (one channel) */
51 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src)
54 assert(_name.val().find("/") == string::npos);
55 midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
58 /* Basic MidiRegion constructor (many channels) */
59 MidiRegion::MidiRegion (const SourceList& srcs)
62 assert(_name.val().find("/") == string::npos);
63 midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
67 /** Create a new MidiRegion, that is part of an existing one */
68 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, frameoffset_t offset, bool offset_relative)
69 : Region (other, offset, offset_relative)
71 assert(_name.val().find("/") == string::npos);
72 midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
75 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, const XMLNode& node)
78 if (set_state (node, Stateful::loading_state_version)) {
79 throw failed_constructor();
82 midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
83 assert(_name.val().find("/") == string::npos);
84 assert(_type == DataType::MIDI);
87 MidiRegion::MidiRegion (const SourceList& srcs, const XMLNode& node)
90 if (set_state (node, Stateful::loading_state_version)) {
91 throw failed_constructor();
94 midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
95 assert(_name.val().find("/") == string::npos);
96 assert(_type == DataType::MIDI);
99 MidiRegion::~MidiRegion ()
104 MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
106 BeatsFramesConverter old_converter(_session.tempo_map(), _position - _start);
107 double length_beats = old_converter.from(_length);
109 Region::set_position_internal(pos, allow_bbt_recompute);
111 BeatsFramesConverter new_converter(_session.tempo_map(), pos - _start);
113 set_length(new_converter.to(length_beats), 0);
117 MidiRegion::read_at (Evoral::EventSink<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
119 return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
123 MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
125 return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
129 MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>& dst, framepos_t position, framecnt_t dur, uint32_t chan_n,
130 NoteMode mode, MidiStateTracker* tracker) const
132 frameoffset_t internal_offset = 0;
133 frameoffset_t src_offset = 0;
134 framecnt_t to_read = 0;
136 /* precondition: caller has verified that we cover the desired section */
141 return 0; /* read nothing */
144 if (position < _position) {
146 src_offset = _position - position;
149 internal_offset = position - _position;
153 if (internal_offset >= _length) {
154 return 0; /* read nothing */
157 if ((to_read = min (dur, _length - internal_offset)) == 0) {
158 return 0; /* read nothing */
161 _read_data_count = 0;
163 boost::shared_ptr<MidiSource> src = midi_source(chan_n);
164 src->set_note_mode(mode);
166 framepos_t output_buffer_position = 0;
167 framepos_t negative_output_buffer_position = 0;
168 if (_position >= _start) {
169 // handle resizing of beginnings of regions correctly
170 output_buffer_position = _position - _start;
172 // when _start is greater than _position, we have to subtract
173 // _start from the note times in the midi source
174 negative_output_buffer_position = _start;
177 /*cerr << "MR read @ " << position << " * " << to_read
178 << " _position = " << _position
179 << " _start = " << _start
180 << " offset = " << output_buffer_position
181 << " negoffset = " << negative_output_buffer_position
182 << " intoffset = " << internal_offset
186 dst, // destination buffer
187 _position - _start, // start position of the source in this read context
188 _start + internal_offset, // where to start reading in the source
189 to_read, // read duration in frames
190 output_buffer_position, // the offset in the output buffer
191 negative_output_buffer_position, // amount to substract from note times
194 return 0; /* "read nothing" */
197 _read_data_count += src->read_data_count();
203 MidiRegion::state (bool full)
205 XMLNode& node (Region::state (full));
208 LocaleGuard lg (X_("POSIX"));
210 // XXX these should move into Region
212 for (uint32_t n=0; n < _sources.size(); ++n) {
213 snprintf (buf2, sizeof(buf2), "source-%d", n);
214 _sources[n]->id().print (buf, sizeof(buf));
215 node.add_property (buf2, buf);
218 for (uint32_t n=0; n < _master_sources.size(); ++n) {
219 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
220 _master_sources[n]->id().print (buf, sizeof (buf));
221 node.add_property (buf2, buf);
224 if (full && _extra_xml) {
225 node.add_child_copy (*_extra_xml);
232 MidiRegion::set_state (const XMLNode& node, int version)
234 return Region::set_state (node, version);
238 MidiRegion::recompute_at_end ()
240 /* our length has changed
241 * (non destructively) "chop" notes that pass the end boundary, to
242 * prevent stuck notes.
247 MidiRegion::recompute_at_start ()
249 /* as above, but the shift was from the front
250 * maybe bump currently active note's note-ons up so they sound here?
251 * that could be undesireable in certain situations though.. maybe
252 * remove the note entirely, including it's note off? something needs to
253 * be done to keep the played MIDI sane to avoid messing up voices of
254 * polyhonic things etc........
259 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
266 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
271 boost::shared_ptr<MidiSource>
272 MidiRegion::midi_source (uint32_t n) const
274 // Guaranteed to succeed (use a static cast?)
275 return boost::dynamic_pointer_cast<MidiSource>(source(n));
280 MidiRegion::switch_source(boost::shared_ptr<Source> src)
282 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
286 // MIDI regions have only one source
288 _sources.push_back(msrc);
290 set_name(msrc->name());