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
67 DiskStream (Session &, const string& name, Flag f = Recordable);
68 DiskStream (Session &, const XMLNode&);
70 string name() const { return _name; }
72 ARDOUR::IO* io() const { return _io; }
73 void set_io (ARDOUR::IO& io);
75 DiskStream& ref() { _refcnt++; return *this; }
76 void unref() { if (_refcnt) _refcnt--; if (_refcnt == 0) delete this; }
77 uint32_t refcnt() const { return _refcnt; }
79 float playback_buffer_load() const;
80 float capture_buffer_load() const;
82 void set_flag (Flag f) {
86 void unset_flag (Flag f) {
90 AlignStyle alignment_style() const { return _alignment_style; }
91 void set_align_style (AlignStyle);
92 void set_persistent_align_style (AlignStyle);
94 bool hidden() const { return _flags & Hidden; }
95 bool recordable() const { return _flags & Recordable; }
96 bool destructive() const { return _flags & Destructive; }
98 void set_destructive (bool yn);
100 jack_nframes_t roll_delay() const { return _roll_delay; }
101 void set_roll_delay (jack_nframes_t);
103 void set_name (string str, void* src);
105 string input_source (uint32_t n=0) const {
106 if (n < channels.size()) {
107 return channels[n].source ? channels[n].source->name() : "";
113 Port *input_source_port (uint32_t n=0) const {
114 if (n < channels.size()) return channels[n].source; return 0;
117 void set_record_enabled (bool yn, void *src);
118 bool record_enabled() const { return atomic_read (&_record_enabled); }
122 bool reversed() const { return _actual_speed < 0.0f; }
123 double speed() const { return _visible_speed; }
124 void set_speed (double);
126 float peak_power(uint32_t n=0) {
127 float x = channels[n].peak_power;
128 channels[n].peak_power = 0.0f;
130 return 20.0f * fast_log10(x);
132 return minus_infinity();
136 int use_playlist (AudioPlaylist *);
137 int use_new_playlist ();
138 int use_copy_playlist ();
140 void start_scrub (jack_nframes_t where);
143 Sample *playback_buffer (uint32_t n=0) {
144 if (n < channels.size())
145 return channels[n].current_playback_buffer;
149 Sample *capture_buffer (uint32_t n=0) {
150 if (n < channels.size())
151 return channels[n].current_capture_buffer;
155 AudioPlaylist *playlist () { return _playlist; }
157 FileSource *write_source (uint32_t n=0) {
158 if (n < channels.size())
159 return channels[n].write_source;
163 jack_nframes_t current_capture_start() const { return capture_start_frame; }
164 jack_nframes_t current_capture_end() const { return capture_start_frame + capture_captured; }
165 jack_nframes_t get_capture_start_frame (uint32_t n=0);
166 jack_nframes_t get_captured_frames (uint32_t n=0);
168 uint32_t n_channels() { return _n_channels; }
171 int remove_channel ();
173 static void set_disk_io_chunk_frames (uint32_t n) {
174 disk_io_chunk_frames = n;
177 static jack_nframes_t disk_io_frames() { return disk_io_chunk_frames; }
179 sigc::signal<void,void*> record_enable_changed;
180 sigc::signal<void> speed_changed;
181 sigc::signal<void,void*> reverse_changed;
182 sigc::signal<void> PlaylistChanged;
183 sigc::signal<void> AlignmentStyleChanged;
185 static sigc::signal<void> DiskOverrun;
186 static sigc::signal<void> DiskUnderrun;
187 static sigc::signal<void,DiskStream*> DiskStreamCreated; // XXX use a ref with sigc2
188 static sigc::signal<void,DiskStream*> CannotRecordNoInput; // XXX use a ref with sigc2
189 static sigc::signal<void,list<Source*>*> DeleteSources;
193 XMLNode& get_state(void);
194 int set_state(const XMLNode& node);
196 void monitor_input (bool);
198 jack_nframes_t capture_offset() const { return _capture_offset; }
199 void set_capture_offset ();
201 static void swap_by_ptr (Sample *first, Sample *last) {
202 while (first < last) {
209 static void swap_by_ptr (Sample *first, Sample *last, jack_nframes_t n) {
217 bool slaved() const { return _slaved; }
218 void set_slaved(bool yn) { _slaved = yn; }
220 int set_loop (Location *loc);
221 sigc::signal<void,Location *> LoopSet;
223 std::list<Region*>& last_capture_regions () {
224 return _last_capture_regions;
227 void handle_input_change (IOChange, void *src);
229 id_t id() const { return _id; }
231 XMLNode* deprecated_io_node;
234 friend class Session;
236 /* the Session is the only point of access for these
237 because they require that the Session is "inactive"
238 while they are called.
241 void set_pending_overwrite (bool);
242 int overwrite_existing_buffers ();
243 void reverse_scrub_buffer (bool to_forward);
244 void set_block_size (jack_nframes_t);
245 int internal_playback_seek (jack_nframes_t distance);
246 int can_internal_playback_seek (jack_nframes_t distance);
247 void reset_write_sources (bool, bool force = false);
248 void non_realtime_input_change ();
250 uint32_t read_data_count() const { return _read_data_count; }
251 uint32_t write_data_count() const { return _write_data_count; }
254 friend class Auditioner;
255 int seek (jack_nframes_t which_sample, bool complete_refill = false);
258 friend class AudioTrack;
261 int process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input);
262 bool commit (jack_nframes_t nframes);
263 void recover (); /* called if commit will not be called, but process was */
267 /* use unref() to destroy a diskstream */
271 enum TransitionType {
276 struct CaptureTransition {
279 // the start or end file frame pos
280 jack_nframes_t capture_val;
285 Sample *playback_wrap_buffer;
286 Sample *capture_wrap_buffer;
287 Sample *speed_buffer;
291 FileSource *fades_source;
292 FileSource *write_source;
295 Sample *current_capture_buffer;
296 Sample *current_playback_buffer;
298 RingBufferNPT<Sample> *playback_buf;
299 RingBufferNPT<Sample> *capture_buf;
301 Sample* scrub_buffer;
302 Sample* scrub_forward_buffer;
303 Sample* scrub_reverse_buffer;
305 RingBufferNPT<Sample>::rw_vector playback_vector;
306 RingBufferNPT<Sample>::rw_vector capture_vector;
308 RingBufferNPT<CaptureTransition> * capture_transition_buf;
309 // the following are used in the butler thread only
310 jack_nframes_t curr_capture_cnt;
313 typedef vector<ChannelInfo> ChannelList;
317 ARDOUR::Session& _session;
319 ChannelList channels;
320 uint32_t _n_channels;
323 atomic_t _record_enabled;
324 AudioPlaylist* _playlist;
325 double _visible_speed;
326 double _actual_speed;
327 /* items needed for speed change logic */
328 bool _buffer_reallocation_required;
332 jack_nframes_t capture_start_frame;
333 jack_nframes_t capture_captured;
335 jack_nframes_t adjust_capture_position;
336 jack_nframes_t _capture_offset;
337 jack_nframes_t _roll_delay;
338 jack_nframes_t first_recordable_frame;
339 jack_nframes_t last_recordable_frame;
340 int last_possibly_recording;
341 AlignStyle _alignment_style;
345 Location* loop_location;
346 jack_nframes_t overwrite_frame;
347 off_t overwrite_offset;
348 bool pending_overwrite;
349 bool overwrite_queued;
350 IOChange input_change_pending;
351 jack_nframes_t wrap_buffer_size;
352 jack_nframes_t speed_buffer_size;
357 jack_nframes_t file_frame;
358 jack_nframes_t playback_sample;
359 jack_nframes_t playback_distance;
361 uint32_t _read_data_count;
362 uint32_t _write_data_count;
365 AlignStyle _persistent_alignment_style;
366 bool first_input_change;
368 PBD::NonBlockingLock state_lock;
370 jack_nframes_t scrub_start;
371 jack_nframes_t scrub_buffer_size;
372 jack_nframes_t scrub_offset;
375 sigc::connection ports_created_c;
376 sigc::connection plmod_connection;
377 sigc::connection plstate_connection;
378 sigc::connection plgone_connection;
380 /* the two central butler operations */
382 int do_flush (char * workbuf, bool force = false);
383 int do_refill (Sample *mixdown_buffer, float *gain_buffer, char *workbuf);
385 int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt,
386 ChannelInfo& channel_info, int channel, bool reversed);
388 uint32_t i_am_the_modifier;
390 /* XXX fix this redundancy ... */
392 void playlist_changed (Change);
393 void playlist_modified ();
394 void playlist_deleted (Playlist*);
395 void session_controls_changed (Session::ControlType);
397 void finish_capture (bool rec_monitors_input);
398 void clean_up_capture (struct tm&, time_t, bool abort);
399 void transport_stopped (struct tm&, time_t, bool abort);
406 vector<CaptureInfo*> capture_info;
407 PBD::Lock capture_info_lock;
411 void init_channel (ChannelInfo &chan);
412 void destroy_channel (ChannelInfo &chan);
414 static jack_nframes_t disk_io_chunk_frames;
416 int use_new_write_source (uint32_t n=0);
417 int use_new_fade_source (uint32_t n=0);
419 int find_and_use_playlist (const string&);
421 void allocate_temporary_buffers ();
423 unsigned char _flags;
425 int create_input_port ();
426 int connect_input_port ();
427 int seek_unlocked (jack_nframes_t which_sample);
429 int ports_created ();
431 bool realtime_set_speed (double, bool global_change);
432 void non_realtime_set_speed ();
434 std::list<Region*> _last_capture_regions;
435 std::vector<FileSource*> capturing_sources;
436 int use_pending_capture_data (XMLNode& node);
438 void get_input_sources ();
440 void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record);
442 void set_align_style_from_io();
446 }; /* namespace ARDOUR */
448 #endif /* __ardour_diskstream_h__ */