1 /*****************************************************************************/
4 * play -- play "wav" files, through the an audio driver.
6 * (C) Copyright 1999-2001, Greg Ungerer (gerg@snapgear.com)
7 * (C) Copyright 2000-2001, Lineo Inc. (www.lineo.com)
10 /*****************************************************************************/
18 #include <linux/soundcard.h>
22 /*****************************************************************************/
24 #define DACDEVICE "/dev/dsp"
28 unsigned char buf[16*1024];
30 struct wavefileheader hdr;
31 struct wavedataheader datahdr;
35 * Endian re-arranger :-)
37 #define SWAPLONG(a) ((((a) & 0xff000000) >> 24) | \
38 (((a) & 0x00ff0000) >> 8) | \
39 (((a) & 0x0000ff00) << 8) | \
40 (((a) & 0x000000ff) << 24))
42 #define SWAPSHORT(a) (((a) >> 8) | ((a) << 8))
44 /*****************************************************************************/
48 printf("usage: play [-vrmsbw] [-f frequency] filename\n\n");
49 printf(" -v verbose mode\n");
50 printf(" -r raw data file (not wav)\n");
51 printf(" -m mono mode\n");
52 printf(" -s stereo mode\n");
53 printf(" -b 8bits per sample (byte)\n");
54 printf(" -w 16bits per sample (word)\n");
55 printf(" -f set replay refquency\n");
59 /*****************************************************************************/
61 int main(int argc, char *argv[])
64 int ofd, ifd, i, j, n;
65 int freq, chans, bitspersample;
68 /* Default to not set for now */
73 while ((c = getopt(argc, argv, "vrmswbf:")) > 0) {
105 filename = argv[optind];
106 if ((ifd = open(filename, O_RDONLY)) < 1) {
107 printf("ERROR: failed to wave file %s\n", filename);
110 if ((ofd = open(DACDEVICE, O_RDWR)) < 1) {
111 printf("ERROR: failed to open DAC device %s\n", DACDEVICE);
116 /* Parse wav header... */
117 if ((n = read(ifd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
118 printf("ERROR: failed to read(), errno=%d\n", errno);
122 hdr.filesize = SWAPLONG(hdr.filesize);
123 hdr.formattag = SWAPSHORT(hdr.formattag);
124 hdr.channels = SWAPSHORT(hdr.channels);
125 hdr.samplerate = SWAPLONG(hdr.samplerate);
126 hdr.bytespersec = SWAPLONG(hdr.bytespersec);
127 hdr.samplesize = SWAPSHORT(hdr.samplesize);
128 hdr.bitspersample = SWAPSHORT(hdr.bitspersample);
131 printf("FILE=%s\n Type=0x%x\n File Length=%d\n",
132 filename, hdr.riffid, hdr.filesize);
133 printf(" Channels=%d\n Sample Rate=%d\n Bytes Per "
134 "Second=%d\n Sample Size=%d\n "
135 "Bits per Sample=%d\n", hdr.channels,
136 hdr.samplerate, hdr.bytespersec,
137 hdr.samplesize, hdr.bitspersample);
140 if (hdr.riffid != WAV_RIFFTYPE) {
141 printf("ERROR: unknown file type, riffid=%x??\n",
145 if (hdr.waveid != WAV_WAVETYPE) {
146 printf("ERROR: unknown file type, waveid=%x??\n",
150 if (hdr.fmtid != WAV_FMTTYPE) {
151 printf("ERROR: unknown file type, fmtid=%x??\n",
157 freq = hdr.samplerate;
159 chans = hdr.channels;
160 if (bitspersample == 0)
161 bitspersample = hdr.bitspersample;
164 /* Set any remaining defaults if neccessary */
169 if (bitspersample == 0)
172 if (ioctl(ofd, SNDCTL_DSP_SPEED, &freq) < 0) {
173 printf("ERROR: ioctl(SNDCTL_DSP_SPEED,freq) failed, "
174 "errno=%d\n", errno);
177 i = (chans == 2) ? 1 : 0;
178 if (ioctl(ofd, SNDCTL_DSP_STEREO, &i) < 0) {
179 printf("ERROR: ioctl(SNDCTL_DSP_STEREO,0) failed, "
180 "errno=%d\n", errno);
183 i = (bitspersample == 16) ? AFMT_S16_LE : AFMT_U8;
184 if (ioctl(ofd, SNDCTL_DSP_SAMPLESIZE, &i) < 0) {
185 printf("ERROR: ioctl(SNDCTL_DSP_SAMPLESIZE,8) failed, "
186 "errno=%d\n", errno);
190 /* Parse data header, and ignore... */
191 if ((n = read(ifd, &datahdr, sizeof(datahdr))) != sizeof(datahdr)) {
192 printf("ERROR: failed to read(), errno=%d\n", errno);
197 printf("PLAYING at: freq=%d chans=%d bitspersample=%d\n", freq, chans, bitspersample);
200 while ((n = read(ifd, buf, sizeof(buf))) > 0) {
202 if ((hdr.bitspersample == 16) && (bitspersample == 8)) {
203 /* Make audio track 8 bits only */
204 for (i = 1, j = 0; (i < n); i += 2, j++)
208 if ((hdr.channels == 2) && (chans == 1)) {
209 /* Make audio track single channel only */
210 for (i = 2, j = 1; (i < n); i += 2, j++)
215 if (write(ofd, buf, n) != n) {
216 printf("ERROR: write(%s) failed, errno=%d\n",
226 /*****************************************************************************/