Credit Andrew R for finding the direct copy mods for exr and ppm sequences
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / audioodevice.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 "playbackconfig.h"
24 #include "recordconfig.h"
25 #include "bctimer.h"
26 #include "condition.h"
27 #include "mutex.h"
28 #include "samples.h"
29 #include "sema.h"
30
31 #include <string.h>
32
33 #define NO_DITHER 0
34 #define DITHER dither()
35
36 // count of on bits
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
42   return n & 0x1f;
43 }
44
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;
49
50 static inline int dither()
51 {
52         if( --dither_bits < 0 ) {
53                 dither_random = random();
54                 dither_bits = random_bits;
55         }
56         return (dither_random>>dither_bits) & 1;
57 }
58
59 static inline int audio_clip(int64_t v, int mx)
60 {
61   if( v > mx ) return mx;
62   if( v < -mx-1 ) return -mx-1;
63   return v;
64 }
65
66 #define FETCH1(m,dither) \
67   audio_clip((output_channel[j]*gain)-dither, m)
68
69 #define FETCH51(m, dither) \
70   audio_clip((gain*(center[j] + 2*front[j] + \
71     2*back[j] + subwoof[j]))-dither, m)
72
73 #define PUT_8BIT_SAMPLES(n,dither) { \
74   sample = FETCH##n(0x7f,dither); \
75   *(int8_t*)&buffer[k] = sample; \
76 }
77
78 #define PUT_16BIT_SAMPLES(n,dither) { \
79   sample = FETCH##n(0x7fff,dither); \
80   *(int16_t*)&buffer[k] = sample; \
81 }
82
83 #define PUT_24BIT_SAMPLES(n,dither) { \
84   sample = FETCH##n(0x7fffff,dither); \
85   if( (k&1) ) { \
86     *(int8_t*)&buffer[k] = sample; \
87     *(int16_t*)&buffer[k+1] = (sample >> 8); \
88   } else { \
89     *(int16_t*)&buffer[k] = sample; \
90     *(int8_t*)&buffer[k+2] = (sample >> 16); \
91   } \
92 }
93
94 #define PUT_32BIT_SAMPLES(n,dither) { \
95   sample = FETCH##n(0x7fffffff,dither); \
96   *(int32_t*)&buffer[k] = sample; \
97 }
98
99 int AudioDevice::write_buffer(double **data,
100         int channels, int samples, double bfr_time)
101 {
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);
110         return 0;
111 }
112
113 int AudioDevice::set_last_buffer()
114 {
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();
121         return 0;
122 }
123
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)
127 {
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;
133
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;
142         }
143
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;
151
152         for( int och=0; och<device_channels; ++och ) {
153                 int sample;
154                 int k = och * bits / 8;
155
156                 if( map51_2 ) {
157                         double *front = data[och];
158                         double *center = data[2];
159                         double *subwoof = data[3];
160                         double *back = data[och+4];
161                         if( play_dither ) {
162                                 switch( bits ) {
163                                 case 8: for( int j=0; j<samples; ++j,k+=frame )
164                                                 PUT_8BIT_SAMPLES(51,DITHER)
165                                         break;
166                                 case 16: for( int j=0; j<samples; ++j,k+=frame )
167                                                 PUT_16BIT_SAMPLES(51,DITHER)
168                                         break;
169                                 case 24: for( int j=0; j<samples; ++j,k+=frame )
170                                                 PUT_24BIT_SAMPLES(51,DITHER)
171                                         break;
172                                 case 32: for( int j=0; j<samples; ++j,k+=frame )
173                                                 PUT_32BIT_SAMPLES(51,DITHER)
174                                         break;
175                                 }
176                         }
177                         else {
178                                 switch( bits ) {
179                                 case 8: for( int j=0; j<samples; ++j,k+=frame )
180                                                 PUT_8BIT_SAMPLES(51,NO_DITHER)
181                                         break;
182                                 case 16: for( int j=0; j<samples; ++j,k+=frame )
183                                                 PUT_16BIT_SAMPLES(51,NO_DITHER)
184                                         break;
185                                 case 24: for( int j=0; j<samples; ++j,k+=frame )
186                                                 PUT_24BIT_SAMPLES(51,NO_DITHER)
187                                         break;
188                                 case 32: for( int j=0; j<samples; ++j,k+=frame )
189                                                 PUT_32BIT_SAMPLES(51,NO_DITHER)
190                                         break;
191                                 }
192                         }
193                 }
194                 else {
195                         double *output_channel = data[och % channels];
196                         if( play_dither ) {
197                                 switch( bits ) {
198                                 case 8: for( int j=0; j<samples; ++j,k+=frame )
199                                                 PUT_8BIT_SAMPLES(1,DITHER)
200                                         break;
201                                 case 16: for( int j=0; j<samples; ++j,k+=frame )
202                                                 PUT_16BIT_SAMPLES(1,DITHER)
203                                         break;
204                                 case 24: for( int j=0; j<samples; ++j,k+=frame )
205                                                 PUT_24BIT_SAMPLES(1,DITHER)
206                                         break;
207                                 case 32: for( int j=0; j<samples; ++j,k+=frame )
208                                                 PUT_32BIT_SAMPLES(1,DITHER)
209                                         break;
210                                 }
211                         }
212                         else {
213                                 switch( bits ) {
214                                 case 8: for( int j=0; j<samples; ++j,k+=frame )
215                                                 PUT_8BIT_SAMPLES(1,NO_DITHER)
216                                         break;
217                                 case 16: for( int j=0; j<samples; ++j,k+=frame )
218                                                 PUT_16BIT_SAMPLES(1,NO_DITHER)
219                                         break;
220                                 case 24: for( int j=0; j<samples; ++j,k+=frame )
221                                                 PUT_24BIT_SAMPLES(1,NO_DITHER)
222                                         break;
223                                 case 32: for( int j=0; j<samples; ++j,k+=frame )
224                                                 PUT_32BIT_SAMPLES(1,NO_DITHER)
225                                         break;
226                                 }
227                         }
228                 }
229         }
230 // make buffer available for playback
231         obfr->play_lock->unlock();
232         return 0;
233 }
234
235 void AudioDevice::end_output()
236 {
237         is_playing_back = 0;
238         monitoring = 0;
239         if( lowlevel_out )
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();
245         }
246 }
247
248 int AudioDevice::reset_output()
249 {
250         for( int i=0; i<TOTAL_AUDIO_BUFFERS; ++i ) {
251                 output_buffer_t *obfr = &output[i];
252                 if( obfr->buffer ) {
253                         delete [] obfr->buffer;
254                         obfr->buffer = 0;
255                 }
256                 obfr->allocated = 0;
257                 obfr->size = 0;
258                 obfr->last_buffer = 0;
259                 obfr->arm_lock->reset();
260                 obfr->play_lock->reset();
261         }
262         is_playing_back = 0;
263         playback_interrupted = 0;
264         monitoring = 0;
265         monitor_open = 0;
266         output_buffer_num = 0;
267         arm_buffer_num = 0;
268         last_position = 0;
269         return 0;
270 }
271
272
273 void AudioDevice::set_play_gain(double gain)
274 {
275         play_gain = gain * out_config->play_gain;
276 }
277
278 void AudioDevice::set_play_dither(int status)
279 {
280         play_dither = status;
281 }
282
283 void AudioDevice::set_software_positioning(int status)
284 {
285         software_position_info = status;
286 }
287
288 int AudioDevice::start_playback()
289 {
290 // arm buffer before doing this
291         is_playing_back = 1;
292         playback_interrupted = 0;
293         total_samples_written = 0;
294         total_samples_output = 0;
295 // zero timers
296         playback_timer->update();
297         last_position = 0;
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();
303         return 0;
304 }
305
306 int AudioDevice::interrupt_playback()
307 {
308 //printf("AudioDevice::interrupt_playback\n");
309         stop_output(0);
310         return 0;
311 }
312
313 int64_t AudioDevice::samples_output()
314 {
315         int64_t result = !lowlevel_out ? -1 : lowlevel_out->samples_output();
316         if( result < 0 ) result = total_samples_output;
317         return result;
318 }
319
320
321 int64_t AudioDevice::current_position()
322 {
323 // try to get OSS position
324         int64_t result = 0;
325
326         if(w) {
327                 int frame = (get_obits() * get_ochannels()) / 8;
328                 if( !frame ) return 0;
329
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;
336                 }
337
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;
347                 }
348
349                 if(result < last_position)
350                         result = last_position;
351                 else
352                         last_position = result;
353
354                 result -= (int64_t)(get_orate() * out_config->audio_offset);
355
356         }
357         else if(r) {
358                 result = total_samples_read +
359                         record_timer->get_scaled_difference(get_irate());
360         }
361
362         return result;
363 }
364
365 void AudioDevice::run_output()
366 {
367         output_buffer_num = 0;
368         playback_timer->update();
369
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 ) {
379                         if( lowlevel_out ) lowlevel_out->flush_device();
380                         break;
381                 }
382 // get size for position information
383 // write converted buffer synchronously
384                 double bfr_time = obfr->bfr_time;
385                 int result = !lowlevel_out ? -1 :
386                         lowlevel_out->write_buffer(obfr->buffer, obfr->size);
387 // allow writing to the buffer
388                 obfr->arm_lock->unlock();
389                 if( !result ) {
390                         timer_lock->lock("AudioDevice::run 3");
391                         int frame = get_ochannels() * get_obits() / 8;
392                         int samples = frame ? obfr->size / frame : 0;
393                         total_samples_output += samples;
394                         last_buffer_time = bfr_time + (double)samples/out_samplerate;
395                         playback_timer->update();
396                         timer_lock->unlock();
397                 }
398 // inform user if the buffer write failed
399                 else if( result < 0 ) {
400                         perror("AudioDevice::write_buffer");
401                         usleep(250000);
402                 }
403         }
404
405         is_playing_back = 0;
406 }
407
408