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 "ardour/ardour.h"
31 #include "ardour/buffer.h"
32 #include "ardour/source.h"
33 #include "ardour/beats_frames_converter.h"
37 class MidiStateTracker;
40 template<typename T> class MidiRingBuffer;
42 /** Source for MIDI data */
43 class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_shared_from_this<MidiSource>
46 typedef double TimeType;
48 MidiSource (Session& session, std::string name, Source::Flag flags = Source::Flag(0));
49 MidiSource (Session& session, const XMLNode&);
50 virtual ~MidiSource ();
52 boost::shared_ptr<MidiSource> clone (const std::string& path,
53 Evoral::MusicalTime begin = Evoral::MinMusicalTime,
54 Evoral::MusicalTime end = Evoral::MaxMusicalTime);
56 /** Read the data in a given time range from the MIDI source.
57 * All time stamps in parameters are in audio frames (even if the source has tempo time).
58 * \param dst Ring buffer where read events are written
59 * \param source_start Start position of the SOURCE in this read context
60 * \param start Start of range to be read
61 * \param cnt Length of range to be read (in audio frames)
62 * \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking
64 virtual framecnt_t midi_read (Evoral::EventSink<framepos_t>& dst,
65 framepos_t source_start,
68 MidiStateTracker* tracker,
69 std::set<Evoral::Parameter> const &) const;
71 /** Write data from a MidiRingBuffer to this source.
72 * @param source Source to read from.
73 * @param source_start This source's start position in session frames.
74 * @param cnt The length of time to write.
76 virtual framecnt_t midi_write (MidiRingBuffer<framepos_t>& src,
77 framepos_t source_start,
80 virtual void append_event_unlocked_beats(const Evoral::Event<Evoral::MusicalTime>& ev) = 0;
82 virtual void append_event_unlocked_frames(const Evoral::Event<framepos_t>& ev,
83 framepos_t source_start) = 0;
85 virtual bool empty () const;
86 virtual framecnt_t length (framepos_t pos) const;
87 virtual void update_length (framecnt_t);
89 virtual void mark_streaming_midi_write_started (NoteMode mode);
90 virtual void mark_streaming_write_started ();
91 virtual void mark_streaming_write_completed ();
93 /** Mark write starting with the given time parameters.
95 * This is called by MidiDiskStream::process before writing to the capture
96 * buffer which will be later read by midi_read().
98 * @param position The timeline position the source now starts at.
99 * @param capture_length The current length of the capture, which may not
100 * be zero if record is armed while rolling.
101 * @param loop_length The loop length if looping, otherwise zero.
103 void mark_write_starting_now (framecnt_t position,
104 framecnt_t capture_length,
105 framecnt_t loop_length);
107 /* like ::mark_streaming_write_completed() but with more arguments to
108 * allow control over MIDI-specific behaviour. Expected to be used only
109 * when recording actual MIDI input, rather then when importing files
112 virtual void mark_midi_streaming_write_completed (
113 Evoral::Sequence<Evoral::MusicalTime>::StuckNoteOption stuck_option,
114 Evoral::MusicalTime when = 0);
116 virtual void session_saved();
118 std::string captured_for() const { return _captured_for; }
119 void set_captured_for (std::string str) { _captured_for = str; }
121 static PBD::Signal1<void,MidiSource*> MidiSourceCreated;
123 XMLNode& get_state ();
124 int set_state (const XMLNode&, int version);
126 bool length_mutable() const { return true; }
128 void set_length_beats(double l) { _length_beats = l; }
129 double length_beats() const { return _length_beats; }
131 virtual void load_model(bool lock=true, bool force_reload=false) = 0;
132 virtual void destroy_model() = 0;
134 /** This must be called with the source lock held whenever the
135 * source/model contents have been changed (reset iterators/cache/etc).
139 void set_note_mode(NoteMode mode);
141 boost::shared_ptr<MidiModel> model() { return _model; }
142 void set_model (boost::shared_ptr<MidiModel>);
145 Evoral::ControlList::InterpolationStyle interpolation_of (Evoral::Parameter) const;
146 void set_interpolation_of (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
147 void copy_interpolation_from (boost::shared_ptr<MidiSource>);
148 void copy_interpolation_from (MidiSource *);
150 AutoState automation_state_of (Evoral::Parameter) const;
151 void set_automation_state_of (Evoral::Parameter, AutoState);
152 void copy_automation_state_from (boost::shared_ptr<MidiSource>);
153 void copy_automation_state_from (MidiSource *);
155 /** Emitted when a different MidiModel is set */
156 PBD::Signal0<void> ModelChanged;
157 /** Emitted when a parameter's interpolation style is changed */
158 PBD::Signal2<void, Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationChanged;
159 /** Emitted when a parameter's automation state is changed */
160 PBD::Signal2<void, Evoral::Parameter, AutoState> AutomationStateChanged;
163 virtual void flush_midi() = 0;
165 virtual framecnt_t read_unlocked (Evoral::EventSink<framepos_t>& dst,
169 MidiStateTracker* tracker) const = 0;
171 /** Write data to this source from a MidiRingBuffer.
172 * @param source Buffer to read from.
173 * @param position This source's start position in session frames.
174 * @param cnt The duration of this block to write for.
176 virtual framecnt_t write_unlocked (MidiRingBuffer<framepos_t>& source,
180 std::string _captured_for;
182 boost::shared_ptr<MidiModel> _model;
185 mutable Evoral::Sequence<Evoral::MusicalTime>::const_iterator _model_iter;
186 mutable bool _model_iter_valid;
188 mutable double _length_beats;
189 mutable framepos_t _last_read_end;
191 /** The total duration of the current capture. */
192 framepos_t _capture_length;
194 /** Length of transport loop during current capture, or zero. */
195 framepos_t _capture_loop_length;
197 /** Map of interpolation styles to use for Parameters; if they are not in this map,
198 * the correct interpolation style can be obtained from EventTypeMap::interpolation_of ()
200 typedef std::map<Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationStyleMap;
201 InterpolationStyleMap _interpolation_style;
203 /** Map of automation states to use for Parameters; if they are not in this map,
204 * the correct automation state is Off.
206 typedef std::map<Evoral::Parameter, AutoState> AutomationStateMap;
207 AutomationStateMap _automation_state;
212 #endif /* __ardour_midi_source_h__ */