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) (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)]))
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 ) {
108 double *input_channel = data[ich]->get_data() + input_offset;
112 case 8: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
115 case 16: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
118 case 24: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
121 case 32: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
127 int k = (ich % input_channels) * bits / 8;
129 case 8: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
132 case 16: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
135 case 24: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
138 case 32: for(int j=0; j<xfr_samples; ++j,k+=frame_size)
143 over[ich] = over_count >= 3 ? 1 : 0;
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);
154 input_offset += xfr_samples;
155 input_buffer_offset += xfr_size;
156 fragment_size -= xfr_size;
160 total_samples_read += samples;
161 record_timer->update();
168 void AudioDevice::run_input()
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);
180 total_samples_input += in_samples;
181 buffer_lock->lock("AudioDevice::run_input 2");
183 ++input_buffer_count;
184 ibfr->size += fragment_size;
186 if( ibfr->size > INPUT_BUFFER_BYTES-fragment_size ) {
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();
193 buffer_lock->lock("AudioDevice::run_input 3");
196 if( input_buffer_count < TOTAL_AUDIO_BUFFERS ) {
197 if( ++input_buffer_in >= TOTAL_AUDIO_BUFFERS )
201 --input_buffer_count;
202 printf("AudioDevice::run_input: buffer overflow\n");
205 ibfr = &input[input_buffer_in];
207 ibfr->timestamp = -1.;
209 buffer_lock->unlock();
210 polling_lock->unlock();
213 perror("AudioDevice::run_input");
219 void AudioDevice::end_input()
222 polling_lock->unlock();
223 buffer_lock->reset();
226 int AudioDevice::reset_input()
228 for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
229 input_buffer_t *ibfr = &input[i];
231 delete [] ibfr->buffer;
235 ibfr->timestamp = -1.;
237 input_buffer_count = 0;
239 input_buffer_out = 0;
240 input_buffer_offset = 0;
242 recording_interrupted = 0;
243 buffer_lock->reset();
244 polling_lock->reset();
250 void AudioDevice::start_recording()
254 for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
255 input[i].buffer = new char[INPUT_BUFFER_BYTES];
258 record_timer->update();
259 audio_in = new AudioThread(this,
260 &AudioDevice::run_input,&AudioDevice::end_input);
261 audio_in->set_realtime(get_irealtime());
265 void AudioDevice::interrupt_recording()
267 recording_interrupted = 1;
268 polling_lock->unlock();
271 void AudioDevice::resume_recording()
273 recording_interrupted = 0;
276 void AudioDevice::set_rec_gain(double gain)
278 rec_gain = gain * in_config->rec_gain;
281 void AudioDevice::set_monitoring(int mode)
283 interrupt_playback();
289 void AudioDevice::monitor_buffer(Samples **data, int channels,
290 int samples, int ioffset, double bfr_time)
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;
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();
302 lowlevel_out->close_all();
306 int ret = open_output(out_config,
307 in_samplerate, in_samples, channels, in_realtime );
308 monitor_open = !ret ? 1 : 0;
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);