prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / 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                                 }
204                                 ibfr = &input[input_buffer_in];
205                                 ibfr->size = 0;
206                                 ibfr->timestamp = -1.;
207                         }
208                         buffer_lock->unlock();
209                         polling_lock->unlock();
210                 }
211                 else {
212                         perror("AudioDevice::run_input");
213                         usleep(250000);
214                 }
215         }
216 }
217
218 void AudioDevice::end_input()
219 {
220         is_recording = 0;
221         polling_lock->unlock();
222         buffer_lock->unlock();
223 }
224
225 int AudioDevice::reset_input()
226 {
227         for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
228                 input_buffer_t *ibfr = &input[i];
229                 if( ibfr->buffer ) {
230                         delete [] ibfr->buffer;
231                         ibfr->buffer = 0;
232                 }
233                 ibfr->size = 0;
234                 ibfr->timestamp = -1.;
235         }
236         input_buffer_count = 0;
237         input_buffer_in = 0;
238         input_buffer_out = 0;
239         input_buffer_offset = 0;
240         is_recording = 0;
241         recording_interrupted = 0;
242         buffer_lock->reset();
243         polling_lock->reset();
244         monitoring = 0;
245         monitor_open = 0;
246         return 0;
247 }
248
249 void AudioDevice::start_recording()
250 {
251         reset_input();
252         is_recording = 1;
253         for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
254                 input[i].buffer = new char[INPUT_BUFFER_BYTES];
255                 input[i].size = 0;
256         }
257         record_timer->update();
258         audio_in = new AudioThread(this,
259                 &AudioDevice::run_input,&AudioDevice::end_input);
260         audio_in->set_realtime(get_irealtime());
261         audio_in->startup();
262 }
263
264 void AudioDevice::interrupt_recording()
265 {
266         recording_interrupted = 1;
267         polling_lock->unlock();
268 }
269
270 void AudioDevice::resume_recording()
271 {
272         recording_interrupted = 0;
273 }
274
275 void AudioDevice::set_rec_gain(double gain)
276 {
277         rec_gain = gain * in_config->rec_gain;
278 }
279
280 void AudioDevice::set_monitoring(int mode)
281 {
282         interrupt_playback();
283         monitoring = mode;
284         if( mode )
285                 start_playback();
286 }
287
288 void AudioDevice::monitor_buffer(Samples **data, int channels,
289          int samples, int ioffset, double bfr_time)
290 {
291         if( !monitoring || !out_config ) return;
292         if( !in_samplerate || !in_samples || !in_bits || !in_channels ) return;
293         int ochannels = out_config->map51_2 && channels == 6 ? 2 : channels;
294         if( !monitor_open
295                 /* follow input config, except for channels */
296                  || in_samplerate != out_samplerate ||
297                  in_bits != out_bits || ochannels != out_channels ||
298                  in_samples != out_samples || in_realtime != out_realtime ) {
299                 interrupt_playback();
300                 if( lowlevel_out ) {
301                         lowlevel_out->close_all();
302                         delete lowlevel_out;
303                         lowlevel_out = 0;
304                 }
305                 int ret = open_output(out_config, 
306                         in_samplerate, in_samples, channels, in_realtime );
307                 monitor_open = !ret ? 1 : 0;
308                 if( monitor_open ) {
309                         start_playback();
310                         monitoring = 1;
311                 }
312         }
313         if( is_monitoring() ) {
314                 double *offset_data[channels];
315                 for( int i=0; i<channels; ++i )
316                         offset_data[i] = data[i]->get_data() + ioffset;
317                 write_buffer(offset_data, channels, samples, bfr_time);
318         }
319 }
320
321