大家给看看这个程序

fight_flight 2012-03-30 01:21:15

#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 22050 /* 采样频率 */
#define SIZE 8 /* 量化位数 */
#define CHANNELS 1 /* 声道数目 */

/* 用于保存数字音频数据的内存缓冲区 */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int openDsp(int rd){
int fd; /* 声音设备的文件描述符 */
int arg; /* 用于ioctl调用的参数 */
int status; /* 系统调用的返回值 */

/* 打开声音设备 */
if(rd==0)
fd= open("/dev/dsp",O_RDONLY);
else
fd= open("/dev/dsp",O_WRONLY);
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 = 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");
return fd;
}

int main()
{
int status;
/* 循环,直到按下Control-C */
while (1) {
memset(buf,0,sizeof(buf));
int fdread=openDsp(0);
printf("Say something:\n");
status = read(fdread, buf, sizeof(buf)); /* 录音 */
if (status != sizeof(buf))
perror("read wrong number of bytes");
status=close(fdread);
if(status==-1){
perror("close Read Dsp wrong");
exit(1);
}

int fdwrite=openDsp(1);
printf("You said:\n");
status = write(fdwrite, buf, sizeof(buf)); /* 回放 */
if (status != sizeof(buf))
perror("wrote wrong number of bytes");

/* 在继续录音前等待回放结束 */
status = ioctl(fdwrite, SOUND_PCM_SYNC, 0);
if (status == -1)
perror("SOUND_PCM_SYNC ioctl failed");
status=close(fdwrite);
if(status==-1){
perror("rclose Write Dsp wrong");
exit(1);
}
}
return 0;
}

程序没有错误,但为什么录音之后方步出来呢,是怎么回事?
我用的ubuntu系统
...全文
140 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ecsape 2012-03-31
  • 打赏
  • 举报
回复
虽然很浅,但我还是想问,你确定你的录音设备是好的吗?在测试这个程序前你用其他软件测试过硬件吗?

很久以前,我在调试一热敏打印机驱动程序时,咋都打不出来,程序也没错,耗了一天,最后发现。。。
串口线掉了一针脚
fight_flight 2012-03-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
放不出来,有报错吗?如果有的话,帖出错误来!
[/Quote]
没有报错,录音的时候不管弄出什么样的声音,播放的时候都是杂音。
JoeBlackzqq 2012-03-30
  • 打赏
  • 举报
回复
放不出来,有报错吗?如果有的话,帖出错误来!

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