Forums

PyAudio audio playback is very choppy after increasing the buffer

Hello, I'm currently working on a project involving the concept of delayed auditory feedback. I'm pretty close with the implementation but there is a small bug that I'm hoping someone can help me fix. I'm changing the buffer each iteration of the while loop when grabbing the audio to produce a "ramping effect" where the delay in the audio increases over time. The effect does work but when the buffer is increased too high (around 200ms) the audio that is playing back gets choppy and unrecognizable. This is what I have for setting up the input and output devices:

streamIn = device.open(
        format=paFloat32,
        channels=self._CHANNELS,
        rate=self._RATE,
        input=True,
        frames_per_buffer=44
    )
streamOut = device.open(
        format=paFloat32,
        channels=self._CHANNELS,
        rate=self._RATE,
        output=True,
        frames_per_buffer=44
    )

Once those are set up, this is the while loop that grabs the audio each iteration:

delayDisplay = 1
while self._streamIn.is_active():
    if self._buffer < self._actualBuffer:
        self._buffer = self._buffer + 44
        delayDisplay = delayDisplay + 1
    print(delayDisplay / 1000)
    audioData = self._streamIn.read(self._buffer)
    self._streamOut.write(audioData)

As you can see, the 'self._buffer' increases by 44 (roughly 1ms) each iteration. This is causing the audio to get very choppy at higher values and I'm not really sure how to prevent this from happening. If anyone has any idea on how to fix this is would be much appreciated, thank you.

Choppy audio appears to be exactly what you're asking for. Stopping an audio stream and then restarting it after a delay, is pretty much the definition of choppy audio. At lower delays, it wouldn't be very noticeable to the ear, but would become noticeable when the delay is longer.