1132451Sroberto/*
2132451Sroberto * audio-pcm.c - Scope out the PCM audio stuff
3132451Sroberto */
4132451Sroberto#ifdef HAVE_CONFIG_H
5132451Sroberto# include <config.h>
6132451Sroberto#endif
7132451Sroberto
8132451Sroberto#if defined(HAVE_MACHINE_SOUNDCARD_H) || defined(HAVE_SYS_SOUNDCARD_H)
9132451Sroberto
10132451Sroberto#include "audio.h"
11132451Sroberto#include "ntp_stdlib.h"
12132451Sroberto#include "ntp_syslog.h"
13132451Sroberto#ifdef HAVE_UNISTD_H
14132451Sroberto# include <unistd.h>
15132451Sroberto#endif
16132451Sroberto#include <stdio.h>
17132451Sroberto#include "ntp_string.h"
18132451Sroberto
19132451Sroberto#ifdef HAVE_SYS_IOCTL_H
20132451Sroberto#include <sys/ioctl.h>
21132451Sroberto#endif /* HAVE_SYS_IOCTL_H */
22132451Sroberto
23132451Sroberto#include <fcntl.h>
24132451Sroberto
25132451Sroberto#ifdef HAVE_MACHINE_SOUNDCARD_H
26132451Sroberto# include <machine/soundcard.h>
27132451Sroberto# define PCM_STYLE_SOUND
28132451Sroberto#else
29132451Sroberto# ifdef HAVE_SYS_SOUNDCARD_H
30132451Sroberto#  include <sys/soundcard.h>
31132451Sroberto#  define PCM_STYLE_SOUND
32132451Sroberto# endif
33132451Sroberto#endif
34132451Sroberto
35132451Sroberto/*
36132451Sroberto * Global variables
37132451Sroberto */
38132451Srobertostatic int ctl_fd;		/* audio control file descriptor */
39132451Sroberto
40132451Srobertoconst char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES ;
41132451Sroberto
42132451Srobertovoid
43132451Srobertod_fmt(
44132451Sroberto      unsigned int format
45132451Sroberto      )
46132451Sroberto{
47132451Sroberto
48132451Sroberto  if (format & AFMT_MU_LAW)	printf("MU_LAW ");
49132451Sroberto  if (format & AFMT_A_LAW)	printf("A_LAW ");
50132451Sroberto  if (format & AFMT_IMA_ADPCM)	printf("IMA_ADPCM ");
51132451Sroberto  if (format & AFMT_U8)		printf("U8 ");
52132451Sroberto  if (format & AFMT_S16_LE)	printf("S16_LE ");
53132451Sroberto  if (format & AFMT_S16_BE)	printf("S16_BE ");
54132451Sroberto  if (format & AFMT_S8)		printf("S8 ");
55132451Sroberto  if (format & AFMT_U16_LE)	printf("U16_LE ");
56132451Sroberto  if (format & AFMT_U16_BE)	printf("U16_BE ");
57132451Sroberto  if (format & AFMT_MPEG)	printf("MPEG ");
58132451Sroberto  if (format & AFMT_AC3)	printf("AC3 ");
59132451Sroberto  printf("\n");
60132451Sroberto}
61132451Sroberto
62132451Srobertovoid
63132451Srobertod_mixer(
64132451Sroberto	unsigned int mixer
65132451Sroberto	)
66132451Sroberto{
67132451Sroberto  int i;
68132451Sroberto  int n = 0;
69132451Sroberto
70132451Sroberto  for (i = 0; i < SOUND_MIXER_NRDEVICES; ++i)
71132451Sroberto    if ((1 << i) & mixer) {
72132451Sroberto      if (n)
73132451Sroberto	printf(", ");
74132451Sroberto      printf("%s", m_names[i]);
75132451Sroberto      n = 1;
76132451Sroberto    }
77132451Sroberto  printf("\n");
78132451Sroberto}
79132451Sroberto
80132451Srobertoint
81132451Srobertomain( )
82132451Sroberto{
83132451Sroberto	int	unit = 0;	/* device unit (0-3) */
84132451Sroberto# define AI_DEV		"/dev/audio%d"
85132451Sroberto# define AC_DEV		"/dev/mixer%d"
86132451Sroberto	char ai_dev[30];
87132451Sroberto	char ac_dev[30];
88132451Sroberto	struct snd_size s_size;
89132451Sroberto	snd_chan_param s_c_p;
90132451Sroberto	snd_capabilities s_c;
91132451Sroberto	int fd;
92132451Sroberto	int rval;
93132451Sroberto	char *dname = ai_dev;		/* device name */
94132451Sroberto	char *actl = ac_dev;
95132451Sroberto	int devmask = 0, recmask = 0, recsrc = 0;
96132451Sroberto
97280849Scy	snprintf(ai_dev, sizeof(ai_dev), AI_DEV, unit);
98280849Scy	snprintf(ac_dev, sizeof(ac_dev), AC_DEV, unit);
99132451Sroberto
100132451Sroberto	/*
101132451Sroberto	 * Open audio device. Do not complain if not there.
102132451Sroberto	 */
103132451Sroberto	fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
104132451Sroberto	if (fd < 0)
105132451Sroberto		return (fd);
106132451Sroberto
107132451Sroberto	/*
108132451Sroberto	 * Open audio control device.
109132451Sroberto	 */
110132451Sroberto	ctl_fd = open(actl, O_RDWR);
111132451Sroberto	if (ctl_fd < 0) {
112132451Sroberto		fprintf(stderr, "invalid control device <%s>\n", actl);
113132451Sroberto		close(fd);
114132451Sroberto		return(ctl_fd);
115132451Sroberto	}
116132451Sroberto
117132451Sroberto	printf("input:   <%s> %d\n", dname, fd);
118132451Sroberto	printf("control: <%s> %d\n", actl, ctl_fd);
119132451Sroberto
120132451Sroberto	if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
121132451Sroberto	    printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
122132451Sroberto	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
123132451Sroberto	    printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));
124132451Sroberto	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
125132451Sroberto	    printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));
126132451Sroberto
127132451Sroberto	printf("devmask: %#x recmask: %#x recsrc: %#x\n",
128132451Sroberto		devmask, recmask, recsrc);
129132451Sroberto	printf("Devmask: "); d_mixer(devmask);
130132451Sroberto	printf("Recmask: "); d_mixer(recmask);
131132451Sroberto	printf("RecSrc:  "); d_mixer(recsrc);
132132451Sroberto
133132451Sroberto	/*
134132451Sroberto	 * Set audio device parameters.
135132451Sroberto	 */
136132451Sroberto	rval = fd;
137132451Sroberto
138132451Sroberto	if (ioctl(fd, AIOGSIZE, &s_size) == -1)
139132451Sroberto	    printf("AIOGSIZE: %s\n", strerror(errno));
140132451Sroberto	else
141132451Sroberto	    printf("play_size %d, rec_size %d\n",
142132451Sroberto		s_size.play_size, s_size.rec_size);
143132451Sroberto
144132451Sroberto	if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
145132451Sroberto	    printf("AIOGFMT: %s\n", strerror(errno));
146132451Sroberto	else {
147132451Sroberto	  printf("play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
148132451Sroberto		 s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
149132451Sroberto	  printf("Play format: "); d_fmt(s_c_p.play_format);
150132451Sroberto	  printf("Rec format:  "); d_fmt(s_c_p.rec_format);
151132451Sroberto	}
152132451Sroberto
153132451Sroberto}
154132451Sroberto#endif /* HAVE_{MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */
155