move _file_frame from DiskIOProcessor into DiskReader (only place where it is needed...
[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/i18n.h"
24
25 #include "ardour/disk_io.h"
26 #include "ardour/midi_buffer.h"
27
28 namespace ARDOUR
29 {
30
31 class Playlist;
32 class AudioPlaylist;
33 class MidiPlaylist;
34 template<typename T> class MidiRingBuffer;
35
36 class LIBARDOUR_API DiskReader : public DiskIOProcessor
37 {
38   public:
39         DiskReader (Session&, std::string const & name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
40         ~DiskReader ();
41
42         bool set_name (std::string const & str);
43         std::string display_name() const { return std::string (_("reader")); }
44
45         static framecnt_t chunk_frames() { return _chunk_frames; }
46         static framecnt_t default_chunk_frames ();
47         static void set_chunk_frames (framecnt_t n) { _chunk_frames = n; }
48
49         void run (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
50         void realtime_handle_transport_stopped ();
51         void realtime_locate ();
52         int overwrite_existing_buffers ();
53         void set_pending_overwrite (bool yn);
54
55         framecnt_t roll_delay() const { return _roll_delay; }
56         void set_roll_delay (framecnt_t);
57
58         virtual XMLNode& state (bool full);
59         int set_state (const XMLNode&, int version);
60
61         PBD::Signal0<void>            AlignmentStyleChanged;
62
63         float buffer_load() const;
64
65         void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<framepos_t> > const &);
66
67         /* called by the Butler in a non-realtime context */
68
69         int do_refill () {
70                 return refill (_mixdown_buffer, _gain_buffer, 0);
71         }
72
73         /** For non-butler contexts (allocates temporary working buffers)
74          *
75          * This accessible method has a default argument; derived classes
76          * must inherit the virtual method that we call which does NOT
77          * have a default argument, to avoid complications with inheritance
78          */
79         int do_refill_with_alloc (bool partial_fill = true) {
80                 return _do_refill_with_alloc (partial_fill);
81         }
82
83         bool pending_overwrite () const { return _pending_overwrite; }
84
85         // Working buffers for do_refill (butler thread)
86         static void allocate_working_buffers();
87         static void free_working_buffers();
88
89         void adjust_buffering ();
90
91         int can_internal_playback_seek (framecnt_t distance);
92         int internal_playback_seek (framecnt_t distance);
93         int seek (framepos_t frame, bool complete_refill = false);
94
95         static PBD::Signal0<void> Underrun;
96
97         void playlist_modified ();
98         void reset_tracker ();
99
100         static void set_midi_readahead_frames (framecnt_t frames_ahead) { midi_readahead = frames_ahead; }
101
102         static void set_no_disk_output (bool yn);
103
104   protected:
105         friend class Track;
106         friend class MidiTrack;
107
108         void resolve_tracker (Evoral::EventSink<framepos_t>& buffer, framepos_t time);
109         boost::shared_ptr<MidiBuffer> get_gui_feed_buffer () const;
110
111         void playlist_changed (const PBD::PropertyChange&);
112         int use_playlist (DataType, boost::shared_ptr<Playlist>);
113         void playlist_ranges_moved (std::list< Evoral::RangeMove<framepos_t> > const &, bool);
114
115   private:
116         /** The number of frames by which this diskstream's output should be delayed
117             with respect to the transport frame.  This is used for latency compensation.
118         */
119         framecnt_t   _roll_delay;
120         framepos_t    overwrite_frame;
121         off_t         overwrite_offset;
122         bool          _pending_overwrite;
123         bool          overwrite_queued;
124         IOChange      input_change_pending;
125         framepos_t    file_frame[DataType::num_types];
126
127         int _do_refill_with_alloc (bool partial_fill);
128
129         static framecnt_t _chunk_frames;
130         static framecnt_t midi_readahead;
131         static bool       no_disk_output;
132
133         /* The MIDI stuff */
134
135         /** A buffer that we use to put newly-arrived MIDI data in for
136             the GUI to read (so that it can update itself).
137         */
138         MidiBuffer                   _gui_feed_buffer;
139         mutable Glib::Threads::Mutex _gui_feed_buffer_mutex;
140
141         int audio_read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
142                         framepos_t& start, framecnt_t cnt,
143                         int channel, bool reversed);
144         int midi_read (framepos_t& start, framecnt_t cnt, bool reversed);
145
146         static Sample* _mixdown_buffer;
147         static gain_t* _gain_buffer;
148
149         int refill (Sample* mixdown_buffer, float* gain_buffer, framecnt_t fill_level);
150         int refill_audio (Sample *mixdown_buffer, float *gain_buffer, framecnt_t fill_level);
151         int refill_midi ();
152
153         frameoffset_t calculate_playback_distance (pframes_t);
154
155         void get_midi_playback (MidiBuffer& dst, framecnt_t nframes, MonitorState, BufferSet&, double speed, framecnt_t distance);
156 };
157
158 } // namespace
159
160 #endif /* __ardour_disk_reader_h__ */