• «
  • 1
  • 2
  • »
  • Pages: 1/2     Go
主题 : 读写音频设备出现的都是杂音,帮忙看下是怎么回事! 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 48584
精华: 0
发帖: 40
金钱: 210 两
威望: 42 点
综合积分: 80 分
注册时间: 2011-06-02
最后登录: 2011-12-25
楼主  发表于: 2011-08-13 16:40

 读写音频设备出现的都是杂音,帮忙看下是怎么回事!

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/soundcard.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>


#define SOUND_TIME 0.2
#define SOUND_RATE 11025
#define SOUND_BITS 8
#define SOUND_CHANNELS 1

int fd = -1;
int arg=-1;

int init_dsp(int mode)
{
    int ctl_fd=-1;
    int status = -1;
    
    if( (ctl_fd=open("/dev/dsp",mode,0777))==-1)
    {
        printf("Open sound device error\n");
        return -1;
    }
    
    arg = SOUND_BITS;
    status = ioctl(ctl_fd,SOUND_PCM_WRITE_BITS,&arg);
    if(status == -1)
    {
        printf("Set sound_pcm_write_bits error\n");
        return -1;
    }
    
    arg = SOUND_CHANNELS;
    status = ioctl(ctl_fd,SOUND_PCM_WRITE_CHANNELS,&arg);
    if(status==-1)
    {
        printf("Set the sound_channels error\n");
        return -1;
    }
    
    arg = SOUND_RATE;
    status = ioctl(ctl_fd,SOUND_PCM_WRITE_RATE,&arg);
    if(status==-1)
    {
        printf("Set the sound_pcm_write_rate error\n");
        return -1;
    }

    return ctl_fd;
}

int main(int argc,char*argv[])
{
    int size = SOUND_TIME*SOUND_RATE*SOUND_BITS/8;
    int read_fd=-1;
    int write_fd=-1;
    char buf[size];
    int len = -1;
    printf("size = %d\n",size);
    
    memset(buf,0,sizeof(buf));
    
    if( (read_fd=init_dsp(O_RDONLY)) == -1)
    {
        printf("Init the read dsp error\n");
        exit(0);
    }
    printf("The read_fd = %d\n",read_fd);
    
    if( (write_fd=init_dsp(O_WRONLY)) == -1)
    {
        printf("Init the read dsp error\n");
        exit(0);
    }
    printf("The write_fd = %d\n",write_fd);
    
    while(1)
    {
        read(read_fd,buf,sizeof(buf));
        //sleep(1);
        write(write_fd,buf,sizeof(buf));
        memset(buf,0,sizeof(buf));
    }
    close(read_fd);
    close(write_fd);
    return 1;
}
级别: 风云使者
UID: 36096
精华: 3
发帖: 2513
金钱: 14075 两
威望: 2815 点
综合积分: 5086 分
注册时间: 2011-01-11
最后登录: 2015-11-15
1楼  发表于: 2011-08-13 20:19
硬件问题吧
极度缺钱,求捐赠……支付宝兼邮箱:huming2207@qq.com
^很多问题的背后都是简单的原因......
级别: 荣誉会员
UID: 34780
精华: 0
发帖: 1219
金钱: 6230 两
威望: 1246 点
综合积分: 2438 分
注册时间: 2010-12-21
最后登录: 2017-09-18
2楼  发表于: 2011-08-15 09:47
mini6410/tiny6410的话, 还是用ALSA接口吧

先用系统自带的功能播放音频测试一下看看是不是有杂音
级别: 侠客
UID: 16811
精华: 0
发帖: 112
金钱: 560 两
威望: 112 点
综合积分: 224 分
注册时间: 2010-03-22
最后登录: 2018-06-27
3楼  发表于: 2011-08-15 16:13
播放用的声音文件是什么取样率的,位数是多少,不匹配当然会暴音
级别: 新手上路
UID: 48584
精华: 0
发帖: 40
金钱: 210 两
威望: 42 点
综合积分: 80 分
注册时间: 2011-06-02
最后登录: 2011-12-25
4楼  发表于: 2011-08-15 22:32

 回 2楼(911gt3) 的帖子

#define SOUND_TIME 0.2
#define SOUND_RATE 11025
#define SOUND_BITS 8
#define SOUND_CHANNELS 1

更改如下

#define SOUND_TIME 0.025
#define SOUND_RATE 8000
#define SOUND_BITS 16
#define SOUND_CHANNELS 2

Rate 8000才能稳定,min2440i的板子,支持的是8000这个频率段
级别: 新手上路
UID: 48584
精华: 0
发帖: 40
金钱: 210 两
威望: 42 点
综合积分: 80 分
注册时间: 2011-06-02
最后登录: 2011-12-25
5楼  发表于: 2011-08-15 22:34

 回 2楼(911gt3) 的帖子

现在出现一个新的问题,就是板子的缓冲不会自动清除,下次在启动的时候,会一直读那些无用的缓冲数据,请问有什么方法解决??
级别: 新手上路
UID: 48584
精华: 0
发帖: 40
金钱: 210 两
威望: 42 点
综合积分: 80 分
注册时间: 2011-06-02
最后登录: 2011-12-25
6楼  发表于: 2011-08-15 22:35

 回 3楼(gzyjw) 的帖子

