-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.c
5598 lines (4843 loc) · 165 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
/* Audio hardware handlers (OSS, ALSA, Sun, Windows, Mac OSX, Jack, HPUX, NetBSD, OpenBSD, pulseaudio, portaudio)
*
* In many cases, only callback driven transfers are supported, so ideally we'd have:
* int mus_audio_playback(caller_data, start_func, fill_func, end_func)
* returns error indication or MUS_NO_ERROR
* calls start_func at startup: void start(caller_data, ...)?
* each times it needs a bufferfull, calls fill_func: bool fill(caller_data, void *buf, buf_size_in_samples, buf_data_type)
* perhaps returns false to signal normal quit?
* at end (either via fill or some interrupt), calls end(caller_data, ...)?
*/
/*
* layout of this file:
* error handlers
* OSS
* ALSA
* Sun
* Windows 95/98
* OSX
* JACK
* HPUX
* NetBSD/OpenBSD
* PulseAudio (in progress?)
* PortAudio
*/
/*
* int mus_audio_open_output(int dev, int srate, int chans, mus_sample_t samp_type, int size)
* int mus_audio_open_input(int dev, int srate, int chans, mus_sample_t samp_type, int size)
* int mus_audio_write(int line, char *buf, int bytes)
* int mus_audio_close(int line)
* int mus_audio_read(int line, char *buf, int bytes)
* int mus_audio_initialize(void) does whatever is needed to get set up
* char *mus_audio_moniker(void) returns some brief description of the overall audio setup (don't free return string).
*/
#include "mus-config.h"
#if USE_SND && __APPLE__ && USE_MOTIF
#undef USE_MOTIF
#define USE_NO_GUI 1
/* Xt's Boolean (/usr/include/X11/Intrinsic.h = char) collides with MacTypes.h Boolean, (actually,
* unsigned char in /Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/A/Headers/CFBase.h)
* but we want snd.h for other stuff, so, if Motif is in use, don't load its headers at this time
* perhaps we could use the -funsigned-char switch in gcc
*/
#endif
#if USE_SND && __APPLE__ && HAVE_RUBY
/* if using Ruby, OpenTransport.h T_* definitions collide with Ruby's -- it isn't needed here, so... */
#define REDEFINE_HAVE_RUBY 1
#undef HAVE_RUBY
#endif
#if USE_SND
#include "snd.h"
#else
#define PRINT_BUFFER_SIZE 512
#define LABEL_BUFFER_SIZE 64
#endif
#if USE_SND && __APPLE__
#define USE_MOTIF 1
#undef USE_NO_GUI
#if REDEFINE_HAVE_RUBY
#define HAVE_RUBY 1
#endif
#endif
#include <math.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <string.h>
#ifdef __APPLE__
#include <CoreServices/CoreServices.h>
#include <CoreAudio/CoreAudio.h>
/* these pull in stdbool.h apparently, so they have to precede sndlib.h */
#endif
/* #define HAVE_JACK_IN_LINUX (MUS_JACK && __linux__) */
/* using JACK on GNU/linux, GNU/kFreeBSD and GNU/Hurd is all the same */
#if ((defined __linux__) || ((defined __FreeBSD_kernel__) && (defined __GLIBC__)) || (defined __GNU__))
#define HAVE_JACK_IN_LINUX MUS_JACK
#else
#define HAVE_JACK_IN_LINUX 0
#endif
#include "_sndlib.h"
#include "sndlib-strings.h"
#if WITH_AUDIO
enum {MUS_AUDIO_IGNORED, MUS_AUDIO_DUPLEX_DEFAULT, MUS_AUDIO_LINE_OUT,
MUS_AUDIO_LINE_IN, MUS_AUDIO_MICROPHONE, MUS_AUDIO_SPEAKERS, MUS_AUDIO_DIGITAL_OUT,
MUS_AUDIO_DAC_OUT, MUS_AUDIO_MIXER, MUS_AUDIO_AUX_OUTPUT
};
#define mus_standard_error(Error_Type, Error_Message) \
mus_print("%s\n [%s[%d] %s]", Error_Message, __FILE__, __LINE__, __func__)
#define mus_standard_io_error(Error_Type, IO_Func, IO_Name) \
mus_print("%s %s: %s\n [%s[%d] %s]", IO_Func, IO_Name, strerror(errno), __FILE__, __LINE__, __func__)
static char *version_name = NULL;
static bool audio_initialized = false;
/* ------------------------------- OSS ----------------------------------------- */
/* Thanks to Yair K. for OSS v4 changes. 22-Jan-08 */
#if (HAVE_OSS || HAVE_ALSA || HAVE_JACK_IN_LINUX)
/* actually it's not impossible that someday we'll have ALSA but not OSS... */
#define AUDIO_OK 1
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#if ((SOUND_VERSION > 360) && (defined(OSS_SYSINFO)))
#define NEW_OSS 1
#endif
#define MUS_OSS_WRITE_RATE SNDCTL_DSP_SPEED
#define MUS_OSS_WRITE_CHANNELS SNDCTL_DSP_CHANNELS
#define MUS_OSS_SET_FORMAT SNDCTL_DSP_SETFMT
#define MUS_OSS_GET_FORMATS SNDCTL_DSP_GETFMTS
#define DAC_NAME "/dev/dsp"
#define MIXER_NAME "/dev/mixer"
/* some programs use /dev/audio */
/* there can be more than one sound card installed, and a card can be handled through
* more than one /dev/dsp device, so we can't use a global dac device here.
* The caller has to keep track of the various cards (via AUDIO_SYSTEM) --
* I toyed with embedding all that in mus_audio_open_output and mus_audio_write, but
* decided it's better to keep them explicit -- the caller may want entirely
* different (non-synchronous) streams going to the various cards. This same
* code (AUDIO_SYSTEM(n)->devn) should work in Windoze (see below), and
* might work on the Mac -- something for a rainy day...
*/
#define return_error_exit(Message_Type, Audio_Line, Ur_Message) \
do { \
char *Message; Message = Ur_Message; \
if (Audio_Line != -1) \
linux_audio_close(Audio_Line); \
if ((Message) && (strlen(Message) > 0)) \
{ \
mus_print("%s\n [%s[%d] %s]", \
Message, \
__FILE__, __LINE__, __func__); \
free(Message); \
} \
else mus_print("%s\n [%s[%d] %s]", \
mus_error_type_to_string(Message_Type), \
__FILE__, __LINE__, __func__); \
return(MUS_ERROR); \
} while (false)
static int FRAGMENTS = 4;
static int FRAGMENT_SIZE = 12;
static bool fragments_locked = false;
/* defaults here are FRAGMENTS 16 and FRAGMENT_SIZE 12; these values however
* cause about a .5 second delay, which is not acceptable in "real-time" situations.
*
* this changed 22-May-01: these are causing more trouble than they're worth
*/
static void oss_mus_oss_set_buffers(int num, int size) {FRAGMENTS = num; FRAGMENT_SIZE = size; fragments_locked = true;}
#define MAX_SOUNDCARDS 8
#define MAX_DSPS 8
#define MAX_MIXERS 8
/* there can be (apparently) any number of mixers and dsps per soundcard, but 8 is enough! */
static int *audio_fd = NULL;
static int *audio_open_ctr = NULL;
static int *audio_dsp = NULL;
static int *audio_mixer = NULL;
static int *audio_mode = NULL;
static int sound_cards = 0;
#ifdef NEW_OSS
static int new_oss_running = 0;
#endif
static char *dev_name = NULL;
static char *oss_mus_audio_moniker(void)
{
if (version_name == NULL) version_name = (char *)calloc(LABEL_BUFFER_SIZE, sizeof(char));
if (SOUND_VERSION < 361)
{
char version[LABEL_BUFFER_SIZE];
snprintf(version, LABEL_BUFFER_SIZE, "%d", SOUND_VERSION);
snprintf(version_name, LABEL_BUFFER_SIZE, "OSS %c.%c.%c", version[0], version[1], version[2]);
}
else
snprintf(version_name, LABEL_BUFFER_SIZE, "OSS %x.%x.%x",
(SOUND_VERSION >> 16) & 0xff,
(SOUND_VERSION >> 8) & 0xff,
SOUND_VERSION & 0xff);
return(version_name);
}
static char *dac_name(int sys, int offset)
{
if ((sys < sound_cards) && (audio_mixer[sys] >= -1))
{
snprintf(dev_name, LABEL_BUFFER_SIZE, "%s%d", DAC_NAME, audio_dsp[sys] + offset);
return(dev_name);
}
return((char *)DAC_NAME);
}
#define MIXER_SIZE SOUND_MIXER_NRDEVICES
static int **mixer_state = NULL;
static int *init_srate = NULL, *init_chans = NULL, *init_format = NULL;
static int oss_mus_audio_initialize(void)
{
/* here we need to set up the map of /dev/dsp and /dev/mixer to a given system */
/* since this info is not passed to us by OSS, we have to work at it... */
/* for the time being, I'll ignore auxiliary dsp and mixer ports (each is a special case) */
int amp, old_mixer_amp, old_dsp_amp, new_mixer_amp;
int devmask;
#ifdef NEW_OSS
int status, ignored;
oss_sysinfo sysinfo;
static mixer_info mixinfo;
int sysinfo_ok = 0;
#endif
if (!audio_initialized)
{
int i, num_mixers, num_dsps, nmix, ndsp, err = 0, fd = -1, responsive_field;
audio_initialized = true;
audio_fd = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
audio_open_ctr = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
audio_dsp = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
audio_mixer = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
audio_mode = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
dev_name = (char *)calloc(LABEL_BUFFER_SIZE, sizeof(char));
init_srate = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
init_chans = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
init_format = (int *)calloc(MAX_SOUNDCARDS, sizeof(int));
mixer_state = (int **)calloc(MAX_SOUNDCARDS, sizeof(int *));
for (i = 0; i < MAX_SOUNDCARDS; i++) mixer_state[i] = (int *)calloc(MIXER_SIZE, sizeof(int));
for (i = 0; i < MAX_SOUNDCARDS; i++)
{
audio_fd[i] = -1;
audio_open_ctr[i] = 0;
audio_dsp[i] = -1;
audio_mixer[i] = -1;
}
num_mixers = MAX_MIXERS;
num_dsps = MAX_DSPS;
#ifdef NEW_OSS
fd = open(DAC_NAME, O_WRONLY | O_NONBLOCK, 0);
if (fd == -1) fd = open(MIXER_NAME, O_RDONLY | O_NONBLOCK, 0);
if (fd != -1)
{
status = ioctl(fd, OSS_GETVERSION, &ignored);
new_oss_running = (status == 0);
if (new_oss_running)
{
status = ioctl(fd, OSS_SYSINFO, &sysinfo);
sysinfo_ok = (status == 0);
}
if ((new_oss_running) && (sysinfo_ok))
{
num_mixers = sysinfo.nummixers;
num_dsps = sysinfo.numaudios;
}
close(fd);
}
#endif
/* need to get which /dev/dsp lines match which /dev/mixer lines,
* find out how many separate systems (soundcards) are available,
* fill the audio_dsp and audio_mixer arrays with the system-related numbers,
* since we have no way to tell from OSS info which mixers/dsps are the
* main ones, we'll do some messing aound to try to deduce this info.
* for example, SB uses two dsp ports and two mixers per card, whereas
* Ensoniq uses 2 dsps and 1 mixer.
*
* the data we are gathering here:
* int audio_dsp[MAX_SOUNDCARDS] -> main_dsp_port[n] (-1 => no such system dsp)
* int audio_mixer[MAX_SOUNDCARDS] -> main_mixer_port[n]
* int sound_cards = 0 -> usable systems
* all auxiliary ports are currently ignored (SB equalizer, etc)
*/
sound_cards = 0;
ndsp = 0;
nmix = 0;
while ((nmix < num_mixers) &&
(ndsp < num_dsps))
{
char dname[LABEL_BUFFER_SIZE];
int md;
/* for each mixer, find associated main dsp (assumed to be first in /dev/dsp ordering) */
/* if mixer's dsp overlaps or we run out of dsps first, ignore it (aux mixer) */
/* our by-guess-or-by-gosh method here is to try to open the mixer.
* if that fails, quit (if very first, try at least to get the dsp setup)
* find volume field, if none, go on, else read current volume
* open next unchecked dsp, try to set volume, read current, if different we found a match -- set and go on.
* if no change, move to next dsp and try again, if no more dsps, quit (checking for null case as before)
*/
snprintf(dname, LABEL_BUFFER_SIZE, "%s%d", MIXER_NAME, nmix);
md = open(dname, O_RDWR, 0);
if (md == -1)
{
if (errno == EBUSY)
{
mus_print("%s is busy: can't access it [%s[%d] %s]",
dname,
__FILE__, __LINE__, __func__);
nmix++;
continue;
}
else break;
}
snprintf(dname, LABEL_BUFFER_SIZE, "%s%d", DAC_NAME, ndsp);
fd = open(dname, O_RDWR | O_NONBLOCK, 0);
if (fd == -1) fd = open(dname, O_RDONLY | O_NONBLOCK, 0);
if (fd == -1) fd = open(dname, O_WRONLY | O_NONBLOCK, 0); /* some output devices need this */
if (fd == -1)
{
close(md);
if (errno == EBUSY) /* in linux /usr/include/asm-generic/errno-base.h */
{
fprintf(stderr, "%s is busy: can't access it\n", dname);
ndsp++;
continue;
}
else
{
if ((errno != ENXIO) && (errno != ENODEV) && (errno != ENOENT))
fprintf(stderr, "%s: %s! ", dname, strerror(errno));
break;
}
}
#ifdef NEW_OSS
status = ioctl(md, SOUND_MIXER_INFO, &mixinfo);
#endif
err = ioctl(md, SOUND_MIXER_READ_DEVMASK, &devmask);
responsive_field = SOUND_MIXER_VOLUME;
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
if ((1 << i) & devmask)
{
responsive_field = i;
break;
}
if (!err)
{
err = ioctl(md, MIXER_READ(responsive_field), &old_mixer_amp);
if (!err)
{
err = ioctl(fd, MIXER_READ(responsive_field), &old_dsp_amp);
if ((!err) && (old_dsp_amp == old_mixer_amp))
{
if (old_mixer_amp == 0) amp = 50; else amp = 0; /* 0..100 */
err = ioctl(fd, MIXER_WRITE(responsive_field), &);
if (!err)
{
err = ioctl(md, MIXER_READ(responsive_field), &new_mixer_amp);
if (!err)
{
if (new_mixer_amp == amp)
{
/* found one! */
audio_dsp[sound_cards] = ndsp; ndsp++;
audio_mixer[sound_cards] = nmix; nmix++;
sound_cards++;
}
else ndsp++;
err = ioctl(fd, MIXER_WRITE(responsive_field), &old_dsp_amp);
}
else nmix++;
}
else ndsp++;
}
else ndsp++;
}
else nmix++;
}
else nmix++;
close(fd);
close(md);
}
if (sound_cards == 0)
{
fd = open(DAC_NAME, O_WRONLY | O_NONBLOCK, 0);
if (fd != -1)
{
sound_cards = 1;
audio_dsp[0] = 0;
audio_mixer[0] = -2; /* hmmm -- need a way to see /dev/dsp as lonely outpost */
close(fd);
fd = open(MIXER_NAME, O_RDONLY | O_NONBLOCK, 0);
if (fd == -1)
audio_mixer[0] = -3;
else close(fd);
}
}
}
return(MUS_NO_ERROR);
}
static int linux_audio_open(const char *pathname, int flags, mode_t mode, int system)
{
/* sometimes this is simply searching for a device (so failure is not a mus_error) */
if (audio_fd[system] == -1)
{
audio_fd[system] = open(pathname, flags, mode);
audio_open_ctr[system] = 0;
}
else audio_open_ctr[system]++;
return(audio_fd[system]);
}
static int linux_audio_open_with_error(const char *pathname, int flags, mode_t mode, int system)
{
int fd;
static bool already_warned = false;
if ((system < 0) ||
(system >= MAX_SOUNDCARDS))
return(-1);
fd = linux_audio_open(pathname, flags, mode, system);
if ((fd == -1) &&
(!already_warned))
{
already_warned = true;
mus_standard_io_error(MUS_AUDIO_CANT_OPEN,
((mode == O_RDONLY) ? "open read" :
(mode == O_WRONLY) ? "open write" : "open read/write"),
pathname);
}
return(fd);
}
static int find_system(int line)
{
int i;
for (i = 0; i < sound_cards; i++)
if (line == audio_fd[i])
return(i);
return(MUS_ERROR);
}
static int linux_audio_close(int fd)
{
if (fd != -1)
{
int err = 0, sys;
sys = find_system(fd);
if (sys != -1)
{
if (audio_open_ctr[sys] > 0)
audio_open_ctr[sys]--;
else
{
err = close(fd);
audio_open_ctr[sys] = 0;
audio_fd[sys] = -1;
}
}
else err = close(fd);
if (err) return_error_exit(MUS_AUDIO_CANT_CLOSE, -1,
mus_format("close %d failed: %s",
fd, strerror(errno)));
}
/* is this an error? */
return(MUS_NO_ERROR);
}
static int to_oss_sample_type(mus_sample_t snd_format)
{
switch (snd_format)
{
case MUS_BYTE: return(AFMT_S8); break;
case MUS_BSHORT: return(AFMT_S16_BE); break;
case MUS_UBYTE: return(AFMT_U8); break;
case MUS_MULAW: return(AFMT_MU_LAW); break;
case MUS_ALAW: return(AFMT_A_LAW); break;
case MUS_LSHORT: return(AFMT_S16_LE); break;
case MUS_UBSHORT: return(AFMT_U16_BE); break;
case MUS_ULSHORT: return(AFMT_U16_LE); break;
#ifdef NEW_OSS
case MUS_LINT: return(AFMT_S32_LE); break;
case MUS_BINT: return(AFMT_S32_BE); break;
#endif
default: break;
}
return(MUS_ERROR);
}
static bool fragment_set_failed = false;
static int oss_mus_audio_open_output(int ur_dev, int srate, int chans, mus_sample_t samp_type, int size)
{
int oss_sample_type, buffer_info, audio_out = -1, sys, dev;
char *dev_name;
#ifndef NEW_OSS
int stereo;
#endif
sys = MUS_AUDIO_SYSTEM(ur_dev);
dev = MUS_AUDIO_DEVICE(ur_dev);
oss_sample_type = to_oss_sample_type(samp_type);
if (oss_sample_type == MUS_ERROR)
return_error_exit(MUS_AUDIO_SAMPLE_TYPE_NOT_AVAILABLE, -1,
mus_format("sample type %d (%s) not available",
samp_type,
mus_sample_type_name(samp_type)));
if (dev == MUS_AUDIO_DEFAULT)
audio_out = linux_audio_open_with_error(dev_name = dac_name(sys, 0),
O_WRONLY, 0, sys);
else audio_out = linux_audio_open_with_error(dev_name = dac_name(sys, (dev == MUS_AUDIO_AUX_OUTPUT) ? 1 : 0),
O_RDWR, 0, sys);
if (audio_out == -1) return(MUS_ERROR);
/* ioctl(audio_out, SNDCTL_DSP_RESET, 0); */ /* causes clicks */
if ((fragments_locked) &&
(!(fragment_set_failed)) &&
((dev == MUS_AUDIO_DUPLEX_DEFAULT) ||
(size != 0))) /* only set if user has previously called set_oss_buffers */
{
buffer_info = (FRAGMENTS << 16) | (FRAGMENT_SIZE);
if (ioctl(audio_out, SNDCTL_DSP_SETFRAGMENT, &buffer_info) == -1)
{
/* older Linuces (or OSS's?) refuse to handle the fragment reset if O_RDWR used --
* someone at OSS forgot to update the version number when this was fixed, so
* I have no way to get around this except to try and retry...
*/
linux_audio_close(audio_out);
audio_out = linux_audio_open_with_error(dev_name = dac_name(sys, (dev == MUS_AUDIO_AUX_OUTPUT) ? 1 : 0),
O_WRONLY, 0, sys);
if (audio_out == -1) return(MUS_ERROR);
buffer_info = (FRAGMENTS << 16) | (FRAGMENT_SIZE);
if (ioctl(audio_out, SNDCTL_DSP_SETFRAGMENT, &buffer_info) == -1)
{
char *tmp;
tmp = mus_format("can't set %s fragments to: %d x %d",
dev_name, FRAGMENTS, FRAGMENT_SIZE); /* not an error if ALSA OSS-emulation */
fprintf(stderr, "%s\n", tmp);
fragment_set_failed = true;
free(tmp);
}
}
}
if ((ioctl(audio_out, MUS_OSS_SET_FORMAT, &oss_sample_type) == -1) ||
(oss_sample_type != to_oss_sample_type(samp_type)))
return_error_exit(MUS_AUDIO_SAMPLE_TYPE_NOT_AVAILABLE, audio_out,
mus_format("sample type %d (%s) not available on %s",
samp_type,
mus_sample_type_name(samp_type),
dev_name));
#ifdef NEW_OSS
if (ioctl(audio_out, MUS_OSS_WRITE_CHANNELS, &chans) == -1)
return_error_exit(MUS_AUDIO_CHANNELS_NOT_AVAILABLE, audio_out,
mus_format("can't get %d channels on %s",
chans, dev_name));
#else
if (chans == 2) stereo = 1; else stereo = 0;
if ((ioctl(audio_out, SNDCTL_DSP_STEREO, &stereo) == -1) ||
((chans == 2) && (stereo == 0)))
return_error_exit(MUS_AUDIO_CHANNELS_NOT_AVAILABLE, audio_out,
mus_format("can't get %d channels on %s",
chans, dev_name));
#endif
if (ioctl(audio_out, MUS_OSS_WRITE_RATE, &srate) == -1)
return_error_exit(MUS_AUDIO_SRATE_NOT_AVAILABLE, audio_out,
mus_format("can't set srate of %s to %d",
dev_name, srate));
/* http://www.4front-tech.com/pguide/audio.html says this order has to be followed */
return(audio_out);
}
static int oss_mus_audio_write(int line, char *buf, int bytes)
{
int err;
if (line < 0) return(-1);
errno = 0;
err = write(line, buf, bytes);
if (err != bytes)
{
if (errno != 0)
return_error_exit(MUS_AUDIO_WRITE_ERROR, -1,
mus_format("write error: %s", strerror(errno)));
else return_error_exit(MUS_AUDIO_WRITE_ERROR, -1,
mus_format("wrote %d bytes of requested %d", err, bytes));
}
return(MUS_NO_ERROR);
}
static int oss_mus_audio_close(int line)
{
return(linux_audio_close(line));
}
static int oss_mus_audio_read(int line, char *buf, int bytes)
{
int err;
if (line < 0) return(-1);
errno = 0;
err = read(line, buf, bytes);
if (err != bytes)
{
if (errno != 0)
return_error_exit(MUS_AUDIO_READ_ERROR, -1,
mus_format("read error: %s", strerror(errno)));
else return_error_exit(MUS_AUDIO_READ_ERROR, -1,
mus_format("read %d bytes of requested %d", err, bytes));
}
return(MUS_NO_ERROR);
}
static char *oss_unsrc(int srcbit)
{
if (srcbit == 0)
return(mus_strdup("none"));
else
{
bool need_and = false;
char *buf;
buf = (char *)calloc(PRINT_BUFFER_SIZE, sizeof(char));
if (srcbit & SOUND_MASK_MIC) {need_and = true; strcat(buf, "mic");}
if (srcbit & SOUND_MASK_LINE) {if (need_and) strcat(buf, " and "); need_and = true; strcat(buf, "line in");}
if (srcbit & SOUND_MASK_CD) {if (need_and) strcat(buf, " and "); strcat(buf, "cd");}
return(buf);
}
}
static int oss_mus_audio_open_input(int ur_dev, int srate, int chans, mus_sample_t samp_type, int requested_size)
{
/* dev can be MUS_AUDIO_DEFAULT or MUS_AUDIO_DUPLEX_DEFAULT as well as the obvious others */
int audio_fd = -1, oss_sample_type, buffer_info, sys, dev, srcbit, cursrc, err;
char *dev_name;
#ifndef NEW_OSS
int stereo;
#endif
sys = MUS_AUDIO_SYSTEM(ur_dev);
dev = MUS_AUDIO_DEVICE(ur_dev);
oss_sample_type = to_oss_sample_type(samp_type);
if (oss_sample_type == MUS_ERROR)
return_error_exit(MUS_AUDIO_SAMPLE_TYPE_NOT_AVAILABLE, -1,
mus_format("sample type %d (%s) not available",
samp_type,
mus_sample_type_name(samp_type)));
if (((dev == MUS_AUDIO_DEFAULT) || (dev == MUS_AUDIO_DUPLEX_DEFAULT)) && (sys == 0))
audio_fd = linux_audio_open(dev_name = dac_name(sys, 0),
O_RDWR, 0, sys);
else audio_fd = linux_audio_open(dev_name = dac_name(sys, 0), O_RDONLY, 0, sys);
if (audio_fd == -1)
{
if (dev == MUS_AUDIO_DUPLEX_DEFAULT)
return_error_exit(MUS_AUDIO_CONFIGURATION_NOT_AVAILABLE, -1,
mus_format("can't open %s: %s",
dev_name, strerror(errno)));
if ((audio_fd = linux_audio_open(dev_name = dac_name(sys, 0), O_RDONLY, 0, sys)) == -1)
{
if ((errno == EACCES) || (errno == ENOENT))
return_error_exit(MUS_AUDIO_NO_READ_PERMISSION, -1,
mus_format("can't open %s: %s\n to get input in Linux, we need read permission on /dev/dsp",
dev_name,
strerror(errno)));
else return_error_exit(MUS_AUDIO_NO_INPUT_AVAILABLE, -1,
mus_format("can't open %s: %s",
dev_name,
strerror(errno)));
}
}
#ifdef SNDCTL_DSP_SETDUPLEX
else
ioctl(audio_fd, SNDCTL_DSP_SETDUPLEX, &err); /* not always a no-op! */
#endif
/* need to make sure the desired recording source is active -- does this actually have any effect? */
switch (dev)
{
case MUS_AUDIO_MICROPHONE: srcbit = SOUND_MASK_MIC; break;
case MUS_AUDIO_LINE_IN: srcbit = SOUND_MASK_LINE; break;
case MUS_AUDIO_DUPLEX_DEFAULT:
case MUS_AUDIO_DEFAULT: srcbit = SOUND_MASK_LINE | SOUND_MASK_MIC; break;
default: srcbit = 0; break;
}
ioctl(audio_fd, MIXER_READ(SOUND_MIXER_RECSRC), &cursrc);
srcbit = (srcbit | cursrc);
ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_RECSRC), &srcbit);
ioctl(audio_fd, MIXER_READ(SOUND_MIXER_RECSRC), &cursrc);
if (cursrc != srcbit)
{
char *str1, *str2;
str1 = oss_unsrc(srcbit);
str2 = oss_unsrc(cursrc);
mus_print("weird: tried to set recorder source to %s, but got %s?", str1, str2);
free(str1);
free(str2);
}
if ((fragments_locked) && (requested_size != 0))
{
buffer_info = (FRAGMENTS << 16) | (FRAGMENT_SIZE);
ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &buffer_info);
}
if ((ioctl(audio_fd, MUS_OSS_SET_FORMAT, &oss_sample_type) == -1) ||
(oss_sample_type != to_oss_sample_type(samp_type)))
return_error_exit(MUS_AUDIO_SAMPLE_TYPE_NOT_AVAILABLE, audio_fd,
mus_format("can't set %s sample type to %d (%s)",
dev_name, samp_type,
mus_sample_type_name(samp_type)));
#ifdef NEW_OSS
if (ioctl(audio_fd, MUS_OSS_WRITE_CHANNELS, &chans) == -1)
return_error_exit(MUS_AUDIO_CHANNELS_NOT_AVAILABLE, audio_fd,
mus_format("can't get %d channels on %s",
chans, dev_name));
#else
if (chans == 2) stereo = 1; else stereo = 0;
if ((ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) == -1) ||
((chans == 2) && (stereo == 0)))
return_error_exit(MUS_AUDIO_CHANNELS_NOT_AVAILABLE, audio_fd,
mus_format("can't get %d channels on %s",
chans, dev_name));
#endif
if (ioctl(audio_fd, MUS_OSS_WRITE_RATE, &srate) == -1)
return_error_exit(MUS_AUDIO_SRATE_NOT_AVAILABLE, audio_fd,
mus_format("can't set srate to %d on %s",
srate, dev_name));
return(audio_fd);
}
#if (!HAVE_ALSA)
static int oss_sample_types(int ur_dev, mus_sample_t *val)
{
int fd, samp_types = 0, sys, ind;
sys = MUS_AUDIO_SYSTEM(ur_dev);
/* dev = MUS_AUDIO_DEVICE(ur_dev); */
fd = open(dac_name(sys, 0), O_WRONLY, 0);
if (fd == -1) fd = open(DAC_NAME, O_WRONLY, 0);
if (fd == -1)
{
return_error_exit(MUS_AUDIO_CANT_OPEN, -1,
mus_format("can't open %s: %s",
DAC_NAME, strerror(errno)));
return(MUS_ERROR);
}
ioctl(fd, MUS_OSS_GET_FORMATS, &samp_types);
ind = 1;
if (samp_types & (to_oss_sample_type(MUS_BSHORT))) val[ind++] = MUS_BSHORT;
if (samp_types & (to_oss_sample_type(MUS_LSHORT))) val[ind++] = MUS_LSHORT;
if (samp_types & (to_oss_sample_type(MUS_MULAW))) val[ind++] = MUS_MULAW;
if (samp_types & (to_oss_sample_type(MUS_ALAW))) val[ind++] = MUS_ALAW;
if (samp_types & (to_oss_sample_type(MUS_BYTE))) val[ind++] = MUS_BYTE;
if (samp_types & (to_oss_sample_type(MUS_UBYTE))) val[ind++] = MUS_UBYTE;
if (samp_types & (to_oss_sample_type(MUS_UBSHORT))) val[ind++] = MUS_UBSHORT;
if (samp_types & (to_oss_sample_type(MUS_ULSHORT))) val[ind++] = MUS_ULSHORT;
val[0] = (mus_sample_t)(ind - 1);
return(MUS_NO_ERROR);
}
#endif
/* ------------------------------- ALSA, OSS, Jack-in-Linux ----------------------------------- */
static int api = MUS_ALSA_API;
int mus_audio_api(void) {return(api);}
/* hopefully first call to sndlib will be this... */
static int probe_api(void);
static int (*vect_mus_audio_initialize)(void);
/* FIXME: add a suitable default for all other vectors
so that a call happening before mus_audio_initialize
can be detected */
/* I don't think this is necessary -- documentation discusses this
* (mus_sound_initialize calls mus_audio_initialize)
*/
/* vectors for the rest of the sndlib api */
static void (*vect_mus_oss_set_buffers)(int num, int size);
static char* (*vect_mus_audio_moniker)(void);
static int (*vect_mus_audio_open_output)(int ur_dev, int srate, int chans, mus_sample_t samp_type, int size);
static int (*vect_mus_audio_open_input)(int ur_dev, int srate, int chans, mus_sample_t samp_type, int requested_size);
static int (*vect_mus_audio_write)(int id, char *buf, int bytes);
static int (*vect_mus_audio_read)(int id, char *buf, int bytes);
static int (*vect_mus_audio_close)(int id);
/* vectors for the rest of the sndlib api */
int mus_audio_initialize(void)
{
return(probe_api());
}
void mus_oss_set_buffers(int num, int size)
{
vect_mus_oss_set_buffers(num, size);
}
#if HAVE_ALSA
static char* alsa_mus_audio_moniker(void);
#endif
char* mus_audio_moniker(void)
{
#if (HAVE_OSS && HAVE_ALSA)
char *both_names;
both_names = (char *)calloc(PRINT_BUFFER_SIZE, sizeof(char));
/* need to be careful here since these use the same constant buffer */
strcpy(both_names, oss_mus_audio_moniker());
strcat(both_names, ", ");
strcat(both_names, alsa_mus_audio_moniker());
return(both_names); /* tiny memory leak ... */
#else
return(vect_mus_audio_moniker());
#endif
}
int mus_audio_open_output(int ur_dev, int srate, int chans, mus_sample_t samp_type, int size)
{
return(vect_mus_audio_open_output(ur_dev, srate, chans, samp_type, size));
}
int mus_audio_open_input(int ur_dev, int srate, int chans, mus_sample_t samp_type, int requested_size)
{
return(vect_mus_audio_open_input(ur_dev, srate, chans, samp_type, requested_size));
}
int mus_audio_write(int id, char *buf, int bytes)
{
return(vect_mus_audio_write(id, buf, bytes));
}
int mus_audio_read(int id, char *buf, int bytes)
{
return(vect_mus_audio_read(id, buf, bytes));
}
int mus_audio_close(int id)
{
return(vect_mus_audio_close(id));
}
#if HAVE_JACK_IN_LINUX
static int jack_mus_audio_initialize(void);
#endif
#if (!HAVE_ALSA)
static int probe_api(void)
{
#if HAVE_JACK_IN_LINUX
{
int jackprobe = jack_mus_audio_initialize();
if (jackprobe == MUS_ERROR)
{
#endif
/* go for the oss api */
api = MUS_OSS_API;
vect_mus_audio_initialize = oss_mus_audio_initialize;
vect_mus_oss_set_buffers = oss_mus_oss_set_buffers;
vect_mus_audio_moniker = oss_mus_audio_moniker;
vect_mus_audio_open_output = oss_mus_audio_open_output;
vect_mus_audio_open_input = oss_mus_audio_open_input;
vect_mus_audio_write = oss_mus_audio_write;
vect_mus_audio_read = oss_mus_audio_read;
vect_mus_audio_close = oss_mus_audio_close;
return(vect_mus_audio_initialize());
#if HAVE_JACK_IN_LINUX
}
return(jackprobe);
}
#endif
}
#endif
#endif
/* ------------------------------- ALSA ----------------------------------------- */
/*
* Changed the names of the environment variables to use MUS, not SNDLIB.
* reformatted and reorganized to be like the rest of the code
* changed default device to "default"
* -- Bill 3-Feb-06
*
* error handling (mus_error) changed by Bill 14-Nov-02
* 0.5 support removed by Bill 24-Mar-02
*
* changed for 0.9.x api by Fernando Lopez-Lezcano <nando@ccrma.stanford.edu>
*
* sndlib "exports" only one soundcard with two directions (if they are available),
* and only deals with the alsa library pcm's. It does not scan for available
* cards and devices at the hardware level. Which device it uses can be defined by:
*
* - setting variables in the environment (searched for in the following order):
* MUS_ALSA_PLAYBACK_DEVICE
* defines the name of the playback device
* MUS_ALSA_CAPTURE_DEVICE
* defines the name of the capture device
* MUS_ALSA_DEVICE
* defines the name of the playback and capture device
* use the first two if the playback and capture devices are different or the
* third if they are the same.
* - if no variables are found in the environment sndlib tries to probe for a
* default device named "sndlib" (in alsa 0.9 devices are configured in
* /usr/share/alsa/alsa.conf or in ~/.asoundrc)
* - if "sndlib" is not a valid device "hw:0,0" was used [but now it looks for "default"] (which by default should
* point to the first device of the first card
*
* Some default settings are controllable through the environment as well:
* MUS_ALSA_BUFFER_SIZE = size of each buffer in frames
* MUS_ALSA_BUFFERS = number of buffers
*
* changed 18-Sep-00 by Bill: new error handling: old mus_audio_error folded into
* mus_error; mus_error itself should be used only for "real" errors -- things
* that can cause a throw (a kind of global jump elsewhere); use mus_print for informational
* stuff -- in Snd, mus_print will also save everything printed in the error dialog.
* In a few cases, I tried to fix the code to unwind before mus_error, and in others
* I've changed mus_error to mus_print, but some of these may be mistaken.
* Look for ?? below for areas where I'm not sure I rewrote code correctly.
*
* changed for 0.6.x api by Paul Barton-Davis, pbd@op.net
*
* changed for 0.5.x api by Fernando Lopez-Lezcano, nando@ccrma.stanford.edu
* 04-10-2000:
* based on original 0.4.x code by Paul Barton-Davis (not much left of it :-)
* also Bill's code and Jaroslav Kysela (aplay.c and friends)
*
* Changes:
* 04/25/2000: finished major rework, snd-dac now automatically decides which
* device or devices it uses for playback. Multiple device use is
* for now restricted to only two at most (more changes in Bill's
* needed to be able to support more). Four channel playback in
* Ensoniq AudioPCI and relatives possible (with proper settings
* of the mixer) as well as using two separate cards.
* 04/11/2000: added reporting of alsa sound formats
*/
#if HAVE_ALSA
#if (!HAVE_OSS)
#define AUDIO_OK 1
#endif
#include <sys/ioctl.h>
#if HAVE_ALSA
#include <alsa/asoundlib.h>
#else
#include <sys/asoundlib.h>
#endif
#if SND_LIB_VERSION < ((0<<16)|(6<<8)|(0))
#error ALSA version is too old -- audio.c needs 0.9 or later
#endif
/* prototypes for the alsa sndlib functions */
static int alsa_mus_audio_initialize(void);
static void alsa_mus_oss_set_buffers(int num, int size);
static int alsa_mus_audio_open_output(int ur_dev, int srate, int chans, mus_sample_t samp_type, int size);
static int alsa_mus_audio_open_input(int ur_dev, int srate, int chans, mus_sample_t samp_type, int requested_size);
static int alsa_mus_audio_write(int id, char *buf, int bytes);
static int alsa_mus_audio_read(int id, char *buf, int bytes);
static int alsa_mus_audio_close(int id);
/* decide which api to activate */
static int probe_api(void)
{
#if HAVE_JACK_IN_LINUX
int jackprobe;
jackprobe = jack_mus_audio_initialize();
if (jackprobe == MUS_ERROR)
{
#endif
int card = -1;
if ((snd_card_next(&card) >= 0) && (card >= 0))
{
/* the alsa library has detected one or more cards */
api = MUS_ALSA_API;
vect_mus_audio_initialize = alsa_mus_audio_initialize;
vect_mus_oss_set_buffers = alsa_mus_oss_set_buffers;