簡介
高級 Linux 聲音體系(Advanced Linux Sound Architecture,ALSA)是Linux中提供聲音設備驅動的內核組件萤捆,用來代替原來的開放聲音系統(tǒng)(Open Sound System怨咪,OSSv3)赋铝。除了聲音設備驅動,ALSA還包含一個用戶空間的函數(shù)庫谍肤,開發(fā)者可以通過這些高級 API 使用驅動,不必直接與內核驅動進行交互(1)灶轰。
安裝
sudo apt install libasound2-dev
流程
- 打開設備
- 分配參數(shù)內存
- 填充默認參數(shù)
- 設置參數(shù)(詳細的參見 ALSA - PCM接口)
- 通道數(shù)
- 采樣率(碼率谣沸,用來指定時間和文件大小,frames/s)
- 幀數(shù)(每次讀取的數(shù)據(jù)長度與該參數(shù)有關)
- 數(shù)據(jù)格式(影響輸出數(shù)據(jù)笋颤、緩存大小)
- 設備訪問類型(直接讀寫乳附、內存映射,交錯模式伴澄、非交錯模式)
- 讀取赋除、寫入數(shù)據(jù)
簡單的例子
- 包含頭文件
#include <alsa/asoundlib.h>
- 查看設備,根據(jù)最后兩個數(shù)字確定設備名稱非凌,通常default就行了
aplay -L
- 定義相關參數(shù)举农,錄放音都要經過相同的步驟,放一起定義
// 設備名稱敞嗡,這里采用默認颁糟,還可以選取"hw:0,0","plughw:0,0"等
const char *device = "default";
// 設備句柄
// 以下均定義兩個,根據(jù)前綴區(qū)分喉悴,c->capture,p->playback,沒有前綴的表示參數(shù)相同
snd_pcm_t *chandle;
snd_pcm_t *phandle;
// 硬件參數(shù)
snd_pcm_hw_params_t *cparams;
snd_pcm_hw_params_t *pparams;
// 數(shù)據(jù)訪問類型棱貌,讀寫方式:內存映射或者讀寫,數(shù)據(jù)
snd_pcm_access_t access_type = SND_PCM_ACCESS_RW_INTERLEAVED;
// 格式箕肃,
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
// 碼率婚脱,采樣率,8000Hz,44100Hz
unsigned int rate = 44100;
// 通道數(shù)
unsigned int channels = 2;
// 幀數(shù),這里取32
snd_pcm_uframes_t frames = 32;
// 以下為可選參數(shù)
unsigned int bytes_per_frame;
// 軟件重采樣
unsigned int soft_resample;
- 打開設備
snd_pcm_open(&chandle, device, SND_PCM_STREAM_CAPTURE, 0);
snd_pcm_open(&phandle, device, SND_PCM_STREAM_PLAYBACK, 0);
增加一個錯誤判斷
int err;
if ((err = snd_pcm_open(&chandle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0)
{
std::cout << "Capture device open failed.";
}
if ((err = snd_pcm_open(&phandle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
std::cout << "Playback device open failed.";
}
- 設置參數(shù),這里就不增加錯誤判斷了障贸,不然顯得有些長了
// 先計算每幀數(shù)據(jù)的大小
bytes_per_frame = snd_pcm_format_width(format) / 8 * 2;
// 計算需要分配的緩存空間的大小
buffer_size = frames * bytes_per_frame;
// 為參數(shù)分配空間
snd_pcm_hw_params_alloca(¶ms);
// 填充參數(shù)空間
snd_pcm_hw_params_any(handle, params);
// 設置數(shù)據(jù)訪問方式
snd_pcm_hw_params_set_access(handle, params, access_type);
// 設置格式
snd_pcm_hw_params_set_format(handle, params, format);
// 設置通道
snd_pcm_hw_params_set_channels(handle, params, channels);
// 設置采樣率
snd_pcm_hw_params_set_rate_near(handle, params, &rate, 0);
// 可選項错森,不改不影響
// 設置緩存大小
buffer_size = period_size * 2;
snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);
// 設置段大小,period與OSS中的segment類似
period_size = buffer_size / 2;
snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, 0));
//設置參數(shù)
snd_pcm_hw_params(handle, params);
- 讀寫數(shù)據(jù)
// 分配緩存空間篮洁,大小上面通過buffer_size計算出了
char *buffer = (char *)malloc(buffer_size);
// 讀寫數(shù)據(jù)
snd_pcm_readi(chandle, buffer, frames);
snd_pcm_writei(phandle, buffer, frames);
- 循環(huán)播放
while(1)
{
snd_pcm_readi(chandle, buffer, frames);
snd_pcm_writei(phandle, buffer, frames);
}
- 捕獲一定時間的音頻數(shù)據(jù)到文件流
ofstream output("test.pcm", ios::trunc);
int loop_sec;
int frames_readed;
loop_sec = 10;
unsigned long loop_limit;
// 計算循環(huán)大小
loop_limit = loop_sec * rate;
for (size_t i = 0; i < loop_limit; )
{
// 這里還需要判斷一下返回值是否為負
frames_readed = snd_pcm_readi(chandle, buffer, frames);
output.write(buffer, buffer_size);
i += frames_readed;
}
- 關閉設備涩维、釋放指針
snd_pcm_close(chandle);
snd_pcm_close(phandle);
free(buffer);
封裝一下
問題
- [已解決] 錄放音過程中出現(xiàn)高音,多半是讀寫設備是緩存大小設置的問題嘀粱。
- [已解決] 放音過程中也許會出現(xiàn)"Broken pipe"的錯誤激挪,添加如下需要重新準備設備
err = snd_pcm_writei(handle, input_buffer, frames);
if (err == -EPIPE)
{
snd_pcm_prepare(handle);
continue;
// 或者
// return 0;
}
- [未解決] 如果錄音之后立即放音,開始不會有延時锋叨,但是時間長了以后延時會累加垄分。