merge MidiDiskstream into DiskReader (playback parts)
[ardour.git] / libs / ardour / ardour / disk_reader.h
1 /*
2     Copyright (C) 2009-2016 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 */
19
20 #ifndef __ardour_disk_reader_h__
21 #define __ardour_disk_reader_h__
22
23 #include "pbd/ringbufferNPT.h"
24 #include "pbd/rcu.h"
25
26 #include "ardour/disk_io.h"
27 #include "ardour/interpolation.h"
28 #include "ardour/midi_buffer.h"
29
30 namespace ARDOUR
31 {
32
33 class Playlist;
34 class AudioPlaylist;
35 class MidiPlaylist;
36 template<typename T> class MidiRingBuffer;
37
38 class LIBARDOUR_API DiskReader : public DiskIOProcessor
39 {
40   public:
41         DiskReader (Session&, std::string const & name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
42         ~DiskReader ();
43
44         bool set_name (std::string const & str);
45
46         static framecnt_t chunk_frames() { return _chunk_frames; }
47         static framecnt_t default_chunk_frames ();
48         static void set_chunk_frames (framecnt_t n) { _chunk_frames = n; }
49
50         void run (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
51         int set_block_size (pframes_t);
52         bool configure_io (ChanCount in, ChanCount out);
53         bool can_support_io_configuration (const ChanCount& in, ChanCount& out) = 0;
54         void realtime_handle_transport_stopped ();
55         void realtime_locate ();
56         void non_realtime_locate (framepos_t);
57         int overwrite_existing_buffers ();
58         void set_pending_overwrite (bool yn);
59
60         framecnt_t roll_delay() const { return _roll_delay; }
61         void set_roll_delay (framecnt_t);
62
63         virtual XMLNode& state (bool full);
64         int set_state (const XMLNode&, int version);
65
66         boost::shared_ptr<Playlist>      get_playlist (DataType dt) const { return _playlists[dt]; }
67         boost::shared_ptr<MidiPlaylist>  midi_playlist() const;
68         boost::shared_ptr<AudioPlaylist> audio_playlist() const;
69
70         virtual void playlist_modified ();
71         virtual int use_playlist (DataType, boost::shared_ptr<Playlist>);
72         virtual int use_new_playlist (DataType);
73         virtual int use_copy_playlist (DataType);
74
75         PBD::Signal1<void,DataType>   PlaylistChanged;
76         PBD::Signal0<void>            AlignmentStyleChanged;
77
78         int set_loop (Location *loc);
79
80         float buffer_load() const;
81
82         void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<framepos_t> > const &);
83
84         /* called by the Butler in a non-realtime context */
85
86         int do_refill () {
87                 return refill (_mixdown_buffer, _gain_buffer, 0);
88         }
89
90         /** For non-butler contexts (allocates temporary working buffers)
91          *
92          * This accessible method has a default argument; derived classes
93          * must inherit the virtual method that we call which does NOT
94          * have a default argument, to avoid complications with inheritance
95          */
96         int do_refill_with_alloc (bool partial_fill = true) {
97                 return _do_refill_with_alloc (partial_fill);
98         }
99
100         bool pending_overwrite () const { return _pending_overwrite; }
101
102         virtual int find_and_use_playlist (DataType, std::string const &);
103
104         // Working buffers for do_refill (butler thread)
105         static void allocate_working_buffers();
106         static void free_working_buffers();
107
108         void adjust_buffering ();
109
110         int can_internal_playback_seek (framecnt_t distance);
111         int seek (framepos_t frame, bool complete_refill = false);
112
113         int add_channel (uint32_t how_many);
114         int remove_channel (uint32_t how_many);
115
116         bool need_butler() const { return _need_butler; }
117
118         PBD::Signal0<void> Underrun;
119
120   protected:
121         boost::shared_ptr<Playlist> _playlists[DataType::num_types];
122
123         virtual void playlist_changed (const PBD::PropertyChange&);
124         virtual void playlist_deleted (boost::weak_ptr<Playlist>);
125         virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<framepos_t> > const &, bool);
126
127         void reset_tracker ();
128         void resolve_tracker (Evoral::EventSink<framepos_t>& buffer, framepos_t time);
129         boost::shared_ptr<MidiBuffer> get_gui_feed_buffer () const;
130
131   private:
132         /** The number of frames by which this diskstream's output should be delayed
133             with respect to the transport frame.  This is used for latency compensation.
134         */
135         framecnt_t   _roll_delay;
136         Location*     loop_location;
137         framepos_t    overwrite_frame;
138         off_t         overwrite_offset;
139         bool          _pending_overwrite;
140         bool          overwrite_queued;
141         IOChange      input_change_pending;
142         framecnt_t    wrap_buffer_size;
143         framecnt_t    speed_buffer_size;
144         framepos_t     file_frame;
145         framepos_t     playback_sample;
146         MonitorChoice   _monitoring_choice;
147         bool            _need_butler;
148
149         PBD::ScopedConnectionList playlist_connections;
150
151         int _do_refill_with_alloc (bool partial_fill);
152
153         static framecnt_t _chunk_frames;
154         static framecnt_t midi_readahead;
155
156         /* The MIDI stuff */
157
158         MidiRingBuffer<framepos_t>*  _midi_buf;
159
160         /** A buffer that we use to put newly-arrived MIDI data in for
161             the GUI to read (so that it can update itself).
162         */
163         MidiBuffer                   _gui_feed_buffer;
164         mutable Glib::Threads::Mutex _gui_feed_buffer_mutex;
165         CubicMidiInterpolation midi_interpolation;
166         gint                         _frames_written_to_ringbuffer;
167         gint                         _frames_read_from_ringbuffer;
168
169         /** Information about one of our channels */
170         struct ChannelInfo : public boost::noncopyable {
171
172                 ChannelInfo (framecnt_t buffer_size,
173                              framecnt_t speed_buffer_size,
174                              framecnt_t wrap_buffer_size);
175                 ~ChannelInfo ();
176
177                 Sample     *wrap_buffer;
178                 Sample     *speed_buffer;
179                 Sample     *current_buffer;
180
181                 /** A ringbuffer for data to be played back, written to in the
182                     butler thread, read from in the process thread.
183                 */
184                 PBD::RingBufferNPT<Sample>* buf;
185
186                 Sample* scrub_buffer;
187                 Sample* scrub_forward_buffer;
188                 Sample* scrub_reverse_buffer;
189
190                 PBD::RingBufferNPT<Sample>::rw_vector read_vector;
191
192                 void resize (framecnt_t);
193         };
194
195         typedef std::vector<ChannelInfo*> ChannelList;
196         SerializedRCUManager<ChannelList> channels;
197
198         CubicInterpolation interpolation;
199
200         int audio_read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
201                         framepos_t& start, framecnt_t cnt,
202                         int channel, bool reversed);
203         int midi_read (framepos_t& start, framecnt_t cnt, bool reversed);
204
205         static Sample* _mixdown_buffer;
206         static gain_t* _gain_buffer;
207
208         int refill (Sample* mixdown_buffer, float* gain_buffer, framecnt_t fill_level);
209         int refill_audio (Sample *mixdown_buffer, float *gain_buffer, framecnt_t fill_level);
210         int refill_midi ();
211
212         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
213         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
214
215         int internal_playback_seek (framecnt_t distance);
216         frameoffset_t calculate_playback_distance (pframes_t);
217
218         void allocate_temporary_buffers();
219         void get_playback (MidiBuffer& dst, framecnt_t nframes);
220         void flush_playback (framepos_t start, framepos_t end);
221 };
222
223 } // namespace
224
225 #endif /* __ardour_disk_reader_h__ */