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