Introduce new time for session-relative frame time, and make source interface capable...
[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 <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>
34
35 namespace ARDOUR {
36
37 class MidiModel;
38 template<typename T> class MidiRingBuffer;
39
40 /** Source for MIDI data */
41 class MidiSource : virtual public Source
42 {
43   public:
44         typedef double TimeType;
45
46         MidiSource (Session& session, std::string name, Source::Flag flags = Source::Flag(0));
47         MidiSource (Session& session, const XMLNode&);
48         virtual ~MidiSource ();
49         
50         /** Read the data in a given time range from the MIDI source.
51          * All time stamps in parameters are in audio frames (even if the source has tempo time).
52          * \param dst Ring buffer where read events are written
53          * \param position Start position of the SOURCE in this read context
54          * \param start Start of range to be read
55          * \param cnt Length of range to be read (in audio frames)
56          * \param stamp_offset Offset to add to event times written to dst
57          * \param negative_stamp_offset Offset to subtract from event times written to dst
58          */
59         virtual nframes_t midi_read (MidiRingBuffer<nframes_t>& dst,
60                         sframes_t position,
61                         sframes_t start, nframes_t cnt,
62                         sframes_t stamp_offset, sframes_t negative_stamp_offset) const;
63
64         virtual nframes_t midi_write (MidiRingBuffer<nframes_t>& src,
65                         sframes_t position,
66                         nframes_t cnt);
67
68         virtual void append_event_unlocked_beats(const Evoral::Event<double>& ev) = 0;
69
70         virtual void append_event_unlocked_frames(const Evoral::Event<nframes_t>& ev,
71                         sframes_t position) = 0;
72
73         virtual void mark_streaming_midi_write_started (NoteMode mode, sframes_t start_time);
74         virtual void mark_streaming_write_started ();
75         virtual void mark_streaming_write_completed ();
76         
77         virtual void session_saved();
78
79         std::string captured_for() const               { return _captured_for; }
80         void        set_captured_for (std::string str) { _captured_for = str; }
81
82         uint32_t read_data_count()  const { return _read_data_count; }
83         uint32_t write_data_count() const { return _write_data_count; }
84
85         static sigc::signal<void,MidiSource*> MidiSourceCreated;
86                
87         // Signal a range of recorded data is available for reading from model()
88         mutable sigc::signal<void,sframes_t,nframes_t> ViewDataRangeReady;
89         
90         XMLNode& get_state ();
91         int set_state (const XMLNode&);
92         
93         bool length_mutable() const { return true; }
94
95         virtual void load_model(bool lock=true, bool force_reload=false) = 0;
96         virtual void destroy_model() = 0;
97
98         /** This must be called with the source lock held whenever the
99          *  source/model contents have been changed (reset iterators/cache/etc).
100          */
101         void invalidate();
102
103         void set_note_mode(NoteMode mode);
104         
105         boost::shared_ptr<MidiModel> model() { return _model; }
106         void set_model(boost::shared_ptr<MidiModel> m) { _model = m; }
107         void drop_model() { _model.reset(); }
108
109   protected:
110         virtual void flush_midi() = 0;
111         
112         virtual nframes_t read_unlocked (MidiRingBuffer<nframes_t>& dst,
113                         sframes_t position,
114                         sframes_t start, nframes_t cnt,
115                         sframes_t stamp_offset, sframes_t negative_stamp_offset) const = 0;
116
117         virtual nframes_t write_unlocked (MidiRingBuffer<nframes_t>& dst,
118                         sframes_t position,
119                         nframes_t cnt) = 0;
120         
121         std::string         _captured_for;
122         mutable uint32_t    _read_data_count;  ///< modified in read()
123         mutable uint32_t    _write_data_count; ///< modified in write()
124         
125         boost::shared_ptr<MidiModel> _model;
126         bool                         _writing;
127         
128         mutable Evoral::Sequence<double>::const_iterator _model_iter;
129         mutable sframes_t                                _last_read_end;
130
131   private:
132         bool file_changed (std::string path);
133 };
134
135 }
136
137 #endif /* __ardour_midi_source_h__ */