Linux下的音訊採集與回放(轉)

post0發表於2007-08-11
Linux下的音訊採集與回放(轉)[@more@]

作者: hotfire [文章出自: ]

以下假設你對ioctl已有一定的認識及瞭解

sndtools.h

程式碼:

#ifndef SNDTOOLS_H

#define SNDTOOLS_H

#include

#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

程式碼:

#include

#include

#include

#include

#include

#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

程式碼:

// A sample to test record and playback.

#include

#include

#include

#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;

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-944785/,如需轉載,請註明出處,否則將追究法律責任。

相關文章