首页| 论坛| 消息

标题:为什么总是设备忙?对声卡 设备文件读写,实现录音放音的程序~
作者:fayfayho
日期:2006-05-10 19:48
内容:

Qt下,音频编程,运行如下程序,声卡文件总是busy
通过检测声卡,能够正常发声后,再运行程序,提示 wrote wrong number of bytes
即是 不能回放
我可以确定,程序是好的,为什么声卡文件总是busy?
直接对设备文件读写,效率应该比较高的呀
附源程序:

/*
* sound.c
*/
#include
#include
#include
#include
#include
#include
#include
#define LENGTH 3 /* 存储秒数 */
#define RATE 8000 /* 采样频率 */
#define SIZE 8 /* 量化位数 */
#define CHANNELS 1/* 声道数目 */
/* 用于保存数字音频数据的内存缓冲区 */
unsigned char buf;
int main()
{
int fd; /* 声音设备的文件描述符 */
int arg; /* 用于ioctl调用的参数 */
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 (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 */
while (1) {
printf("Say something:\n");
status = read(fd, buf, sizeof(buf)); /* 录音 */
if (status != sizeof(buf))
perror("read wrong number of bytes");
printf(&q ..


#1 [shiroki 05-11 15:17]
你的程序和qt没有任何关系亚, 单独运行试过吗?
#2 [fengshenx 05-12 01:39]
open就出错了?有进程在使用声卡?
#3 [fayfayho 05-17 13:17]
测试过
open就出错了,打不开 声卡的设备文件
在linux下检测声卡,有时不能听到 声卡出声
一直测到听到声卡出声,再运行程序,可以打开声卡的设备文件,但不能创建自己的文件
当两个都可以open的时候,再make一下程序,又打不开:)
ps查看进程,发现有进程在用 声卡
这说明什么?
我只在运行这个程序而已,其他没开任何程序
应该是这个程序本身的问题吧?
我该怎样处理?
#4 [shiroki 05-17 21:21]
应该不是你的进程的问题, 别的进程占用了声音设备吧
想办法查一查
#5 [fayfayho 05-22 14:46]
请问,有人试过这个程序吗?
我读懂的,程序是没问题的

回复 发表
主题 版块