Time unit translation for AutomationLine (correctly display MIDI controller data).
[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 : public Source
42 {
43   public:
44         typedef double TimeType;
45
46         MidiSource (Session& session, std::string name);
47         MidiSource (Session& session, const XMLNode&);
48         virtual ~MidiSource ();
49         
50         /* Stub Readable interface */
51         virtual nframes64_t read (Sample*, nframes64_t pos, nframes64_t cnt, int channel) const { return 0; }
52         virtual nframes64_t readable_length() const { return length(); }
53         virtual uint32_t    n_channels () const { return 1; }
54         
55         // FIXME: integrate this with the Readable::read interface somehow
56         virtual nframes_t midi_read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t cnt,
57                         nframes_t stamp_offset, nframes_t negative_stamp_offset) const;
58         virtual nframes_t midi_write (MidiRingBuffer<nframes_t>& src, nframes_t cnt);
59
60         virtual void append_event_unlocked_beats(const Evoral::Event<double>& ev) = 0;
61         virtual void append_event_unlocked_frames(const Evoral::Event<nframes_t>& ev) = 0;
62
63         virtual void mark_for_remove() = 0;
64         virtual void mark_streaming_midi_write_started (NoteMode mode, nframes_t start_time);
65         virtual void mark_streaming_write_started ();
66         virtual void mark_streaming_write_completed ();
67         
68         uint64_t timeline_position () { return _timeline_position; }
69         void     set_timeline_position (nframes_t when);
70         
71         virtual void session_saved();
72
73         std::string captured_for() const               { return _captured_for; }
74         void        set_captured_for (std::string str) { _captured_for = str; }
75
76         uint32_t read_data_count()  const { return _read_data_count; }
77         uint32_t write_data_count() const { return _write_data_count; }
78
79         static sigc::signal<void,MidiSource*> MidiSourceCreated;
80                
81         // Signal a range of recorded data is available for reading from model()
82         mutable sigc::signal<void,nframes_t,nframes_t> ViewDataRangeReady;
83         
84         XMLNode& get_state ();
85         int set_state (const XMLNode&);
86         
87         bool length_mutable() const { return true; }
88
89         virtual void load_model(bool lock=true, bool force_reload=false) = 0;
90         virtual void destroy_model() = 0;
91
92         void set_note_mode(NoteMode mode);
93
94         boost::shared_ptr<MidiModel> model() { return _model; }
95         void set_model(boost::shared_ptr<MidiModel> m) { _model = m; }
96         void drop_model() { _model.reset(); }
97
98         const Evoral::TimeConverter<double, nframes_t>& time_converter() const {
99                 return _converter;
100         }
101
102   protected:
103         virtual void flush_midi() = 0;
104         
105         virtual nframes_t read_unlocked (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t cnt,
106                         nframes_t stamp_offset, nframes_t negative_stamp_offset) const = 0;
107         virtual nframes_t write_unlocked (MidiRingBuffer<nframes_t>& dst, nframes_t cnt) = 0;
108         
109         mutable Glib::Mutex _lock;
110         std::string         _captured_for;
111         uint64_t            _timeline_position;
112         mutable uint32_t    _read_data_count;  ///< modified in read()
113         mutable uint32_t    _write_data_count; ///< modified in write()
114         
115         BeatsFramesConverter _converter;
116
117         boost::shared_ptr<MidiModel> _model;
118         bool                         _writing;
119         
120         mutable Evoral::Sequence<double>::const_iterator _model_iter;
121         mutable nframes_t                                _last_read_end;
122
123   private:
124         bool file_changed (std::string path);
125 };
126
127 }
128
129 #endif /* __ardour_midi_source_h__ */