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