Linux下音頻編程實例


在android下跑失敗,read讀放回-1

#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

//**********************************************************************************************

#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/snd/pcmC0D0c", 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);
}

//**********************************************************************************************

源文件 recorder.c

//**********************************************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sndtools.h"
#define WAVOUTDEV FMT8K
typedef unsigned int DWORD;
typedef unsigned short int WORD; 
struct RIFF_HEADER
{
char szRiffID[4]; // 'R','I','F','F'
DWORD dwRiffSize;
char szRiffFormat[4]; // 'W','A','V','E'
};
struct WAVE_FORMAT
{
WORD wFormatTag;
WORD wChannels;
DWORD dwSamplesPerSec;
DWORD dwAvgBytesPerSec;
WORD wBlockAlign;
WORD wBitsPerSample;
};
struct FMT_BLOCK
{
char szFmtID[4]; // 'f','m','t',' '
DWORD dwFmtSize;
struct WAVE_FORMAT wavFormat;
};
struct FACT_BLOCK
{
char szFactID[4]; // 'f','a','c','t'
DWORD dwFactSize;
};
struct DATA_BLOCK
{
char szDataID[4]; // 'd','a','t','a'
DWORD dwDataSize;
};
int main()
{
char *buf;
int dwSize;
int i;
struct RIFF_HEADER riffheader;
struct FMT_BLOCK fmtblock;
struct DATA_BLOCK datablock; 
FILE * fp;
printf("WORD %d \n",sizeof(WORD));
printf("DWORD %d\n",sizeof(DWORD));
riffheader.szRiffID[0]='R';
riffheader.szRiffID[1]='I';
riffheader.szRiffID[2]='F';
riffheader.szRiffID[3]='F';
riffheader.dwRiffSize=1024*50+8+16+8+4;
riffheader.szRiffFormat[0]='W';
riffheader.szRiffFormat[1]='A';
riffheader.szRiffFormat[2]='V';
riffheader.szRiffFormat[3]='E';

fmtblock.szFmtID[0]='f';
fmtblock.szFmtID[1]='m';
fmtblock.szFmtID[2]='t';
fmtblock.szFmtID[3]=' ';
fmtblock.dwFmtSize=16;
fmtblock.wavFormat.wFormatTag=0x0001;
fmtblock.wavFormat.wChannels=1;
fmtblock.wavFormat.dwSamplesPerSec=8000;
fmtblock.wavFormat.dwAvgBytesPerSec= 8000*2;////////////////////
fmtblock.wavFormat.wBlockAlign=2;
fmtblock.wavFormat.wBitsPerSample=16;

datablock.szDataID[0]='d';
datablock.szDataID[1]='a';
datablock.szDataID[2]='t';
datablock.szDataID[3]='a';

datablock.dwDataSize=1024*50;


if((fp=fopen("test.wav","wb")) == NULL)
{
   printf("Cannot open test.wav");
   return;
}
fwrite(&riffheader,sizeof(riffheader),1,fp);
fwrite(&fmtblock,sizeof(fmtblock),1,fp);
fwrite(&datablock,sizeof(datablock),1,fp);
 

if(!OpenSnd())
    {
printf("Open sound device error!\\n");
exit(-1);
}
SetFormat(FMT16BITS, FMT8K);
SetChannel(MONO);
buf = (char *)malloc(1024);
if(buf == NULL)
exit(-1);
for(i = 0; i <50; i++)
{
printf("%d \n",i);
dwSize = Record(buf, 1024);
fwrite(buf,dwSize,1,fp);
//dwSize = Play(buf, dwSize);
}
fclose(fp);
exit(1);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章