megaopus commit: (1) add __STD_(LIMIT|FORMAT)_MACROS to command line flags for cc...
[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 <glibmm/thread.h>
28
29 #include "pbd/basename.h"
30 #include "pbd/xml++.h"
31 #include "pbd/enumwriter.h"
32
33 #include "ardour/midi_region.h"
34 #include "ardour/session.h"
35 #include "ardour/gain.h"
36 #include "ardour/dB.h"
37 #include "ardour/playlist.h"
38 #include "ardour/midi_source.h"
39 #include "ardour/region_factory.h"
40 #include "ardour/types.h"
41 #include "ardour/midi_ring_buffer.h"
42
43 #include "i18n.h"
44 #include <locale.h>
45
46 using namespace std;
47 using namespace ARDOUR;
48 using namespace PBD;
49
50 namespace ARDOUR {
51         namespace Properties {
52                 PBD::PropertyDescriptor<void*> midi_data;
53         }
54 }
55
56 void
57 MidiRegion::make_property_quarks ()
58 {
59         Properties::midi_data.property_id = g_quark_from_static_string (X_("midi-data"));
60         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for midi-data = %1\n", Properties::midi_data.property_id));
61 }
62
63 void
64 MidiRegion::register_properties ()
65 {
66         /* none yet, but its only a matter of time */
67 }
68
69 /* Basic MidiRegion constructor (many channels) */
70 MidiRegion::MidiRegion (const SourceList& srcs)
71         : Region (srcs)
72 {
73         register_properties ();
74
75         // midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
76         midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
77         model_changed ();
78         assert(_name.val().find("/") == string::npos);
79         assert(_type == DataType::MIDI);
80 }
81
82 /** Create a new MidiRegion, that is part of an existing one */
83 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, frameoffset_t offset, bool offset_relative)
84         : Region (other, offset, offset_relative)
85 {
86         register_properties ();
87
88         assert(_name.val().find("/") == string::npos);
89         // midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
90         midi_source(0)->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
91         model_changed ();
92 }
93
94 MidiRegion::~MidiRegion ()
95 {
96 }
97
98 /** Create a new MidiRegion that has its own version of some/all of the Source used by another. 
99  */
100 boost::shared_ptr<MidiRegion>
101 MidiRegion::clone ()
102 {
103         BeatsFramesConverter bfc (_session.tempo_map(), _position);
104         double bbegin = bfc.from (_position);
105         double bend = bfc.from (last_frame() + 1);
106
107         boost::shared_ptr<MidiSource> ms = midi_source(0)->clone (bbegin, bend);
108
109         PropertyList plist;
110
111         plist.add (Properties::name, ms->name());
112         plist.add (Properties::whole_file, true);
113         plist.add (Properties::start, 0);
114         plist.add (Properties::length, _length);
115         plist.add (Properties::layer, 0);
116
117         return boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (ms, plist, true));
118 }
119
120 void
121 MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
122 {
123         BeatsFramesConverter old_converter(_session.tempo_map(), _position - _start);
124         double length_beats = old_converter.from(_length);
125
126         Region::set_position_internal(pos, allow_bbt_recompute);
127
128         BeatsFramesConverter new_converter(_session.tempo_map(), pos - _start);
129
130         set_length(new_converter.to(length_beats), 0);
131 }
132
133 framecnt_t
134 MidiRegion::read_at (Evoral::EventSink<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
135 {
136         return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
137 }
138
139 framecnt_t
140 MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
141 {
142         return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
143 }
144
145 framecnt_t
146 MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>& dst, framepos_t position, framecnt_t dur, uint32_t chan_n, 
147                       NoteMode mode, MidiStateTracker* tracker) const
148 {
149         frameoffset_t internal_offset = 0;
150         framecnt_t to_read         = 0;
151
152         /* precondition: caller has verified that we cover the desired section */
153
154         assert(chan_n == 0);
155
156         if (muted()) {
157                 return 0; /* read nothing */
158         }
159
160         if (position < _position) {
161                 /* we are starting the read from before the start of the region */
162                 internal_offset = 0;
163                 dur -= _position - position;
164         } else {
165                 /* we are starting the read from after the start of the region */
166                 internal_offset = position - _position;
167         }
168
169         if (internal_offset >= _length) {
170                 return 0; /* read nothing */
171         }
172
173         if ((to_read = min (dur, _length - internal_offset)) == 0) {
174                 return 0; /* read nothing */
175         }
176
177         _read_data_count = 0;
178
179         boost::shared_ptr<MidiSource> src = midi_source(chan_n);
180         src->set_note_mode(mode);
181         
182         /*
183           cerr << "MR read @ " << position << " * " << to_read
184           << " _position = " << _position
185           << " _start = " << _start
186           << " intoffset = " << internal_offset
187           << endl;
188         */
189
190         /* This call reads events from a source and writes them to `dst' timed in session frames */
191
192         if (src->midi_read (
193                         dst, // destination buffer
194                         _position - _start, // start position of the source in session frames
195                         _start + internal_offset, // where to start reading in the source
196                         to_read, // read duration in frames
197                         tracker,
198                         _filtered_parameters
199                     ) != to_read) {
200                 return 0; /* "read nothing" */
201         }
202
203         _read_data_count += src->read_data_count();
204
205         return to_read;
206 }
207
208 XMLNode&
209 MidiRegion::state ()
210 {
211         return Region::state ();
212 }
213
214 int
215 MidiRegion::set_state (const XMLNode& node, int version)
216 {
217         return Region::set_state (node, version);
218 }
219
220 void
221 MidiRegion::recompute_at_end ()
222 {
223         /* our length has changed
224          * so what? stuck notes are dealt with via a note state tracker
225          */
226 }
227
228 void
229 MidiRegion::recompute_at_start ()
230 {
231         /* as above, but the shift was from the front
232          * maybe bump currently active note's note-ons up so they sound here?
233          * that could be undesireable in certain situations though.. maybe
234          * remove the note entirely, including it's note off?  something needs to
235          * be done to keep the played MIDI sane to avoid messing up voices of
236          * polyhonic things etc........
237          */
238 }
239
240 int
241 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
242 {
243         // TODO
244         return -1;
245 }
246
247 int
248 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
249 {
250         return -1;
251 }
252
253 boost::shared_ptr<MidiSource>
254 MidiRegion::midi_source (uint32_t n) const
255 {
256         // Guaranteed to succeed (use a static cast?)
257         return boost::dynamic_pointer_cast<MidiSource>(source(n));
258 }
259
260
261 void
262 MidiRegion::switch_source(boost::shared_ptr<Source> src)
263 {
264         _source_connection.disconnect ();
265         
266         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
267         if (!msrc) {
268                 return;
269         }
270
271         // MIDI regions have only one source
272         SourceList srcs;
273         srcs.push_back (msrc);
274
275         drop_sources ();
276         use_sources (srcs);
277         
278         set_name (msrc->name());
279
280         msrc->ModelChanged.connect_same_thread (_source_connection, boost::bind (&MidiRegion::model_changed, this));
281 }
282
283 void
284 MidiRegion::model_changed ()
285 {
286         if (!model()) {
287                 return;
288         }
289         
290         /* build list of filtered Parameters, being those whose automation state is not `Play' */
291
292         _filtered_parameters.clear ();
293
294         Automatable::Controls const & c = model()->controls();
295
296         for (Automatable::Controls::const_iterator i = c.begin(); i != c.end(); ++i) {
297                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
298                 assert (ac);
299                 if (ac->alist()->automation_state() != Play) {
300                         _filtered_parameters.insert (ac->parameter ());
301                 }
302         }
303
304         /* watch for changes to controls' AutoState */
305         midi_source()->AutomationStateChanged.connect_same_thread (
306                 _model_connection, boost::bind (&MidiRegion::model_automation_state_changed, this, _1)
307                 );
308
309         model()->ContentsChanged.connect_same_thread (
310                 _model_contents_connection, boost::bind (&MidiRegion::model_contents_changed, this));
311 }
312
313 void
314 MidiRegion::model_contents_changed ()
315 {
316         send_change (PropertyChange (Properties::midi_data));        
317 }
318
319 void
320 MidiRegion::model_automation_state_changed (Evoral::Parameter const & p)
321 {
322         /* Update our filtered parameters list after a change to a parameter's AutoState */
323         
324         boost::shared_ptr<AutomationControl> ac = model()->automation_control (p);
325         assert (ac);
326
327         if (ac->alist()->automation_state() == Play) {
328                 _filtered_parameters.erase (p);
329         } else {
330                 _filtered_parameters.insert (p);
331         }
332
333         /* the source will have an iterator into the model, and that iterator will have been set up
334            for a given set of filtered_paramters, so now that we've changed that list we must invalidate
335            the iterator.
336         */
337         Glib::Mutex::Lock lm (midi_source(0)->mutex());
338         midi_source(0)->invalidate ();
339 }