xref: /freebsd/contrib/ntp/util/audio-pcm.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*
2  * audio-pcm.c - Scope out the PCM audio stuff
3  */
4 #ifdef HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7 
8 #if defined(HAVE_MACHINE_SOUNDCARD_H) || defined(HAVE_SYS_SOUNDCARD_H)
9 
10 #include "audio.h"
11 #include "ntp_stdlib.h"
12 #include "ntp_syslog.h"
13 #ifdef HAVE_UNISTD_H
14 # include <unistd.h>
15 #endif
16 #include <stdio.h>
17 #include "ntp_string.h"
18 
19 #ifdef HAVE_SYS_IOCTL_H
20 #include <sys/ioctl.h>
21 #endif /* HAVE_SYS_IOCTL_H */
22 
23 #include <fcntl.h>
24 
25 #ifdef HAVE_MACHINE_SOUNDCARD_H
26 # include <machine/soundcard.h>
27 # define PCM_STYLE_SOUND
28 #else
29 # ifdef HAVE_SYS_SOUNDCARD_H
30 #  include <sys/soundcard.h>
31 #  define PCM_STYLE_SOUND
32 # endif
33 #endif
34 
35 /*
36  * Global variables
37  */
38 static int ctl_fd;		/* audio control file descriptor */
39 
40 const char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES ;
41 
42 void
43 d_fmt(
44       unsigned int format
45       )
46 {
47 
48   if (format & AFMT_MU_LAW)	printf("MU_LAW ");
49   if (format & AFMT_A_LAW)	printf("A_LAW ");
50   if (format & AFMT_IMA_ADPCM)	printf("IMA_ADPCM ");
51   if (format & AFMT_U8)		printf("U8 ");
52   if (format & AFMT_S16_LE)	printf("S16_LE ");
53   if (format & AFMT_S16_BE)	printf("S16_BE ");
54   if (format & AFMT_S8)		printf("S8 ");
55   if (format & AFMT_U16_LE)	printf("U16_LE ");
56   if (format & AFMT_U16_BE)	printf("U16_BE ");
57   if (format & AFMT_MPEG)	printf("MPEG ");
58   if (format & AFMT_AC3)	printf("AC3 ");
59   printf("\n");
60 }
61 
62 void
63 d_mixer(
64 	unsigned int mixer
65 	)
66 {
67   int i;
68   int n = 0;
69 
70   for (i = 0; i < SOUND_MIXER_NRDEVICES; ++i)
71     if ((1 << i) & mixer) {
72       if (n)
73 	printf(", ");
74       printf("%s", m_names[i]);
75       n = 1;
76     }
77   printf("\n");
78 }
79 
80 int
81 main( )
82 {
83 	int	unit = 0;	/* device unit (0-3) */
84 # define AI_DEV		"/dev/audio%d"
85 # define AC_DEV		"/dev/mixer%d"
86 	char ai_dev[30];
87 	char ac_dev[30];
88 	struct snd_size s_size;
89 	snd_chan_param s_c_p;
90 	snd_capabilities s_c;
91 	int fd;
92 	int rval;
93 	char *dname = ai_dev;		/* device name */
94 	char *actl = ac_dev;
95 	int devmask = 0, recmask = 0, recsrc = 0;
96 
97 	snprintf(ai_dev, sizeof(ai_dev), AI_DEV, unit);
98 	snprintf(ac_dev, sizeof(ac_dev), AC_DEV, unit);
99 
100 	/*
101 	 * Open audio device. Do not complain if not there.
102 	 */
103 	fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
104 	if (fd < 0)
105 		return (fd);
106 
107 	/*
108 	 * Open audio control device.
109 	 */
110 	ctl_fd = open(actl, O_RDWR);
111 	if (ctl_fd < 0) {
112 		fprintf(stderr, "invalid control device <%s>\n", actl);
113 		close(fd);
114 		return(ctl_fd);
115 	}
116 
117 	printf("input:   <%s> %d\n", dname, fd);
118 	printf("control: <%s> %d\n", actl, ctl_fd);
119 
120 	if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
121 	    printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
122 	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
123 	    printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));
124 	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
125 	    printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));
126 
127 	printf("devmask: %#x recmask: %#x recsrc: %#x\n",
128 		devmask, recmask, recsrc);
129 	printf("Devmask: "); d_mixer(devmask);
130 	printf("Recmask: "); d_mixer(recmask);
131 	printf("RecSrc:  "); d_mixer(recsrc);
132 
133 	/*
134 	 * Set audio device parameters.
135 	 */
136 	rval = fd;
137 
138 	if (ioctl(fd, AIOGSIZE, &s_size) == -1)
139 	    printf("AIOGSIZE: %s\n", strerror(errno));
140 	else
141 	    printf("play_size %d, rec_size %d\n",
142 		s_size.play_size, s_size.rec_size);
143 
144 	if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
145 	    printf("AIOGFMT: %s\n", strerror(errno));
146 	else {
147 	  printf("play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
148 		 s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
149 	  printf("Play format: "); d_fmt(s_c_p.play_format);
150 	  printf("Rec format:  "); d_fmt(s_c_p.rec_format);
151 	}
152 
153 }
154 #endif /* HAVE_{MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */
155