//====================================================================== // speaker.cpp -- small application that reads audio samples // from standar output and writes them to the audio device. // Author: Agustin J. Gonzalez // October 2002 //====================================================================== #include #include // open requires #include #include #include // Linux IOCTL and soundcard #include #include // close requires: #include // errno #include int main(int argc, char * argv[]) { int fd, value, nSamples; char buff[256]; printf("usage: %s [sample rate in Hz]\n", argv[0]); if ((fd = open("/dev/dsp", O_WRONLY)) < 0) { if ((errno == EINTR) || (errno == EBUSY)) { printf("Audio device is Busy... \n"); exit(-1); } printf("Error opening audio device ... \n"); exit(-1); } // Set to Mono value=0; ioctl(fd,SNDCTL_DSP_STEREO,&value); // Set Audio format to the 8 bits per sample. value=AFMT_S8; ioctl(fd,SNDCTL_DSP_SETFMT, &value); // Set sample rate if (argc==2) value= atoi(argv[1]); else value=8000; ioctl(fd,SNDCTL_DSP_SPEED, &value); while(1) { nSamples=read(0, buff, sizeof(buff)); write(fd, buff, nSamples); } }