Linux下的音频采集与回放

王朝system·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

作者: hotfire [文章出自: www.phpx.com]

以下假设你对ioctl已有一定的认识及了解

sndtools.h

代码:

#ifndef SNDTOOLS_H

#define SNDTOOLS_H

#include <linux/soundcard.h>

#define FMT8BITSAFMT_S8_LE

#define FMT16BITSAFMT_S16_LE

#define FMT8K8000

#define FMT16K16000

#define FMT22K22000

#define FMT44K44000

#define MONO1

#define STERO2

#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

代码:

#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\\n", nWhich);

#endif

return 0;

}

tmp = hz;

if( -1 == ioctl(devfd, SNDCTL_DSP_SPEED, &tmp))

{

#ifdef DEBUG_WARN

printf("Set speed to %d:%d\\n", 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\\n", 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

代码:

// 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!\\n");

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航