forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.c
1417 lines (1088 loc) · 37.7 KB
/
audio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011, 2012, 2013, 2014, 2015 John Langner, WB2OSZ
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Module: audio.c
*
* Purpose: Interface to audio device commonly called a "sound card" for
* historical reasons.
*
* This version is for Linux and Cygwin.
*
* Two different types of sound interfaces are supported:
*
* * OSS - For Cygwin or Linux versions with /dev/dsp.
*
* * ALSA - For Linux versions without /dev/dsp.
* In this case, define preprocessor symbol USE_ALSA.
*
* References: Some tips on on using Linux sound devices.
*
* http://www.oreilly.de/catalog/multilinux/excerpt/ch14-05.htm
* http://cygwin.com/ml/cygwin-patches/2004-q1/msg00116/devdsp.c
* http://manuals.opensound.com/developer/fulldup.c.html
*
* "Introduction to Sound Programming with ALSA"
* http://www.linuxjournal.com/article/6735?page=0,1
*
* http://www.alsa-project.org/main/index.php/Asoundrc
*
* Credits: Release 1.0: Fabrice FAURE contributed code for the SDR UDP interface.
*
* Discussion here: http://gqrx.dk/doc/streaming-audio-over-udp
*
* Release 1.1: Gabor Berczi provided fixes for the OSS code
* which had fallen into decay.
*
* Major Revisions:
*
* 1.2 - Add ability to use more than one audio device.
*
*---------------------------------------------------------------*/
#include "direwolf.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#if USE_ALSA
#include <alsa/asoundlib.h>
#else
#include <errno.h>
#ifdef __OpenBSD__
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif
#endif
#include "audio.h"
#include "audio_stats.h"
#include "textcolor.h"
#include "dtime_now.h"
#include "demod.h" /* for alevel_t & demod_get_audio_level() */
/* Audio configuration. */
static struct audio_s *save_audio_config_p;
/* Current state for each of the audio devices. */
static struct adev_s {
#if USE_ALSA
snd_pcm_t *audio_in_handle;
snd_pcm_t *audio_out_handle;
int bytes_per_frame; /* number of bytes for a sample from all channels. */
/* e.g. 4 for stereo 16 bit. */
#else
int oss_audio_device_fd; /* Single device, both directions. */
#endif
int inbuf_size_in_bytes; /* number of bytes allocated */
unsigned char *inbuf_ptr;
int inbuf_len; /* number byte of actual data available. */
int inbuf_next; /* index of next to remove. */
int outbuf_size_in_bytes;
unsigned char *outbuf_ptr;
int outbuf_len;
enum audio_in_type_e g_audio_in_type;
int udp_sock; /* UDP socket for receiving data */
} adev[MAX_ADEVS];
// Originally 40. Version 1.2, try 10 for lower latency.
#define ONE_BUF_TIME 10
#if USE_ALSA
static int set_alsa_params (int a, snd_pcm_t *handle, struct audio_s *pa, char *name, char *dir);
//static void alsa_select_device (char *pick_dev, int direction, char *result);
#else
static int set_oss_params (int a, int fd, struct audio_s *pa);
#endif
#define roundup1k(n) (((n) + 0x3ff) & ~0x3ff)
static int calcbufsize(int rate, int chans, int bits)
{
int size1 = (rate * chans * bits / 8 * ONE_BUF_TIME) / 1000;
int size2 = roundup1k(size1);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio_open: calcbufsize (rate=%d, chans=%d, bits=%d) calc size=%d, round up to %d\n",
rate, chans, bits, size1, size2);
#endif
return (size2);
}
/*------------------------------------------------------------------
*
* Name: audio_open
*
* Purpose: Open the digital audio device.
* For "OSS", the device name is typically "/dev/dsp".
* For "ALSA", it's a lot more complicated. See User Guide.
*
* New in version 1.0, we recognize "udp:" optionally
* followed by a port number.
*
* Inputs: pa - Address of structure of type audio_s.
*
* Using a structure, rather than separate arguments
* seemed to make sense because we often pass around
* the same set of parameters various places.
*
* The fields that we care about are:
* num_channels
* samples_per_sec
* bits_per_sample
* If zero, reasonable defaults will be provided.
*
* The device names are in adevice_in and adevice_out.
* - For "OSS", the device name is typically "/dev/dsp".
* - For "ALSA", the device names are hw:c,d
* where c is the "card" (for historical purposes)
* and d is the "device" within the "card."
*
*
* Outputs: pa - The ACTUAL values are returned here.
*
* These might not be exactly the same as what was requested.
*
* Example: ask for stereo, 16 bits, 22050 per second.
* An ordinary desktop/laptop PC should be able to handle this.
* However, some other sort of smaller device might be
* more restrictive in its capabilities.
* It might say, the best I can do is mono, 8 bit, 8000/sec.
*
* The sofware modem must use this ACTUAL information
* that the device is supplying, that could be different
* than what the user specified.
*
* Returns: 0 for success, -1 for failure.
*
*
*----------------------------------------------------------------*/
int audio_open (struct audio_s *pa)
{
int err;
int chan;
int a;
char audio_in_name[30];
char audio_out_name[30];
save_audio_config_p = pa;
memset (adev, 0, sizeof(adev));
for (a=0; a<MAX_ADEVS; a++) {
#ifndef USE_ALSA
adev[a].oss_audio_device_fd = -1;
#endif
adev[a].udp_sock = -1;
}
/*
* Fill in defaults for any missing values.
*/
for (a=0; a<MAX_ADEVS; a++) {
if (pa->adev[a].num_channels == 0)
pa->adev[a].num_channels = DEFAULT_NUM_CHANNELS;
if (pa->adev[a].samples_per_sec == 0)
pa->adev[a].samples_per_sec = DEFAULT_SAMPLES_PER_SEC;
if (pa->adev[a].bits_per_sample == 0)
pa->adev[a].bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
for (chan=0; chan<MAX_CHANS; chan++) {
if (pa->achan[chan].mark_freq == 0)
pa->achan[chan].mark_freq = DEFAULT_MARK_FREQ;
if (pa->achan[chan].space_freq == 0)
pa->achan[chan].space_freq = DEFAULT_SPACE_FREQ;
if (pa->achan[chan].baud == 0)
pa->achan[chan].baud = DEFAULT_BAUD;
if (pa->achan[chan].num_subchan == 0)
pa->achan[chan].num_subchan = 1;
}
}
/*
* Open audio device(s).
*/
for (a=0; a<MAX_ADEVS; a++) {
if (pa->adev[a].defined) {
adev[a].inbuf_size_in_bytes = 0;
adev[a].inbuf_ptr = NULL;
adev[a].inbuf_len = 0;
adev[a].inbuf_next = 0;
adev[a].outbuf_size_in_bytes = 0;
adev[a].outbuf_ptr = NULL;
adev[a].outbuf_len = 0;
/*
* Determine the type of audio input.
*/
adev[a].g_audio_in_type = AUDIO_IN_TYPE_SOUNDCARD;
if (strcasecmp(pa->adev[a].adevice_in, "stdin") == 0 || strcmp(pa->adev[a].adevice_in, "-") == 0) {
adev[a].g_audio_in_type = AUDIO_IN_TYPE_STDIN;
/* Change "-" to stdin for readability. */
strlcpy (pa->adev[a].adevice_in, "stdin", sizeof(pa->adev[a].adevice_in));
}
if (strncasecmp(pa->adev[a].adevice_in, "udp:", 4) == 0) {
adev[a].g_audio_in_type = AUDIO_IN_TYPE_SDR_UDP;
/* Supply default port if none specified. */
if (strcasecmp(pa->adev[a].adevice_in,"udp") == 0 ||
strcasecmp(pa->adev[a].adevice_in,"udp:") == 0) {
snprintf (pa->adev[a].adevice_in, sizeof(pa->adev[a].adevice_in), "udp:%d", DEFAULT_UDP_AUDIO_PORT);
}
}
/* Let user know what is going on. */
/* If not specified, the device names should be "default". */
strlcpy (audio_in_name, pa->adev[a].adevice_in, sizeof(audio_in_name));
strlcpy (audio_out_name, pa->adev[a].adevice_out, sizeof(audio_out_name));
char ctemp[40];
if (pa->adev[a].num_channels == 2) {
snprintf (ctemp, sizeof(ctemp), " (channels %d & %d)", ADEVFIRSTCHAN(a), ADEVFIRSTCHAN(a)+1);
}
else {
snprintf (ctemp, sizeof(ctemp), " (channel %d)", ADEVFIRSTCHAN(a));
}
text_color_set(DW_COLOR_INFO);
if (strcmp(audio_in_name,audio_out_name) == 0) {
dw_printf ("Audio device for both receive and transmit: %s %s\n", audio_in_name, ctemp);
}
else {
dw_printf ("Audio input device for receive: %s %s\n", audio_in_name, ctemp);
dw_printf ("Audio out device for transmit: %s %s\n", audio_out_name, ctemp);
}
/*
* Now attempt actual opens.
*/
/*
* Input device.
*/
switch (adev[a].g_audio_in_type) {
/*
* Soundcard - ALSA.
*/
case AUDIO_IN_TYPE_SOUNDCARD:
#if USE_ALSA
err = snd_pcm_open (&(adev[a].audio_in_handle), audio_in_name, SND_PCM_STREAM_CAPTURE, 0);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not open audio device %s for input\n%s\n",
audio_in_name, snd_strerror(err));
return (-1);
}
adev[a].inbuf_size_in_bytes = set_alsa_params (a, adev[a].audio_in_handle, pa, audio_in_name, "input");
#else // OSS
adev[a].oss_audio_device_fd = open (pa->adev[a].adevice_in, O_RDWR);
if (adev[a].oss_audio_device_fd < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("%s:\n", pa->adev[a].adevice_in);
// snprintf (message, sizeof(message), "Could not open audio device %s", pa->adev[a].adevice_in);
// perror (message);
return (-1);
}
adev[a].outbuf_size_in_bytes = adev[a].inbuf_size_in_bytes = set_oss_params (a, adev[a].oss_audio_device_fd, pa);
if (adev[a].inbuf_size_in_bytes <= 0 || adev[a].outbuf_size_in_bytes <= 0) {
return (-1);
}
#endif
break;
/*
* UDP.
*/
case AUDIO_IN_TYPE_SDR_UDP:
//Create socket and bind socket
{
struct sockaddr_in si_me;
//int slen=sizeof(si_me);
//int data_size = 0;
//Create UDP Socket
if ((adev[a].udp_sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Couldn't create socket, errno %d\n", errno);
return -1;
}
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons((short)atoi(audio_in_name+4));
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
//Bind to the socket
if (bind(adev[a].udp_sock, (const struct sockaddr *) &si_me, sizeof(si_me))==-1) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Couldn't bind socket, errno %d\n", errno);
return -1;
}
}
adev[a].inbuf_size_in_bytes = SDR_UDP_BUF_MAXLEN;
break;
/*
* stdin.
*/
case AUDIO_IN_TYPE_STDIN:
/* Do we need to adjust any properties of stdin? */
adev[a].inbuf_size_in_bytes = 1024;
break;
default:
text_color_set(DW_COLOR_ERROR);
dw_printf ("Internal error, invalid audio_in_type\n");
return (-1);
}
/*
* Output device. Only "soundcard" is supported at this time.
*/
#if USE_ALSA
err = snd_pcm_open (&(adev[a].audio_out_handle), audio_out_name, SND_PCM_STREAM_PLAYBACK, 0);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not open audio device %s for output\n%s\n",
audio_out_name, snd_strerror(err));
return (-1);
}
adev[a].outbuf_size_in_bytes = set_alsa_params (a, adev[a].audio_out_handle, pa, audio_out_name, "output");
if (adev[a].inbuf_size_in_bytes <= 0 || adev[a].outbuf_size_in_bytes <= 0) {
return (-1);
}
#endif
/*
* Finally allocate buffer for each direction.
*/
adev[a].inbuf_ptr = malloc(adev[a].inbuf_size_in_bytes);
assert (adev[a].inbuf_ptr != NULL);
adev[a].inbuf_len = 0;
adev[a].inbuf_next = 0;
adev[a].outbuf_ptr = malloc(adev[a].outbuf_size_in_bytes);
assert (adev[a].outbuf_ptr != NULL);
adev[a].outbuf_len = 0;
} /* end of audio device defined */
} /* end of for each audio device */
return (0);
} /* end audio_open */
#if USE_ALSA
/*
* Set parameters for sound card.
*
* See ?? for details.
*/
/*
* Terminology:
* Sample - for one channel. e.g. 2 bytes for 16 bit.
* Frame - one sample for all channels. e.g. 4 bytes for 16 bit stereo
* Period - size of one transfer.
*/
static int set_alsa_params (int a, snd_pcm_t *handle, struct audio_s *pa, char *devname, char *inout)
{
snd_pcm_hw_params_t *hw_params;
snd_pcm_uframes_t fpp; /* Frames per period. */
unsigned int val;
int dir;
int err;
int buf_size_in_bytes; /* result, number of bytes per transfer. */
err = snd_pcm_hw_params_malloc (&hw_params);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not alloc hw param structure.\n%s\n",
snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
err = snd_pcm_hw_params_any (handle, hw_params);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not init hw param structure.\n%s\n",
snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
/* Interleaved data: L, R, L, R, ... */
err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not set interleaved mode.\n%s\n",
snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
/* Signed 16 bit little endian or unsigned 8 bit. */
err = snd_pcm_hw_params_set_format (handle, hw_params,
pa->adev[a].bits_per_sample == 8 ? SND_PCM_FORMAT_U8 : SND_PCM_FORMAT_S16_LE);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not set bits per sample.\n%s\n",
snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
/* Number of audio channels. */
err = snd_pcm_hw_params_set_channels (handle, hw_params, pa->adev[a].num_channels);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not set number of audio channels.\n%s\n",
snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
/* Audio sample rate. */
val = pa->adev[a].samples_per_sec;
dir = 0;
err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &val, &dir);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not set audio sample rate.\n%s\n",
snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
if (val != pa->adev[a].samples_per_sec) {
text_color_set(DW_COLOR_INFO);
dw_printf ("Asked for %d samples/sec but got %d.\n",
pa->adev[a].samples_per_sec, val);
dw_printf ("for %s %s.\n", devname, inout);
pa->adev[a].samples_per_sec = val;
}
/* Original: */
/* Guessed around 20 reads/sec might be good. */
/* Period too long = too much latency. */
/* Period too short = more overhead of many small transfers. */
/* fpp = pa->adev[a].samples_per_sec / 20; */
/* The suggested period size was 2205 frames. */
/* I thought the later "...set_period_size_near" might adjust it to be */
/* some more optimal nearby value based hardware buffer sizes but */
/* that didn't happen. We ended up with a buffer size of 4410 bytes. */
/* In version 1.2, let's take a different approach. */
/* Reduce the latency and round up to a multiple of 1 Kbyte. */
/* For the typical case of 44100 sample rate, 1 channel, 16 bits, we calculate */
/* a buffer size of 882 and round it up to 1k. This results in 512 frames per period. */
/* A period comes out to be about 80 periods per second or about 12.5 mSec each. */
buf_size_in_bytes = calcbufsize(pa->adev[a].samples_per_sec, pa->adev[a].num_channels, pa->adev[a].bits_per_sample);
#if __arm__
/* Ugly hack for RPi. */
/* Reducing buffer size is fine for input but not so good for output. */
if (*inout == 'o') {
buf_size_in_bytes = buf_size_in_bytes * 4;
}
#endif
fpp = buf_size_in_bytes / (pa->adev[a].num_channels * pa->adev[a].bits_per_sample / 8);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("suggest period size of %d frames\n", (int)fpp);
#endif
dir = 0;
err = snd_pcm_hw_params_set_period_size_near (handle, hw_params, &fpp, &dir);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not set period size\n%s\n", snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
err = snd_pcm_hw_params (handle, hw_params);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not set hw params\n%s\n", snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
/* Driver might not like our suggested period size */
/* and might have another idea. */
err = snd_pcm_hw_params_get_period_size (hw_params, &fpp, NULL);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not get audio period size.\n%s\n", snd_strerror(err));
dw_printf ("for %s %s.\n", devname, inout);
return (-1);
}
snd_pcm_hw_params_free (hw_params);
/* A "frame" is one sample for all channels. */
/* The read and write use units of frames, not bytes. */
adev[a].bytes_per_frame = snd_pcm_frames_to_bytes (handle, 1);
assert (adev[a].bytes_per_frame == pa->adev[a].num_channels * pa->adev[a].bits_per_sample / 8);
buf_size_in_bytes = fpp * adev[a].bytes_per_frame;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio buffer size = %d (bytes per frame) x %d (frames per period) = %d \n", adev[a].bytes_per_frame, (int)fpp, buf_size_in_bytes);
#endif
/* Version 1.3 - after a report of this situation for Mac OSX version. */
if (buf_size_in_bytes < 256 || buf_size_in_bytes > 32768) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Audio buffer has unexpected extreme size of %d bytes.\n", buf_size_in_bytes);
dw_printf ("Detected at %s, line %d.\n", __FILE__, __LINE__);
dw_printf ("This might be caused by unusual audio device configuration values.\n");
buf_size_in_bytes = 2048;
dw_printf ("Using %d to attempt recovery.\n", buf_size_in_bytes);
}
return (buf_size_in_bytes);
} /* end alsa_set_params */
#else
/*
* Set parameters for sound card. (OSS only)
*
* See /usr/include/sys/soundcard.h for details.
*/
static int set_oss_params (int a, int fd, struct audio_s *pa)
{
int err;
int devcaps;
int asked_for;
char message[100];
int ossbuf_size_in_bytes;
err = ioctl (fd, SNDCTL_DSP_CHANNELS, &(pa->adev[a].num_channels));
if (err == -1) {
text_color_set(DW_COLOR_ERROR);
perror("Not able to set audio device number of channels");
return (-1);
}
asked_for = pa->adev[a].samples_per_sec;
err = ioctl (fd, SNDCTL_DSP_SPEED, &(pa->adev[a].samples_per_sec));
if (err == -1) {
text_color_set(DW_COLOR_ERROR);
perror("Not able to set audio device sample rate");
return (-1);
}
if (pa->adev[a].samples_per_sec != asked_for) {
text_color_set(DW_COLOR_INFO);
dw_printf ("Asked for %d samples/sec but actually using %d.\n",
asked_for, pa->adev[a].samples_per_sec);
}
/* This is actually a bit mask but it happens that */
/* 0x8 is unsigned 8 bit samples and */
/* 0x10 is signed 16 bit little endian. */
err = ioctl (fd, SNDCTL_DSP_SETFMT, &(pa->adev[a].bits_per_sample));
if (err == -1) {
text_color_set(DW_COLOR_ERROR);
perror("Not able to set audio device sample size");
return (-1);
}
/*
* Determine capabilities.
*/
err = ioctl (fd, SNDCTL_DSP_GETCAPS, &devcaps);
if (err == -1) {
text_color_set(DW_COLOR_ERROR);
perror("Not able to get audio device capabilities");
// Is this fatal? // return (-1);
}
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio_open(): devcaps = %08x\n", devcaps);
if (devcaps & DSP_CAP_DUPLEX) dw_printf ("Full duplex record/playback.\n");
if (devcaps & DSP_CAP_BATCH) dw_printf ("Device has some kind of internal buffers which may cause delays.\n");
if (devcaps & ~ (DSP_CAP_DUPLEX | DSP_CAP_BATCH)) dw_printf ("Others...\n");
#endif
if (!(devcaps & DSP_CAP_DUPLEX)) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Audio device does not support full duplex\n");
// Do we care? // return (-1);
}
err = ioctl (fd, SNDCTL_DSP_SETDUPLEX, NULL);
if (err == -1) {
// text_color_set(DW_COLOR_ERROR);
// perror("Not able to set audio full duplex mode");
// Unfortunate but not a disaster.
}
/*
* Get preferred block size.
* Presumably this will provide the most efficient transfer.
*
* In my particular situation, this turned out to be
* 2816 for 11025 Hz 16 bit mono
* 5568 for 11025 Hz 16 bit stereo
* 11072 for 44100 Hz 16 bit mono
*
* This was long ago under different conditions.
* Should study this again some day.
*
* Your milage may vary.
*/
err = ioctl (fd, SNDCTL_DSP_GETBLKSIZE, &ossbuf_size_in_bytes);
if (err == -1) {
text_color_set(DW_COLOR_ERROR);
perror("Not able to get audio block size");
ossbuf_size_in_bytes = 2048; /* pick something reasonable */
}
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio_open(): suggestd block size is %d\n", ossbuf_size_in_bytes);
#endif
/*
* That's 1/8 of a second which seems rather long if we want to
* respond quickly.
*/
ossbuf_size_in_bytes = calcbufsize(pa->adev[a].samples_per_sec, pa->adev[a].num_channels, pa->adev[a].bits_per_sample);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio_open(): using block size of %d\n", ossbuf_size_in_bytes);
#endif
#if 0
/* Original - dies without good explanation. */
assert (ossbuf_size_in_bytes >= 256 && ossbuf_size_in_bytes <= 32768);
#else
/* Version 1.3 - after a report of this situation for Mac OSX version. */
if (ossbuf_size_in_bytes < 256 || ossbuf_size_in_bytes > 32768) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Audio buffer has unexpected extreme size of %d bytes.\n", ossbuf_size_in_bytes);
dw_printf ("Detected at %s, line %d.\n", __FILE__, __LINE__);
dw_printf ("This might be caused by unusual audio device configuration values.\n");
ossbuf_size_in_bytes = 2048;
dw_printf ("Using %d to attempt recovery.\n", ossbuf_size_in_bytes);
}
#endif
return (ossbuf_size_in_bytes);
} /* end set_oss_params */
#endif
/*------------------------------------------------------------------
*
* Name: audio_get
*
* Purpose: Get one byte from the audio device.
*
* Inputs: a - Our number for audio device.
*
* Returns: 0 - 255 for a valid sample.
* -1 for any type of error.
*
* Description: The caller must deal with the details of mono/stereo
* and number of bytes per sample.
*
* This will wait if no data is currently available.
*
*----------------------------------------------------------------*/
// Use hot attribute for all functions called for every audio sample.
__attribute__((hot))
int audio_get (int a)
{
int n;
int retries = 0;
#if STATISTICS
/* Gather numbers for read from audio device. */
#define duration 100 /* report every 100 seconds. */
static time_t last_time[MAX_ADEVS];
time_t this_time[MAX_ADEVS];
static int sample_count[MAX_ADEVS];
static int error_count[MAX_ADEVS];
#endif
#if DEBUGx
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio_get():\n");
#endif
assert (adev[a].inbuf_size_in_bytes >= 100 && adev[a].inbuf_size_in_bytes <= 32768);
switch (adev[a].g_audio_in_type) {
/*
* Soundcard - ALSA
*/
case AUDIO_IN_TYPE_SOUNDCARD:
#if USE_ALSA
while (adev[a].inbuf_next >= adev[a].inbuf_len) {
assert (adev[a].audio_in_handle != NULL);
#if DEBUGx
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio_get(): readi asking for %d frames\n", adev[a].inbuf_size_in_bytes / adev[a].bytes_per_frame);
#endif
n = snd_pcm_readi (adev[a].audio_in_handle, adev[a].inbuf_ptr, adev[a].inbuf_size_in_bytes / adev[a].bytes_per_frame);
#if DEBUGx
text_color_set(DW_COLOR_DEBUG);
dw_printf ("audio_get(): readi asked for %d and got %d frames\n",
adev[a].inbuf_size_in_bytes / adev[a].bytes_per_frame, n);
#endif
if (n > 0) {
/* Success */
adev[a].inbuf_len = n * adev[a].bytes_per_frame; /* convert to number of bytes */
adev[a].inbuf_next = 0;
audio_stats (a,
save_audio_config_p->adev[a].num_channels,
n,
save_audio_config_p->statistics_interval);
}
else if (n == 0) {
/* Didn't expect this, but it's not a problem. */
/* Wait a little while and try again. */
text_color_set(DW_COLOR_ERROR);
dw_printf ("Audio input got zero bytes: %s\n", snd_strerror(n));
SLEEP_MS(10);
adev[a].inbuf_len = 0;
adev[a].inbuf_next = 0;
}
else {
/* Error */
// TODO: Needs more study and testing.
// Only expected error conditions:
// -EBADFD PCM is not in the right state (SND_PCM_STATE_PREPARED or SND_PCM_STATE_RUNNING)
// -EPIPE an overrun occurred
// -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
// Data overrun is displayed as "broken pipe" which seems a little misleading.
// Add our own message which says something about CPU being too slow.
text_color_set(DW_COLOR_ERROR);
dw_printf ("Audio input device %d error code %d: %s\n", a, n, snd_strerror(n));
if (n == (-EPIPE)) {
dw_printf ("This is most likely caused by the CPU being too slow to keep up with the audio stream.\n");
dw_printf ("Use the \"top\" command, in another command window, to look at CPU usage.\n");
dw_printf ("This might be a temporary condition so we will attempt to recover a few times before giving up.\n");
}
audio_stats (a,
save_audio_config_p->adev[a].num_channels,
0,
save_audio_config_p->statistics_interval);
/* Try to recover a few times and eventually give up. */
if (++retries > 10) {
adev[a].inbuf_len = 0;
adev[a].inbuf_next = 0;
return (-1);
}
if (n == -EPIPE) {
/* EPIPE means overrun */
snd_pcm_recover (adev[a].audio_in_handle, n, 1);
}
else {
/* Could be some temporary condition. */
/* Wait a little then try again. */
/* Sometimes I get "Resource temporarily available" */
/* when the Update Manager decides to run. */
SLEEP_MS (250);
snd_pcm_recover (adev[a].audio_in_handle, n, 1);
}
}
}
#else /* end ALSA, begin OSS */
/* Fixed in 1.2. This was formerly outside of the switch */
/* so the OSS version did not process stdin or UDP. */
while (adev[a].g_audio_in_type == AUDIO_IN_TYPE_SOUNDCARD && adev[a].inbuf_next >= adev[a].inbuf_len) {
assert (adev[a].oss_audio_device_fd > 0);
n = read (adev[a].oss_audio_device_fd, adev[a].inbuf_ptr, adev[a].inbuf_size_in_bytes);
//text_color_set(DW_COLOR_DEBUG);
// dw_printf ("audio_get(): read %d returns %d\n", adev[a].inbuf_size_in_bytes, n);
if (n < 0) {
text_color_set(DW_COLOR_ERROR);
perror("Can't read from audio device");
adev[a].inbuf_len = 0;
adev[a].inbuf_next = 0;
audio_stats (a,
save_audio_config_p->adev[a].num_channels,
0,
save_audio_config_p->statistics_interval);
return (-1);
}
adev[a].inbuf_len = n;
adev[a].inbuf_next = 0;
audio_stats (a,
save_audio_config_p->adev[a].num_channels,