Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1286349
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类:

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;
}


阅读(373) | 评论(0) | 转发(0) |
0

上一篇:netnote

下一篇:oss_mixer_user_17.4程序

给主人留下些什么吧!~~