r370: Heroine Virutal's official release 1.2.1
[cinelerra_cv/mob.git] / hvirtual / cinelerra / audiodevice.h
blobd471427c358e66dc913e835fec9be3e53fee33f0
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 int64_t current_position();
123 // If interrupted
124 int get_interrupted();
125 int get_device_buffer();
126 // Used by video devices to share audio devices
127 AudioLowLevel* get_lowlevel_out();
128 AudioLowLevel* get_lowlevel_in();
130 private:
131 int initialize();
132 // Create a lowlevel driver out of the driver ID
133 int create_lowlevel(AudioLowLevel* &lowlevel, int driver);
134 int arm_buffer(int buffer, double **output, int samples, int channels);
135 int get_obits();
136 int get_ochannels();
137 int get_ibits();
138 int get_ichannels();
139 int get_orate();
140 int get_irate();
141 int get_orealtime();
143 DC_Offset *dc_offset_thread;
144 // Override configured parameters depending on the driver
145 int in_samplerate, in_bits, in_channels, in_samples;
146 int out_samplerate, out_bits, out_channels, out_samples;
147 int duplex_samplerate, duplex_bits, duplex_channels, duplex_samples;
148 int out_realtime, duplex_realtime;
150 // Access mode
151 int r, w, d;
152 // Samples per buffer
153 int osamples, isamples, dsamples;
154 // Video device to pass data to if the same device handles video
155 VideoDevice *vdevice;
156 // OSS < 3.9 --> playback before recording
157 // OSS >= 3.9 --> doesn't matter
158 // Got better synchronization by starting playback first
159 int record_before_play;
160 Condition *duplex_lock;
161 Condition *startup_lock;
162 // notify playback routines to test the duplex lock
163 int duplex_init;
164 // bits in output file
165 int rec_dither;
166 // 1 or 0
167 int play_dither;
168 int sharing;
170 int buffer_size[TOTAL_BUFFERS];
171 int last_buffer[TOTAL_BUFFERS]; // not written to device
172 // formatted buffers for output
173 char *buffer[TOTAL_BUFFERS], *input_buffer;
174 Sema *play_lock[TOTAL_BUFFERS];
175 Sema *arm_lock[TOTAL_BUFFERS];
176 Mutex *timer_lock;
177 int arm_buffer_num;
179 // for position information
180 int total_samples, last_buffer_size, position_correction;
181 int device_buffer;
182 int last_position; // prevent the counter from going backwards
183 Timer playback_timer, record_timer;
184 // Current operation
185 int is_playing_back, is_recording, global_timer_started, software_position_info;
186 int interrupt;
187 int driver;
189 // Multiple data paths can be opened simultaneously by RecordEngine
190 AudioLowLevel *lowlevel_in, *lowlevel_out, *lowlevel_duplex;
192 AudioOutConfig *out_config;
193 AudioInConfig *in_config;
194 // Extra configuration if shared with video
195 VideoInConfig *vconfig;
197 private:
198 int thread_buffer_num, thread_result;
199 int64_t total_samples_read;
204 #endif