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