r668: Configure.in and autogen.sh cleanup based on ideas by giskard.
[cinelerra_cv.git] / cinelerra / audiodevice.h
blob760baba0a22be430d0e7466769f1ec5bc5b50f94
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 "audio1394.inc"
11 #include "audioalsa.inc"
12 #include "audioconfig.inc"
13 #include "audiodevice.inc"
14 #include "audioesound.inc"
15 #include "audiooss.inc"
16 #include "bctimer.h"
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 "preferences.inc"
24 #include "recordgui.inc"
25 #include "sema.inc"
26 #include "thread.h"
27 #include "vdevice1394.inc"
28 #include "videodevice.inc"
30 class AudioLowLevel
32 public:
33 AudioLowLevel(AudioDevice *device);
34 virtual ~AudioLowLevel();
36 virtual int open_input() { return 1; };
37 virtual int open_output() { return 1; };
38 virtual int open_duplex() { return 1; };
39 virtual int close_all() { return 1; };
40 virtual int interrupt_crash() { return 0; };
41 virtual int64_t device_position() { return -1; };
42 virtual int write_buffer(char *buffer, int size) { return 1; };
43 virtual int read_buffer(char *buffer, int size) { return 1; };
44 virtual int flush_device() { return 1; };
45 virtual int interrupt_playback() { return 1; };
47 AudioDevice *device;
51 class AudioDevice : public Thread
53 public:
54 AudioDevice();
55 ~AudioDevice();
57 friend class AudioALSA;
58 friend class AudioOSS;
59 friend class AudioESound;
60 friend class Audio1394;
61 friend class VDevice1394;
62 friend class Device1394Output;
64 int open_input(AudioInConfig *config,
65 VideoInConfig *vconfig,
66 int rate,
67 int samples);
68 int open_output(AudioOutConfig *config, int rate, int samples, int realtime);
69 int open_duplex(AudioOutConfig *config, int rate, int samples, int realtime);
70 int close_all();
71 int reset_output();
72 int restart();
74 // Specify a video device to pass data to if the same device handles video
75 int set_vdevice(VideoDevice *vdevice);
77 // ================================ recording
79 // read from the record device
80 // Conversion between double and int is done in AudioDevice
81 int read_buffer(double **input,
82 int samples,
83 int channels,
84 int *over,
85 double *max,
86 int input_offset = 0);
87 int set_record_dither(int value);
89 int stop_recording();
90 // If a firewire device crashed
91 int interrupt_crash();
93 // ================================== dc offset
95 // get and set offset
96 int get_dc_offset(int *output, RecordGUIDCOffsetText **dc_offset_text);
97 // set new offset
98 int set_dc_offset(int dc_offset, int channel);
99 // writes to whichever buffer is free or blocks until one becomes free
100 int write_buffer(double **output, int samples, int channels = -1);
102 // play back buffers
103 void run();
105 // After the last buffer is written call this to terminate.
106 // A separate buffer for termination is required since the audio device can be
107 // finished without writing a single byte
108 int set_last_buffer();
110 int wait_for_startup();
111 // wait for the playback thread to clean up
112 int wait_for_completion();
114 // start the thread processing buffers
115 int start_playback();
116 // interrupt the playback thread
117 int interrupt_playback();
118 int set_play_dither(int status);
119 // set software positioning on or off
120 int set_software_positioning(int status = 1);
121 // total samples played
122 // + audio offset from configuration if playback
123 int64_t current_position();
124 // If interrupted
125 int get_interrupted();
126 int get_device_buffer();
127 // Used by video devices to share audio devices
128 AudioLowLevel* get_lowlevel_out();
129 AudioLowLevel* get_lowlevel_in();
131 private:
132 int initialize();
133 // Create a lowlevel driver out of the driver ID
134 int create_lowlevel(AudioLowLevel* &lowlevel, int driver);
135 int arm_buffer(int buffer, double **output, int samples, int channels);
136 int get_obits();
137 int get_ochannels();
138 int get_ibits();
139 int get_ichannels();
140 int get_orate();
141 int get_irate();
142 int get_orealtime();
144 DC_Offset *dc_offset_thread;
145 // Override configured parameters depending on the driver
146 int in_samplerate, in_bits, in_channels, in_samples;
147 int out_samplerate, out_bits, out_channels, out_samples;
148 int duplex_samplerate, duplex_bits, duplex_channels, duplex_samples;
149 int out_realtime, duplex_realtime;
151 // Access mode
152 int r, w, d;
153 // Samples per buffer
154 int osamples, isamples, dsamples;
155 // Video device to pass data to if the same device handles video
156 VideoDevice *vdevice;
157 // OSS < 3.9 --> playback before recording
158 // OSS >= 3.9 --> doesn't matter
159 // Got better synchronization by starting playback first
160 int record_before_play;
161 Condition *duplex_lock;
162 Condition *startup_lock;
163 // notify playback routines to test the duplex lock
164 int duplex_init;
165 // bits in output file
166 int rec_dither;
167 // 1 or 0
168 int play_dither;
169 int sharing;
171 int buffer_size[TOTAL_BUFFERS];
172 int last_buffer[TOTAL_BUFFERS]; // not written to device
173 // formatted buffers for output
174 char *buffer[TOTAL_BUFFERS], *input_buffer;
175 Sema *play_lock[TOTAL_BUFFERS];
176 Sema *arm_lock[TOTAL_BUFFERS];
177 Mutex *timer_lock;
178 int arm_buffer_num;
180 // for position information
181 int total_samples, last_buffer_size, position_correction;
182 int device_buffer;
183 int last_position; // prevent the counter from going backwards
184 Timer playback_timer, record_timer;
185 // Current operation
186 int is_playing_back, is_recording, global_timer_started, software_position_info;
187 int interrupt;
188 int driver;
190 // Multiple data paths can be opened simultaneously by RecordEngine
191 AudioLowLevel *lowlevel_in, *lowlevel_out, *lowlevel_duplex;
193 AudioOutConfig *out_config;
194 AudioInConfig *in_config;
195 // Extra configuration if shared with video
196 VideoInConfig *vconfig;
198 int thread_buffer_num, thread_result;
199 int64_t total_samples_read;
204 #endif