add and use Source::empty() since it can be done more efficiently than length(pos...
[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 "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"
33
34 namespace ARDOUR {
35
36 class MidiStateTracker;
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         boost::shared_ptr<MidiSource> clone (Evoral::MusicalTime begin = Evoral::MinMusicalTime, 
51                                              Evoral::MusicalTime end = Evoral::MaxMusicalTime);
52
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 stamp_offset Offset to add to event times written to dst
60          * \param negative_stamp_offset Offset to subtract from event times written to dst
61          * \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking
62          */
63         virtual nframes_t midi_read (Evoral::EventSink<nframes_t>& dst,
64                                      sframes_t source_start,
65                                      sframes_t start, nframes_t cnt,
66                                      sframes_t stamp_offset, sframes_t negative_stamp_offset, MidiStateTracker*) const;
67
68         virtual nframes_t midi_write (MidiRingBuffer<nframes_t>& src,
69                                       sframes_t source_start,
70                                       nframes_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<nframes_t>& ev,
75                         sframes_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, sframes_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         // Signal a range of recorded data is available for reading from model()
96         mutable PBD::Signal2<void,sframes_t,nframes_t> ViewDataRangeReady;
97
98         XMLNode& get_state ();
99         int set_state (const XMLNode&, int version);
100
101         bool length_mutable() const { return true; }
102
103         virtual void load_model(bool lock=true, bool force_reload=false) = 0;
104         virtual void destroy_model() = 0;
105
106         /** This must be called with the source lock held whenever the
107          *  source/model contents have been changed (reset iterators/cache/etc).
108          */
109         void invalidate();
110
111         void set_note_mode(NoteMode mode);
112
113         boost::shared_ptr<MidiModel> model() { return _model; }
114         void set_model(boost::shared_ptr<MidiModel> m) { _model = m; }
115         void drop_model();
116
117   protected:
118         virtual void flush_midi() = 0;
119
120         virtual nframes_t read_unlocked (Evoral::EventSink<nframes_t>& dst,
121                                          sframes_t position,
122                                          sframes_t start, nframes_t cnt,
123                                          sframes_t stamp_offset, sframes_t negative_stamp_offset,
124                                          MidiStateTracker* tracker) const = 0;
125
126         virtual nframes_t write_unlocked (MidiRingBuffer<nframes_t>& dst,
127                         sframes_t position,
128                         nframes_t cnt) = 0;
129
130         std::string      _captured_for;
131         mutable uint32_t _read_data_count;  ///< modified in read()
132         mutable uint32_t _write_data_count; ///< modified in write()
133
134         boost::shared_ptr<MidiModel> _model;
135         bool                         _writing;
136
137         mutable Evoral::Sequence<Evoral::MusicalTime>::const_iterator _model_iter;
138         mutable bool                                                  _model_iter_valid;
139
140         mutable double    _length_beats;
141         mutable sframes_t _last_read_end;
142         sframes_t         _last_write_end;
143 };
144
145 }
146
147 #endif /* __ardour_midi_source_h__ */