4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "audiodevice.h"
24 #include "playbackconfig.h"
25 #include "recordconfig.h"
26 #include "bcprogressbox.h"
27 #include "bcsignals.h"
29 #include "condition.h"
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
43 #define GET_8BIT(i) ((double)(buffer[(i)]))
44 #define GET_16BIT(i) ((double)(*(int16_t*)&buffer[(i)]))
45 #define GET_24BIT(i) ((i&1) ? \
46 ((double)((*(uint8_t*)&buffer[i]) | (*(int16_t*)&buffer[i+1] << 8))) : \
47 ((double)((*(uint16_t*)&buffer[i]) | (*(int8_t*)&buffer[i+2] << 16))))
48 #define GET_32BIT(i) ((double)(*(int32_t *)&buffer[(i)]))
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); }
55 #define GET_NBIT(sz,n,k,ich) \
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))
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); }
66 int AudioDevice::read_buffer(Samples **data, int channels,
67 int samples, int *over, double *max, int input_offset)
69 for( int i=0; i<channels; ++i ) {
70 over[i] = 0; max[i] = 0.;
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;
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;
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");
93 buffer_lock->unlock();
94 if( ++input_buffer_out >= TOTAL_AUDIO_BUFFERS )
96 ibfr = &input[input_buffer_out];
97 input_buffer_offset = 0;
99 char *buffer = ibfr->buffer + input_buffer_offset;
100 int ibfr_remaining = ibfr->size - input_buffer_offset;
102 int xfr_size = MIN(fragment_size, ibfr_remaining);
103 int xfr_samples = xfr_size / frame_size;
105 for( int ich=0; ich<channels; ++ich ) {
107 double *input_channel = data[ich]->get_data() + input_offset;
111 case 8: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
114 case 16: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
117 case 24: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
120 case 32: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
126 int k = (ich % input_channels) * bits / 8;
128 case 8: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
131 case 16: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
134 case 24: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
137 case 32: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
142 over[ich] = over_count >= 3 ? 1 : 0;
146 int sample_offset = input_buffer_offset / frame_size;
147 double buffer_time = ibfr->timestamp +
148 (double) sample_offset / in_samplerate;
149 monitor_buffer(data, channels,
150 xfr_samples, input_offset, buffer_time);
153 input_offset += xfr_samples;
154 input_buffer_offset += xfr_size;
155 fragment_size -= xfr_size;
159 total_samples_read += samples;
160 record_timer->update();
167 void AudioDevice::run_input()
169 while( is_recording ) {
170 // Get available input buffer
171 input_buffer_t *ibfr = &input[input_buffer_in];
172 char *data = &ibfr->buffer[ibfr->size];
173 if( !ibfr->size || ibfr->timestamp < 0. )
174 ibfr->timestamp = lowlevel_in->device_timestamp();
175 int frame_size = get_ichannels() * get_ibits() / 8;
176 int fragment_size = in_samples * frame_size;
177 int result = lowlevel_in->read_buffer(data, fragment_size);
179 total_samples_input += in_samples;
180 buffer_lock->lock("AudioDevice::run_input 2");
182 ++input_buffer_count;
183 ibfr->size += fragment_size;
185 if( ibfr->size > INPUT_BUFFER_BYTES-fragment_size ) {
187 // jam job dvb file testing, enable code
188 while( is_recording ) {
189 if( input_buffer_count < TOTAL_AUDIO_BUFFERS ) break;
190 buffer_lock->unlock();
192 buffer_lock->lock("AudioDevice::run_input 3");
195 if( input_buffer_count < TOTAL_AUDIO_BUFFERS ) {
196 if( ++input_buffer_in >= TOTAL_AUDIO_BUFFERS )
200 --input_buffer_count;
201 printf("AudioDevice::run_input: buffer overflow\n");
204 ibfr = &input[input_buffer_in];
206 ibfr->timestamp = -1.;
208 buffer_lock->unlock();
209 polling_lock->unlock();
212 perror("AudioDevice::run_input");
218 void AudioDevice::end_input()
221 polling_lock->unlock();
222 buffer_lock->reset();
225 int AudioDevice::reset_input()
227 for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
228 input_buffer_t *ibfr = &input[i];
230 delete [] ibfr->buffer;
234 ibfr->timestamp = -1.;
236 input_buffer_count = 0;
238 input_buffer_out = 0;
239 input_buffer_offset = 0;
241 recording_interrupted = 0;
242 buffer_lock->reset();
243 polling_lock->reset();
249 void AudioDevice::start_recording()
253 for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
254 input[i].buffer = new char[INPUT_BUFFER_BYTES];
257 record_timer->update();
258 audio_in = new AudioThread(this,
259 &AudioDevice::run_input,&AudioDevice::end_input);
260 audio_in->set_realtime(get_irealtime());
264 void AudioDevice::interrupt_recording()
266 recording_interrupted = 1;
267 polling_lock->unlock();
270 void AudioDevice::resume_recording()
272 recording_interrupted = 0;
275 void AudioDevice::set_rec_gain(double gain)
277 rec_gain = gain * in_config->rec_gain;
280 void AudioDevice::set_monitoring(int mode)
282 interrupt_playback();
288 void AudioDevice::monitor_buffer(Samples **data, int channels,
289 int samples, int ioffset, double bfr_time)
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;
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();
301 lowlevel_out->close_all();
305 int ret = open_output(out_config,
306 in_samplerate, in_samples, channels, in_realtime );
307 monitor_open = !ret ? 1 : 0;
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);