Linux 音頻 驅(qū)動 接口 簡介 之 OSS 編程 方法 采樣頻率 量化 位數(shù) 實例 driver interface
音頻器件與DSP間的接口
兩個概念:
* 采樣頻率:每秒采多少次;
* 量化精度(位數(shù)):每采一次采出的電壓值分為多少份,如:量化精度為8bit,則分為2的8次方等份。
PCM接口:
由時鐘脈沖(BCLK)、幀同步(FS)、數(shù)據(jù)接收(DR)、數(shù)據(jù)發(fā)送(DX)組成,每個音頻通道獲得一條獨立的數(shù)據(jù)隊列;
IIS(Inter-IC Sound)接口:
1980s-LRCLK(Left/Right Clock) ,其高位為左聲道,低位為右聲道;
AC97接口:
是一種數(shù)據(jù)格式,用于音頻編碼的內(nèi)部架構(gòu)規(guī)格,具有控制功能;AC-Link接口:位時鐘(BITCLK),同步校正(SYNC)、編碼到處理器(SDATAIN)與CPU到編碼器(SDATAOUT)。
OSS驅(qū)動的組成與用戶空間編程
OSS驅(qū)動實現(xiàn)兩個基本音頻設備:
mixer(混音器)
分為 input mixer 和 output mixer
打開設備 /dev/mixer 后,
可使用 ioctl(mixerfd, SOUND_MIXER_READ(SOUND_MIXER_MIC), &value);
及 ioctl(mixerfd, SOUND_MIXER_WRITE(SOUND_MIXER_VOLUME), &value);
來讀取和寫入麥克及喇叭的輸入、輸出增益;
dsp(數(shù)字信號處理器)
首先打開 /dev/dsp (聲卡若支持全雙工模式,則可以 O_RDWR 模式打開)后,
使用 ioctl(dspfd, SOUND_PCM_WRITE_CHANNELS, &value); 設置通道數(shù),
ioctl(dspfd, SOUND_PCM_WRITE_RATE, &value); 設置采樣頻率,
ioctl(dspfd, SOUND_PCM_WRITE_BITS, &value); 設置量化位數(shù),
使用 read 和 write 對 dspfd 設備操作,可實現(xiàn)錄音放音,
ioctl(dspfd, SOUND_PCM_SYNC, 0); 函數(shù)可實現(xiàn)錄音前等待放音結(jié)束。
簡單的錄音放音程序?qū)嵗?br />/*
* ===================================================================
*
*?????? Filename: oss.c
*
*??? Description:
*
*??????? Version: 1.0
*??????? Created: Monday, May 24, 2010 11:07:27 HKT HKT
*?????? Revision: none
*?????? Compiler: gcc
*
*???????? Author: Wang Cheng (Dancy), dancesimply@gmail.com
*??????? Company: Kunming Lang Ju Inc.
*
* ===================================================================
*/
#include
#include
#include
#include
#include
#include
#include
#include
#define RATE 8000
#define SIZE 8
#define CHANNELS 1
unsigned char buf[RATE*SIZE*CHANNELS/(8)];
char voice_name[] = "test.wav";//record file name
volatile int file_voice_fd=-1;//record file fd;
volatile int fd; /* 聲音設備的文件描述符 */
//關閉聲卡設備
void file_close()
{
??? if(fd >0)
?????? {
?????????? close(fd);
?????????? fd=-1;
?????? }
}
int file_open()
{
int arg; /* 用于ioctl調(diào)用的參數(shù) */
int status;?? /* 系統(tǒng)調(diào)用的返回值 */
status = 1;
int ret;
ret = 0;
??? //判斷聲音設備是否打開
if (fd > 0) return fd;
/* 打開聲音設備 */
fd = open("/dev/dsp", O_RDWR,0);
if (fd < 0) {
??? perror("open of /dev/dsp failed");
??? ret = -1;
??? goto quit;
}
ret = fd;
/* 設置采樣時的量化位數(shù) */
arg = SIZE;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
{
??? perror("SOUND_PCM_WRITE_BITS ioctl failed");
??? ret = -2;
??? goto fd_err;
}
if (arg != SIZE)
{
?? perror("unable to set sample size");
?? ret = -3;
?? goto fd_err;
}
/* 設置采樣時的聲道數(shù)目 */
arg = CHANNELS;
status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
{
?? perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
?? ret = -4;
???? file_close();
??? goto fd_err;
}
if (arg != CHANNELS)
{
????? perror("unable to set number of channels");
????? ret = -5;
???? goto fd_err;
}
/* 設置采樣時的采樣頻率 */
arg = RATE;
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
{
?? perror("SOUND_PCM_WRITE_WRITE ioctl failed");
?? ret = -6;
?? }
fd_err:
if(ret < 0)
{
???? file_close();
}
quit:
??? printf("ret = %dn",ret);
return ret;
}
int create_voice_file()
{
??? file_voice_fd = open(voice_name , O_RDWR | O_CREAT | O_TRUNC,S_IRUSR|S_IWUSR|S_IXUSR);
??? if(file_voice_fd < 0)
?? {
??????? printf("create new file failure");
?????? return -1;
??? }
??? printf("voice_name = %s, file fd is %dn",voice_name, file_voice_fd);
??? return 0;
}
int open_voice_file()
{
??? file_voice_fd = open(voice_name , O_RDWR | O_CREAT | O_APPEND,S_IRUSR|S_IWUSR|S_IXUSR);
??? if(file_voice_fd < 0)
?? {
??????? printf("create new file failure");
?????? return -1;
??? }
??? printf("voice_name = %s, file fd is %dn",voice_name, file_voice_fd);
??? return 0;
}
int close_record_file()
{
??? if(file_voice_fd >0 )
??? {
??????? close(file_voice_fd);
??????? file_voice_fd = -1;
??????? return 0;
??? }
??? else
??? {
??????? return -1;
??? }
}
int main(int agrc, char **argv)
{
file_open();
if ('c' == **(argv+1))
{
?? printf("voice record!n");
?? create_voice_file();
?? int i = 0;
?? while (i<5)
?? {
??? read(fd, buf, sizeof(buf));
??? write(file_voice_fd, buf, sizeof(buf));
??? printf("No. %dn", i++);
?? }
?? close_record_file();
?? goto play;
}
else if ('p' == **(argv+1))
{
play:
?? printf("voice play!n");
?? open_voice_file();
?? char playbuf[RATE*SIZE*CHANNELS/8*5];
?? read(file_voice_fd, playbuf, sizeof(playbuf));
?? write(fd, playbuf, sizeof(playbuf));
?? close_record_file();
}
else
{
?? printf("please input args...nc for record and p for play.n");
}
file_close();
}
參考文獻:宋寶華 《Linux設備驅(qū)動開發(fā)詳解》