Make SMFSource suck significantly less.
[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
25 #include <time.h>
26
27 #include <glibmm/thread.h>
28
29 #include <sigc++/signal.h>
30
31 #include <ardour/source.h>
32 #include <ardour/ardour.h>
33 #include <ardour/buffer.h>
34 #include <ardour/midi_model.h>
35 #include <pbd/stateful.h>
36 #include <pbd/xml++.h>
37
38 using std::string;
39
40 namespace ARDOUR {
41
42 class MidiRingBuffer;
43
44 /** Source for MIDI data */
45 class MidiSource : public Source
46 {
47   public:
48         MidiSource (Session& session, string name);
49         MidiSource (Session& session, const XMLNode&);
50         virtual ~MidiSource ();
51         
52         virtual nframes_t read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const;
53         virtual nframes_t write (MidiRingBuffer& src, nframes_t cnt);
54         virtual void append_event_unlocked(const MidiEvent& ev) = 0;
55
56         virtual void mark_for_remove() = 0;
57         virtual void mark_streaming_midi_write_started (NoteMode mode);
58         virtual void mark_streaming_write_started ();
59         virtual void mark_streaming_write_completed ();
60         
61         void set_timeline_position (nframes_t when) { _timeline_position = when; }
62         
63         virtual void session_saved();
64
65         string captured_for() const { return _captured_for; }
66         void   set_captured_for (string str) { _captured_for = str; }
67
68         uint32_t read_data_count()  const { return _read_data_count; }
69         uint32_t write_data_count() const { return _write_data_count; }
70
71         static sigc::signal<void,MidiSource*> MidiSourceCreated;
72                
73         // Signal a range of recorded data is available for reading from model()
74         mutable sigc::signal<void,nframes_t,nframes_t> ViewDataRangeReady;
75         
76         XMLNode& get_state ();
77         int set_state (const XMLNode&);
78
79         virtual void load_model(bool lock=true, bool force_reload=false) = 0;
80         virtual void destroy_model() = 0;
81
82         void set_note_mode(NoteMode mode) { if (_model) _model->set_note_mode(mode); }
83
84         boost::shared_ptr<MidiModel> model() { return _model; }
85         void set_model(boost::shared_ptr<MidiModel> m) { _model = m; }
86
87   protected:
88         virtual int flush_header() = 0;
89         virtual int flush_footer() = 0;
90         
91         virtual nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const = 0;
92         virtual nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt) = 0;
93         
94         mutable Glib::Mutex _lock;
95         string              _captured_for;
96         uint64_t            _timeline_position;
97         mutable uint32_t    _read_data_count;  ///< modified in read()
98         mutable uint32_t    _write_data_count; ///< modified in write()
99
100         boost::shared_ptr<MidiModel> _model;
101         bool                         _writing;
102
103   private:
104         bool file_changed (string path);
105 };
106
107 }
108
109 #endif /* __ardour_midi_source_h__ */