2 Copyright (C) 2000 Paul Davis
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.
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.
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.
21 #ifndef __ardour_diskstream_h__
22 #define __ardour_diskstream_h__
24 #include <sigc++/signal.h>
34 #include <pbd/fastlog.h>
35 #include <pbd/ringbufferNPT.h>
36 #include <pbd/atomic.h>
38 #include <ardour/ardour.h>
39 #include <ardour/configuration.h>
40 #include <ardour/session.h>
41 #include <ardour/route_group.h>
42 #include <ardour/route.h>
43 #include <ardour/port.h>
44 #include <ardour/utils.h>
45 #include <ardour/stateful.h>
58 class DiskStream : public Stateful, public sigc::trackable
66 DiskStream (Session &, const string& name, Flag f = Recordable);
67 DiskStream (Session &, const XMLNode&);
69 string name() const { return _name; }
71 ARDOUR::IO* io() const { return _io; }
72 void set_io (ARDOUR::IO& io);
74 DiskStream& ref() { _refcnt++; return *this; }
75 void unref() { if (_refcnt) _refcnt--; if (_refcnt == 0) delete this; }
76 uint32_t refcnt() const { return _refcnt; }
78 float playback_buffer_load() const;
79 float capture_buffer_load() const;
81 void set_flag (Flag f) {
85 void unset_flag (Flag f) {
89 AlignStyle alignment_style() const { return _alignment_style; }
90 void set_align_style (AlignStyle);
91 void set_persistent_align_style (AlignStyle);
93 bool hidden() const { return _flags & Hidden; }
94 bool recordable() const { return _flags & Recordable; }
96 jack_nframes_t roll_delay() const { return _roll_delay; }
97 void set_roll_delay (jack_nframes_t);
99 void set_name (string str, void* src);
101 string input_source (uint32_t n=0) const {
102 if (n < channels.size()) {
103 return channels[n].source ? channels[n].source->name() : "";
109 Port *input_source_port (uint32_t n=0) const {
110 if (n < channels.size()) return channels[n].source; return 0;
113 void set_record_enabled (bool yn, void *src);
114 bool record_enabled() const { return atomic_read (&_record_enabled); }
118 bool reversed() const { return _actual_speed < 0.0f; }
119 double speed() const { return _visible_speed; }
120 void set_speed (double);
122 float peak_power(uint32_t n=0) {
123 float x = channels[n].peak_power;
124 channels[n].peak_power = 0.0f;
126 return 20.0f * fast_log10(x);
128 return minus_infinity();
132 int use_playlist (AudioPlaylist *);
133 int use_new_playlist ();
134 int use_copy_playlist ();
136 void start_scrub (jack_nframes_t where);
139 Sample *playback_buffer (uint32_t n=0) {
140 if (n < channels.size())
141 return channels[n].current_playback_buffer;
145 Sample *capture_buffer (uint32_t n=0) {
146 if (n < channels.size())
147 return channels[n].current_capture_buffer;
151 AudioPlaylist *playlist () { return _playlist; }
153 FileSource *fades_source (uint32_t n=0) {
154 if (n < channels.size())
155 return channels[n].fades_source;
158 FileSource *write_source (uint32_t n=0) {
159 if (n < channels.size())
160 return channels[n].write_source;
164 jack_nframes_t current_capture_start() const { return capture_start_frame; }
165 jack_nframes_t current_capture_end() const { return capture_start_frame + capture_captured; }
166 jack_nframes_t get_capture_start_frame (uint32_t n=0);
167 jack_nframes_t get_captured_frames (uint32_t n=0);
169 uint32_t n_channels() { return _n_channels; }
172 int remove_channel ();
174 static void set_disk_io_chunk_frames (uint32_t n) {
175 disk_io_chunk_frames = n;
178 static jack_nframes_t disk_io_frames() { return disk_io_chunk_frames; }
180 sigc::signal<void,void*> record_enable_changed;
181 sigc::signal<void> speed_changed;
182 sigc::signal<void,void*> reverse_changed;
183 sigc::signal<void> PlaylistChanged;
184 sigc::signal<void> AlignmentStyleChanged;
186 static sigc::signal<void> DiskOverrun;
187 static sigc::signal<void> DiskUnderrun;
188 static sigc::signal<void,DiskStream*> DiskStreamCreated; // XXX use a ref with sigc2
189 static sigc::signal<void,DiskStream*> CannotRecordNoInput; // XXX use a ref with sigc2
190 static sigc::signal<void,list<Source*>*> DeleteSources;
194 XMLNode& get_state(void);
195 int set_state(const XMLNode& node);
197 void monitor_input (bool);
199 jack_nframes_t capture_offset() const { return _capture_offset; }
200 void set_capture_offset ();
202 static void swap_by_ptr (Sample *first, Sample *last) {
203 while (first < last) {
210 static void swap_by_ptr (Sample *first, Sample *last, jack_nframes_t n) {
218 bool slaved() const { return _slaved; }
219 void set_slaved(bool yn) { _slaved = yn; }
221 int set_loop (Location *loc);
222 sigc::signal<void,Location *> LoopSet;
224 std::list<Region*>& last_capture_regions () {
225 return _last_capture_regions;
228 void handle_input_change (IOChange, void *src);
230 id_t id() const { return _id; }
232 XMLNode* deprecated_io_node;
235 friend class Session;
237 /* the Session is the only point of access for these
238 because they require that the Session is "inactive"
239 while they are called.
242 void set_pending_overwrite (bool);
243 int overwrite_existing_buffers ();
244 void reverse_scrub_buffer (bool to_forward);
245 void set_block_size (jack_nframes_t);
246 int internal_playback_seek (jack_nframes_t distance);
247 int can_internal_playback_seek (jack_nframes_t distance);
248 void reset_write_sources (bool);
249 void non_realtime_input_change ();
251 uint32_t read_data_count() const { return _read_data_count; }
252 uint32_t write_data_count() const { return _write_data_count; }
255 friend class Auditioner;
256 int seek (jack_nframes_t which_sample, bool complete_refill = false);
259 friend class AudioTrack;
262 int process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input);
263 bool commit (jack_nframes_t nframes);
264 void recover (); /* called if commit will not be called, but process was */
268 /* use unref() to destroy a diskstream */
274 Sample *playback_wrap_buffer;
275 Sample *capture_wrap_buffer;
276 Sample *speed_buffer;
280 FileSource *fades_source;
281 FileSource *write_source;
284 Sample *current_capture_buffer;
285 Sample *current_playback_buffer;
287 RingBufferNPT<Sample> *playback_buf;
288 RingBufferNPT<Sample> *capture_buf;
290 Sample* scrub_buffer;
291 Sample* scrub_forward_buffer;
292 Sample* scrub_reverse_buffer;
294 RingBufferNPT<Sample>::rw_vector playback_vector;
295 RingBufferNPT<Sample>::rw_vector capture_vector;
298 typedef vector<ChannelInfo> ChannelList;
301 ARDOUR::Session& _session;
303 ChannelList channels;
304 uint32_t _n_channels;
307 atomic_t _record_enabled;
308 AudioPlaylist* _playlist;
309 double _visible_speed;
310 double _actual_speed;
311 /* items needed for speed change logic */
312 bool _buffer_reallocation_required;
316 jack_nframes_t capture_start_frame;
317 jack_nframes_t capture_captured;
319 jack_nframes_t adjust_capture_position;
320 jack_nframes_t _capture_offset;
321 jack_nframes_t _roll_delay;
322 jack_nframes_t first_recordable_frame;
323 jack_nframes_t last_recordable_frame;
324 int last_possibly_recording;
325 AlignStyle _alignment_style;
329 Location* loop_location;
330 jack_nframes_t overwrite_frame;
331 off_t overwrite_offset;
332 bool pending_overwrite;
333 bool overwrite_queued;
334 IOChange input_change_pending;
335 jack_nframes_t wrap_buffer_size;
336 jack_nframes_t speed_buffer_size;
341 jack_nframes_t file_frame;
342 jack_nframes_t playback_sample;
343 jack_nframes_t playback_distance;
345 uint32_t _read_data_count;
346 uint32_t _write_data_count;
349 AlignStyle _persistent_alignment_style;
350 bool first_input_change;
352 PBD::NonBlockingLock state_lock;
354 jack_nframes_t scrub_start;
355 jack_nframes_t scrub_buffer_size;
356 jack_nframes_t scrub_offset;
359 sigc::connection ports_created_c;
360 sigc::connection plmod_connection;
361 sigc::connection plstate_connection;
362 sigc::connection plgone_connection;
364 /* the two central butler operations */
366 int do_flush (bool force = false);
367 int do_refill (Sample *mixdown_buffer, float *gain_buffer);
369 int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt,
370 ChannelInfo& channel_info, int channel, bool reversed);
372 uint32_t i_am_the_modifier;
374 /* XXX fix this redundancy ... */
376 void playlist_changed (Change);
377 void playlist_modified ();
378 void playlist_deleted (Playlist*);
379 void session_controls_changed (Session::ControlType);
381 void finish_capture (bool rec_monitors_input);
382 void clean_up_capture (struct tm&, time_t, bool abort);
383 void transport_stopped (struct tm&, time_t, bool abort);
390 vector<CaptureInfo*> capture_info;
391 PBD::Lock capture_info_lock;
395 void init_channel (ChannelInfo &chan);
396 void destroy_channel (ChannelInfo &chan);
398 static jack_nframes_t disk_io_chunk_frames;
400 int use_new_write_source (uint32_t n=0);
401 int use_new_fade_source (uint32_t n=0);
403 int find_and_use_playlist (const string&);
405 void allocate_temporary_buffers ();
407 unsigned char _flags;
409 int create_input_port ();
410 int connect_input_port ();
411 int seek_unlocked (jack_nframes_t which_sample);
413 int ports_created ();
415 bool realtime_set_speed (double, bool global_change);
416 void non_realtime_set_speed ();
418 std::list<Region*> _last_capture_regions;
419 std::vector<FileSource*> capturing_sources;
420 int use_pending_capture_data (XMLNode& node);
422 void get_input_sources ();
424 void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record);
426 void set_align_style_from_io();
430 }; /* namespace ARDOUR */
432 #endif /* __ardour_diskstream_h__ */