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"
23 #include "playbackconfig.h"
24 #include "recordconfig.h"
26 #include "condition.h"
34 #define DITHER dither()
37 inline static unsigned int on_bits(unsigned int n) {
38 n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
39 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
40 n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);
41 n += n >> 8; n += n >> 16; //ok, fldsz > 5 bits
45 // assumes RAND_MAX is (2**n)-1 and n<=32 bits
46 static int random_bits = on_bits(RAND_MAX);
47 static int dither_random = 0;
48 static long dither_bits = 0;
50 static inline int dither()
52 if( --dither_bits < 0 ) {
53 dither_random = random();
54 dither_bits = random_bits;
56 return (dither_random>>dither_bits) & 1;
59 static inline int audio_clip(int64_t v, int mx)
61 if( v > mx ) return mx;
62 if( v < -mx-1 ) return -mx-1;
66 #define FETCH1(m,dither) \
67 audio_clip((output_channel[j]*gain)-dither, m)
69 #define FETCH51(m, dither) \
70 audio_clip((gain*(center[j] + 2*front[j] + \
71 2*back[j] + subwoof[j]))-dither, m)
73 #define PUT_8BIT_SAMPLES(n,dither) { \
74 sample = FETCH##n(0x7f,dither); \
75 *(int8_t*)&buffer[k] = sample; \
78 #define PUT_16BIT_SAMPLES(n,dither) { \
79 sample = FETCH##n(0x7fff,dither); \
80 *(int16_t*)&buffer[k] = sample; \
83 #define PUT_24BIT_SAMPLES(n,dither) { \
84 sample = FETCH##n(0x7fffff,dither); \
86 *(int8_t*)&buffer[k] = sample; \
87 *(int16_t*)&buffer[k+1] = (sample >> 8); \
89 *(int16_t*)&buffer[k] = sample; \
90 *(int8_t*)&buffer[k+2] = (sample >> 16); \
94 #define PUT_32BIT_SAMPLES(n,dither) { \
95 sample = FETCH##n(0x7fffffff,dither); \
96 *(int32_t*)&buffer[k] = sample; \
99 int AudioDevice::write_buffer(double **data,
100 int channels, int samples, double bfr_time)
102 // find free buffer to fill
103 if(playback_interrupted) return 0;
104 int64_t sample_position = total_samples_written;
105 total_samples_written += samples;
106 int buffer_num = arm_buffer_num;
107 if( ++arm_buffer_num >= TOTAL_AUDIO_BUFFERS ) arm_buffer_num = 0;
108 arm_buffer(buffer_num, data, channels, samples,
109 sample_position, bfr_time);
113 int AudioDevice::set_last_buffer()
115 output_buffer_t *obfr = &output[arm_buffer_num];
116 if( !obfr ) return 0;
117 if( ++arm_buffer_num >= TOTAL_AUDIO_BUFFERS ) arm_buffer_num = 0;
118 obfr->arm_lock->lock("AudioDevice::set_last_buffer");
119 obfr->last_buffer = 1;
120 obfr->play_lock->unlock();
124 // little endian byte order
125 int AudioDevice::arm_buffer(int buffer_num, double **data, int channels,
126 int samples, int64_t sample_position, double bfr_time)
128 if(!is_playing_back || playback_interrupted) return 1;
129 // wait for buffer to become available for writing
130 output_buffer_t *obfr = &output[buffer_num];
131 obfr->arm_lock->lock("AudioDevice::arm_buffer");
132 if(!is_playing_back || playback_interrupted) return 1;
134 int bits = get_obits();
135 int device_channels = get_ochannels();
136 int frame = device_channels * (bits / 8);
137 int fragment_size = frame * samples;
138 if( fragment_size > obfr->allocated ) {
139 delete [] obfr->buffer;
140 obfr->buffer = new char[fragment_size];
141 obfr->allocated = fragment_size;
144 char *buffer = obfr->buffer;
145 obfr->size = fragment_size;
146 obfr->bfr_time = bfr_time;
147 obfr->sample_position = sample_position;
148 int map51_2 = out51_2 && channels == 6 && device_channels == 2;
149 double gain = ((1u<<(bits-1))-1) * play_gain;
150 if( map51_2 ) gain *= 0.2;
152 for( int och=0; och<device_channels; ++och ) {
154 int k = och * bits / 8;
157 double *front = data[och];
158 double *center = data[2];
159 double *subwoof = data[3];
160 double *back = data[och+4];
163 case 8: for( int j=0; j<samples; ++j,k+=frame )
164 PUT_8BIT_SAMPLES(51,DITHER)
166 case 16: for( int j=0; j<samples; ++j,k+=frame )
167 PUT_16BIT_SAMPLES(51,DITHER)
169 case 24: for( int j=0; j<samples; ++j,k+=frame )
170 PUT_24BIT_SAMPLES(51,DITHER)
172 case 32: for( int j=0; j<samples; ++j,k+=frame )
173 PUT_32BIT_SAMPLES(51,DITHER)
179 case 8: for( int j=0; j<samples; ++j,k+=frame )
180 PUT_8BIT_SAMPLES(51,NO_DITHER)
182 case 16: for( int j=0; j<samples; ++j,k+=frame )
183 PUT_16BIT_SAMPLES(51,NO_DITHER)
185 case 24: for( int j=0; j<samples; ++j,k+=frame )
186 PUT_24BIT_SAMPLES(51,NO_DITHER)
188 case 32: for( int j=0; j<samples; ++j,k+=frame )
189 PUT_32BIT_SAMPLES(51,NO_DITHER)
195 double *output_channel = data[och % channels];
198 case 8: for( int j=0; j<samples; ++j,k+=frame )
199 PUT_8BIT_SAMPLES(1,DITHER)
201 case 16: for( int j=0; j<samples; ++j,k+=frame )
202 PUT_16BIT_SAMPLES(1,DITHER)
204 case 24: for( int j=0; j<samples; ++j,k+=frame )
205 PUT_24BIT_SAMPLES(1,DITHER)
207 case 32: for( int j=0; j<samples; ++j,k+=frame )
208 PUT_32BIT_SAMPLES(1,DITHER)
214 case 8: for( int j=0; j<samples; ++j,k+=frame )
215 PUT_8BIT_SAMPLES(1,NO_DITHER)
217 case 16: for( int j=0; j<samples; ++j,k+=frame )
218 PUT_16BIT_SAMPLES(1,NO_DITHER)
220 case 24: for( int j=0; j<samples; ++j,k+=frame )
221 PUT_24BIT_SAMPLES(1,NO_DITHER)
223 case 32: for( int j=0; j<samples; ++j,k+=frame )
224 PUT_32BIT_SAMPLES(1,NO_DITHER)
230 // make buffer available for playback
231 obfr->play_lock->unlock();
235 void AudioDevice::end_output()
240 lowlevel_out->interrupt_playback();
241 for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
242 output_buffer_t *obfr = &output[i];
243 obfr->play_lock->unlock();
244 obfr->arm_lock->unlock();
248 int AudioDevice::reset_output()
250 for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
251 output_buffer_t *obfr = &output[i];
253 delete [] obfr->buffer;
258 obfr->last_buffer = 0;
259 obfr->arm_lock->reset();
260 obfr->play_lock->reset();
263 playback_interrupted = 0;
266 output_buffer_num = 0;
273 void AudioDevice::set_play_gain(double gain)
275 play_gain = gain * out_config->play_gain;
278 void AudioDevice::set_play_dither(int status)
280 play_dither = status;
283 void AudioDevice::set_software_positioning(int status)
285 software_position_info = status;
288 int AudioDevice::start_playback()
290 // arm buffer before doing this
292 playback_interrupted = 0;
293 total_samples_written = 0;
294 total_samples_output = 0;
296 playback_timer->update();
298 // start output thread
299 audio_out = new AudioThread(this,
300 &AudioDevice::run_output,&AudioDevice::end_output);
301 audio_out->set_realtime(get_orealtime());
302 audio_out->startup();
306 int AudioDevice::interrupt_playback()
308 //printf("AudioDevice::interrupt_playback\n");
313 int64_t AudioDevice::samples_output()
315 int64_t result = !lowlevel_out ? -1 : lowlevel_out->samples_output();
316 if( result < 0 ) result = total_samples_output;
321 int64_t AudioDevice::current_position()
323 // try to get OSS position
327 int frame = (get_obits() * get_ochannels()) / 8;
328 if( !frame ) return 0;
330 // get hardware position
331 if(!software_position_info && lowlevel_out) {
332 result = lowlevel_out->device_position();
333 int64_t sample_count = samples_output();
334 if( result > sample_count )
335 result = sample_count;
338 // get software position
339 if(result < 0 || software_position_info) {
340 timer_lock->lock("AudioDevice::current_position");
341 result = total_samples_output - device_buffer / frame;
342 result += playback_timer->get_scaled_difference(get_orate());
343 timer_lock->unlock();
344 int64_t sample_count = samples_output();
345 if( result > sample_count )
346 result = sample_count;
349 if(result < last_position)
350 result = last_position;
352 last_position = result;
354 result -= (int64_t)(get_orate() * out_config->audio_offset);
358 result = total_samples_read +
359 record_timer->get_scaled_difference(get_irate());
365 void AudioDevice::run_output()
367 output_buffer_num = 0;
368 playback_timer->update();
370 //printf("AudioDevice::run 1 %d\n", Thread::calculate_realtime());
371 while( is_playing_back && !playback_interrupted ) {
372 // wait for buffer to become available
373 int buffer_num = output_buffer_num;
374 if( ++output_buffer_num >= TOTAL_AUDIO_BUFFERS ) output_buffer_num = 0;
375 output_buffer_t *obfr = &output[buffer_num];
376 obfr->play_lock->lock("AudioDevice::run 1");
377 if( !is_playing_back || playback_interrupted ) break;
378 if( obfr->last_buffer ) { lowlevel_out->flush_device(); break; }
379 // get size for position information
380 // write converted buffer synchronously
381 double bfr_time = obfr->bfr_time;
382 int result = lowlevel_out->write_buffer(obfr->buffer, obfr->size);
383 // allow writing to the buffer
384 obfr->arm_lock->unlock();
386 timer_lock->lock("AudioDevice::run 3");
387 int frame = get_ochannels() * get_obits() / 8;
388 int samples = frame ? obfr->size / frame : 0;
389 total_samples_output += samples;
390 last_buffer_time = bfr_time + (double)samples/out_samplerate;
391 playback_timer->update();
392 timer_lock->unlock();
394 // inform user if the buffer write failed
395 else if( result < 0 ) {
396 perror("AudioDevice::write_buffer");