2 Copyright (C) 2006 Paul Davis
3 Written by Dave Robillard, 2006
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.
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.
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.
20 #ifndef __ardour_midi_source_h__
21 #define __ardour_midi_source_h__
25 #include <glibmm/thread.h>
26 #include "pbd/stateful.h"
27 #include "pbd/xml++.h"
28 #include "evoral/Sequence.hpp"
29 #include "ardour/ardour.h"
30 #include "ardour/buffer.h"
31 #include "ardour/source.h"
32 #include "ardour/beats_frames_converter.h"
36 class MidiStateTracker;
38 template<typename T> class MidiRingBuffer;
40 /** Source for MIDI data */
41 class MidiSource : virtual public Source
44 typedef double TimeType;
46 MidiSource (Session& session, std::string name, Source::Flag flags = Source::Flag(0));
47 MidiSource (Session& session, const XMLNode&);
48 virtual ~MidiSource ();
50 boost::shared_ptr<MidiSource> clone (Evoral::MusicalTime begin = Evoral::MinMusicalTime,
51 Evoral::MusicalTime end = Evoral::MaxMusicalTime);
53 /** Read the data in a given time range from the MIDI source.
54 * All time stamps in parameters are in audio frames (even if the source has tempo time).
55 * \param dst Ring buffer where read events are written
56 * \param source_start Start position of the SOURCE in this read context
57 * \param start Start of range to be read
58 * \param cnt Length of range to be read (in audio frames)
59 * \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking
61 virtual nframes_t midi_read (Evoral::EventSink<nframes_t>& dst,
62 framepos_t source_start,
63 framepos_t start, nframes_t cnt,
65 std::set<Evoral::Parameter> const &) const;
67 virtual nframes_t midi_write (MidiRingBuffer<nframes_t>& src,
68 framepos_t source_start,
71 virtual void append_event_unlocked_beats(const Evoral::Event<Evoral::MusicalTime>& ev) = 0;
73 virtual void append_event_unlocked_frames(const Evoral::Event<nframes_t>& ev,
74 framepos_t source_start) = 0;
76 virtual bool empty () const;
77 virtual framecnt_t length (framepos_t pos) const;
78 virtual void update_length (framepos_t pos, framecnt_t cnt);
80 virtual void mark_streaming_midi_write_started (NoteMode mode, framepos_t start_time);
81 virtual void mark_streaming_write_started ();
82 virtual void mark_streaming_write_completed ();
84 virtual void session_saved();
86 std::string captured_for() const { return _captured_for; }
87 void set_captured_for (std::string str) { _captured_for = str; }
89 uint32_t read_data_count() const { return _read_data_count; }
90 uint32_t write_data_count() const { return _write_data_count; }
92 static PBD::Signal1<void,MidiSource*> MidiSourceCreated;
94 // Signal a range of recorded data is available for reading from model()
95 mutable PBD::Signal2<void,framepos_t,nframes_t> ViewDataRangeReady;
97 XMLNode& get_state ();
98 int set_state (const XMLNode&, int version);
100 bool length_mutable() const { return true; }
102 virtual void load_model(bool lock=true, bool force_reload=false) = 0;
103 virtual void destroy_model() = 0;
105 /** This must be called with the source lock held whenever the
106 * source/model contents have been changed (reset iterators/cache/etc).
110 void set_note_mode(NoteMode mode);
112 boost::shared_ptr<MidiModel> model() { return _model; }
113 void set_model (boost::shared_ptr<MidiModel>);
116 Evoral::ControlList::InterpolationStyle interpolation_of (Evoral::Parameter) const;
117 void set_interpolation_of (Evoral::Parameter, Evoral::ControlList::InterpolationStyle);
118 void copy_interpolation_from (boost::shared_ptr<MidiSource>);
119 void copy_interpolation_from (MidiSource *);
121 AutoState automation_state_of (Evoral::Parameter) const;
122 void set_automation_state_of (Evoral::Parameter, AutoState);
123 void copy_automation_state_from (boost::shared_ptr<MidiSource>);
124 void copy_automation_state_from (MidiSource *);
126 /** Emitted when a different MidiModel is set */
127 PBD::Signal0<void> ModelChanged;
128 /** Emitted when a parameter's interpolation style is changed */
129 PBD::Signal2<void, Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationChanged;
130 /** Emitted when a parameter's automation state is changed */
131 PBD::Signal2<void, Evoral::Parameter, AutoState> AutomationStateChanged;
134 virtual void flush_midi() = 0;
136 virtual nframes_t read_unlocked (Evoral::EventSink<nframes_t>& dst,
138 framepos_t start, nframes_t cnt,
139 MidiStateTracker* tracker) const = 0;
141 virtual nframes_t write_unlocked (MidiRingBuffer<nframes_t>& dst,
145 std::string _captured_for;
146 mutable uint32_t _read_data_count; ///< modified in read()
147 mutable uint32_t _write_data_count; ///< modified in write()
149 boost::shared_ptr<MidiModel> _model;
152 mutable Evoral::Sequence<Evoral::MusicalTime>::const_iterator _model_iter;
153 mutable bool _model_iter_valid;
155 mutable double _length_beats;
156 mutable framepos_t _last_read_end;
157 framepos_t _last_write_end;
159 /** Map of interpolation styles to use for Parameters; if they are not in this map,
160 * the correct interpolation style can be obtained from EventTypeMap::interpolation_of ()
162 typedef std::map<Evoral::Parameter, Evoral::ControlList::InterpolationStyle> InterpolationStyleMap;
163 InterpolationStyleMap _interpolation_style;
165 /** Map of automation states to use for Parameters; if they are not in this map,
166 * the correct automation state is Off.
168 typedef std::map<Evoral::Parameter, AutoState> AutomationStateMap;
169 AutomationStateMap _automation_state;
174 #endif /* __ardour_midi_source_h__ */