listen.c

来自「s3c2410IIS总线实验代码 实现录音以及放音功能」· C语言 代码 · 共 58 行

C
58
字号
#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ioctl.h>#include <stdlib.h>#include <stdio.h>#include<string.h>#include <linux/soundcard.h>#define RATE 44000#define SIZE 16#define CHANNELS 1int main(int argc,char * argv[]){	int id,fd;	int arg;	int status;	char buf[1024];	if((id = open("/dev/dsp",O_WRONLY)) < 0 )	{		printf("Can't open sound device!\n");		exit(1);	}	if((fd = open(argv[1],O_RDWR)) < 0)	{		printf("Can't open output file!\n");		exit (1);	}	arg = SIZE;  	status = ioctl(id,SOUND_PCM_WRITE_BITS,&arg);  	if(status == -1)  		printf("SOUND_PCM_WRITE_BITS ioctl failed\n");  	if(arg != SIZE)  		printf("unable to set sample size\n");	arg = CHANNELS;   	status = ioctl(id,SOUND_PCM_WRITE_CHANNELS,&arg);  	if(status == -1)    		printf("SOUND_PCM_WRITE_CHANNELS ioctl failed\n");  	if(arg != CHANNELS)    		printf("unable to set number of channels\n");	arg = RATE;  	status = ioctl(id,SOUND_PCM_WRITE_RATE,&arg);  	if(status == -1)    		printf("SOUND_PCM_WRITE_WRITE ioctl failed\n");	while(read(fd,buf,sizeof(buf)) > 0)		write(id,buf,sizeof(buf));	close(fd);	close(id);}

⌨️ 快捷键说明

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