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