More style-only changes.
[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         void mark_write_starting_now ();
92
93         /* like ::mark_streaming_write_completed() but with more arguments to
94          * allow control over MIDI-specific behaviour. Expected to be used only
95          * when recording actual MIDI input, rather then when importing files
96          * etc.
97          */
98         virtual void mark_midi_streaming_write_completed (
99                 Evoral::Sequence<Evoral::MusicalTime>::StuckNoteOption stuck_option,
100                 Evoral::MusicalTime                                    when = 0);
101
102         virtual void session_saved();
103
104         std::string captured_for() const               { return _captured_for; }
105         void        set_captured_for (std::string str) { _captured_for = str; }
106
107         framepos_t last_write_end() const { return _last_write_end; }
108         void set_last_write_end (framepos_t pos) { _last_write_end = pos; }
109
110         static PBD::Signal1<void,MidiSource*> MidiSourceCreated;
111
112         XMLNode& get_state ();
113         int set_state (const XMLNode&, int version);
114
115         bool length_mutable() const { return true; }
116
117         void set_length_beats(double l) { _length_beats = l; }
118         double length_beats() const { return _length_beats; }
119
120         virtual void load_model(bool lock=true, bool force_reload=false) = 0;
121         virtual void destroy_model() = 0;
122
123         /** This must be called with the source lock held whenever the
124          *  source/model contents have been changed (reset iterators/cache/etc).
125          */
126         void invalidate();
127
128         void set_note_mode(NoteMode mode);
129
130         boost::shared_ptr<MidiModel> model() { return _model; }
131         void set_model (boost::shared_ptr<MidiModel>);
132         void drop_model();
133
134         Evoral::ControlList::InterpolationStyle interpolation_of (Evoral::Parameter) const;
135         void set_interpolation_of (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
136         void copy_interpolation_from (boost::shared_ptr<MidiSource>);
137         void copy_interpolation_from (MidiSource *);
138
139         AutoState automation_state_of (Evoral::Parameter) const;
140         void set_automation_state_of (Evoral::Parameter, AutoState);
141         void copy_automation_state_from (boost::shared_ptr<MidiSource>);
142         void copy_automation_state_from (MidiSource *);
143
144         /** Emitted when a different MidiModel is set */
145         PBD::Signal0<void> ModelChanged;
146         /** Emitted when a parameter's interpolation style is changed */
147         PBD::Signal2<void, Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationChanged;
148         /** Emitted when a parameter's automation state is changed */
149         PBD::Signal2<void, Evoral::Parameter, AutoState> AutomationStateChanged;
150
151   protected:
152         virtual void flush_midi() = 0;
153
154         virtual framecnt_t read_unlocked (Evoral::EventSink<framepos_t>& dst,
155                                           framepos_t                     position,
156                                           framepos_t                     start,
157                                           framecnt_t                     cnt,
158                                           MidiStateTracker*              tracker) const = 0;
159
160         virtual framecnt_t write_unlocked (MidiRingBuffer<framepos_t>& dst,
161                                            framepos_t                  position,
162                                            framecnt_t                  cnt) = 0;
163
164         std::string      _captured_for;
165
166         boost::shared_ptr<MidiModel> _model;
167         bool                         _writing;
168
169         mutable Evoral::Sequence<Evoral::MusicalTime>::const_iterator _model_iter;
170         mutable bool                                                  _model_iter_valid;
171
172         mutable double     _length_beats;
173         mutable framepos_t _last_read_end;
174         framepos_t         _last_write_end;
175
176         /** Map of interpolation styles to use for Parameters; if they are not in this map,
177          *  the correct interpolation style can be obtained from EventTypeMap::interpolation_of ()
178          */
179         typedef std::map<Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationStyleMap;
180         InterpolationStyleMap _interpolation_style;
181
182         /** Map of automation states to use for Parameters; if they are not in this map,
183          *  the correct automation state is Off.
184          */
185         typedef std::map<Evoral::Parameter, AutoState> AutomationStateMap;
186         AutomationStateMap  _automation_state;
187 };
188
189 }
190
191 #endif /* __ardour_midi_source_h__ */