//====================================================================== // mic.cpp -- small application that reads audio samples // from audio device and throws them to the standard output. // 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_RDONLY)) < 0) { if ((errno == EINTR) || (errno == EBUSY)) { printf("Audio device is Busy... \n"); exit(-1); } printf("Error opening audio device ... \n"); exit(-1); } printf("Apertura OK\n"); // 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) { // printf("before read OK\n"); nSamples=read(fd, buff, sizeof(buff)); printf("%i %s\n", nSamples,buff ); write(STDOUT_FILENO, buff, nSamples); } }