我有一個int16_t
的緩衝區,裏面有一些音頻PCM數據。我需要從a點到b點重複播放緩衝區,以便聽到無限的音頻循環。C:在linux中播放音頻循環
我發現播放聲音最簡單的方法是使用libao,但我同意其他方法。 這是我的代碼:
int play(int a, int b, char *buf);
int main()
{
int16_t *buf; /*my buffer*/
int a, b;
/* a and b are the indexes of the buffer;
* because libao wants a buffer of char,
* and buf points to of int16_t, I'll pass
* the value a and b multiplied with 2.
*/
[···]
play(2*a, 2*b, (char *) buf);
return 0;
}
int play(int a, int b, char *buf)
{
ao_device *device;
ao_sample_format format;
int default_driver;
/* -- Initialize -- */
fprintf(stderr, "libao example program\n");
ao_initialize();
/* -- Setup for default driver -- */
default_driver = ao_default_driver_id();
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 1;
format.rate = 44100;
format.byte_format = AO_FMT_LITTLE;
/* -- Open driver -- */
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
fprintf(stderr, "Error opening device.\n");
exit(1);
}
/* -- Play the infinite loop -- */
for (;;){
ao_play(device, buf+a, b-a+1);
/*buf+a is the start of the loop, b-a+1 the number of byte to play--edited*/
}
/* -- Close and shutdown -- */
ao_close(device);
ao_shutdown();
return 0;
}
的問題是,我聽到一個時期的結束和循環的開始之間的沉默。因爲我正在使用此代碼來測試其他代碼,所以我絕對需要知道它是否可能是由於錯誤地使用了libao而導致的。
我一直認爲做無害化在Linux中最簡單的方法只是爲了管'的/ dev/snd' :) – nneonneo 2013-03-10 13:41:40
它似乎在最近的內核中不再允許,因爲安全性問題。我正在使用Debian 6.0.6和內核2.6.32-5 – fortea 2013-03-10 13:48:42