Use 'show contents' note range by default (fix uninitialized value).
[ardour.git] / libs / ardour / midi_region.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis 
3
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.
8
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.
13
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.
17
18     $Id: midiregion.cc 746 2006-08-02 02:44:23Z drobilla $
19 */
20
21 #include <cmath>
22 #include <climits>
23 #include <cfloat>
24
25 #include <set>
26
27 #include <sigc++/bind.h>
28 #include <sigc++/class_slot.h>
29
30 #include <glibmm/thread.h>
31
32 #include <pbd/basename.h>
33 #include <pbd/xml++.h>
34 #include <pbd/enumwriter.h>
35
36 #include <ardour/midi_region.h>
37 #include <ardour/session.h>
38 #include <ardour/gain.h>
39 #include <ardour/dB.h>
40 #include <ardour/playlist.h>
41 #include <ardour/midi_source.h>
42 #include <ardour/types.h>
43 #include <ardour/midi_ring_buffer.h>
44
45 #include "i18n.h"
46 #include <locale.h>
47
48 using namespace std;
49 using namespace ARDOUR;
50
51 /** Basic MidiRegion constructor (one channel) */
52 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length)
53         : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::MIDI, 0,  Region::Flag(Region::DefaultFlags|Region::External))
54 {
55         assert(_name.find("/") == string::npos);
56 }
57
58 /* Basic MidiRegion constructor (one channel) */
59 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
60         : Region (src, start, length, name, DataType::MIDI, layer, flags)
61 {
62         assert(_name.find("/") == string::npos);
63 }
64
65 /* Basic MidiRegion constructor (many channels) */
66 MidiRegion::MidiRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
67         : Region (srcs, start, length, name, DataType::MIDI, layer, flags)
68 {
69         assert(_name.find("/") == string::npos);
70 }
71
72
73 /** Create a new MidiRegion, that is part of an existing one */
74 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
75         : Region (other, offset, length, name, layer, flags)
76 {
77         assert(_name.find("/") == string::npos);
78 }
79
80 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other)
81         : Region (other)
82 {
83         assert(_name.find("/") == string::npos);
84 }
85
86 MidiRegion::MidiRegion (boost::shared_ptr<MidiSource> src, const XMLNode& node)
87         : Region (src, node)
88 {
89         if (set_state (node)) {
90                 throw failed_constructor();
91         }
92
93         assert(_name.find("/") == string::npos);
94         assert(_type == DataType::MIDI);
95 }
96
97 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
98         : Region (srcs, node)
99 {
100         if (set_state (node)) {
101                 throw failed_constructor();
102         }
103
104         assert(_name.find("/") == string::npos);
105         assert(_type == DataType::MIDI);
106 }
107
108 MidiRegion::~MidiRegion ()
109 {
110 }
111
112 nframes_t
113 MidiRegion::read_at (MidiRingBuffer& out, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
114 {
115         return _read_at (_sources, out, position, dur, chan_n, mode);
116 }
117
118 nframes_t
119 MidiRegion::master_read_at (MidiRingBuffer& out, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
120 {
121         return _read_at (_master_sources, out, position, dur, chan_n, mode);
122 }
123
124 nframes_t
125 MidiRegion::_read_at (const SourceList& srcs, MidiRingBuffer& dst, nframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode) const
126 {
127         // cerr << _name << "._read_at(" << position << ") - " << _position << endl;
128
129         nframes_t internal_offset = 0;
130         nframes_t src_offset      = 0;
131         nframes_t to_read         = 0;
132         
133         /* precondition: caller has verified that we cover the desired section */
134
135         assert(chan_n == 0);
136         
137         if (position < _position) {
138                 internal_offset = 0;
139                 src_offset = _position - position;
140                 dur -= src_offset;
141         } else {
142                 internal_offset = position - _position;
143                 src_offset = 0;
144         }
145
146         if (internal_offset >= _length) {
147                 return 0; /* read nothing */
148         }
149         
150
151         if ((to_read = min (dur, _length - internal_offset)) == 0) {
152                 return 0; /* read nothing */
153         }
154
155         // FIXME: non-opaque MIDI regions not yet supported
156         assert(opaque());
157
158         if (muted()) {
159                 return 0; /* read nothing */
160         }
161
162         _read_data_count = 0;
163
164         boost::shared_ptr<MidiSource> src = midi_source(chan_n);
165         src->set_note_mode(mode);
166
167         if (src->read (dst, _start + internal_offset, to_read, _position) != to_read) {
168                 return 0; /* "read nothing" */
169         }
170
171         _read_data_count += src->read_data_count();
172
173         return to_read;
174 }
175         
176 XMLNode&
177 MidiRegion::state (bool full)
178 {
179         XMLNode& node (Region::state (full));
180         char buf[64];
181         char buf2[64];
182         LocaleGuard lg (X_("POSIX"));
183         
184         node.add_property ("flags", enum_2_string (_flags));
185         
186         for (uint32_t n=0; n < _sources.size(); ++n) {
187                 snprintf (buf2, sizeof(buf2), "source-%d", n);
188                 _sources[n]->id().print (buf, sizeof(buf));
189                 node.add_property (buf2, buf);
190         }
191
192         if (full && _extra_xml) {
193                 node.add_child_copy (*_extra_xml);
194         }
195
196         return node;
197 }
198
199 int
200 MidiRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
201 {
202         const XMLProperty *prop;
203         LocaleGuard lg (X_("POSIX"));
204
205         Region::set_live_state (node, what_changed, false);
206
207         uint32_t old_flags = _flags;
208                 
209         if ((prop = node.property ("flags")) != 0) {
210                 _flags = Flag (string_2_enum (prop->value(), _flags));
211
212                 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
213
214                 _flags = Flag (_flags & ~Region::LeftOfSplit);
215                 _flags = Flag (_flags & ~Region::RightOfSplit);
216         }
217
218         if ((old_flags ^ _flags) & Muted) {
219                 what_changed = Change (what_changed|MuteChanged);
220         }
221         if ((old_flags ^ _flags) & Opaque) {
222                 what_changed = Change (what_changed|OpacityChanged);
223         }
224         if ((old_flags ^ _flags) & Locked) {
225                 what_changed = Change (what_changed|LockChanged);
226         }
227
228         if (send) {
229                 send_change (what_changed);
230         }
231
232         return 0;
233 }
234
235 int
236 MidiRegion::set_state (const XMLNode& node)
237 {
238         /* Region::set_state() calls the virtual set_live_state(),
239            which will get us back to AudioRegion::set_live_state()
240            to handle the relevant stuff.
241         */
242
243         return Region::set_state (node);
244 }
245
246 void
247 MidiRegion::recompute_at_end ()
248 {
249         /* our length has changed
250          * (non destructively) "chop" notes that pass the end boundary, to
251          * prevent stuck notes.
252          */
253 }       
254
255 void
256 MidiRegion::recompute_at_start ()
257 {
258         /* as above, but the shift was from the front
259          * maybe bump currently active note's note-ons up so they sound here?
260          * that could be undesireable in certain situations though.. maybe
261          * remove the note entirely, including it's note off?  something needs to
262          * be done to keep the played MIDI sane to avoid messing up voices of
263          * polyhonic things etc........
264          */
265 }
266
267 int
268 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
269 {
270         // Separate by MIDI channel?  bit different from audio since this is separating based
271         // on the actual contained data and destructively modifies and creates new sources..
272         
273 #if 0
274         SourceList srcs;
275         string new_name;
276
277         for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
278
279                 srcs.clear ();
280                 srcs.push_back (*i);
281
282                 /* generate a new name */
283                 
284                 if (session.region_name (new_name, _name)) {
285                         return -1;
286                 }
287
288                 /* create a copy with just one source */
289
290                 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
291         }
292 #endif
293
294         // Actually, I would prefer not if that's alright
295         return -1;
296 }
297
298 boost::shared_ptr<MidiSource>
299 MidiRegion::midi_source (uint32_t n) const
300 {
301         // Guaranteed to succeed (use a static cast?)
302         return boost::dynamic_pointer_cast<MidiSource>(source(n));
303 }
304