Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / audiodevice.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * Copyright (C) 2003-2016 Cinelerra CV contributors
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #ifndef AUDIODEVICE_H
24 #define AUDIODEVICE_H
25
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/ioctl.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32
33 #include "audioalsa.inc"
34 #include "audioconfig.inc"
35 #include "audiodevice.inc"
36 #include "audiodvb.inc"
37 #include "audioesound.inc"
38 #include "audiooss.inc"
39 #include "audiopulse.inc"
40 #include "audiov4l2mpeg.inc"
41 #include "bctimer.inc"
42 #include "binary.h"
43 #include "condition.inc"
44 #include "dcoffset.inc"
45 #include "device1394output.inc"
46 #include "devicedvbinput.inc"
47 #include "devicempeginput.inc"
48 #include "maxchannels.h"
49 #include "mutex.inc"
50 #include "mwindow.inc"
51 #include "preferences.inc"
52 #include "recordgui.inc"
53 #include "samples.inc"
54 #include "sema.inc"
55 #include "thread.h"
56 #include "videodevice.inc"
57 #ifdef HAVE_FIREWIRE
58 #include "audio1394.inc"
59 #include "device1394output.inc"
60 #include "vdevice1394.inc"
61 #endif
62
63 class AudioThread : public Thread
64 {
65         AudioDevice *device;
66         void (AudioDevice::*arun)();
67         void (AudioDevice::*aend)();
68         Condition *startup_lock;
69         int started;
70 public:
71         AudioThread(AudioDevice *device,
72                 void (AudioDevice::*arun)(),
73                 void (AudioDevice::*aend)());
74         ~AudioThread();
75         void initialize();
76         void run();
77         void startup();
78         void stop(int wait);
79 };
80
81 class AudioDevice
82 {
83         friend class RecordAudio;
84 public:
85 // MWindow is required where global input is used, to get the pointer.
86         AudioDevice(MWindow *mwindow = 0);
87         ~AudioDevice();
88
89         friend class AudioALSA;
90         friend class AudioMPEG;
91         friend class AudioDVB;
92         friend class AudioV4L2MPEG;
93         friend class AudioOSS;
94         friend class AudioPulse;
95         friend class AudioESound;
96         friend class Audio1394;
97         friend class VDevice1394;
98         friend class Device1394Output;
99         friend class DeviceMPEGInput;
100         friend class DeviceDVBInput;
101         friend class DeviceV4L2Input;
102
103         int open_input(AudioInConfig *config,
104                 VideoInConfig *vconfig,
105                 int rate,
106                 int samples,
107                 int channels,
108                 int realtime);
109         int open_output(AudioOutConfig *config,
110                 int rate,
111                 int samples,
112                 int channels,
113                 int realtime);
114         int open_monitor(AudioOutConfig *config, int mode);
115         int close_all();
116
117 // ================================ recording
118
119 // read from the record device
120 // Conversion between double and int is done in AudioDevice
121         int read_buffer(Samples **input, int channels,
122                 int samples, int *over, double *max, int input_offset = 0);
123         void start_recording();
124 // If a firewire device crashed
125         int interrupt_crash();
126
127 // ================================== dc offset
128
129 // writes to whichever buffer is free or blocks until one becomes free
130         int write_buffer(double **output, int channels, int samples, double bfr_time=-1.);
131 // Stop threads
132         int stop_audio(int wait);
133
134 // After the last buffer is written call this to terminate.
135 // A separate buffer for termination is required since the audio device can be
136 // finished without writing a single byte
137         int set_last_buffer();
138
139 // start the thread processing buffers
140         int start_playback();
141         int is_monitoring() { return monitoring && monitor_open; }
142         void set_monitoring(int mode);
143 // record menu auto config update
144         void auto_update(int channels, int samplerate, int bits);
145         int config_updated();
146         void config_update();
147 // record raw stream on file descr fd, to close use fd=-1
148 //  bsz records up to bsz bytes of past buffer data in first_write
149         int start_toc(const char *path, const char *toc_path);
150         int start_record(int fd, int bsz=0);
151         int stop_record();
152         int total_audio_channels();
153         DeviceMPEGInput *mpeg_device();
154
155 // interrupt the playback thread
156         int interrupt_playback();
157 // drop the current record buffer and wait for resume
158         void interrupt_recording();
159         void resume_recording();
160         void set_play_dither(int status);
161         void set_rec_gain(double gain=1.);
162         void set_play_gain(double gain=1.);
163 // set software positioning on or off
164         void set_software_positioning(int status = 1);
165 // total samples played
166 //  + audio offset from configuration if playback
167         int64_t current_position();
168         int64_t samples_output();
169         double get_itimestamp();
170         double get_otimestamp();
171         double device_timestamp();
172         void set_vdevice(VideoDevice *vdevice) { this->vdevice = vdevice; }
173         int get_interrupted() { return playback_interrupted; }
174         int get_ichannels() { return in_channels; }
175         int get_ochannels() { return out_channels; }
176         int get_ibits() { return in_bits; }
177         int get_obits() { return out_bits; }
178         int get_irate() { return in_samplerate; }
179         int get_orate() { return out_samplerate; }
180         int get_irealtime() { return in_realtime; }
181         int get_orealtime() { return out_realtime; }
182         int get_device_buffer() { return device_buffer; }
183         int64_t get_samples_written() { return total_samples_written; }
184         int64_t get_samples_output() { return total_samples_output; }
185         int64_t get_samples_read() { return total_samples_read; }
186         int64_t get_samples_input() { return total_samples_input; }
187         int get_idmix() { return in51_2; }
188         int get_odmix() { return out51_2; }
189         int get_play_gain() { return play_gain; }
190         int get_record_gain() { return rec_gain; }
191 private:
192         int initialize();
193         int reset_output();
194         int reset_input();
195 // Create a lowlevel driver out of the driver ID
196         int create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in);
197         int arm_buffer(int buffer, double **output, int channels,
198                  int samples, int64_t sample_position, double bfr_time);
199         int read_inactive() {
200                 int result = !is_recording || recording_interrupted || in_config_updated;
201                 return result;
202         }
203 // Override configured parameters depending on the driver
204         int in_samplerate, out_samplerate;
205         int in_bits, out_bits;
206         int in_channels, out_channels;
207         int in_samples, out_samples;
208         int in_realtime, out_realtime;
209         int in51_2, out51_2;
210         int in_config_updated;
211
212 // open mode
213         int r, w;
214 // Video device to pass data to if the same device handles video
215         VideoDevice *vdevice;
216 // bits in output file 1 or 0
217         int rec_dither, play_dither;
218         double rec_gain, play_gain;
219         int sharing;
220         int monitoring;
221         int monitor_open;
222 // background loop for playback
223         AudioThread *audio_out;
224         void run_output();
225         void end_output();
226         void stop_output(int wait);
227 // background loop for recording
228         AudioThread *audio_in;
229         void run_input();
230         void end_input();
231         void stop_input();
232 // writes input to output, follows input config
233         void monitor_buffer(Samples **input, int channels,
234                  int samples, int ioffset, double bfr_time);
235
236         Mutex *device_lock;
237         class input_buffer_t {
238         public:
239                 char *buffer;
240                 int size;
241                 double timestamp;
242         } input[TOTAL_AUDIO_BUFFERS];
243         class output_buffer_t {
244         public:
245                 char *buffer;
246                 int allocated, size;
247                 int last_buffer;
248                 int64_t sample_position;
249                 double bfr_time;
250                 Condition *play_lock;
251                 Condition *arm_lock;
252         } output[TOTAL_AUDIO_BUFFERS];
253
254         Mutex *timer_lock;
255 // Get buffer_lock to delay before locking to allow read_buffer to lock it.
256         Mutex *buffer_lock;
257         Condition *polling_lock;
258         int arm_buffer_num;
259
260 // samples in buffer
261         double last_buffer_time;
262         int position_correction;
263         int device_buffer;
264 // prevent the counter from going backwards
265         int last_position;
266         Timer *playback_timer;
267         Timer *record_timer;
268 // Current operation
269         int is_playing_back;
270         int is_recording;
271         int global_timer_started;
272         int software_position_info;
273         int playback_interrupted;
274         int recording_interrupted;
275         int idriver, odriver;
276
277 // Multiple data paths can be opened simultaneously by Record when monitoring
278         AudioLowLevel *lowlevel_in, *lowlevel_out;
279         AudioOutConfig *out_config;
280         AudioInConfig *in_config;
281 // Extra configuration if shared with video
282         VideoInConfig *vconfig;
283
284 // Buffer being used by the hardware
285         int input_buffer_count;
286         int input_buffer_in;
287         int input_buffer_out;
288         int input_buffer_offset;
289         int output_buffer_num;
290 // for position information
291         int64_t total_samples_read;
292         int64_t total_samples_input;
293         int64_t total_samples_written;
294         int64_t total_samples_output;
295         MWindow *mwindow;
296 };
297
298
299 class AudioLowLevel
300 {
301 public:
302         AudioLowLevel(AudioDevice *device);
303         virtual ~AudioLowLevel();
304
305         virtual int open_input() { return 1; };
306         virtual int open_output() { return 1; };
307         virtual int close_all() { return 1; };
308         virtual int interrupt_crash() { return 0; };
309         virtual int64_t device_position() { return -1; };
310         virtual int64_t samples_output() { return -1; };
311         virtual int write_buffer(char *buffer, int size) { return 1; };
312         virtual int read_buffer(char *buffer, int size) { return -1; };
313         virtual int flush_device() { return 1; };
314         virtual int interrupt_playback() { return 1; };
315         virtual double device_timestamp() {
316                 struct timeval tv;  gettimeofday(&tv, 0);
317                 return tv.tv_sec + tv.tv_usec / 1000000.0;
318         }
319         virtual int start_toc(const char *path, const char *toc_path) { return -1; }
320         virtual int start_record(int fd, int bsz=0) { return -1; }
321         virtual int stop_record() { return -1; }
322         virtual int total_audio_channels() {
323                 return device ? device->get_ichannels() : 0;
324         }
325         virtual DeviceMPEGInput *mpeg_device() { return 0; }
326
327         AudioDevice *device;
328 };
329
330
331 #endif