音頻錄放框架
下面給出一個(gè)利用聲卡上的DSP設(shè)備進(jìn)行聲音錄制和回放的基本框架,它的功能是先錄制幾秒種音頻數(shù)據(jù),將其存放在內(nèi)存緩沖區(qū)中,然后再進(jìn)行回放,其所有的功能都是通過(guò)讀寫/dev/dsp設(shè)備文件來(lái)完成的:
?
/*
* sound.c
*/
#include
#include
#include
#include
#include
#include
#include
?
#define LENGTH 3??? /* 存儲(chǔ)秒數(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)用的返回值 */
?
?/* 打開(kāi)聲音設(shè)備 */
?fd = open("/dev/dsp", O_RDWR);
?if (fd < 0) {
??? perror("open of /dev/dsp failed");
??? exit(1);
?}
?
?/* 設(shè)置采樣時(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í)的聲道數(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è)置采樣時(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
混音器框架
下面再給出一個(gè)對(duì)混音器進(jìn)行編程的基本框架,利用它可以對(duì)各種混音通道的增益進(jìn)行調(diào)節(jié),其所有的功能都是通過(guò)讀寫/dev/mixer設(shè)備文件來(lái)完成的:
?
/*
* mixer.c
*/
#include
#include
#include
#include
#include
#include
?
/* 用來(lái)存儲(chǔ)所有可用混音設(shè)備的名稱 */
const char *sound_device_names[] = SOUND_DEVICE_NAMES;
?
int fd;????????????????? /* 混音設(shè)備所對(duì)應(yīng)的文件描述符 */
int devmask, stereodevs; /* 混音器信息對(duì)應(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];
?
?/* 以只讀方式打開(kāi)混音設(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) { /* 沒(méi)有找到匹配項(xiàng) */
??? 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]);
?}
?
??/* 對(duì)非立體聲設(shè)備給出警告信息 */
?if ((left != right) && !((1 << i) & stereodevs)) {
??? fprintf(stderr, "warning: %s is not a stereo devicen", dev);
?}
?
??/* 將兩個(gè)聲道的值合到同一變量中 */
?level = (right << 8) + left;
?
??/* 設(shè)置增益 */
?status = ioctl(fd, MIXER_WRITE(device), &level);
?if (status == -1) {
??? perror("MIXER_WRITE ioctl failed");
??? exit(1);
?}
?
?/* 獲得從驅(qū)動(dòng)返回的左右聲道的增益 */
?left?= level & 0xff;
?right = (level & 0xff00) >> 8;
?
?/* 顯示實(shí)際設(shè)置的增益 */
?fprintf(stderr, "%s gain set to %d%% / %d%%n", dev, left, right);
?
?/* 關(guān)閉混音設(shè)備 */
?close(fd);
?return 0;
}
?
編譯好上面的程序之后,先不帶任何參數(shù)執(zhí)行一遍,此時(shí)會(huì)列出聲卡上所有可用的混音通道:
?
[xiaowp@linuxgam sound]$ ./mixer
usage: ./mixer
?????? ./mixer
Where is one of:
vol pcm speaker line mic cd igain line1 phin video
?
之后就可以很方便地設(shè)置各個(gè)混音通道的增益大小了,例如下面的命令就能夠?qū)D輸入的左、右聲道的增益分別設(shè)置為80%和90%:
?
[xiaowp@linuxgam sound]$ ./mixer cd 80 90
cd gain set to 80% / 90%