rework set default transtion, nested proxy edl fixes, doubleclick proxy media fix...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / audioidevice.C
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 #include "audiodevice.h"
23 #include "clip.h"
24 #include "playbackconfig.h"
25 #include "recordconfig.h"
26 #include "bcprogressbox.h"
27 #include "bcsignals.h"
28 #include "bctimer.h"
29 #include "condition.h"
30 #include "dcoffset.h"
31 #include "samples.h"
32 #include "mutex.h"
33
34 #include <string.h>
35
36
37 #define STORE(k) \
38   double v = fabs(sample); \
39   if(v > 1) { ++over_count; sample = sample>0 ? 1 : -1; } \
40   if(v > max[ich]) max[ich] = v; \
41   input_channel[k] = sample
42
43 #define GET_8BIT(i) ((double)(buffer[(i)]))
44 #define GET_16BIT(i) ((double)(*(int16_t*)&buffer[(i)]))
45 #define GET_24BIT(i) (zi=(i), ((zi&1) ? \
46     ((double)((*(uint8_t*)&buffer[zi]) | (*(int16_t*)&buffer[zi+1] << 8))) : \
47     ((double)((*(uint16_t*)&buffer[zi]) | (*(int8_t*)&buffer[zi+2] << 16)))))
48 #define GET_32BIT(i) ((double)(*(int32_t *)&buffer[(i)]))
49
50 #define GET_8BITS(j,k)  { double sample = gain*GET_8BIT(k);  STORE(j); }
51 #define GET_16BITS(j,k) { double sample = gain*GET_16BIT(k); STORE(j); }
52 #define GET_24BITS(j,k) { double sample = gain*GET_24BIT(k); STORE(j); }
53 #define GET_32BITS(j,k) { double sample = gain*GET_32BIT(k); STORE(j); }
54
55 #define GET_NBIT(sz,n,k,ich) \
56   (GET_##n##BIT(k) + \
57  2*GET_##n##BIT((k)+sz*(1+(ich))) + \
58  2*GET_##n##BIT((k)+sz*(3+(ich))) + \
59    GET_##n##BIT((k)+sz*5))
60
61 #define GET_8BITZ(j,k,ich)  { double sample = gain*GET_NBIT(1,8,k,ich);  STORE(j); }
62 #define GET_16BITZ(j,k,ich) { double sample = gain*GET_NBIT(2,16,k,ich); STORE(j); }
63 #define GET_24BITZ(j,k,ich) { double sample = gain*GET_NBIT(3,24,k,ich); STORE(j); }
64 #define GET_32BITZ(j,k,ich) { double sample = gain*GET_NBIT(4,32,k,ich); STORE(j); }
65
66 int AudioDevice::read_buffer(Samples **data, int channels,
67         int samples, int *over, double *max, int input_offset)
68 {
69         for( int i=0; i<channels; ++i ) {
70                 over[i] = 0;  max[i] = 0.;
71         }
72         int input_channels = get_ichannels();
73         int map51_2 = in51_2 && channels == 2 && input_channels == 6;
74         int bits = get_ibits();
75         int frame_size = input_channels * bits / 8;
76         int fragment_size = samples * frame_size;
77         int result = !fragment_size ? 1 : 0;
78         double gain = bits ? rec_gain / ((1<<(bits-1))-1) : 0.;
79         if( map51_2 ) gain *= 0.2;
80
81         while( !result && fragment_size > 0 ) {
82                 if( (result=read_inactive()) ) break;
83                 polling_lock->lock("AudioDevice::read_buffer");
84                 if( (result=read_inactive()) ) break;
85                 if( !input_buffer_count ) continue;
86
87                 input_buffer_t *ibfr = &input[input_buffer_out];
88                 if( input_buffer_offset >= ibfr->size ) {
89                         // guarentee the buffer has finished loading
90                         if( input_buffer_in == input_buffer_out ) continue;
91                         buffer_lock->lock("AudioDevice::read_buffer 1");
92                         --input_buffer_count;
93                         buffer_lock->unlock();
94                         if( ++input_buffer_out >= TOTAL_AUDIO_BUFFERS )
95                                 input_buffer_out = 0;
96                         ibfr = &input[input_buffer_out];
97                         input_buffer_offset = 0;
98                 }
99                 char *buffer = ibfr->buffer + input_buffer_offset;
100                 int ibfr_remaining = ibfr->size - input_buffer_offset;
101
102                 int xfr_size = MIN(fragment_size, ibfr_remaining);
103                 int xfr_samples = xfr_size / frame_size;
104
105                 for( int ich=0; ich<channels; ++ich ) {
106                         int zi;
107                         int over_count = 0;
108                         double *input_channel = data[ich]->get_data() + input_offset;
109                         if( map51_2 ) {
110                                 int k = 0;
111                                 switch( bits ) {
112                                 case 8: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
113                                                 GET_8BITZ(j,k,ich)
114                                         break;
115                                 case 16: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
116                                                 GET_16BITZ(j,k,ich)
117                                         break;
118                                 case 24: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
119                                                 GET_24BITZ(j,k,ich)
120                                         break;
121                                 case 32: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
122                                                 GET_32BITZ(j,k,ich)
123                                         break;
124                                 }
125                         }
126                         else {
127                                 int k = (ich % input_channels) * bits / 8;
128                                 switch( bits) {
129                                 case 8: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
130                                                 GET_8BITS(j,k)
131                                         break;
132                                 case 16: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
133                                                 GET_16BITS(j,k)
134                                         break;
135                                 case 24: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
136                                                 GET_24BITS(j,k)
137                                         break;
138                                 case 32: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
139                                                 GET_32BITS(j,k)
140                                         break;
141                                 }
142                         }
143                         over[ich] = over_count >= 3 ? 1 : 0;
144                 }
145
146                 if( monitoring ) {
147                         int sample_offset = input_buffer_offset / frame_size;
148                         double buffer_time = ibfr->timestamp +
149                                 (double) sample_offset / in_samplerate;
150                         monitor_buffer(data, channels,
151                                 xfr_samples, input_offset, buffer_time);
152                 }
153
154                 input_offset += xfr_samples;
155                 input_buffer_offset += xfr_size;
156                 fragment_size -= xfr_size;
157         }
158
159         if( !result ) {
160                 total_samples_read += samples;
161                 record_timer->update();
162         }
163
164         return result;
165 }
166
167
168 void AudioDevice::run_input()
169 {
170         while( is_recording ) {
171 // Get available input buffer
172                 input_buffer_t *ibfr = &input[input_buffer_in];
173                 char *data = &ibfr->buffer[ibfr->size];
174                 if( !ibfr->size || ibfr->timestamp < 0. )
175                         ibfr->timestamp = lowlevel_in->device_timestamp();
176                 int frame_size = get_ichannels() * get_ibits() / 8;
177                 int fragment_size = in_samples * frame_size;
178                 int result = lowlevel_in->read_buffer(data, fragment_size);
179                 if( !result ) {
180                         total_samples_input += in_samples;
181                         buffer_lock->lock("AudioDevice::run_input 2");
182                         if( !ibfr->size )
183                                 ++input_buffer_count;
184                         ibfr->size += fragment_size;
185
186                         if( ibfr->size > INPUT_BUFFER_BYTES-fragment_size ) {
187 #if 0
188 // jam job dvb file testing, enable code
189 while( is_recording ) {
190         if( input_buffer_count < TOTAL_AUDIO_BUFFERS ) break;
191         buffer_lock->unlock();
192         Timer::delay(250);
193         buffer_lock->lock("AudioDevice::run_input 3");
194 }
195 #endif
196                                 if( input_buffer_count < TOTAL_AUDIO_BUFFERS ) {
197                                         if( ++input_buffer_in >= TOTAL_AUDIO_BUFFERS )
198                                                 input_buffer_in = 0;
199                                 }
200                                 else {
201                                         --input_buffer_count;
202                                         printf("AudioDevice::run_input: buffer overflow\n");
203                                         result = 1;
204                                 }
205                                 ibfr = &input[input_buffer_in];
206                                 ibfr->size = 0;
207                                 ibfr->timestamp = -1.;
208                         }
209                         buffer_lock->unlock();
210                         polling_lock->unlock();
211                 }
212                 if( result ) {
213                         perror("AudioDevice::run_input");
214                         usleep(250000);
215                 }
216         }
217 }
218
219 void AudioDevice::end_input()
220 {
221         is_recording = 0;
222         polling_lock->unlock();
223         buffer_lock->reset();
224 }
225
226 int AudioDevice::reset_input()
227 {
228         for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
229                 input_buffer_t *ibfr = &input[i];
230                 if( ibfr->buffer ) {
231                         delete [] ibfr->buffer;
232                         ibfr->buffer = 0;
233                 }
234                 ibfr->size = 0;
235                 ibfr->timestamp = -1.;
236         }
237         input_buffer_count = 0;
238         input_buffer_in = 0;
239         input_buffer_out = 0;
240         input_buffer_offset = 0;
241         is_recording = 0;
242         recording_interrupted = 0;
243         buffer_lock->reset();
244         polling_lock->reset();
245         monitoring = 0;
246         monitor_open = 0;
247         return 0;
248 }
249
250 void AudioDevice::start_recording()
251 {
252         reset_input();
253         is_recording = 1;
254         for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
255                 input[i].buffer = new char[INPUT_BUFFER_BYTES];
256                 input[i].size = 0;
257         }
258         record_timer->update();
259         audio_in = new AudioThread(this,
260                 &AudioDevice::run_input,&AudioDevice::end_input);
261         audio_in->set_realtime(get_irealtime());
262         audio_in->startup();
263 }
264
265 void AudioDevice::interrupt_recording()
266 {
267         recording_interrupted = 1;
268         polling_lock->unlock();
269 }
270
271 void AudioDevice::resume_recording()
272 {
273         recording_interrupted = 0;
274 }
275
276 void AudioDevice::set_rec_gain(double gain)
277 {
278         rec_gain = gain * in_config->rec_gain;
279 }
280
281 void AudioDevice::set_monitoring(int mode)
282 {
283         interrupt_playback();
284         monitoring = mode;
285         if( mode )
286                 start_playback();
287 }
288
289 void AudioDevice::monitor_buffer(Samples **data, int channels,
290          int samples, int ioffset, double bfr_time)
291 {
292         if( !monitoring || !out_config ) return;
293         if( !in_samplerate || !in_samples || !in_bits || !in_channels ) return;
294         int ochannels = out_config->map51_2 && channels == 6 ? 2 : channels;
295         if( !monitor_open
296                 /* follow input config, except for channels */
297                  || in_samplerate != out_samplerate ||
298                  in_bits != out_bits || ochannels != out_channels ||
299                  in_samples != out_samples || in_realtime != out_realtime ) {
300                 interrupt_playback();
301                 if( lowlevel_out ) {
302                         lowlevel_out->close_all();
303                         delete lowlevel_out;
304                         lowlevel_out = 0;
305                 }
306                 int ret = open_output(out_config,
307                         in_samplerate, in_samples, channels, in_realtime );
308                 monitor_open = !ret ? 1 : 0;
309                 if( monitor_open ) {
310                         start_playback();
311                         monitoring = 1;
312                 }
313         }
314         if( is_monitoring() ) {
315                 double *offset_data[channels];
316                 for( int i=0; i<channels; ++i )
317                         offset_data[i] = data[i]->get_data() + ioffset;
318                 write_buffer(offset_data, channels, samples, bfr_time);
319         }
320 }
321
322