/* * Copyright (c) 1998-2001 Yoshihide SONODA * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include #include #include #include #include #include #include #if defined (__FreeBSD__) && __FreeBSD__ < 4 #include #else #include #endif #include "wavefmt.h" #ifndef DEFAULT_DSP #define DEFAULT_DSP "/dev/dspW" #endif #ifndef DEFAULT_BUFFERSIZE #define DEFAULT_BUFFERSIZE 2048 #endif int openDSP(const char* devname, PWAVEFORMATEX pwf); int closeDSP(int fd); char buf[1024]; int main(void) { WAVEFORMATEX waveFormatEx; int fd, i, cnt; waveFormatEx.wFormatTag = WAVE_FORMAT_PCM; waveFormatEx.nChannels = 1; waveFormatEx.nSamplesPerSec = 22050; waveFormatEx.nAvgBytesPerSec = 44100; waveFormatEx.nBlockAlign = 2; waveFormatEx.wBitsPerSample = 16; waveFormatEx.cbSize = 0; fd = openDSP (DEFAULT_DSP, &waveFormatEx); if (fd == -1) { perror ("open"); exit(-1); } for (i = 0; i < 1024; i++) { cnt = read (fd, &buf, 1024); if (cnt == -1) { perror ("read failed\n"); exit(-1); } write (fileno(stdout), buf, 1024); } closeDSP(fd); } int openDSP(const char* devname, PWAVEFORMATEX pwf) { int fd; int status; int arg; if ((fd = open(devname, O_RDONLY, 0)) == -1) return fd; arg = (int)(pwf->nChannels); status = ioctl(fd, SOUND_PCM_READ_CHANNELS, &arg); if (status < 0) { perror("openDSP"); close(fd); return -1; } if (arg != (int)(pwf->nChannels)) { fprintf(stderr, "Can't set channels.\n"); close(fd); return -1; } arg = (int)(pwf->nSamplesPerSec); status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); if (status < 0) { perror("openDSP"); fprintf(stderr, "Can't set sampling rate.\n"); close(fd); return -1; } if (arg != (int)pwf->nSamplesPerSec) { fprintf(stderr, "Warning: Can't set sampling rate %d Hz.\n", (int)pwf->nSamplesPerSec); fprintf(stderr, "Using %d Hz instead.\n", arg); } #ifdef DEBUG printf("DSP - Sampling rate: %d\n", arg); #endif #ifdef notdef arg = (int)(pwf->wBitsPerSample); status = ioctl(fd, SOUND_PCM_READ_BITS, &arg); if (status < 0) { perror("openDSP"); close(fd); return -1; } if (arg != (int)(pwf->wBitsPerSample)) { fprintf(stderr, "Can't set bit width.\n"); close(fd); return -1; } #endif return fd; } int closeDSP(int fd) { ioctl(fd, SNDCTL_DSP_SYNC); return close(fd); }