- Changed IO's vector<Port*>'s to PortList
[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
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
43 #include "i18n.h"
44 #include <locale.h>
45
46 using namespace std;
47 using namespace ARDOUR;
48
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))
52 {
53         save_state ("initial state");
54
55         if (announce) {
56                  CheckNewRegion (this); /* EMIT SIGNAL */
57         }
58 }
59
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)
63 {
64         save_state ("initial state");
65
66         if (announce) {
67                  CheckNewRegion (this); /* EMIT SIGNAL */
68         }
69 }
70
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)
74 {
75         save_state ("initial state");
76
77         if (announce) {
78                  CheckNewRegion (this); /* EMIT SIGNAL */
79         }
80 }
81
82
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)
86 {
87         save_state ("initial state");
88
89         if (announce) {
90                 CheckNewRegion (this); /* EMIT SIGNAL */
91         }
92 }
93
94 MidiRegion::MidiRegion (const MidiRegion &other)
95         : Region (other)
96 {
97         save_state ("initial state");
98
99         /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
100 }
101
102 MidiRegion::MidiRegion (MidiSource& src, const XMLNode& node)
103         : Region (src, node)
104 {
105         if (set_state (node)) {
106                 throw failed_constructor();
107         }
108
109         save_state ("initial state");
110
111         CheckNewRegion (this); /* EMIT SIGNAL */
112 }
113
114 MidiRegion::MidiRegion (SourceList& srcs, const XMLNode& node)
115         : Region (srcs, node)
116 {
117         if (set_state (node)) {
118                 throw failed_constructor();
119         }
120
121         save_state ("initial state");
122
123         CheckNewRegion (this); /* EMIT SIGNAL */
124 }
125
126 MidiRegion::~MidiRegion ()
127 {
128         GoingAway (this);
129 }
130
131 StateManager::State*
132 MidiRegion::state_factory (std::string why) const
133 {
134         RegionState* state = new RegionState (why);
135
136         Region::store_state (*state);
137
138         return state;
139 }       
140
141 Change
142 MidiRegion::restore_state (StateManager::State& sstate) 
143 {
144         RegionState* state = dynamic_cast<RegionState*> (&sstate);
145         Change what_changed = Region::restore_and_return_flags (*state);
146         
147         if (_flags != Flag (state->_flags)) {
148                 
149                 //uint32_t old_flags = _flags;
150                 
151                 _flags = Flag (state->_flags);
152         }
153                 
154         what_changed = Change (what_changed);
155
156         return what_changed;
157 }
158
159 UndoAction
160 MidiRegion::get_memento() const
161 {
162         return sigc::bind (mem_fun (*(const_cast<MidiRegion *> (this)), &StateManager::use_state), _current_state_id);
163 }
164
165 jack_nframes_t
166 MidiRegion::read_at (RawMidi *out, RawMidi* mix_buf, jack_nframes_t position, 
167                       jack_nframes_t cnt, 
168                       uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
169 {
170         return _read_at (_sources, out, position, cnt, chan_n, read_frames, skip_frames);
171 }
172
173 jack_nframes_t
174 MidiRegion::master_read_at (RawMidi *out, RawMidi* mix_buf, jack_nframes_t position, 
175                              jack_nframes_t cnt, uint32_t chan_n) const
176 {
177         return _read_at (_master_sources, out, position, cnt, chan_n, 0, 0);
178 }
179
180 jack_nframes_t
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
184 {
185         jack_nframes_t internal_offset;
186         jack_nframes_t buf_offset;
187         jack_nframes_t to_read;
188         
189         /* precondition: caller has verified that we cover the desired section */
190
191         assert(chan_n == 0);
192         
193         if (position < _position) {
194                 internal_offset = 0;
195                 buf_offset = _position - position;
196                 cnt -= buf_offset;
197         } else {
198                 internal_offset = position - _position;
199                 buf_offset = 0;
200         }
201
202         if (internal_offset >= _length) {
203                 return 0; /* read nothing */
204         }
205         
206
207         if ((to_read = min (cnt, _length - internal_offset)) == 0) {
208                 return 0; /* read nothing */
209         }
210
211         // FIXME: non-opaque MIDI regions not yet supported
212         assert(opaque());
213
214         if (muted()) {
215                 return 0; /* read nothing */
216         }
217
218         _read_data_count = 0;
219
220         MidiSource& src = midi_source(chan_n);
221         if (src.read (buf, _start + internal_offset, to_read) != to_read) {
222                 return 0; /* "read nothing" */
223         }
224
225         _read_data_count += src.read_data_count();
226
227         return to_read;
228 }
229         
230 XMLNode&
231 MidiRegion::state (bool full)
232 {
233         XMLNode& node (Region::state (full));
234         XMLNode *child;
235         char buf[64];
236         char buf2[64];
237         LocaleGuard lg (X_("POSIX"));
238         
239         snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
240         node.add_property ("flags", buf);
241
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);
246         }
247
248         snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
249         node.add_property ("channels", buf);
250
251         child = node.add_child ("Envelope");
252
253         if ( ! full) {
254                 child->add_property ("default", "yes");
255         }
256
257         if (full && _extra_xml) {
258                 node.add_child_copy (*_extra_xml);
259         }
260
261         return node;
262 }
263
264 int
265 MidiRegion::set_state (const XMLNode& node)
266 {
267         const XMLNodeList& nlist = node.children();
268         const XMLProperty *prop;
269         LocaleGuard lg (X_("POSIX"));
270
271         Region::set_state (node);
272
273         if ((prop = node.property ("flags")) != 0) {
274                 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
275         }
276
277         /* Now find child items */
278         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
279                 
280                 XMLNode *child;
281                 //XMLProperty *prop;
282                 
283                 child = (*niter);
284                 
285                 /** Hello, children */
286         }
287
288         return 0;
289 }
290
291 void
292 MidiRegion::recompute_at_end ()
293 {
294         /* our length has changed
295          * (non destructively) "chop" notes that pass the end boundary, to
296          * prevent stuck notes.
297          */
298 }       
299
300 void
301 MidiRegion::recompute_at_start ()
302 {
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........
309          */
310 }
311
312 int
313 MidiRegion::separate_by_channel (Session& session, vector<MidiRegion*>& v) const
314 {
315 #if 0
316         SourceList srcs;
317         string new_name;
318
319         for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
320
321                 srcs.clear ();
322                 srcs.push_back (*i);
323
324                 /* generate a new name */
325                 
326                 if (session.region_name (new_name, _name)) {
327                         return -1;
328                 }
329
330                 /* create a copy with just one source */
331
332                 v.push_back (new MidiRegion (srcs, _start, _length, new_name, _layer, _flags));
333         }
334 #endif
335         return -1;
336 }
337
338 MidiSource&
339 MidiRegion::midi_source (uint32_t n) const
340 {
341         // Guaranteed to succeed (use a static cast?)
342         return dynamic_cast<MidiSource&>(source(n));
343 }
344