2 Copyright (C) 2006 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __ardour_midi_source_h__
21 #define __ardour_midi_source_h__
25 #include <glibmm/threads.h>
26 #include <boost/enable_shared_from_this.hpp>
27 #include "pbd/stateful.h"
28 #include "pbd/xml++.h"
29 #include "evoral/Sequence.hpp"
30 #include "evoral/Range.hpp"
31 #include "ardour/ardour.h"
32 #include "ardour/buffer.h"
33 #include "ardour/source.h"
34 #include "ardour/beats_frames_converter.h"
38 class MidiChannelFilter;
39 class MidiStateTracker;
42 template<typename T> class MidiRingBuffer;
44 /** Source for MIDI data */
45 class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_shared_from_this<MidiSource>
48 typedef Evoral::Beats TimeType;
50 MidiSource (Session& session, std::string name, Source::Flag flags = Source::Flag(0));
51 MidiSource (Session& session, const XMLNode&);
52 virtual ~MidiSource ();
54 /** Write the data in the given time range to another MidiSource
55 * \param newsrc MidiSource to which data will be written. Should be a
56 * new, empty source. If it already has contents, the results are
57 * undefined. Source must be writable.
58 * \param begin time of earliest event that can be written.
59 * \param end time of latest event that can be written.
60 * \return zero on success, non-zero if the write failed for any reason.
62 int write_to (const Lock& lock,
63 boost::shared_ptr<MidiSource> newsrc,
64 Evoral::Beats begin = Evoral::MinBeats,
65 Evoral::Beats end = Evoral::MaxBeats);
67 /** Export the midi data in the given time range to another MidiSource
68 * \param newsrc MidiSource to which data will be written. Should be a
69 * new, empty source. If it already has contents, the results are
70 * undefined. Source must be writable.
71 * \param begin time of earliest event that can be written.
72 * \param end time of latest event that can be written.
73 * \return zero on success, non-zero if the write failed for any reason.
75 int export_write_to (const Lock& lock,
76 boost::shared_ptr<MidiSource> newsrc,
80 /** Read the data in a given time range from the MIDI source.
81 * All time stamps in parameters are in audio frames (even if the source has tempo time).
82 * \param dst Ring buffer where read events are written.
83 * \param source_start Start position of the SOURCE in this read context.
84 * \param start Start of range to be read.
85 * \param cnt Length of range to be read (in audio frames).
86 * \param loop_range If non-null, all event times will be mapped into this loop range.
87 * \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking.
88 * \param filtered Parameters whose MIDI messages will not be returned.
91 virtual framecnt_t midi_read (const Lock& lock,
92 Evoral::EventSink<framepos_t>& dst,
93 framepos_t source_start,
96 Evoral::Range<framepos_t>* loop_range,
97 MidiStateTracker* tracker,
98 MidiChannelFilter* filter,
99 const std::set<Evoral::Parameter>& filtered,
101 const double start_beats) const;
103 /** Write data from a MidiRingBuffer to this source.
104 * @param source Source to read from.
105 * @param source_start This source's start position in session frames.
106 * @param cnt The length of time to write.
108 virtual framecnt_t midi_write (const Lock& lock,
109 MidiRingBuffer<framepos_t>& src,
110 framepos_t source_start,
113 /** Append a single event with a timestamp in beats.
115 * Caller must ensure that the event is later than the last written event.
117 virtual void append_event_beats(const Lock& lock,
118 const Evoral::Event<Evoral::Beats>& ev) = 0;
120 /** Append a single event with a timestamp in frames.
122 * Caller must ensure that the event is later than the last written event.
124 virtual void append_event_frames(const Lock& lock,
125 const Evoral::Event<framepos_t>& ev,
126 framepos_t source_start) = 0;
128 virtual bool empty () const;
129 virtual framecnt_t length (framepos_t pos) const;
130 virtual void update_length (framecnt_t);
132 virtual void mark_streaming_midi_write_started (const Lock& lock, NoteMode mode);
133 virtual void mark_streaming_write_started (const Lock& lock);
134 virtual void mark_streaming_write_completed (const Lock& lock);
136 /** Mark write starting with the given time parameters.
138 * This is called by MidiDiskStream::process before writing to the capture
139 * buffer which will be later read by midi_read().
141 * @param position The timeline position the source now starts at.
142 * @param capture_length The current length of the capture, which may not
143 * be zero if record is armed while rolling.
144 * @param loop_length The loop length if looping, otherwise zero.
146 void mark_write_starting_now (framecnt_t position,
147 framecnt_t capture_length,
148 framecnt_t loop_length);
150 /* like ::mark_streaming_write_completed() but with more arguments to
151 * allow control over MIDI-specific behaviour. Expected to be used only
152 * when recording actual MIDI input, rather then when importing files
155 virtual void mark_midi_streaming_write_completed (
157 Evoral::Sequence<Evoral::Beats>::StuckNoteOption stuck_option,
158 Evoral::Beats when = Evoral::Beats());
160 virtual void session_saved();
162 std::string captured_for() const { return _captured_for; }
163 void set_captured_for (std::string str) { _captured_for = str; }
165 static PBD::Signal1<void,MidiSource*> MidiSourceCreated;
167 XMLNode& get_state ();
168 int set_state (const XMLNode&, int version);
170 bool length_mutable() const { return true; }
172 void set_length_beats(TimeType l) { _length_beats = l; }
173 TimeType length_beats() const { return _length_beats; }
174 double length_pulse() const { return _length_pulse; }
176 virtual void load_model(const Glib::Threads::Mutex::Lock& lock, bool force_reload=false) = 0;
177 virtual void destroy_model(const Glib::Threads::Mutex::Lock& lock) = 0;
179 /** Reset cached information (like iterators) when things have changed.
180 * @param lock Source lock, which must be held by caller.
181 * @param notes If non-NULL, currently active notes are added to this set.
183 void invalidate(const Glib::Threads::Mutex::Lock& lock,
184 std::set<Evoral::Sequence<Evoral::Beats>::WeakNotePtr>* notes=NULL);
186 void set_note_mode(const Glib::Threads::Mutex::Lock& lock, NoteMode mode);
188 boost::shared_ptr<MidiModel> model() { return _model; }
189 void set_model(const Glib::Threads::Mutex::Lock& lock, boost::shared_ptr<MidiModel>);
190 void drop_model(const Glib::Threads::Mutex::Lock& lock);
192 Evoral::ControlList::InterpolationStyle interpolation_of (Evoral::Parameter) const;
193 void set_interpolation_of (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
194 void copy_interpolation_from (boost::shared_ptr<MidiSource>);
195 void copy_interpolation_from (MidiSource *);
197 AutoState automation_state_of (Evoral::Parameter) const;
198 void set_automation_state_of (Evoral::Parameter, AutoState);
199 void copy_automation_state_from (boost::shared_ptr<MidiSource>);
200 void copy_automation_state_from (MidiSource *);
202 /** Emitted when a different MidiModel is set */
203 PBD::Signal0<void> ModelChanged;
204 /** Emitted when a parameter's interpolation style is changed */
205 PBD::Signal2<void, Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationChanged;
206 /** Emitted when a parameter's automation state is changed */
207 PBD::Signal2<void, Evoral::Parameter, AutoState> AutomationStateChanged;
210 virtual void flush_midi(const Lock& lock) = 0;
212 virtual framecnt_t read_unlocked (const Lock& lock,
213 Evoral::EventSink<framepos_t>& dst,
217 Evoral::Range<framepos_t>* loop_range,
218 MidiStateTracker* tracker,
219 MidiChannelFilter* filter) const = 0;
221 /** Write data to this source from a MidiRingBuffer.
222 * @param source Buffer to read from.
223 * @param position This source's start position in session frames.
224 * @param cnt The duration of this block to write for.
226 virtual framecnt_t write_unlocked (const Lock& lock,
227 MidiRingBuffer<framepos_t>& source,
231 std::string _captured_for;
233 boost::shared_ptr<MidiModel> _model;
236 mutable Evoral::Sequence<Evoral::Beats>::const_iterator _model_iter;
237 mutable bool _model_iter_valid;
239 mutable Evoral::Beats _length_beats;
240 mutable double _length_pulse;
241 mutable framepos_t _last_read_end;
243 /** The total duration of the current capture. */
244 framepos_t _capture_length;
246 /** Length of transport loop during current capture, or zero. */
247 framepos_t _capture_loop_length;
249 /** Map of interpolation styles to use for Parameters; if they are not in this map,
250 * the correct interpolation style can be obtained from EventTypeMap::interpolation_of ()
252 typedef std::map<Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationStyleMap;
253 InterpolationStyleMap _interpolation_style;
255 /** Map of automation states to use for Parameters; if they are not in this map,
256 * the correct automation state is Off.
258 typedef std::map<Evoral::Parameter, AutoState> AutomationStateMap;
259 AutomationStateMap _automation_state;
264 #endif /* __ardour_midi_source_h__ */