Rewrote MidiRingBuffer to more efficiently pack data (flat pack stamps, sizes, and...
[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 <pbd/stateful.h>
35 #include <pbd/xml++.h>
36
37 using std::string;
38
39 namespace ARDOUR {
40
41 class MidiRingBuffer;
42
43 /** Source for MIDI data */
44 class MidiSource : public Source
45 {
46   public:
47         MidiSource (Session& session, string name);
48         MidiSource (Session& session, const XMLNode&);
49         virtual ~MidiSource ();
50         
51         virtual nframes_t read (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const;
52         virtual nframes_t write (MidiRingBuffer& src, nframes_t cnt);
53
54         virtual void mark_for_remove() = 0;
55         virtual void mark_streaming_write_completed () {}
56
57         string captured_for() const { return _captured_for; }
58         void   set_captured_for (string str) { _captured_for = str; }
59
60         uint32_t read_data_count()  const { return _read_data_count; }
61         uint32_t write_data_count() const { return _write_data_count; }
62
63         static sigc::signal<void,MidiSource*> MidiSourceCreated;
64                
65         // The MIDI equivalent to "peaks"
66         mutable sigc::signal<void,boost::shared_ptr<MidiBuffer>,nframes_t,nframes_t> ViewDataRangeReady;
67         
68         XMLNode& get_state ();
69         int set_state (const XMLNode&);
70
71   protected:
72         virtual nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset) const = 0;
73         virtual nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt) = 0;
74         
75         mutable Glib::Mutex _lock;
76         string              _captured_for;
77         mutable uint32_t    _read_data_count;  ///< modified in read()
78         mutable uint32_t    _write_data_count; ///< modified in write()
79
80   private:
81         bool file_changed (string path);
82 };
83
84 }
85
86 #endif /* __ardour_midi_source_h__ */