Move EventRingBuffer to libardour.
[ardour.git] / libs / ardour / ardour / midi_diskstream.h
1 /*
2     Copyright (C) 2000 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: diskstream.h 579 2006-06-12 19:56:37Z essej $
19 */
20
21 #ifndef __ardour_midi_diskstream_h__
22 #define __ardour_midi_diskstream_h__
23
24
25 #include <cmath>
26 #include <cassert>
27 #include <string>
28 #include <queue>
29 #include <map>
30 #include <vector>
31
32 #include <time.h>
33
34 #include "pbd/fastlog.h"
35 #include "pbd/ringbufferNPT.h"
36
37 #include "ardour/ardour.h"
38 #include "ardour/diskstream.h"
39 #include "ardour/midi_buffer.h"
40 #include "ardour/midi_playlist.h"
41 #include "ardour/utils.h"
42
43 struct tm;
44
45 namespace ARDOUR {
46
47 class IO;
48 class MidiEngine;
49 class MidiPort;
50 class MidiRingbuffer;
51 class MidiSource;
52 class SMFSource;
53 class Send;
54 class Session;
55
56 class LIBARDOUR_API MidiDiskstream : public Diskstream
57 {
58   public:
59         MidiDiskstream (Session &, const std::string& name, Diskstream::Flag f = Recordable);
60         MidiDiskstream (Session &, const XMLNode&);
61         ~MidiDiskstream();
62
63         float playback_buffer_load() const;
64         float capture_buffer_load() const;
65
66         void get_playback (MidiBuffer& dst, framecnt_t);
67         void flush_playback (framepos_t, framepos_t);
68
69         void set_record_enabled (bool yn);
70         
71         void reset_tracker ();
72
73         boost::shared_ptr<MidiPlaylist> midi_playlist () { return boost::dynamic_pointer_cast<MidiPlaylist>(_playlist); }
74
75         int use_playlist (boost::shared_ptr<Playlist>);
76         int use_new_playlist ();
77         int use_copy_playlist ();
78
79         bool set_name (std::string const &);
80         bool set_write_source_name (const std::string& str);
81
82         /* stateful */
83         XMLNode& get_state(void);
84         int set_state(const XMLNode&, int version);
85
86         void ensure_input_monitoring (bool);
87
88         boost::shared_ptr<SMFSource> write_source ()    { return _write_source; }
89
90         int set_destructive (bool yn); // doom!
91
92         void set_note_mode (NoteMode m);
93
94         /** Emitted when some MIDI data has been received for recording.
95          *  Parameter is the source that it is destined for.
96          *  A caller can get a copy of the data with get_gui_feed_buffer ()
97          */
98         PBD::Signal1<void, boost::weak_ptr<MidiSource> > DataRecorded;
99
100         boost::shared_ptr<MidiBuffer> get_gui_feed_buffer () const;
101
102   protected:
103         friend class Session;
104         friend class Butler;
105
106         /* the Session is the only point of access for these
107            because they require that the Session is "inactive"
108            while they are called.
109         */
110
111         void set_pending_overwrite(bool);
112         int  overwrite_existing_buffers ();
113         void set_block_size (pframes_t);
114         int  internal_playback_seek (framecnt_t distance);
115         int  can_internal_playback_seek (framecnt_t distance);
116         std::string steal_write_source_name();
117         void reset_write_sources (bool, bool force = false);
118         void non_realtime_input_change ();
119         void non_realtime_locate (framepos_t location);
120
121         static void set_readahead_frames (framecnt_t frames_ahead) { midi_readahead = frames_ahead; }
122
123   protected:
124         friend class MidiTrack;
125         friend class Auditioner;
126         int seek (framepos_t which_sample, bool complete_refill = false);
127
128         int  process (BufferSet&, framepos_t transport_frame, pframes_t nframes, framecnt_t &, bool need_diskstream);
129         frameoffset_t calculate_playback_distance (pframes_t nframes);
130         bool commit  (framecnt_t nframes);
131         static framecnt_t midi_readahead;
132
133   private:
134
135         /* The two central butler operations */
136         int do_flush (RunContext context, bool force = false);
137         int do_refill ();
138
139         int do_refill_with_alloc();
140
141         int read (framepos_t& start, framecnt_t cnt, bool reversed);
142
143         void finish_capture ();
144         void transport_stopped_wallclock (struct tm&, time_t, bool abort);
145         void transport_looped (framepos_t transport_frame);
146
147         void init ();
148
149         int use_new_write_source (uint32_t n=0);
150
151         int find_and_use_playlist (const std::string&);
152
153         void allocate_temporary_buffers ();
154
155         int use_pending_capture_data (XMLNode& node);
156
157         void get_input_sources ();
158         void set_align_style_from_io();
159
160         /* fixed size buffers per instance of ardour for now (non-dynamic)
161          */
162
163         void adjust_playback_buffering () {}
164         void adjust_capture_buffering () {}
165
166         bool prep_record_enable ();
167         bool prep_record_disable ();
168     
169         MidiRingBuffer<framepos_t>*  _playback_buf;
170         MidiRingBuffer<framepos_t>*  _capture_buf;
171         boost::weak_ptr<MidiPort>    _source_port;
172         boost::shared_ptr<SMFSource> _write_source;
173         NoteMode                     _note_mode;
174         gint                         _frames_written_to_ringbuffer;
175         gint                         _frames_read_from_ringbuffer;
176         volatile gint                _frames_pending_write;
177         volatile gint                _num_captured_loops;
178         framepos_t                   _accumulated_capture_offset;
179
180         /** A buffer that we use to put newly-arrived MIDI data in for
181             the GUI to read (so that it can update itself).
182         */
183         MidiBuffer                   _gui_feed_buffer;
184         mutable Glib::Threads::Mutex _gui_feed_buffer_mutex;
185 };
186
187 }; /* namespace ARDOUR */
188
189 #endif /* __ardour_midi_diskstream_h__ */