采样率 8000
双通道 CHANNELS2
位数BIT 16位
现在遇到一个问题,,缓冲无法释放请问该如何解决??
级别: 侠客
UID: 16811
精华: 0
发帖: 112
金钱: 560 两
威望: 112 点
综合积分: 224 分
注册时间: 2010-03-22
最后登录: 2018-06-27
7楼  发表于: 2011-08-16 11:39

 回 6楼(12555) 的帖子

下面是我很早时的一段代码:

#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 30    /* 存储秒数 */
//#define RATE 8000   /* 采样频率 */
#define RATE 44000  /* 采样频率 */
#define SIZE 16      /* 量化位数 */
#define CHANNELS 2  /* 声道数目 */

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

void availChannel(unsigned int mask) {


    if (mask & (1 << SOUND_MIXER_VOLUME))
{
        printf("SOUND_MIXER_VOLUME  \n");
}    
    if (mask & (1 << SOUND_MIXER_MIC))
{
        printf("SOUND_MIXER_ MIC \n");
}
    if (mask & (1 << SOUND_MIXER_BASS))
{
       printf("SOUND_MIXER_ BASS \n");
}
}

int main()
{

int fd;/* 声音设备的文件描述符 */
int id; /*声音输出文件描述符*/
//add by gzyjw
int md;
int mask;
int arg;/* 用于ioctl调用的参数 */
int status;   /* 系统调用的返回值 */
int i;
int j;



/* 打开声音设备 */
//fd = open("/dev/dsp", O_RDWR);//arm下的音频文件是"/dev/sound/dsp";
fd = open("/dev/dsp", O_WRONLY);//arm下的音频文件是"/dev/sound/dsp";
if (fd < 0) {
   perror("open of /dev/dsp failed");
   exit(1);
}

md = open("/dev/mixer",O_RDWR);
if(md < 0){
    perror("open of /dev/mixer failed");
    exit(1);
}

arg =0x6464;
status = ioctl(md, MIXER_WRITE(SOUND_MIXER_VOLUME), &arg);
if (status == -1)
   perror("SOUND_MIXER_VOLUME ioctl failed");
printf("the sound mixer is %x \n",arg);

arg =0x6464;
status = ioctl(md, MIXER_WRITE(SOUND_MIXER_PCM), &arg);
if (status == -1)
   perror("SOUND_MIXER_PCM ioctl failed");
printf("the sound mixer is %x \n",arg);

// if (arg != 0x3f3f)
//   perror("unable to set sound volumn");

/*打开输出文件*/
id=open("shanghai.wav",O_RDWR);
if(id < 0){
   perror("open of sound file failed");
   exit(1);
}
/* 设置采样时的量化位数 */
arg = SIZE;
//arg = AFMT_U16_NE;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
//status = ioctl(fd, SNDCTL_DSP_SETFMT, &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);
//status = ioctl(fd, SNDCTL_DSP_STEREO, &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);
// status = ioctl(fd, SNDCTL_DSP_STEREO, &arg);
if (status == -1)
   perror("SOUND_PCM_WRITE_WRITE ioctl failed");

//int enable_bits = ~ PCM_ENABLE_OUTPUT;
//ioctl(fd, SNDCTL_DSP_SETTRIGGER, & enable_bits);

// get available mixer mask
    if (ioctl(md, SOUND_MIXER_READ_DEVMASK, &mask) == -1)
        perror("checking available mixer channels error!");
    else {
         printf("Available Mixer Channel:\n");  
        availChannel(mask);
    }

    // get available recording device(channel)

if (ioctl(md, SOUND_MIXER_READ_RECMASK, &mask) == -1)
                perror("checking available recroding  channels error!");
    else {
        printf("Available Recording Channel:\n");    
        availChannel(mask);
    }                          


// get stereo or mono info, 1 for stereo, 0 for mono
if (ioctl(md, SOUND_MIXER_READ_STEREODEVS, &mask) == -1)
{
                perror("checking stereo channels error!");
}
    else {
printf("Available Stereo Channel:\n");      
            availChannel(mask);
    }    

/* 读取一定数量的音频数据,并将之写到输出文件中去 */
for(i=0;i<10;i++){
      j=read(id, buf, sizeof(buf));
      if (j > 0){
        write(fd, buf, j); /* 放音 */
    printf("playback sound \n");
      }
    }

/* 关闭输入、输出文件 */
close(fd);
close(id);
return 1;
}
级别: 侠客
UID: 16811
精华: 0
发帖: 112
金钱: 560 两
威望: 112 点
综合积分: 224 分
注册时间: 2010-03-22
最后登录: 2018-06-27
8楼  发表于: 2011-08-16 11:40
给你做参考
级别: 新手上路
UID: 48584
精华: 0
发帖: 40
金钱: 210 两
威望: 42 点
综合积分: 80 分
注册时间: 2011-06-02
最后登录: 2011-12-25
9楼  发表于: 2011-08-16 23:57

 回 8楼(gzyjw) 的帖子

你这个是mixer编程吗??
  • «
  • 1
  • 2
  • »
  • Pages: 1/2     Go