?? linux音頻編程2.txt
字號(hào):
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) { /* 沒有找到匹配項(xiàng) */
fprintf(stderr, "%s is not a valid mixer device\n", 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 device\n", 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 <device> <left-gain%> <right-gain%>
./mixer <device> <gain%>
Where <device> 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%
五、小結(jié)
隨 著Linux平臺(tái)下多媒體應(yīng)用的逐漸深入,需要用到數(shù)字音頻的場(chǎng)合必將越來越廣泛。雖然數(shù)字音頻牽涉到的概念非常多,但在Linux下進(jìn)行最基本的音頻編 程卻并不十分復(fù)雜,關(guān)鍵是掌握如何與OSS或者ALSA這類聲卡驅(qū)動(dòng)程序進(jìn)行交互,以及如何充分利用它們提供的各種功能,熟悉一些最基本的音頻編程框架和 模式對(duì)初學(xué)者來講大有裨益。
六、參考資料
1. OSS是Linux上最早出現(xiàn)的聲卡驅(qū)動(dòng)程序,http://www.opensound.com是它的核心網(wǎng)站,從中可以了解到許多與OSS相關(guān)的信息。
2. ALSA是目前廣泛使用的Linux聲卡驅(qū)動(dòng)程序,并且提供了一些庫函數(shù)來簡(jiǎn)化音頻程序的編寫,在其官方網(wǎng)站http://www.alsa-project.org/上可以了解到ALSA的許多信息,并能夠下載到最新的驅(qū)動(dòng)程序和工具軟件。
3. Ken C. Pohlmann著,蘇菲譯,數(shù)字音頻原理與應(yīng)用(第四合版),北京:電子工業(yè)出版社,2002
4. 鐘玉琢等編著,多媒體技術(shù)及其應(yīng)用,北京:機(jī)械工業(yè)出版社,2003
一個(gè)測(cè)試通過的音頻播放程序
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#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 id; /*聲音輸出文件描述符*/
int arg;/* 用于ioctl調(diào)用的參數(shù) */
int status; /* 系統(tǒng)調(diào)用的返回值 */
int i;
int j;
/* 打開聲音設(shè)備 */
fd = open("/dev/dsp", O_RDWR);//arm下的音頻文件是"/dev/sound/dsp";
if (fd < 0) {
perror("open of /dev/dsp failed");
exit(1);
}
/*打開輸出文件*/
id=open("Music.wav",O_RDWR);
if(id < 0){
perror("open of sound file 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");
/* 讀取一定數(shù)量的音頻數(shù)據(jù),并將之寫到輸出文件中去 */
for(i=0;i<10;i++){
j=read(id, buf, sizeof(buf));
if (j > 0){
write(fd, buf, j); /* 放音 */
}
}
/* 關(guān)閉輸入、輸出文件 */
close(fd);
close(id);
return 1;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -