버퍼에 오디오 PCM 데이터가 포함 된 int16_t
버퍼가 있습니다. 점 a에서 점 b까지 버퍼를 반복 재생해야하므로 무한 오디오 루프가 들립니다.C : 리눅스에서 오디오 루프 재생
사운드를 재생하는 가장 쉬운 방법은 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으로 인해 발생할 수 있는지를 꼭 알아야합니다.
난 항상 리눅스에서 소리를 할 수있는 쉬운 방법을 생각 단지 파이프는/dev/snd''에 :) – nneonneo
였습니다이 보인다 최근의 커널에서는 보안 문제 때문에 더 이상 허용되지 않습니다. 데비안 6.0.6과 커널 2.6.32-5를 사용하고 있습니다. – fortea