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