f13b71e0f18b7548130601b11eb9830e7e184a8c
[ardour.git] / libs / ardour / ardour / midi_source.h
1 /*
2     Copyright (C) 2006 Paul Davis
3         Written by Dave Robillard, 2006
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/thread.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 (Evoral::MusicalTime begin = Evoral::MinMusicalTime, 
52                                              Evoral::MusicalTime end = Evoral::MaxMusicalTime);
53
54         /** Read the data in a given time range from the MIDI source.
55          * All time stamps in parameters are in audio frames (even if the source has tempo time).
56          * \param dst Ring buffer where read events are written
57          * \param source_start Start position of the SOURCE in this read context
58          * \param start Start of range to be read
59          * \param cnt Length of range to be read (in audio frames)
60          * \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking
61          */
62         virtual framecnt_t midi_read (Evoral::EventSink<framepos_t>& dst,
63                                       framepos_t source_start,
64                                       framepos_t start, framecnt_t cnt,
65                                       MidiStateTracker*,
66                                       std::set<Evoral::Parameter> const &) const;
67
68         virtual framecnt_t midi_write (MidiRingBuffer<framepos_t>& src,
69                                        framepos_t source_start,
70                                        framecnt_t cnt);
71
72         virtual void append_event_unlocked_beats(const Evoral::Event<Evoral::MusicalTime>& ev) = 0;
73
74         virtual void append_event_unlocked_frames(const Evoral::Event<framepos_t>& ev,
75                         framepos_t source_start) = 0;
76
77         virtual bool       empty () const;
78         virtual framecnt_t length (framepos_t pos) const;
79         virtual void       update_length (framepos_t pos, framecnt_t cnt);
80
81         virtual void mark_streaming_midi_write_started (NoteMode mode, framepos_t start_time);
82         virtual void mark_streaming_write_started ();
83         virtual void mark_streaming_write_completed ();
84
85         virtual void session_saved();
86
87         std::string captured_for() const               { return _captured_for; }
88         void        set_captured_for (std::string str) { _captured_for = str; }
89
90         uint32_t read_data_count()  const { return _read_data_count; }
91         uint32_t write_data_count() const { return _write_data_count; }
92
93         static PBD::Signal1<void,MidiSource*> MidiSourceCreated;
94
95         XMLNode& get_state ();
96         int set_state (const XMLNode&, int version);
97
98         bool length_mutable() const { return true; }
99         double length_beats() const { return _length_beats; }
100
101         virtual void load_model(bool lock=true, bool force_reload=false) = 0;
102         virtual void destroy_model() = 0;
103
104         /** This must be called with the source lock held whenever the
105          *  source/model contents have been changed (reset iterators/cache/etc).
106          */
107         void invalidate();
108
109         void set_note_mode(NoteMode mode);
110
111         boost::shared_ptr<MidiModel> model() { return _model; }
112         void set_model (boost::shared_ptr<MidiModel>);
113         void drop_model();
114
115         Evoral::ControlList::InterpolationStyle interpolation_of (Evoral::Parameter) const;
116         void set_interpolation_of (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
117         void copy_interpolation_from (boost::shared_ptr<MidiSource>);
118         void copy_interpolation_from (MidiSource *);
119
120         AutoState automation_state_of (Evoral::Parameter) const;
121         void set_automation_state_of (Evoral::Parameter, AutoState);
122         void copy_automation_state_from (boost::shared_ptr<MidiSource>);
123         void copy_automation_state_from (MidiSource *);
124
125         /** Emitted when a different MidiModel is set */
126         PBD::Signal0<void> ModelChanged;
127         /** Emitted when a parameter's interpolation style is changed */
128         PBD::Signal2<void, Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationChanged;
129         /** Emitted when a parameter's automation state is changed */
130         PBD::Signal2<void, Evoral::Parameter, AutoState> AutomationStateChanged;
131
132   protected:
133         virtual void flush_midi() = 0;
134
135         virtual framepos_t read_unlocked (Evoral::EventSink<framepos_t>& dst,
136                                           framepos_t position,
137                                           framepos_t start, framecnt_t cnt,
138                                           MidiStateTracker* tracker) const = 0;
139
140         virtual framepos_t write_unlocked (MidiRingBuffer<framepos_t>& dst,
141                                            framepos_t position,
142                                            framecnt_t cnt) = 0;
143
144         std::string      _captured_for;
145         mutable uint32_t _read_data_count;  ///< modified in read()
146         mutable uint32_t _write_data_count; ///< modified in write()
147
148         boost::shared_ptr<MidiModel> _model;
149         bool                         _writing;
150
151         mutable Evoral::Sequence<Evoral::MusicalTime>::const_iterator _model_iter;
152         mutable bool                                                  _model_iter_valid;
153
154         mutable double     _length_beats;
155         mutable framepos_t _last_read_end;
156         framepos_t         _last_write_end;
157
158         /** Map of interpolation styles to use for Parameters; if they are not in this map,
159          *  the correct interpolation style can be obtained from EventTypeMap::interpolation_of ()
160          */
161         typedef std::map<Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationStyleMap;
162         InterpolationStyleMap _interpolation_style;
163
164         /** Map of automation states to use for Parameters; if they are not in this map,
165          *  the correct automation state is Off.
166          */
167         typedef std::map<Evoral::Parameter, AutoState> AutomationStateMap;
168         AutomationStateMap  _automation_state;
169 };
170
171 }
172
173 #endif /* __ardour_midi_source_h__ */