permit different sizes for audio playback & capture buffers
[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
28 #include <glibmm/thread.h>
29
30 #include "pbd/basename.h"
31 #include "pbd/xml++.h"
32 #include "pbd/enumwriter.h"
33
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/region_factory.h"
41 #include "ardour/types.h"
42 #include "ardour/midi_ring_buffer.h"
43
44 #include "i18n.h"
45 #include <locale.h>
46
47 using namespace std;
48 using namespace ARDOUR;
49 using namespace PBD;
50
51 /* Basic MidiRegion constructor (many channels) */
52 MidiRegion::MidiRegion (const SourceList& srcs)
53         : Region (srcs)
54 {
55         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
56         assert(_name.val().find("/") == string::npos);
57         assert(_type == DataType::MIDI);
58 }
59
60 /** Create a new MidiRegion, that is part of an existing one */
61 MidiRegion::MidiRegion (boost::shared_ptr<const MidiRegion> other, frameoffset_t offset, bool offset_relative)
62         : Region (other, offset, offset_relative)
63 {
64         assert(_name.val().find("/") == string::npos);
65         midi_source(0)->Switched.connect_same_thread (*this, boost::bind (&MidiRegion::switch_source, this, _1));
66 }
67
68 MidiRegion::~MidiRegion ()
69 {
70 }
71
72 /** Create a new MidiRegion that has its own version of some/all of the Source used by another. 
73  */
74 boost::shared_ptr<MidiRegion>
75 MidiRegion::clone ()
76 {
77         BeatsFramesConverter bfc (_session.tempo_map(), _position);
78         double bbegin = bfc.from (_position);
79         double bend = bfc.from (last_frame() + 1);
80
81         boost::shared_ptr<MidiSource> ms = midi_source(0)->clone (bbegin, bend);
82
83         PropertyList plist;
84
85         plist.add (Properties::name, ms->name());
86         plist.add (Properties::whole_file, true);
87         plist.add (Properties::start, 0);
88         plist.add (Properties::length, _length);
89         plist.add (Properties::layer, 0);
90
91         return boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (ms, plist, true));
92 }
93
94 void
95 MidiRegion::set_position_internal (framepos_t pos, bool allow_bbt_recompute)
96 {
97         BeatsFramesConverter old_converter(_session.tempo_map(), _position - _start);
98         double length_beats = old_converter.from(_length);
99
100         Region::set_position_internal(pos, allow_bbt_recompute);
101
102         BeatsFramesConverter new_converter(_session.tempo_map(), pos - _start);
103
104         set_length(new_converter.to(length_beats), 0);
105 }
106
107 framecnt_t
108 MidiRegion::read_at (Evoral::EventSink<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
109 {
110         return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
111 }
112
113 framecnt_t
114 MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, framepos_t position, framecnt_t dur, uint32_t chan_n, NoteMode mode) const
115 {
116         return _read_at (_master_sources, out, position, dur, chan_n, mode); /* no tracker */
117 }
118
119 framecnt_t
120 MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>& dst, framepos_t position, framecnt_t dur, uint32_t chan_n, 
121                       NoteMode mode, MidiStateTracker* tracker) const
122 {
123         frameoffset_t internal_offset = 0;
124         frameoffset_t src_offset      = 0;
125         framecnt_t to_read         = 0;
126
127         /* precondition: caller has verified that we cover the desired section */
128
129         assert(chan_n == 0);
130
131         if (muted()) {
132                 return 0; /* read nothing */
133         }
134
135         if (position < _position) {
136                 internal_offset = 0;
137                 src_offset = _position - position;
138                 dur -= src_offset;
139         } else {
140                 internal_offset = position - _position;
141                 src_offset = 0;
142         }
143
144         if (internal_offset >= _length) {
145                 return 0; /* read nothing */
146         }
147
148         if ((to_read = min (dur, _length - internal_offset)) == 0) {
149                 return 0; /* read nothing */
150         }
151
152         _read_data_count = 0;
153
154         boost::shared_ptr<MidiSource> src = midi_source(chan_n);
155         src->set_note_mode(mode);
156
157         framepos_t output_buffer_position = 0;
158         framepos_t negative_output_buffer_position = 0;
159         if (_position >= _start) {
160                 // handle resizing of beginnings of regions correctly
161                 output_buffer_position = _position - _start;
162         } else {
163                 // when _start is greater than _position, we have to subtract
164                 // _start from the note times in the midi source
165                 negative_output_buffer_position = _start;
166         }
167
168         /*cerr << "MR read @ " << position << " * " << to_read
169                 << " _position = " << _position
170             << " _start = " << _start
171             << " offset = " << output_buffer_position
172             << " negoffset = " << negative_output_buffer_position
173             << " intoffset = " << internal_offset
174             << endl;*/
175
176         if (src->midi_read (
177                         dst, // destination buffer
178                         _position - _start, // start position of the source in this read context
179                         _start + internal_offset, // where to start reading in the source
180                         to_read, // read duration in frames
181                         output_buffer_position, // the offset in the output buffer
182                         negative_output_buffer_position, // amount to substract from note times
183                         tracker
184                     ) != to_read) {
185                 return 0; /* "read nothing" */
186         }
187
188         _read_data_count += src->read_data_count();
189
190         return to_read;
191 }
192
193 XMLNode&
194 MidiRegion::state (bool full)
195 {
196         XMLNode& node (Region::state (full));
197         char buf[64];
198         char buf2[64];
199         LocaleGuard lg (X_("POSIX"));
200
201         // XXX these should move into Region
202
203         for (uint32_t n=0; n < _sources.size(); ++n) {
204                 snprintf (buf2, sizeof(buf2), "source-%d", n);
205                 _sources[n]->id().print (buf, sizeof(buf));
206                 node.add_property (buf2, buf);
207         }
208
209         for (uint32_t n=0; n < _master_sources.size(); ++n) {
210                 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
211                 _master_sources[n]->id().print (buf, sizeof (buf));
212                 node.add_property (buf2, buf);
213         }
214
215         if (full && _extra_xml) {
216                 node.add_child_copy (*_extra_xml);
217         }
218
219         return node;
220 }
221
222 int
223 MidiRegion::set_state (const XMLNode& node, int version)
224 {
225         return Region::set_state (node, version);
226 }
227
228 void
229 MidiRegion::recompute_at_end ()
230 {
231         /* our length has changed
232          * so what? stuck notes are dealt with via a note state tracker
233          */
234 }
235
236 void
237 MidiRegion::recompute_at_start ()
238 {
239         /* as above, but the shift was from the front
240          * maybe bump currently active note's note-ons up so they sound here?
241          * that could be undesireable in certain situations though.. maybe
242          * remove the note entirely, including it's note off?  something needs to
243          * be done to keep the played MIDI sane to avoid messing up voices of
244          * polyhonic things etc........
245          */
246 }
247
248 int
249 MidiRegion::separate_by_channel (ARDOUR::Session&, vector< boost::shared_ptr<Region> >&) const
250 {
251         // TODO
252         return -1;
253 }
254
255 int
256 MidiRegion::exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&)
257 {
258         return -1;
259 }
260
261 boost::shared_ptr<MidiSource>
262 MidiRegion::midi_source (uint32_t n) const
263 {
264         // Guaranteed to succeed (use a static cast?)
265         return boost::dynamic_pointer_cast<MidiSource>(source(n));
266 }
267
268
269 void
270 MidiRegion::switch_source(boost::shared_ptr<Source> src)
271 {
272         boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
273         if (!msrc)
274                 return;
275
276         // MIDI regions have only one source
277         _sources.clear();
278         _sources.push_back(msrc);
279
280         set_name(msrc->name());
281 }
282