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