录音

作者: zjh3029 | 来源:发表于2018-04-07 12:11 被阅读0次

一:第一种方式:

#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 44100  // 采样频率  
#define SIZE 16     // 量化位数  
#define CHANNELS 2  // 声道数目  
/* 用于保存数字音频数据的内存缓冲区*/  
unsigned charbuf[LENGTH*RATE*SIZE*CHANNELS/8];  
int main()  
{  
                intfd;     // 声音设备的文件描述符  
                intarg;    // 用于ioctl调用的参数  
                intstatus; // 系统调用的返回值      
                /*打开声音设备 */  
                fd= open("/dev/dsp", O_RDONLY);  
                if(fd< 0)  
                {  
                        perror("openof /dev/sound/dsp failed");  
                        exit(1);  
                }  
   
                /*设置采样时的量化位数 */  
                arg= SIZE;  
                status= ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);  
                if(status== -1)  
                        perror("SOUND_PCM_WRITE_BITSioctl failed");  
                if(arg!= SIZE)  
                        perror("unableto set sample size");  
                /*设置采样时的声道数目 */  
                arg= CHANNELS;  
                status= ioctl(fd, SNDCTL_DSP_STEREO, &arg);  
                if(status== -1)  
                        perror("SNDCTL_DSP_STEREOioctl failed");  
                if(arg!= CHANNELS)  
                        perror("unableto set number of channels");  
                /*设置采样时的采样频率 */  
                arg= RATE;  
                status= ioctl(fd, SNDCTL_DSP_SPEED, &arg);  
                if(status== -1)  
                        perror("SNDCTL_DSP_SPEEDioctl failed");  
                if(arg!= RATE)  
                        perror("unableto set rate");  
   
                printf("Saysomething:\n");  
                status= read(fd, buf, sizeof(buf)); //recording  
                if(status!= sizeof(buf))  
                        perror("readwrong number of bytes");  
                printf("Yousaid:\n");  
                close(fd);  
                fd= open("/dev/sound/dsp", O_WRONLY);  
                if(fd< 0)  
                {  
                        perror("openof /dev/sound/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 toset sample size");  
                /*设置采样时的声道数目 */  
                arg = CHANNELS;  
                status = ioctl(fd, SNDCTL_DSP_STEREO,&arg);  
                if(status == -1)  
                       perror("SNDCTL_DSP_STEREO ioctl failed");  
                if(arg != CHANNELS)  
                        perror("unable toset number of channels");  
                /* 设置采样时的采样频率 */  
                arg = RATE;  
                status = ioctl(fd,SNDCTL_DSP_SPEED, &arg);  
                if(status == -1)  
                       perror("SNDCTL_DSP_SPEED ioctl failed");  
                if(arg != RATE)  
                        perror("unable toset rate");  
   
                status= write(fd, buf, sizeof(buf)); //playing  
                if(status!= sizeof(buf))  
                        perror("wrotewrong number of bytes");  
                close(fd);  
                return0;  
}  

第二种方式:

相关文章

网友评论

      本文标题:录音

      本文链接:https://www.haomeiwen.com/subject/wavdqftx.html