⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fork_descriptor.c

📁 UDA1431 语音芯片的驱动源码及测试程序。在s3c2440开发板linux2.6内核实现语音双工。使用方法:在内核源码中找到相应的C源文件替换即可。压缩包内有语音同时输入输出的测试程序。
💻 C
字号:
#include<string.h>
#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 FORMAT  AFMT_S16_LE  /*采样格式 */
#define LENGTH 3  /* 存储秒数 */
#define RATE 44100   /* 采样频率 */
#define SIZE 16     /* 量化位数 */
#define CHANNELS 1  /* 声道数目 */
#define buflen 800
/* 用于保存数字音频数据的内存缓冲区 */
//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];
//unsigned char buf1[LENGTH*RATE*SIZE*CHANNELS/8];//96000//unsigned char buf2[LENGTH*RATE*SIZE*CHANNELS/8];unsigned char buf[buflen];
unsigned char buf1[buflen];//96000unsigned char buf2[buflen];//529200int main(int argc,char *argv[]){    pid_t pid;    int fd,fdfile;	/* 声音设备的文件描述符 */                 
    int arg,arg1;	/* 用于ioctl调用的参数 */                
    int status,status1;   /* 系统调用的返回值 */             int i=1;    fd = open("/dev/dsp", O_RDWR);//soundcard  device     if (fd < 0)     {                                      
        perror("open of /dev/dsp failed");               
        exit(1);                                         
    }                      if((pid=fork())==-1)    {	perror("fork");	exit(EXIT_FAILURE);    }    else if(pid==0)//child w    {	//sleep(5);        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 (status == -1)                                  
            perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); 
        if (arg != CHANNELS)                               
            perror("unable to set number of channels");      
  	/* 设置采样时的采样频率 */                         
  	arg = RATE;                                        
  	status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);    
  	if (status == -1)                                  
    	perror("SOUND_PCM_WRITE_WRITE ioctl failed");    
  	/* 循环,直到按下Control-C */                     

        //////////////////////////////////////////////
        // 
  	fdfile = open("11.wav", O_RDONLY|O_NONBLOCK);                     
  	if (fdfile < 0) {                                      
    		perror("open of /dev/dsp failed");               
    		exit(1);                                         
  	}              
  	while(read(fdfile,buf1,buflen))
  	{

	//////////////////////////////////////////////
        //
    	    status = write(fd, buf1, buflen);
            if (status != sizeof(buf))                       
      		perror("wrote wrong number of bytes");         
  	    /* 在继续录音前等待回放结束 */                   
    	    //status = ioctl(fd, SOUND_PCM_SYNC, 0);           
    	    //if (status == -1)                                
      		//perror("SOUND_PCM_SYNC ioctl failed");         

  	}
    }    //////////////////////////////////    //    else//father    {	sleep(1);	printf("\n father begin ,read from soundcard and write to aaa.wav");/****  ****/  	arg1=FORMAT;
  	int result = ioctl(fd, SNDCTL_DSP_SETFMT, &arg1);
  	if ( result == -1 ) 
   	{
    		perror("ioctl sample format");
    		return -1;
   	}
   	printf("the format:%d\n",arg); 
  	// 设置采样时的量化位数 
  	arg1 = SIZE;
  	status1 = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg1);
  	if (status1 == -1)
    		perror("SOUND_PCM_WRITE_BITS ioctl failed");
  	if (arg1 != SIZE)
    		perror("unable to set sample size");
  	printf( "bits:%d \n",arg);
 	// 设置采样时的声道数目 
  	arg1 = CHANNELS; 
  	status1 = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg1);
	if (status1 == -1)
    		perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  	if (arg != CHANNELS)
    		perror("unable to set number of  channels");\ 
	printf( " channels:%d \n",arg);
 	// 设置采样时的采样频率 
  	arg1 = RATE;
  	status1 = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg1);
  	if (status1 == -1)
    		perror("SOUND_PCM_WRITE_WRITE ioctl failed");
   
  	FILE * in;
  	in=fopen("zzz.wav","wb"); 
    
  	while(  status = read(fd, buf2,buflen))
  	{
  		if (status != sizeof(buf))
    		perror("read wrong number of bytes");

  		//保存声音到文件
  		fwrite(buf2,buflen,1,in);
  	}
  
  	fclose(in);    
  

  
    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -