音頻錄放框架
下面給出一個利用聲卡上的DSP設(shè)備進(jìn)行聲音錄制和回放的基本框架,它的功能是先錄制幾秒種音頻數(shù)據(jù),將其存放在內(nèi)存緩沖區(qū)中,然后再進(jìn)行回放,其所有的功能都是通過讀寫/dev/dsp設(shè)備文件來完成的:
?
/*
* sound.c
*/
#include
#include
#include
#include
#include
#include
#include
?
#define LENGTH 3??? /*
存儲秒數(shù)
*/
#define RATE 8000?? /*
采樣頻率
*/
#define SIZE 8????? /*
量化位數(shù)
*/
#define CHANNELS 1?/*
聲道數(shù)目
*/
?
/*
用于保存數(shù)字音頻數(shù)據(jù)的內(nèi)存緩沖區(qū)
*/
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];
?
int main()
{
?int fd;?/*
聲音設(shè)備的文件描述符
*/
?int arg; /*
用于
ioctl
調(diào)用的參數(shù)
*/
?int status;?? /*
系統(tǒng)調(diào)用的返回值
*/
?
?/*
打開聲音設(shè)備
*/
?fd = open("/dev/dsp", O_RDWR);
?if (fd < 0) {
??? perror("open of /dev/dsp failed");
??? exit(1);
?}
?
?/*
設(shè)置采樣時的量化位數(shù)
*/
?arg = SIZE;
?status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
?if (status == -1)
??? perror("SOUND_PCM_WRITE_BITS ioctl failed");
?if (arg != SIZE)
??? perror("unable to set sample size");
?
?/*
設(shè)置采樣時的聲道數(shù)目
*/
?arg = CHANNELS;
??status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
?if (status == -1)
??? perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
?if (arg != CHANNELS)
??? perror("unable to set number of channels");
?
?/*
設(shè)置采樣時的采樣頻率
*/
?arg = RATE;
?status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
?if (status == -1)
??? perror("SOUND_PCM_WRITE_WRITE ioctl failed");
?
?/*
循環(huán),直到按下
Control-C */
?while (1) {
??? printf("Say something:n");
??? status = read(fd, buf, sizeof(buf)); /*
錄音
*/
??? if (status != sizeof(buf))
????? perror("read wrong number of bytes");
?
??? printf("You said:n");
??? status = write(fd, buf, sizeof(buf)); /*
回放
*/
??? if (status != sizeof(buf))
????? perror("wrote wrong number of bytes");
?
??? /*
在繼續(xù)錄音前等待回放結(jié)束
*/
??? status = ioctl(fd, SOUND_PCM_SYNC, 0);
????if (status == -1)
????? perror("SOUND_PCM_SYNC ioctl failed");
?}
}
?
4.4
混音器框架
下面再給出一個對混音器進(jìn)行編程的基本框架,利用它可以對各種混音通道的增益進(jìn)行調(diào)節(jié),其所有的功能都是通過讀寫/dev/mixer設(shè)備文件來完成的:
?
/*
* mixer.c
*/
#include
#include
#include
#include
#include
#include
?
/*
用來存儲所有可用混音設(shè)備的名稱
*/
const char *sound_device_names[] = SOUND_DEVICE_NAMES;
?
int fd;????????????????? /*
混音設(shè)備所對應(yīng)的文件描述符
*/
int devmask, stereodevs; /*
混音器信息對應(yīng)的位圖掩碼
*/
char *name;
?
/*
顯示命令的使用方法及所有可用的混音設(shè)備
*/
void usage()
{
?int i;
?
?fprintf(stderr, "usage: %s n"
????? ?"?????? %s nn"
????? ?"Where is one of:n", name, name);
?for (i = 0 ; i < SOUND_MIXER_NRDEVICES ; i++)
??? if ((1 << i) & devmask) /*
只顯示有效的混音設(shè)備
*/
????? fprintf(stderr, "%s ", sound_device_names[i]);
?fprintf(stderr, "n");
?exit(1);
}
?
int main(int argc, char *argv[])
{
?int left, right, level;?/*
增益設(shè)置
*/
?int status;????????????? /*
系統(tǒng)調(diào)用的返回值
*/
?int device;????????????? /*
選用的混音設(shè)備
*/
?char *dev;?????????????? /*
混音設(shè)備的名稱
*/
?int i;
?
?name = argv[0];
?
?/*
以只讀方式打開混音設(shè)備
*/
?fd = open("/dev/mixer", O_RDONLY);
?if (fd == -1) {
??? perror("unable to open /dev/mixer");
??? exit(1);
?}
?
??/*
獲得所需要的信息
*/
?status = ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devmask);
?if (status == -1)
??? perror("SOUND_MIXER_READ_DEVMASK ioctl failed");
?status = ioctl(fd, SOUND_MIXER_READ_STEREODEVS, &stereodevs);
?if (status == -1)
??? perror("SOUND_MIXER_READ_STEREODEVS ioctl failed");
?
?/*
檢查用戶輸入
*/
?if (argc != 3 && argc != 4)
??? usage();
?
?/*
保存用戶輸入的混音器名稱
*/
?dev = argv[1];
?
?/*
確定即將用到的混音設(shè)備
*/
?for (i = 0 ; i < SOUND_MIXER_NRDEVICES ; i++)
??? if (((1 << i) & devmask) && !strcmp(dev, sound_device_names[i]))
????? break;
?if (i == SOUND_MIXER_NRDEVICES) { /*
沒有找到匹配項
*/
??? fprintf(stderr, "%s is not a valid mixer devicen", dev);
??? usage();
?}
?
?/*
查找到有效的混音設(shè)備
*/
?device = i;
?
?/*
獲取增益值
*/
?if (argc == 4) {
??? /*
左、右聲道均給定
*/
??? left?= atoi(argv[2]);
??? right = atoi(argv[3]);
?} else {
??? /*
左、右聲道設(shè)為相等
*/
??? left?= atoi(argv[2]);
??? right = atoi(argv[2]);
?}
?
??/*
對非立體聲設(shè)備給出警告信息
*/
?if ((left != right) && !((1 << i) & stereodevs)) {
??? fprintf(stderr, "warning: %s is not a stereo devicen", dev);
?}
?
??/*
將兩個聲道的值合到同一變量中
*/
?level = (right << 8) + left;
?
??/*
設(shè)置增益
*/
?status = ioctl(fd, MIXER_WRITE(device), &level);
?if (status == -1) {
??? perror("MIXER_WRITE ioctl failed");
??? exit(1);
?}
?
?/*
獲得從驅(qū)動返回的左右聲道的增益
*/
?left?= level & 0xff;
?right = (level & 0xff00) >> 8;
?
?/*
顯示實際設(shè)置的增益
*/
?fprintf(stderr, "%s gain set to %d%% / %d%%n", dev, left, right);
?
?/*
關(guān)閉混音設(shè)備
*/
?close(fd);
?return 0;
}
?
編譯好上面的程序之后,先不帶任何參數(shù)執(zhí)行一遍,此時會列出聲卡上所有可用的混音通道:
?
[xiaowp@linuxgam sound]$ ./mixer
usage: ./mixer
?????? ./mixer
Where is one of:
vol pcm speaker line mic cd igain line1 phin video
?
之后就可以很方便地設(shè)置各個混音通道的增益大小了,例如下面的命令就能夠?qū)D輸入的左、右聲道的增益分別設(shè)置為80%和90%:
?
[xiaowp@linuxgam sound]$ ./mixer cd 80 90
cd gain set to 80% / 90%