?? p6.8.c
字號:
#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ioctl.h>#include <stdlib.h>#include <stdio.h>#include <linux/soundcard.h>//錄音時間#define LENGTH 3 //采樣頻率#define RATE 8000 //量化位數#define SIZE 16// 聲道數目#define CHANNELS 2//保存錄取的音頻數據 unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];int main(void){ //聲音設備的文件描述符 int fd; int arg; //用于保存ioctl的返回值 int status; // 打開聲音設備 fd = open("/dev/dsp", O_RDWR); if (fd < 0) { perror("Cannot open /dev/dsp device"); return 1; } /*=========以下設置聲卡參數========*/ // 設置采樣時的量化位數 arg = SIZE; status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); if (status == -1){ perror("Cannot set SOUND_PCM_WRITE_BITS "); return 1; } // 設置采樣時的聲道數目 arg = CHANNELS; status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1){ perror("Cannot set SOUND_PCM_WRITE_CHANNELS"); return 1; } // 設置采樣時的采樣頻率 arg = RATE; status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); if (status == -1){ perror("Cannot set SOUND_PCM_WRITE_WRITE"); return 1; } //一直錄音,直到按下Control-C停止 while (1) { printf("Recording...:\n"); status = read(fd, buf, sizeof(buf)); if (status != sizeof(buf)){ perror("read wrong number of bytes"); } printf("Play...:\n"); status = write(fd, buf, sizeof(buf)); if (status != sizeof(buf)) perror("wrote wrong number of bytes"); // 在繼續錄音前等待回放結束 status = ioctl(fd, SOUND_PCM_SYNC, 0); if (status == -1) perror("Cannot set SOUND_PCM_SYNC"); } return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -