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.
18 $Id: diskstream.h 579 2006-06-12 19:56:37Z essej $
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/stateful.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>
54 class AudioFileSource;
57 class AudioDiskstream : public Stateful, public sigc::trackable
66 AudioDiskstream (Session &, const string& name, Flag f = Recordable);
67 AudioDiskstream (Session &, const XMLNode&);
69 string name() const { return _name; }
71 ARDOUR::IO* io() const { return _io; }
72 void set_io (ARDOUR::IO& io);
74 AudioDiskstream& 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; }
95 bool destructive() const { return _flags & Destructive; }
97 void set_destructive (bool yn);
99 jack_nframes_t roll_delay() const { return _roll_delay; }
100 void set_roll_delay (jack_nframes_t);
102 int set_name (string str, void* src);
104 string input_source (uint32_t n=0) const {
105 if (n < channels.size()) {
106 return channels[n].source ? channels[n].source->name() : "";
112 Port *input_source_port (uint32_t n=0) const {
113 if (n < channels.size()) return channels[n].source; return 0;
116 void set_record_enabled (bool yn, void *src);
117 bool record_enabled() const { return g_atomic_int_get (&_record_enabled); }
121 bool reversed() const { return _actual_speed < 0.0f; }
122 double speed() const { return _visible_speed; }
123 void set_speed (double);
125 float peak_power(uint32_t n=0) {
126 float x = channels[n].peak_power;
127 channels[n].peak_power = 0.0f;
129 return 20.0f * fast_log10(x);
131 return minus_infinity();
135 int use_playlist (AudioPlaylist *);
136 int use_new_playlist ();
137 int use_copy_playlist ();
139 void start_scrub (jack_nframes_t where);
142 Sample *playback_buffer (uint32_t n=0) {
143 if (n < channels.size())
144 return channels[n].current_playback_buffer;
148 Sample *capture_buffer (uint32_t n=0) {
149 if (n < channels.size())
150 return channels[n].current_capture_buffer;
154 AudioPlaylist *playlist () { return _playlist; }
156 AudioFileSource *write_source (uint32_t n=0) {
157 if (n < channels.size())
158 return channels[n].write_source;
162 jack_nframes_t current_capture_start() const { return capture_start_frame; }
163 jack_nframes_t current_capture_end() const { return capture_start_frame + capture_captured; }
164 jack_nframes_t get_capture_start_frame (uint32_t n=0);
165 jack_nframes_t get_captured_frames (uint32_t n=0);
167 uint32_t n_channels() { return _n_channels; }
170 int remove_channel ();
172 static void set_disk_io_chunk_frames (uint32_t n) {
173 disk_io_chunk_frames = n;
176 static jack_nframes_t disk_io_frames() { return disk_io_chunk_frames; }
178 sigc::signal<void,void*> record_enable_changed;
179 sigc::signal<void> speed_changed;
180 sigc::signal<void,void*> reverse_changed;
181 sigc::signal<void> PlaylistChanged;
182 sigc::signal<void> AlignmentStyleChanged;
184 static sigc::signal<void> DiskOverrun;
185 static sigc::signal<void> DiskUnderrun;
186 static sigc::signal<void,AudioDiskstream*> AudioDiskstreamCreated; // XXX use a ref with sigc2
187 static sigc::signal<void,list<AudioFileSource*>*> DeleteSources;
191 XMLNode& get_state(void);
192 int set_state(const XMLNode& node);
194 void monitor_input (bool);
196 jack_nframes_t capture_offset() const { return _capture_offset; }
197 void set_capture_offset ();
199 static void swap_by_ptr (Sample *first, Sample *last) {
200 while (first < last) {
207 static void swap_by_ptr (Sample *first, Sample *last, jack_nframes_t n) {
215 bool slaved() const { return _slaved; }
216 void set_slaved(bool yn) { _slaved = yn; }
218 int set_loop (Location *loc);
219 sigc::signal<void,Location *> LoopSet;
221 std::list<Region*>& last_capture_regions () {
222 return _last_capture_regions;
225 void handle_input_change (IOChange, void *src);
227 const PBD::ID& id() const { return _id; }
229 XMLNode* deprecated_io_node;
232 friend class Session;
234 /* the Session is the only point of access for these
235 because they require that the Session is "inactive"
236 while they are called.
239 void set_pending_overwrite (bool);
240 int overwrite_existing_buffers ();
241 void reverse_scrub_buffer (bool to_forward);
242 void set_block_size (jack_nframes_t);
243 int internal_playback_seek (jack_nframes_t distance);
244 int can_internal_playback_seek (jack_nframes_t distance);
245 int rename_write_sources ();
246 void reset_write_sources (bool, bool force = false);
247 void non_realtime_input_change ();
249 uint32_t read_data_count() const { return _read_data_count; }
250 uint32_t write_data_count() const { return _write_data_count; }
253 friend class Auditioner;
254 int seek (jack_nframes_t which_sample, bool complete_refill = false);
257 friend class AudioTrack;
260 int process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input);
261 bool commit (jack_nframes_t nframes);
262 void recover (); /* called if commit will not be called, but process was */
266 /* use unref() to destroy a diskstream */
270 enum TransitionType {
275 struct CaptureTransition {
278 // the start or end file frame pos
279 jack_nframes_t capture_val;
284 Sample *playback_wrap_buffer;
285 Sample *capture_wrap_buffer;
286 Sample *speed_buffer;
290 AudioFileSource *fades_source;
291 AudioFileSource *write_source;
294 Sample *current_capture_buffer;
295 Sample *current_playback_buffer;
297 RingBufferNPT<Sample> *playback_buf;
298 RingBufferNPT<Sample> *capture_buf;
300 Sample* scrub_buffer;
301 Sample* scrub_forward_buffer;
302 Sample* scrub_reverse_buffer;
304 RingBufferNPT<Sample>::rw_vector playback_vector;
305 RingBufferNPT<Sample>::rw_vector capture_vector;
307 RingBufferNPT<CaptureTransition> * capture_transition_buf;
308 // the following are used in the butler thread only
309 jack_nframes_t curr_capture_cnt;
312 typedef vector<ChannelInfo> ChannelList;
316 ARDOUR::Session& _session;
318 ChannelList channels;
319 uint32_t _n_channels;
322 mutable gint _record_enabled;
323 AudioPlaylist* _playlist;
324 double _visible_speed;
325 double _actual_speed;
326 /* items needed for speed change logic */
327 bool _buffer_reallocation_required;
331 jack_nframes_t capture_start_frame;
332 jack_nframes_t capture_captured;
334 jack_nframes_t adjust_capture_position;
335 jack_nframes_t _capture_offset;
336 jack_nframes_t _roll_delay;
337 jack_nframes_t first_recordable_frame;
338 jack_nframes_t last_recordable_frame;
339 int last_possibly_recording;
340 AlignStyle _alignment_style;
344 Location* loop_location;
345 jack_nframes_t overwrite_frame;
346 off_t overwrite_offset;
347 bool pending_overwrite;
348 bool overwrite_queued;
349 IOChange input_change_pending;
350 jack_nframes_t wrap_buffer_size;
351 jack_nframes_t speed_buffer_size;
356 jack_nframes_t file_frame;
357 jack_nframes_t playback_sample;
358 jack_nframes_t playback_distance;
360 uint32_t _read_data_count;
361 uint32_t _write_data_count;
364 AlignStyle _persistent_alignment_style;
365 bool first_input_change;
367 Glib::Mutex state_lock;
369 jack_nframes_t scrub_start;
370 jack_nframes_t scrub_buffer_size;
371 jack_nframes_t scrub_offset;
374 sigc::connection ports_created_c;
375 sigc::connection plmod_connection;
376 sigc::connection plstate_connection;
377 sigc::connection plgone_connection;
379 /* the two central butler operations */
381 int do_flush (char * workbuf, bool force = false);
382 int do_refill (Sample *mixdown_buffer, float *gain_buffer, char *workbuf);
384 int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt,
385 ChannelInfo& channel_info, int channel, bool reversed);
387 uint32_t i_am_the_modifier;
389 /* XXX fix this redundancy ... */
391 void playlist_changed (Change);
392 void playlist_modified ();
393 void playlist_deleted (Playlist*);
394 void session_controls_changed (Session::ControlType);
396 void finish_capture (bool rec_monitors_input);
397 void clean_up_capture (struct tm&, time_t, bool abort);
398 void transport_stopped (struct tm&, time_t, bool abort);
405 vector<CaptureInfo*> capture_info;
406 Glib::Mutex capture_info_lock;
410 void init_channel (ChannelInfo &chan);
411 void destroy_channel (ChannelInfo &chan);
413 static jack_nframes_t disk_io_chunk_frames;
415 int use_new_write_source (uint32_t n=0);
416 int use_new_fade_source (uint32_t n=0);
418 int find_and_use_playlist (const string&);
420 void allocate_temporary_buffers ();
422 unsigned char _flags;
424 int create_input_port ();
425 int connect_input_port ();
426 int seek_unlocked (jack_nframes_t which_sample);
428 int ports_created ();
430 bool realtime_set_speed (double, bool global_change);
431 void non_realtime_set_speed ();
433 std::list<Region*> _last_capture_regions;
434 std::vector<AudioFileSource*> capturing_sources;
435 int use_pending_capture_data (XMLNode& node);
437 void get_input_sources ();
438 void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record);
439 void set_align_style_from_io();
440 void setup_destructive_playlist ();
441 void use_destructive_playlist ();
442 void engage_record_enable (void* src);
443 void disengage_record_enable (void* src);
446 } // namespace ARDOUR
448 #endif /* __ardour_diskstream_h__ */