add finite state machine to control/manage transport state
[ardour.git] / libs / ardour / ardour / disk_io.h
1 /*
2  * Copyright (C) 2016-2018 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #ifndef __ardour_disk_io_h__
21 #define __ardour_disk_io_h__
22
23 #include <vector>
24 #include <string>
25 #include <exception>
26
27 #include "pbd/ringbufferNPT.h"
28 #include "pbd/rcu.h"
29
30 #include "ardour/interpolation.h"
31 #include "ardour/processor.h"
32
33 namespace PBD {
34         template<class T> class PlaybackBuffer;
35 }
36
37 namespace ARDOUR {
38
39 class AudioFileSource;
40 class AudioPlaylist;
41 class Location;
42 class MidiPlaylist;
43 class Playlist;
44 class Route;
45 class Route;
46 class Session;
47
48 template<typename T> class MidiRingBuffer;
49
50 class LIBARDOUR_API DiskIOProcessor : public Processor
51 {
52 public:
53         enum Flag {
54                 Recordable  = 0x1,
55                 Hidden      = 0x2,
56                 Destructive = 0x4,
57                 NonLayered  = 0x8 // deprecated (kept only for enum compat)
58         };
59
60         static const std::string state_node_name;
61
62         DiskIOProcessor (Session&, const std::string& name, Flag f);
63         virtual ~DiskIOProcessor ();
64
65         void set_route (boost::shared_ptr<Route>);
66         void drop_route ();
67
68         static void set_buffering_parameters (BufferingPreset bp);
69
70         int set_block_size (pframes_t);
71         bool configure_io (ChanCount in, ChanCount out);
72         bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
73
74         /** @return A number between 0 and 1, where 0 indicates that the playback/capture buffer
75          *  is dry (ie the disk subsystem could not keep up) and 1 indicates that the
76          *  buffer is full.
77          */
78         virtual float buffer_load() const = 0;
79
80         void set_flag (Flag f)   { _flags = Flag (_flags | f); }
81         void unset_flag (Flag f) { _flags = Flag (_flags & ~f); }
82
83         bool           hidden()      const { return _flags & Hidden; }
84         bool           recordable()  const { return _flags & Recordable; }
85
86         virtual void non_realtime_locate (samplepos_t);
87
88         virtual void punch_in()  {}
89         virtual void punch_out() {}
90
91         bool slaved() const      { return _slaved; }
92         void set_slaved(bool yn) { _slaved = yn; }
93
94         PBD::Signal0<void>            SpeedChanged;
95         PBD::Signal0<void>            ReverseChanged;
96
97         int set_state (const XMLNode&, int version);
98
99         int add_channel (uint32_t how_many);
100         int remove_channel (uint32_t how_many);
101
102         bool need_butler() const { return _need_butler; }
103
104         boost::shared_ptr<Playlist>      get_playlist (DataType dt) const { return _playlists[dt]; }
105         boost::shared_ptr<MidiPlaylist>  midi_playlist() const;
106         boost::shared_ptr<AudioPlaylist> audio_playlist() const;
107
108         virtual void playlist_modified () {}
109         virtual int use_playlist (DataType, boost::shared_ptr<Playlist>);
110
111         virtual void adjust_buffering() = 0;
112
113 protected:
114         friend class Auditioner;
115         virtual int  seek (samplepos_t which_sample, bool complete_refill = false) = 0;
116
117 protected:
118         Flag         _flags;
119         uint32_t      i_am_the_modifier;
120         bool         _slaved;
121         bool          in_set_state;
122         samplepos_t   playback_sample;
123         bool         _need_butler;
124         boost::shared_ptr<Route> _route;
125
126         void init ();
127
128         Glib::Threads::Mutex state_lock;
129
130         static bool get_buffering_presets (BufferingPreset bp,
131                                            samplecnt_t& read_chunk_size,
132                                            samplecnt_t& read_buffer_size,
133                                            samplecnt_t& write_chunk_size,
134                                            samplecnt_t& write_buffer_size);
135
136         enum TransitionType {
137                 CaptureStart = 0,
138                 CaptureEnd
139         };
140
141         struct CaptureTransition {
142                 TransitionType   type;
143                 samplepos_t       capture_val; ///< The start or end file sample position
144         };
145
146         /** Information about one audio channel, playback or capture
147          * (depending on the derived class)
148          */
149         struct ChannelInfo : public boost::noncopyable {
150
151                 ChannelInfo (samplecnt_t buffer_size);
152                 virtual ~ChannelInfo ();
153
154                 /** A random-access ringbuffer for data to be played back.
155                  * written to in the butler thread, read from in the process thread.
156                  */
157                 PBD::PlaybackBuffer<Sample>* rbuf;
158
159                 /** A ringbuffer for data to be recorded back, written to in the
160                  * process thread, read from in the butler thread.
161                  */
162                 PBD::RingBufferNPT<Sample>* wbuf;
163                 PBD::RingBufferNPT<Sample>::rw_vector rw_vector;
164
165                 /* used only by capture */
166                 boost::shared_ptr<AudioFileSource> write_source;
167                 PBD::RingBufferNPT<CaptureTransition>* capture_transition_buf;
168
169                 /* used in the butler thread only */
170                 samplecnt_t curr_capture_cnt;
171
172                 virtual void resize (samplecnt_t) = 0;
173         };
174
175         typedef std::vector<ChannelInfo*> ChannelList;
176         SerializedRCUManager<ChannelList> channels;
177
178         virtual int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many) = 0;
179         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
180
181         boost::shared_ptr<Playlist> _playlists[DataType::num_types];
182         PBD::ScopedConnectionList playlist_connections;
183
184         virtual void playlist_changed (const PBD::PropertyChange&) {}
185         virtual void playlist_deleted (boost::weak_ptr<Playlist>);
186         virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<samplepos_t> > const &, bool) {}
187
188         /* The MIDI stuff */
189
190         MidiRingBuffer<samplepos_t>*  _midi_buf;
191         gint                         _samples_written_to_ringbuffer;
192         gint                         _samples_read_from_ringbuffer;
193
194         static void get_location_times (const Location* location, samplepos_t* start, samplepos_t* end, samplepos_t* length);
195 };
196
197 } // namespace ARDOUR
198
199 #endif /* __ardour_disk_io_h__ */