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