Removed unused midicontrollable.cc
[ardour.git] / libs / ardour / ardour / audio_diskstream.h
1 /*
2     Copyright (C) 2000 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     $Id: diskstream.h 579 2006-06-12 19:56:37Z essej $
19 */
20
21 #ifndef __ardour_diskstream_h__
22 #define __ardour_diskstream_h__
23
24 #include <sigc++/signal.h>
25
26 #include <cmath>
27 #include <string>
28 #include <queue>
29 #include <map>
30 #include <vector>
31
32 #include <time.h>
33
34 #include <pbd/fastlog.h>
35 #include <pbd/ringbufferNPT.h>
36 #include <pbd/stateful.h> 
37
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
46 struct tm;
47
48 namespace ARDOUR {
49
50 class AudioEngine;
51 class Send;
52 class Session;
53 class AudioPlaylist;
54 class AudioFileSource;
55 class IO;
56
57 class AudioDiskstream : public Stateful, public sigc::trackable
58 {       
59   public:
60         enum Flag {
61                 Recordable = 0x1,
62                 Hidden = 0x2,
63                 Destructive = 0x4
64         };
65
66         AudioDiskstream (Session &, const string& name, Flag f = Recordable);
67         AudioDiskstream (Session &, const XMLNode&);
68
69         string name() const { return _name; }
70
71         ARDOUR::IO* io() const { return _io; }
72         void set_io (ARDOUR::IO& io);
73
74         AudioDiskstream& ref() { _refcnt++; return *this; }
75         void unref() { if (_refcnt) _refcnt--; if (_refcnt == 0) delete this; }
76         uint32_t refcnt() const { return _refcnt; }
77
78         float playback_buffer_load() const;
79         float capture_buffer_load() const;
80
81         void set_flag (Flag f) {
82                 _flags |= f;
83         }
84
85         void unset_flag (Flag f) {
86                 _flags &= ~f;
87         }
88
89         AlignStyle alignment_style() const { return _alignment_style; }
90         void set_align_style (AlignStyle);
91         void set_persistent_align_style (AlignStyle);
92
93         bool hidden() const { return _flags & Hidden; }
94         bool recordable() const { return _flags & Recordable; }
95         bool destructive() const { return _flags & Destructive; }
96
97         void set_destructive (bool yn);
98
99         jack_nframes_t roll_delay() const { return _roll_delay; }
100         void set_roll_delay (jack_nframes_t);
101
102         int set_name (string str, void* src);
103
104         string input_source (uint32_t n=0) const {
105                 if (n < channels.size()) {
106                         return channels[n].source ? channels[n].source->name() : "";
107                 } else {
108                         return ""; 
109                 }
110         }
111
112         Port *input_source_port (uint32_t n=0) const { 
113                 if (n < channels.size()) return channels[n].source; return 0; 
114         }
115
116         void set_record_enabled (bool yn, void *src);
117         bool record_enabled() const { return g_atomic_int_get (&_record_enabled); }
118         void punch_in ();
119         void punch_out ();
120
121         bool  reversed() const { return _actual_speed < 0.0f; }
122         double speed() const { return _visible_speed; }
123         void set_speed (double);
124
125         float peak_power(uint32_t n=0) { 
126                 float x = channels[n].peak_power;
127                 channels[n].peak_power = 0.0f;
128                 if (x > 0.0f) {
129                         return 20.0f * fast_log10(x);
130                 } else {
131                         return minus_infinity();
132                 }
133         }
134
135         int  use_playlist (AudioPlaylist *);
136         int use_new_playlist ();
137         int use_copy_playlist ();
138
139         void start_scrub (jack_nframes_t where);
140         void end_scrub ();
141
142         Sample *playback_buffer (uint32_t n=0) {
143                 if (n < channels.size())
144                         return channels[n].current_playback_buffer;
145                 return 0;
146         }
147         
148         Sample *capture_buffer (uint32_t n=0) {
149                 if (n < channels.size())
150                         return channels[n].current_capture_buffer;
151                 return 0;
152         }
153
154         AudioPlaylist *playlist () { return _playlist; }
155
156         AudioFileSource *write_source (uint32_t n=0) {
157                 if (n < channels.size())
158                         return channels[n].write_source;
159                 return 0;
160         }
161
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);
166         
167         uint32_t n_channels() { return _n_channels; }
168
169         int add_channel ();
170         int remove_channel ();
171         
172         static void set_disk_io_chunk_frames (uint32_t n) {
173                 disk_io_chunk_frames = n;
174         }
175
176         static jack_nframes_t disk_io_frames() { return disk_io_chunk_frames; }
177         
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;
183
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;
188
189         /* stateful */
190
191         XMLNode& get_state(void);
192         int set_state(const XMLNode& node);
193
194         void monitor_input (bool);
195
196         jack_nframes_t capture_offset() const { return _capture_offset; }
197         void           set_capture_offset ();
198
199         static void swap_by_ptr (Sample *first, Sample *last) {
200                 while (first < last) {
201                         Sample tmp = *first;
202                         *first++ = *last;
203                         *last-- = tmp;
204                 }
205         }
206
207         static void swap_by_ptr (Sample *first, Sample *last, jack_nframes_t n) {
208                 while (n--) {
209                         Sample tmp = *first;
210                         *first++ = *last;
211                         *last-- = tmp;
212                 }
213         }
214
215         bool slaved() const { return _slaved; }
216         void set_slaved(bool yn) { _slaved = yn; }
217
218         int set_loop (Location *loc);
219         sigc::signal<void,Location *> LoopSet;
220
221         std::list<Region*>& last_capture_regions () {
222                 return _last_capture_regions;
223         }
224
225         void handle_input_change (IOChange, void *src);
226
227         const PBD::ID& id() const { return _id; }
228
229         XMLNode* deprecated_io_node;
230
231   protected:
232         friend class Session;
233
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.
237         */
238
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 ();
248
249         uint32_t read_data_count() const { return _read_data_count; }
250         uint32_t write_data_count() const { return _write_data_count; }
251
252   protected:
253         friend class Auditioner;
254         int  seek (jack_nframes_t which_sample, bool complete_refill = false);
255
256   protected:
257         friend class AudioTrack;
258
259         void prepare ();
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 */
263
264   private:
265
266         /* use unref() to destroy a diskstream */
267
268         ~AudioDiskstream();
269
270         enum TransitionType {
271                 CaptureStart = 0,
272                 CaptureEnd
273         };
274         
275         struct CaptureTransition {
276
277                 TransitionType   type;
278                 // the start or end file frame pos
279                 jack_nframes_t   capture_val;
280         };
281         
282         struct ChannelInfo {
283
284                 Sample     *playback_wrap_buffer;
285                 Sample     *capture_wrap_buffer;
286                 Sample     *speed_buffer;
287
288                 float       peak_power;
289
290                 AudioFileSource   *fades_source;
291                 AudioFileSource   *write_source;
292
293                 Port         *source;
294                 Sample       *current_capture_buffer;
295                 Sample       *current_playback_buffer;
296
297                 RingBufferNPT<Sample> *playback_buf;
298                 RingBufferNPT<Sample> *capture_buf;
299
300                 Sample* scrub_buffer;
301                 Sample* scrub_forward_buffer;
302                 Sample* scrub_reverse_buffer;
303
304                 RingBufferNPT<Sample>::rw_vector playback_vector;
305                 RingBufferNPT<Sample>::rw_vector capture_vector;
306
307                 RingBufferNPT<CaptureTransition> * capture_transition_buf;
308                 // the following are used in the butler thread only
309                 jack_nframes_t                     curr_capture_cnt;
310         };
311
312         typedef vector<ChannelInfo> ChannelList;
313
314
315         string            _name;
316         ARDOUR::Session&  _session;
317         ARDOUR::IO*       _io;
318         ChannelList        channels;
319         uint32_t      _n_channels;
320         PBD::ID           _id;
321
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;
328         bool                     _seek_required;
329         
330         bool                      force_refill;
331         jack_nframes_t            capture_start_frame;
332         jack_nframes_t            capture_captured;
333         bool                      was_recording;
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;
341         bool                     _scrubbing;
342         bool                     _slaved;
343         bool                     _processed;
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;
352
353         uint64_t                  last_phase;
354         uint64_t                  phi;
355         
356         jack_nframes_t            file_frame;           
357         jack_nframes_t            playback_sample;
358         jack_nframes_t            playback_distance;
359
360         uint32_t                 _read_data_count;
361         uint32_t                 _write_data_count;
362
363         bool                      in_set_state;
364         AlignStyle               _persistent_alignment_style;
365         bool                      first_input_change;
366
367         Glib::Mutex  state_lock;
368
369         jack_nframes_t scrub_start;
370         jack_nframes_t scrub_buffer_size;
371         jack_nframes_t scrub_offset;
372         uint32_t _refcnt;
373
374         sigc::connection ports_created_c;
375         sigc::connection plmod_connection;
376         sigc::connection plstate_connection;
377         sigc::connection plgone_connection;
378
379         /* the two central butler operations */
380
381         int do_flush (char * workbuf, bool force = false);
382         int do_refill (Sample *mixdown_buffer, float *gain_buffer, char *workbuf);
383
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);
386
387         uint32_t i_am_the_modifier;
388         
389         /* XXX fix this redundancy ... */
390
391         void playlist_changed (Change);
392         void playlist_modified ();
393         void playlist_deleted (Playlist*);
394         void session_controls_changed (Session::ControlType);
395
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);
399
400         struct CaptureInfo {
401             uint32_t start;
402             uint32_t frames;
403         };
404
405         vector<CaptureInfo*> capture_info;
406         Glib::Mutex  capture_info_lock;
407         
408         void init (Flag);
409
410         void init_channel (ChannelInfo &chan);
411         void destroy_channel (ChannelInfo &chan);
412         
413         static jack_nframes_t disk_io_chunk_frames;
414
415         int use_new_write_source (uint32_t n=0);
416         int use_new_fade_source (uint32_t n=0);
417
418         int find_and_use_playlist (const string&);
419
420         void allocate_temporary_buffers ();
421
422         unsigned char _flags;
423
424         int  create_input_port ();
425         int  connect_input_port ();
426         int  seek_unlocked (jack_nframes_t which_sample);
427
428         int ports_created ();
429
430         bool realtime_set_speed (double, bool global_change);
431         void non_realtime_set_speed ();
432
433         std::list<Region*> _last_capture_regions;
434         std::vector<AudioFileSource*> capturing_sources;
435         int use_pending_capture_data (XMLNode& node);
436
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);
444 };
445
446 } // namespace ARDOUR
447
448 #endif /* __ardour_diskstream_h__ */