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 <sigc++/signal.h>
26 #include <glibmm/thread.h>
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"
37 class MidiStateTracker;
39 template<typename T> class MidiRingBuffer;
41 /** Source for MIDI data */
42 class MidiSource : virtual public Source
45 typedef double TimeType;
47 MidiSource (Session& session, std::string name, Source::Flag flags = Source::Flag(0));
48 MidiSource (Session& session, const XMLNode&);
49 virtual ~MidiSource ();
51 /** Read the data in a given time range from the MIDI source.
52 * All time stamps in parameters are in audio frames (even if the source has tempo time).
53 * \param dst Ring buffer where read events are written
54 * \param source_start Start position of the SOURCE in this read context
55 * \param start Start of range to be read
56 * \param cnt Length of range to be read (in audio frames)
57 * \param stamp_offset Offset to add to event times written to dst
58 * \param negative_stamp_offset Offset to subtract from event times written to dst
59 * \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking
61 virtual nframes_t midi_read (MidiRingBuffer<nframes_t>& dst,
62 sframes_t source_start,
63 sframes_t start, nframes_t cnt,
64 sframes_t stamp_offset, sframes_t negative_stamp_offset, MidiStateTracker*) const;
66 virtual nframes_t midi_write (MidiRingBuffer<nframes_t>& src,
67 sframes_t source_start,
70 virtual void append_event_unlocked_beats(const Evoral::Event<Evoral::MusicalTime>& ev) = 0;
72 virtual void append_event_unlocked_frames(const Evoral::Event<nframes_t>& ev,
73 sframes_t source_start) = 0;
75 virtual sframes_t length (sframes_t pos) const;
76 virtual void update_length (sframes_t pos, sframes_t cnt);
78 virtual void mark_streaming_midi_write_started (NoteMode mode, sframes_t start_time);
79 virtual void mark_streaming_write_started ();
80 virtual void mark_streaming_write_completed ();
82 virtual void session_saved();
84 std::string captured_for() const { return _captured_for; }
85 void set_captured_for (std::string str) { _captured_for = str; }
87 uint32_t read_data_count() const { return _read_data_count; }
88 uint32_t write_data_count() const { return _write_data_count; }
90 static sigc::signal<void,MidiSource*> MidiSourceCreated;
92 // Signal a range of recorded data is available for reading from model()
93 mutable sigc::signal<void,sframes_t,nframes_t> ViewDataRangeReady;
95 XMLNode& get_state ();
96 int set_state (const XMLNode&, int version);
98 bool length_mutable() const { return true; }
100 virtual void load_model(bool lock=true, bool force_reload=false) = 0;
101 virtual void destroy_model() = 0;
103 /** This must be called with the source lock held whenever the
104 * source/model contents have been changed (reset iterators/cache/etc).
108 void set_note_mode(NoteMode mode);
110 boost::shared_ptr<MidiModel> model() { return _model; }
111 void set_model(boost::shared_ptr<MidiModel> m) { _model = m; }
112 void drop_model() { _model.reset(); }
115 virtual void flush_midi() = 0;
117 virtual nframes_t read_unlocked (MidiRingBuffer<nframes_t>& dst,
119 sframes_t start, nframes_t cnt,
120 sframes_t stamp_offset, sframes_t negative_stamp_offset,
121 MidiStateTracker* tracker) const = 0;
123 virtual nframes_t write_unlocked (MidiRingBuffer<nframes_t>& dst,
127 std::string _captured_for;
128 mutable uint32_t _read_data_count; ///< modified in read()
129 mutable uint32_t _write_data_count; ///< modified in write()
131 boost::shared_ptr<MidiModel> _model;
134 mutable Evoral::Sequence<Evoral::MusicalTime>::const_iterator _model_iter;
136 mutable double _length_beats;
137 mutable sframes_t _last_read_end;
138 sframes_t _last_write_end;
141 bool file_changed (std::string path);
146 #endif /* __ardour_midi_source_h__ */