r1053: Add Russian translation.
[cinelerra_cv.git] / cinelerra / audiodevice.h
blobc25411be8712846d638116d9dd5876f66188b54d
1 #ifndef AUDIODEVICE_H
2 #define AUDIODEVICE_H
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/ioctl.h>
8 #include <unistd.h>
10 #include "audioalsa.inc"
11 #include "audioconfig.inc"
12 #include "audiodvb.inc"
13 #include "audiodevice.inc"
14 #include "audioesound.inc"
15 #include "audiooss.inc"
16 #include "bctimer.inc"
17 #include "binary.h"
18 #include "condition.inc"
19 #include "dcoffset.inc"
20 #include "device1394output.inc"
21 #include "maxchannels.h"
22 #include "mutex.inc"
23 #include "mwindow.inc"
24 #include "preferences.inc"
25 #include "recordgui.inc"
26 #include "sema.inc"
27 #include "thread.h"
28 #include "videodevice.inc"
29 #ifdef HAVE_FIREWIRE
30 #include "audio1394.inc"
31 #include "device1394output.inc"
32 #include "vdevice1394.inc"
33 #endif
35 class AudioLowLevel
37 public:
38 AudioLowLevel(AudioDevice *device);
39 virtual ~AudioLowLevel();
41 virtual int open_input() { return 1; };
42 virtual int open_output() { return 1; };
43 virtual int open_duplex() { return 1; };
44 virtual int close_all() { return 1; };
45 virtual int interrupt_crash() { return 0; };
46 virtual int64_t device_position() { return -1; };
47 virtual int write_buffer(char *buffer, int size) { return 1; };
48 virtual int read_buffer(char *buffer, int size) { return 1; };
49 virtual int flush_device() { return 1; };
50 virtual int interrupt_playback() { return 1; };
52 AudioDevice *device;
56 class AudioDevice : public Thread
58 public:
59 // MWindow is required where global input is used, to get the pointer.
60 AudioDevice(MWindow *mwindow = 0);
61 ~AudioDevice();
63 friend class AudioALSA;
64 friend class AudioDVB;
65 friend class AudioOSS;
66 friend class AudioESound;
67 friend class Audio1394;
68 friend class VDevice1394;
69 friend class Device1394Output;
71 int open_input(AudioInConfig *config,
72 VideoInConfig *vconfig,
73 int rate,
74 int samples,
75 int channels,
76 int realtime);
77 int open_output(AudioOutConfig *config,
78 int rate,
79 int samples,
80 int channels,
81 int realtime);
82 int close_all();
83 int reset_output();
84 int restart();
86 // Specify a video device to pass data to if the same device handles video
87 int set_vdevice(VideoDevice *vdevice);
89 // ================================ recording
91 // read from the record device
92 // Conversion between double and int is done in AudioDevice
93 int read_buffer(double **input,
94 int samples,
95 int *over,
96 double *max,
97 int input_offset = 0);
98 int set_record_dither(int value);
100 void start_recording();
101 int stop_recording();
102 // If a firewire device crashed
103 int interrupt_crash();
105 // ================================== dc offset
107 // writes to whichever buffer is free or blocks until one becomes free
108 int write_buffer(double **output, int samples);
110 // background loop for buffering
111 void run();
112 // background loop for playback
113 void run_output();
114 // background loop for recording
115 void run_input();
117 // After the last buffer is written call this to terminate.
118 // A separate buffer for termination is required since the audio device can be
119 // finished without writing a single byte
120 int set_last_buffer();
122 int wait_for_startup();
123 // wait for the playback thread to clean up
124 int wait_for_completion();
126 // start the thread processing buffers
127 int start_playback();
131 // interrupt the playback thread
132 int interrupt_playback();
133 int set_play_dither(int status);
134 // set software positioning on or off
135 int set_software_positioning(int status = 1);
136 // total samples played
137 // + audio offset from configuration if playback
138 int64_t current_position();
139 // If interrupted
140 int get_interrupted();
141 int get_device_buffer();
142 // Used by video devices to share audio devices
143 AudioLowLevel* get_lowlevel_out();
144 AudioLowLevel* get_lowlevel_in();
146 private:
147 int initialize();
148 // Create a lowlevel driver out of the driver ID
149 int create_lowlevel(AudioLowLevel* &lowlevel, int driver);
150 int arm_buffer(int buffer, double **output, int samples);
151 int get_obits();
152 int get_ochannels();
153 int get_ibits();
154 int get_ichannels();
155 int get_orate();
156 int get_irate();
157 int get_orealtime();
158 int get_irealtime();
160 // Override configured parameters depending on the driver
161 int in_samplerate, in_bits, in_channels, in_samples;
162 int in_realtime;
163 int out_samplerate, out_bits, out_channels, out_samples;
164 int duplex_samplerate, duplex_bits, duplex_channels, duplex_samples;
165 int out_realtime, duplex_realtime;
167 // Access mode
168 int r, w, d;
169 // Samples per buffer
170 int osamples, isamples, dsamples;
171 // Video device to pass data to if the same device handles video
172 VideoDevice *vdevice;
173 // OSS < 3.9 --> playback before recording
174 // OSS >= 3.9 --> doesn't matter
175 // Got better synchronization by starting playback first
176 int record_before_play;
177 Condition *duplex_lock;
178 Condition *startup_lock;
179 // notify playback routines to test the duplex lock
180 int duplex_init;
181 // bits in output file
182 int rec_dither;
183 // 1 or 0
184 int play_dither;
185 int sharing;
187 // bytes in buffer
188 int buffer_size[TOTAL_BUFFERS];
189 int last_buffer[TOTAL_BUFFERS]; // not written to device
190 // formatted buffers for reading and writing the soundcard
191 char *output_buffer[TOTAL_BUFFERS];
192 char *input_buffer[TOTAL_BUFFERS];
193 Sema *play_lock[TOTAL_BUFFERS];
194 Sema *arm_lock[TOTAL_BUFFERS];
195 Mutex *timer_lock;
196 // Get buffer_lock to delay before locking to allow read_buffer to lock it.
197 int read_waiting;
198 Mutex *buffer_lock;
199 Condition *polling_lock;
200 int arm_buffer_num;
202 // for position information
203 int total_samples;
204 // samples in buffer
205 int last_buffer_size;
206 int position_correction;
207 int device_buffer;
208 // prevent the counter from going backwards
209 int last_position;
210 Timer *playback_timer;
211 Timer *record_timer;
212 // Current operation
213 int is_playing_back;
214 int is_recording;
215 int global_timer_started;
216 int software_position_info;
217 int interrupt;
218 int driver;
220 // Multiple data paths can be opened simultaneously by RecordEngine
221 AudioLowLevel *lowlevel_in, *lowlevel_out, *lowlevel_duplex;
223 AudioOutConfig *out_config;
224 AudioInConfig *in_config;
225 // Extra configuration if shared with video
226 VideoInConfig *vconfig;
228 // Buffer being used by the hardware
229 int thread_buffer_num;
230 int thread_result;
231 int64_t total_samples_read;
232 MWindow *mwindow;
237 #endif