[開發記錄] 函式庫調用 - 使用ALSA進行音訊資料擷取 之其一
2012-08-17 11:05
267 查看
目前打算使用ALSA進行資料擷取,
關於ALSA API所搜尋到的網路資料( http://equalarea.com/paul/alsa-audio.html ),
其中有一段 Mini Capture程式,並作了些許修改,如底下所示:
在編輯時遭遇零件庫問題,直接將 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值,就可進行截取聲音訊號的工作,
內部參數的設定意義,等我了解後在另外補上。
關於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值,就可進行截取聲音訊號的工作,
內部參數的設定意義,等我了解後在另外補上。
相关文章推荐
- [開發記錄] 函式庫調用 - 使用ALSA進行音訊資料擷取 之其三
- [開發記錄] 函式庫調用 - 使用ALSA進行音訊資料擷取 之其二
- [開發記錄] 函式庫調用 - 使用C6accel 資料庫進行FIR函式調用 之其一
- [開發記錄] 函式庫調用 - 使用C6accel 資料庫進行FFT函式調用 之其一
- [開發記錄] 函式庫調用 - 使用C6accel 資料庫進行FFT函式調用 之其二
- [開發記錄] 函式庫調用 - GPIO控制 之二
- 【ALSA】使用ALSA進行音訊資料擷取 之其二
- (原創) M型化的NB使用族群:談ASUS Eee PC的爆紅對應用程式開發的影響 (OS) (Linux)
- 使用 Eclipse 開發J2EE 應用程式
- 伺服器應用程式無法使用,系統管理員注意:Web伺服器的應用程式事件記錄檔中的錯誤信息詳細說明了這項特定要求失敗的原因 請檢閱這個記錄項目,查看造成這個錯誤的原因為何
- [開發記錄] BBxM 系統建立
- [.NET] 使用 .NET Framework 開發 ActiveX Control (3) - 封裝,簽章與部署(转)
- 在存儲過程中調用別的存儲過程返回的記錄
- C# WebService (一)開發接口&調用
- VS2008調用時,出現「無法連接到 ASP.NET程式開發伺服器」或「webdev.webserver.exe」錯誤
- 用Demo記錄knockoutjs之mapping擴展的使用
- 使用XSLT和Struts框架開發Web應用
- 使用C#調用外部程式或是執行DOS命令
- 使用 Jersey + Tomcat 輕鬆開發 RESTful Web Service
- 自制反汇编工具使用实例 其一