LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 838|回复: 8

linux下语音编程问题?

[复制链接]
发表于 2005-3-15 21:29:06 | 显示全部楼层 |阅读模式
现在的情况是我用的linux-2.4.19
检测声卡的时候没问题
但是在编程时想设置采样位数是8位,采样频率是8000,1通道的时候不成功
请高手指点
发表于 2005-3-16 08:12:15 | 显示全部楼层
“不成功”怎么理解?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-16 09:15:09 | 显示全部楼层
错误是这样的
Linux下声卡编程问题

--------------------------------------------------------------------------------

#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ioctl.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/soundcard.h>

#define LENGTH 3
#define RATE 8000
#define SIZE 8
#define CHANNELS 1

unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main()
{
int fd;
int arg;
int status;
fd=open("/dev/dsp",0_RDWR);
if(fd<0)
{
printf("open of dev failed\n");
exit(1);
}

arg=SIZE;
status=ioctl(fd,SOUND_PCM_WRITE_BITS,&arg);
if(status==-1)
printf("SOUND_PCM_WRITE_BITS,ioctl\n");
if(arg!=SIZE)
printf("unable to set SOUND_PCM_WRITE_BITS\n");

arg=CHANNELS;
status=ioctl(fd,SOUND_PCM_WRITE_CHANNELS,&arg);
if(status==-1)
printf("SOUND_PCM_WRITE_CHANNELS,ioctl\n");
if(arg!=CHANNELS)
printf("unable to set SOUND_PCM_WRITE_CHANNELS\n");


arg=RATE;
status=ioctl(fd,SOUND_PCM_WRITE_RATE,&arg);
if(status==-1)
printf("SOUND_PCM_WRITE_RATE,ioctl\n");
//if(arg!=RATE)
//printf("unable to set SOUND_PCM_WRITE_RATE\n");
while(1)
{
printf("say something\n");
status=read(fd,buf,sizeof(buf));
if(status!=sizeof(buf))
printf("read wrong number of bytes\n");
printf("You said\n");
status=write(fd,buf,sizeof(buf));
if(status!=sizeof(buf))
printf("wrote wrong number of bytes\n");
status=ioctl(fd,SOUND_PCM_SYNC,0);
if(status==-1)
printf("SOUND_PCM_SYNC,ioctl\n");

}


}
结果:
unable to set SOUND_PCM_WRITE_BITS
unable to set SOUND_PCM_WRITE_CHANNELS
然后就是不停的显示wrote wrong number of bytes
好象参数不能设置
回复 支持 反对

使用道具 举报

发表于 2005-3-16 11:02:26 | 显示全部楼层
看看这个,也是设那几个的属性,但 ioctl 用的参数和你的不一样。

  1.         format = AFMT_S16_LE;  /* 位数 */
  2.         if (ioctl(soundfd, [color=Red]SNDCTL_DSP_SETFMT[/color], &format) < 0) {
  3.                 error_exit("failed to ioctl SNDCTL_DSP_SETFMT\n");
  4.         }
  5.         if (format != AFMT_S16_LE) {
  6.                 error_exit("16 bit signed samples not supported.\n");
  7.         }

  8.         stereo = 0;                   /* 单声道 */
  9.         if (ioctl(soundfd, [color=Red]SNDCTL_DSP_STEREO[/color], &stereo) < 0) {
  10.                 error_exit("failed to ioctl SNDCTL_DSP_STEREO\n");
  11.         }
  12.         if (stereo != 0) {
  13.                 error_exit("mono not supported.\n");
  14.         }

  15.         speed = AUDIO_DEFAULT_SPEED;    /* 采样率 */
  16.         if (ioctl(soundfd, [color=Red]SNDCTL_DSP_SPEED[/color], &speed) < 0) {
  17.                 error_exit("failed to ioctl SNDCTL_DSP_SPEED\n");
  18.         }
  19.         if (speed < AUDIO_DEFAULT_SPEED - 50 || speed > AUDIO_DEFAULT_SPEED + 50) {
  20.                 error_exit("8khz sampling not supported.\n");
  21.         }
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-3-16 11:10:10 | 显示全部楼层
用 alsa-lib 吧:)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-16 13:56:31 | 显示全部楼层
还是说"mono not supported.\n"
16 bit signed samples not supported.\n
改了8bit还是不行
我在pc上是通过装了alsa才实现的
但是我现在的环境是sitsang pxa255上
他也有一个arm-linux而且用自带的录音程序也是可以录音的
就是不能设置成自己想要的参数
回复 支持 反对

使用道具 举报

发表于 2005-3-16 14:30:40 | 显示全部楼层
是不是声卡不支持单声道?我以前在笔记本上也遇到类似问题。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-16 15:02:44 | 显示全部楼层
就是啊
在pc上原来也是不行
后来装了alsa才行,可是现在在sitsang pxa255的arm-linux上我不会装啊
我也想用2声道和其他的采样频率,可是在window下我参数不会设置啊
我在windows下原来做的都是1声道和8000hz的
请高手继续指点
回复 支持 反对

使用道具 举报

发表于 2005-3-17 08:49:31 | 显示全部楼层
装了 alsa 就可以设成单声道了? 那看看 alsa 的源码是怎么设的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表