您的位置:首页 > 其它

[開發記錄] 函式庫調用 - 使用ALSA進行音訊資料擷取 之其一

2012-08-17 11:05 267 查看
目前打算使用ALSA進行資料擷取,

關於ALSA API所搜尋到的網路資料( http://equalarea.com/paul/alsa-audio.html ),
其中有一段 Mini Capture程式,並作了些許修改,如底下所示:

//---------------ALSA 測試程式---------------
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

int main (int argc, char *argv[])
{
int i;
int err;

unsigned int sample_rate;
int sample_dir;

int size;
snd_pcm_uframes_t frames;
char *buf;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;

if ((err = snd_pcm_open(&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
argv[1],
snd_strerror (err));
exit(1);
}	printf("open OK!\n");

if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit(1);
}	printf("set allocation OK!\n");

if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit(1);
}	printf("set para OK!\n");

if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit(1);
}	printf("set access OK!\n");

if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit(1);
}	printf("set sample format OK!\n");

sample_rate=44100;
sample_dir=0;
//snd_pcm_hw_params_set_rate_near (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir) 
if ((err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &sample_rate, &sample_dir)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit(1);
}	printf("set sample rate OK!\n");

if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, 2)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit(1);
}	printf("set channel OK!\n");

if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit(1);
}	printf("set para OK!\n");

snd_pcm_hw_params_free(hw_params);

if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit(1);
}	printf("prepare audio interface OK!\n");

frames=128;
size=frames*4;
buf=(char*)malloc(size);
for (i = 0; i < 10; ++i) {
//snd_pcm_readi(snd_pcm_t *pcm,void *buffer,snd_pcm_uframes_t size)
if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) {
fprintf (stderr, "read from audio interface failed (%s)\n",
snd_strerror (err));
exit (1);
}
}
free(buf);
snd_pcm_close (capture_handle);
printf("end of Mini-Capture!\n");
exit (0);
}

在編輯時遭遇零件庫問題,直接將 linux-devkit/arm-none-linux-guneabi/usr/include以及lib都複製到 CodeSourcery/Sourcery_G++_Lite/arm-none-linux-guneabi/相對應的資料夾,解決!

使用 gcc時,需要指定使用 -lasound 函式庫。

程式內,部分增加註解的地方,是由於函式庫的參數錯誤,出現Segmentation fail的訊息,修改參數類型後重新編譯,

在BBxM內,執行./MiniCapture default 指定設備使用default值,就可進行截取聲音訊號的工作,

內部參數的設定意義,等我了解後在另外補上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息