嵌入式软件工程师&&太极拳
全部博文(548)
分类:
2011-02-20 19:51:52
#include#include #include #include #include #include #include #include #define LENGTH 3 /* 存储秒数 */ #define RATE 8000 /* 采样频率 */ #define SIZE 8 /* 量化位数 */ #define CHANNELS 1 /* 声道数目 */ unsigned char buf[LENGTH * RATE * SIZE * CHANNELS / 8]; int main(void) { int fd; int arg; int status; fd = open("/dev/dsp", O_RDWR); if(fd < 0) { perror("open of /dev/dsp failed"); exit(1); } arg = SIZE; status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); /* 设置采样位数 */ if(status == -1) { perror("SOUND_PCM_WRITE_BITS ioctl failed"); } if(arg != SIZE) { perror("unable to set sample size"); } arg = CHANNELS; status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); /* 设置采样时的通道数 */ if(-1 == status) { perror("SOUND_PCM_WRITE_CHANNELS iotcl failed"); } if(CHANNELS != arg) { perror("unable to set number of channels"); } arg = RATE; status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); /* 设置采样率 */ if(-1 == status) { perror("SOUND_PCM_WRITE_RATE ioctl failed"); } while(1) { printf("Say something:\n"); status = read(fd, buf, sizeof(buf)); /* 录音 */ if(sizeof(buf) != status) { perror("read wrong number of bytes"); } printf("You said:\n"); status = write(fd, buf, sizeof(buf)); /* 放音 */ if(sizeof(buf) != status) { perror("wrote wrong number of bytes"); } status = ioctl(fd, SOUND_PCM_SYNC, 0); /* 在继续录音前等待放音完毕 */ if(-1 == status) { perror("SOUND_PCM_SYNC ioctl failed"); } } return 0; }