I am translating a metronome I wrote for Android into Python for classic Desktop machines.
Under Android, the buffer was 2s long and always filled wich means it was 2s-lag-proof. With Python and Pyaudio, I am doing something like (bar with 4 beats):
bar = 0
while self.isRunning:
stream.write(beat[bar])
print("beat {} was read".format(bar)
bar += 1
bar %= 4
And see:
*sound of beat[0] is played*
beat 0 was read
*sound of beat[1] is played*
beat 1 was read
...
PyAudio is waiting for his buffer to get empty before resuming. The "Blocking" way as I understand.
Instead, I would like to see (as in Android)
*sound of beat[0] is played*
beat 0 was read
beat 1 was read
beat 2 was read
*sound of beat[1] is played*
beat 3 was read
*sound of beat[2] is played*
beat 3 was read
With a BPM of 120 it means the stream always has a 2sec buffer ready to be read.
My question: Is there any to feed the buffer at the same time it is read? When I try my metronome in a virtual machine on crappy hardware host, the first beats are lagging wich worry me...
I am not sure the callback method would allow that and since I need to play specific sound depending on where I am in the structure (my metronome handle complex structure) and bar, it would be painful to implement.