Optimize automation-event process splitting
[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 (_("player")); }
44
45         static samplecnt_t chunk_samples() { return _chunk_samples; }
46         static samplecnt_t default_chunk_samples ();
47         static void set_chunk_samples (samplecnt_t n) { _chunk_samples = n; }
48
49         void run (BufferSet& /*bufs*/, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, 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         int set_state (const XMLNode&, int version);
56
57         PBD::Signal0<void>            AlignmentStyleChanged;
58
59         float buffer_load() const;
60
61         void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<samplepos_t> > const &);
62
63         /* called by the Butler in a non-realtime context */
64
65         int do_refill () {
66                 return refill (_mixdown_buffer, _gain_buffer, 0);
67         }
68
69         /** For non-butler contexts (allocates temporary working buffers)
70          *
71          * This accessible method has a default argument; derived classes
72          * must inherit the virtual method that we call which does NOT
73          * have a default argument, to avoid complications with inheritance
74          */
75         int do_refill_with_alloc (bool partial_fill = true) {
76                 return _do_refill_with_alloc (partial_fill);
77         }
78
79         bool pending_overwrite () const { return _pending_overwrite; }
80
81         // Working buffers for do_refill (butler thread)
82         static void allocate_working_buffers();
83         static void free_working_buffers();
84
85         void adjust_buffering ();
86
87         int can_internal_playback_seek (samplecnt_t distance);
88         int internal_playback_seek (samplecnt_t distance);
89         int seek (samplepos_t sample, bool complete_refill = false);
90
91         static PBD::Signal0<void> Underrun;
92
93         void playlist_modified ();
94         void reset_tracker ();
95
96         bool declick_in_progress () const {
97                 return _declick_gain != 0; // declick-out
98         }
99
100         static void set_midi_readahead_samples (samplecnt_t samples_ahead) { midi_readahead = samples_ahead; }
101
102         static void set_no_disk_output (bool yn);
103         static bool no_disk_output() { return _no_disk_output; }
104
105 protected:
106         friend class Track;
107         friend class MidiTrack;
108
109         struct ReaderChannelInfo : public DiskIOProcessor::ChannelInfo {
110                 ReaderChannelInfo (samplecnt_t buffer_size)
111                         : DiskIOProcessor::ChannelInfo (buffer_size)
112                 {
113                         resize (buffer_size);
114                 }
115                 void resize (samplecnt_t);
116         };
117
118         XMLNode& state ();
119
120         void resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time);
121
122         void playlist_changed (const PBD::PropertyChange&);
123         int use_playlist (DataType, boost::shared_ptr<Playlist>);
124         void playlist_ranges_moved (std::list< Evoral::RangeMove<samplepos_t> > const &, bool);
125
126         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
127
128 private:
129         /** The number of samples by which this diskstream's output should be delayed
130             with respect to the transport sample.  This is used for latency compensation.
131         */
132         samplepos_t   overwrite_sample;
133         off_t         overwrite_offset;
134         bool          _pending_overwrite;
135         bool          overwrite_queued;
136         IOChange      input_change_pending;
137         samplepos_t   file_sample[DataType::num_types];
138
139         gain_t        _declick_gain;
140
141         int _do_refill_with_alloc (bool partial_fill);
142
143         static samplecnt_t _chunk_samples;
144         static samplecnt_t midi_readahead;
145         static bool       _no_disk_output;
146
147         int audio_read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
148                         samplepos_t& start, samplecnt_t cnt,
149                         int channel, bool reversed);
150         int midi_read (samplepos_t& start, samplecnt_t cnt, bool reversed);
151
152         static Sample* _mixdown_buffer;
153         static gain_t* _gain_buffer;
154
155         int refill (Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level);
156         int refill_audio (Sample *mixdown_buffer, float *gain_buffer, samplecnt_t fill_level);
157         int refill_midi ();
158
159         sampleoffset_t calculate_playback_distance (pframes_t);
160
161         void get_midi_playback (MidiBuffer& dst, samplepos_t start_sample, samplepos_t end_sample, MonitorState, BufferSet&, double speed, samplecnt_t distance);
162 };
163
164 } // namespace
165
166 #endif /* __ardour_disk_reader_h__ */