PulseAudio

HI, I am new to the pulse audio libraries, and buffers, data streams, draining, flushing, and the sort. I am trying to play a sound generated by my program. Here is my code:
1
2
3
4
for (double sec = 0;;sec += 1){
        uint8_t d = (int)((w.getOutput(sec/44100))*128);
        pa_simple_write(s,&d,sizeof(uint8_t), NULL);
    }


w.getOutput(sec/44100)
in this situation is a double, shaped like a sin wave playing C5 from -1 to 1
It seems to be playing the sin wave very smoothly, except every half second or so it goes quiet, and then immediately continues in another half second.
Do i need to send more data to pa_simple_write() at once by using a buffer? also am I possibly sending too much data (more than 44100 points per second) ?
Writing one sample at a time is not usually how it works.

Streaming audio can be visualized by imagining an hour glass. You have sand in the top of the hourglass, and is slowly and steadily pours out the bottom at a fixed rate. To keep the sand flowing, you have to periodically check the top of the hourglass and see if it's running out of sand. If it is, you dump a bunch more sand in it.


Streaming audio is the same way. There's an audio buffer, and it slowly and steadily empties over time as the audio is played. Your job as the programmer is to make sure there is always audio in that buffer so that there is always something to play.

If the buffer ever runs empty, you'll have breaks in the sound (similar to what you're hearing).



I've never used PulseAudio, so I don't know the details of how to do this with that lib, but the concept should be more or less the same across all audio libs.
thanks for the help! I changed around the code write a buffer of 1024 samples at once and now it works perfect.
1
2
3
4
5
6
7
uint8_t buf[1024];
    for(double sec = 0;;){
        for (int i = 0; i<1024;i++,sec++){
            buf[i]=(int)((w.getOutput(sec/44100))*128);
        }
        pa_simple_write(s,buf,sizeof(buf),NULL);
    }
Topic archived. No new replies allowed.