PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 操作系统(Windows、Linux) » Linux下的音频采集与回放
本页主题: Linux下的音频采集与回放 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

自由的龙

该用户目前不在线
级别: 中级程序员
精华: 1
发帖: 1264
威望: 1267 点
金钱: 12660 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2006-04-16
最后登录:2006-06-27

Linux下的音频采集与回放


#ifndef SNDTOOLS_H
#define SNDTOOLS_H

#include <linux/soundcard.h>

#define FMT8BITS     AFMT_S8_LE    
#define FMT16BITS     AFMT_S16_LE    

#define FMT8K           8000
#define FMT16K           16000
#define FMT22K           22000
#define FMT44K           44000

#define MONO           1
#define STERO           2

#ifndef VAR_STATIC
extern int devfd;
extern int CapMask;
#endif //ifndef VAR_STATIC

//Open sound device, return 1 if open success
//else return 0
int OpenSnd();

//Close sound device
int CloseSnd();

//Set record or playback format, return 1 if success
//else return 0
int SetFormat(int bits, int hz);

//Set record or playback channel, return 1 if success
//else return 1
int SetChannel(int chn);

//Record
int Record(char *buf, int size);

//Playback
int Play(char *buf, int size);

#endif //ifndef SNDTOOLS_H
sndtools.c


CODE:[Copy to clipboard]#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>

#define VAR_STATIC

#include "sndtools.h"

int devfd = 0;

/*
* Open Sound device
* Return 1 if success, else return 0.
*/
int OpenSnd(/* add by new version */int nWhich)
{
    if(devfd > 0)
          close(devfd);

    devfd = open("/dev/dsp", O_RDWR);

    if(devfd < 0)
          return 0;
    return 1;
}

/*
* Close Sound device
* return 1 if success, else return 0.
*/
int CloseSnd(/* add by new version */int nWhich)
{
    close(devfd);
    devfd = 0;
    return 1;
}


/*
* Set Record an Playback format
* return 1 if success, else return 0.
* bits -- FMT8BITS(8bits), FMT16BITS(16bits)
* hz -- FMT8K(8000HZ), FMT16K(16000HZ), FMT22K(22000HZ), FMT44K(44000HZ)
*/
int SetFormat(int bits, int hz)
{
    int tmp = bits;

    if( -1 == ioctl(devfd, SNDCTL_DSP_SETFMT, &tmp))
    {

#ifdef DEBUG_WARN
          printf("Set fmt to s16_little faile:%d
", nWhich);
#endif

          return 0;
    }
   
    tmp = hz;
    if( -1 == ioctl(devfd, SNDCTL_DSP_SPEED, &tmp))
    {

#ifdef DEBUG_WARN
          printf("Set speed to %d:%d
", hz, nWhich);
#endif

          return 0;
    }
   
    return 1;
}

/*
* Set Sound Card Channel
* return 1 if success, else return 0.
* chn -- MONO, STERO
*/
int SetChannel(int chn)
{
    int tmp = chn;

    if(-1 == ioctl(devfd, SNDCTL_DSP_CHANNELS, &tmp))
    {

#ifdef DEBUG_WARN
          printf("Set Audio Channel faile:%d
", nWhich);
#endif

          return 0;
    }
    return 1;
}

/*
* Record
* return numbers of byte for read.
*/
int Record(char *buf, int size)
{
    return read(devfd, buf, size);
}

/*
* Playback
* return numbers of byte for write.
*/
int Play(char *buf, int size)
{
    return write(devfd, buf, size);
}
test.c


CODE:[Copy to clipboard]// A sample to test record and playback.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "sndtools.h"

int main()
{
    char *buf;
    int dwSize;
    if(!OpenSnd())
    {
          printf("Open sound device error!
");
          exit(-1);
    }

    SetFormat(FMT16BITS, FMT8K, WAVOUTDEV);

    SetChannel(MONO, WAVOUTDEV);

    buf = (char *)malloc(320);

    if(buf == NULL)
          exit(-1);
    for(int i = 0; i <1000; i++)
    {
          dwSize = Record(buf, 640);
          dwSize = Play(buf, dwSize);
    }

    exit 1;
}
顶端 Posted: 2006-04-20 16:39 | [楼 主]
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 操作系统(Windows、Linux)

时:11-23 19:20 Copyright © 2006 phpwhy.com 权
ICP05060669

曳息 -