Make Region's _beat a PBD::Property.
[ardour.git] / libs / ardour / ardour / midi_source.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: David Robillard
4
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.
9
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.
14
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.
18 */
19
20 #ifndef __ardour_midi_source_h__
21 #define __ardour_midi_source_h__
22
23 #include <string>
24 #include <time.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"
34
35 namespace ARDOUR {
36
37 class MidiChannelFilter;
38 class MidiStateTracker;
39 class MidiModel;
40
41 template<typename T> class MidiRingBuffer;
42
43 /** Source for MIDI data */
44 class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_shared_from_this<MidiSource>
45 {
46   public:
47         typedef Evoral::Beats TimeType;
48
49         MidiSource (Session& session, std::string name, Source::Flag flags = Source::Flag(0));
50         MidiSource (Session& session, const XMLNode&);
51         virtual ~MidiSource ();
52
53         /** Write the data in the given time range to another MidiSource
54          * \param newsrc MidiSource to which data will be written. Should be a
55          *        new, empty source. If it already has contents, the results are
56          *        undefined. Source must be writable.
57          * \param begin time of earliest event that can be written.
58          * \param end time of latest event that can be written.
59          * \return zero on success, non-zero if the write failed for any reason.
60          */
61         int write_to (const Lock&                   lock,
62                       boost::shared_ptr<MidiSource> newsrc,
63                       Evoral::Beats                 begin = Evoral::MinBeats,
64                       Evoral::Beats                 end   = Evoral::MaxBeats);
65
66         /** Export the midi data in the given time range to another MidiSource
67          * \param newsrc MidiSource to which data will be written. Should be a
68          *        new, empty source. If it already has contents, the results are
69          *        undefined. Source must be writable.
70          * \param begin time of earliest event that can be written.
71          * \param end time of latest event that can be written.
72          * \return zero on success, non-zero if the write failed for any reason.
73          */
74         int export_write_to (const Lock&                   lock,
75                              boost::shared_ptr<MidiSource> newsrc,
76                              Evoral::Beats                 begin,
77                              Evoral::Beats                 end);
78
79         /** Read the data in a given time range from the MIDI source.
80          * All time stamps in parameters are in audio frames (even if the source has tempo time).
81          * \param dst Ring buffer where read events are written.
82          * \param source_start Start position of the SOURCE in this read context.
83          * \param start Start of range to be read.
84          * \param cnt Length of range to be read (in audio frames).
85          * \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking.
86          * \param filtered Parameters whose MIDI messages will not be returned.
87          */
88         virtual framecnt_t midi_read (const Lock&                        lock,
89                                       Evoral::EventSink<framepos_t>&     dst,
90                                       framepos_t                         source_start,
91                                       framepos_t                         start,
92                                       framecnt_t                         cnt,
93                                       MidiStateTracker*                  tracker,
94                                       MidiChannelFilter*                 filter,
95                                       const std::set<Evoral::Parameter>& filtered,
96                                       double                             beat,
97                                       double                             start_beat) const;
98
99         /** Write data from a MidiRingBuffer to this source.
100          *  @param source Source to read from.
101          *  @param source_start This source's start position in session frames.
102          *  @param cnt The length of time to write.
103          */
104         virtual framecnt_t midi_write (const Lock&                 lock,
105                                        MidiRingBuffer<framepos_t>& src,
106                                        framepos_t                  source_start,
107                                        framecnt_t                  cnt);
108
109         /** Append a single event with a timestamp in beats.
110          *
111          * Caller must ensure that the event is later than the last written event.
112          */
113         virtual void append_event_beats(const Lock&                         lock,
114                                         const Evoral::Event<Evoral::Beats>& ev) = 0;
115
116         /** Append a single event with a timestamp in frames.
117          *
118          * Caller must ensure that the event is later than the last written event.
119          */
120         virtual void append_event_frames(const Lock&                      lock,
121                                          const Evoral::Event<framepos_t>& ev,
122                                          framepos_t                       source_start) = 0;
123
124         virtual bool       empty () const;
125         virtual framecnt_t length (framepos_t pos) const;
126         virtual void       update_length (framecnt_t);
127
128         virtual void mark_streaming_midi_write_started (const Lock& lock, NoteMode mode);
129         virtual void mark_streaming_write_started (const Lock& lock);
130         virtual void mark_streaming_write_completed (const Lock& lock);
131
132         /** Mark write starting with the given time parameters.
133          *
134          * This is called by MidiDiskStream::process before writing to the capture
135          * buffer which will be later read by midi_read().
136          *
137          * @param position The timeline position the source now starts at.
138          * @param capture_length The current length of the capture, which may not
139          * be zero if record is armed while rolling.
140          * @param loop_length The loop length if looping, otherwise zero.
141          */
142         void mark_write_starting_now (framecnt_t position,
143                                       framecnt_t capture_length,
144                                       framecnt_t loop_length);
145
146         /* like ::mark_streaming_write_completed() but with more arguments to
147          * allow control over MIDI-specific behaviour. Expected to be used only
148          * when recording actual MIDI input, rather then when importing files
149          * etc.
150          */
151         virtual void mark_midi_streaming_write_completed (
152                 const Lock&                                      lock,
153                 Evoral::Sequence<Evoral::Beats>::StuckNoteOption stuck_option,
154                 Evoral::Beats                                    when = Evoral::Beats());
155
156         virtual void session_saved();
157
158         std::string captured_for() const               { return _captured_for; }
159         void        set_captured_for (std::string str) { _captured_for = str; }
160
161         static PBD::Signal1<void,MidiSource*> MidiSourceCreated;
162
163         XMLNode& get_state ();
164         int set_state (const XMLNode&, int version);
165
166         bool length_mutable() const { return true; }
167
168         void     set_length_beats(TimeType l) { _length_beats = l; }
169         TimeType length_beats() const         { return _length_beats; }
170
171         virtual void load_model(const Glib::Threads::Mutex::Lock& lock, bool force_reload=false) = 0;
172         virtual void destroy_model(const Glib::Threads::Mutex::Lock& lock) = 0;
173
174         /** Reset cached information (like iterators) when things have changed.
175          * @param lock Source lock, which must be held by caller.
176          * @param notes If non-NULL, currently active notes are added to this set.
177          */
178         void invalidate(const Glib::Threads::Mutex::Lock&                       lock,
179                         std::set<Evoral::Sequence<Evoral::Beats>::WeakNotePtr>* notes=NULL);
180
181         void set_note_mode(const Glib::Threads::Mutex::Lock& lock, NoteMode mode);
182
183         boost::shared_ptr<MidiModel> model() { return _model; }
184         void set_model(const Glib::Threads::Mutex::Lock& lock, boost::shared_ptr<MidiModel>);
185         void drop_model(const Glib::Threads::Mutex::Lock& lock);
186
187         Evoral::ControlList::InterpolationStyle interpolation_of (Evoral::Parameter) const;
188         void set_interpolation_of (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
189         void copy_interpolation_from (boost::shared_ptr<MidiSource>);
190         void copy_interpolation_from (MidiSource *);
191
192         AutoState automation_state_of (Evoral::Parameter) const;
193         void set_automation_state_of (Evoral::Parameter, AutoState);
194         void copy_automation_state_from (boost::shared_ptr<MidiSource>);
195         void copy_automation_state_from (MidiSource *);
196
197         /** Emitted when a different MidiModel is set */
198         PBD::Signal0<void> ModelChanged;
199         /** Emitted when a parameter's interpolation style is changed */
200         PBD::Signal2<void, Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationChanged;
201         /** Emitted when a parameter's automation state is changed */
202         PBD::Signal2<void, Evoral::Parameter, AutoState> AutomationStateChanged;
203
204   protected:
205         virtual void flush_midi(const Lock& lock) = 0;
206
207         virtual framecnt_t read_unlocked (const Lock&                    lock,
208                                           Evoral::EventSink<framepos_t>& dst,
209                                           framepos_t                     position,
210                                           framepos_t                     start,
211                                           framecnt_t                     cnt,
212                                           MidiStateTracker*              tracker,
213                                           MidiChannelFilter*             filter) const = 0;
214
215         /** Write data to this source from a MidiRingBuffer.
216          *  @param source Buffer to read from.
217          *  @param position This source's start position in session frames.
218          *  @param cnt The duration of this block to write for.
219          */
220         virtual framecnt_t write_unlocked (const Lock&                 lock,
221                                            MidiRingBuffer<framepos_t>& source,
222                                            framepos_t                  position,
223                                            framecnt_t                  cnt) = 0;
224
225         std::string _captured_for;
226
227         boost::shared_ptr<MidiModel> _model;
228         bool                         _writing;
229
230         mutable Evoral::Sequence<Evoral::Beats>::const_iterator _model_iter;
231         mutable bool                                            _model_iter_valid;
232
233         mutable Evoral::Beats _length_beats;
234         mutable framepos_t    _last_read_end;
235
236         /** The total duration of the current capture. */
237         framepos_t _capture_length;
238
239         /** Length of transport loop during current capture, or zero. */
240         framepos_t _capture_loop_length;
241
242         /** Map of interpolation styles to use for Parameters; if they are not in this map,
243          *  the correct interpolation style can be obtained from EventTypeMap::interpolation_of ()
244          */
245         typedef std::map<Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationStyleMap;
246         InterpolationStyleMap _interpolation_style;
247
248         /** Map of automation states to use for Parameters; if they are not in this map,
249          *  the correct automation state is Off.
250          */
251         typedef std::map<Evoral::Parameter, AutoState> AutomationStateMap;
252         AutomationStateMap  _automation_state;
253 };
254
255 }
256
257 #endif /* __ardour_midi_source_h__ */