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