简体   繁体   English

C:在Linux中播放音频循环

[英]C: playing audio loops in linux

I have a buffer of int16_t with some audio PCM data in it. 我有一个int16_t缓冲区, int16_t有一些音频PCM数据。 I need to play the buffer repetitively from a point a to a point b, so that you hear an infinite audio loop. 我需要从点a到点b重复播放缓冲区,以便您听到无限的音频循环。

I found that the easiest way to play sound is by using libao, but I agree with any other method. 我发现最简单的播放声音的方法是使用力宝,但是我同意任何其他方法。 This is my code: 这是我的代码:

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;  
}

The problem is that I hear a period of silence between the end and the start of the loop. 问题是我听到循环的结束和开始之间有一段沉默。 Because I'm using this code to testing other code, I absolutely need to know if it could be caused by an incorrect use of libao . 因为我正在使用此代码来测试其他代码,所以我绝对需要知道它是否可能是由于错误使用libao引起的。

Yes, it absolutely could be caused by incorrect use of libao. 是的,这绝对可能是由于错误使用力宝造成的。 Please remove the +1 from the ao_play() call, like so: 请从ao_play()调用中删除+1,如下所示:

ao_play(device, buf+a, b-a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM