Pygame audio and buffers - how does this work?

Viewed 192

I am trying to get 8 16bit 44khz stereo wav files playing simultaneously off a raspberry-pi like board with only 512MB of ram.

My Python kept crashing with the fairly nondescript error: Killed so I decided to have a look with python console. When I added wav files to the mixer in the following manner, I noticed that in htop the Python memory usage was going up by the size of the files I was adding:

pygame.mixer.init(buffer=4096)
pygame.mixer.Channel(0).play(pygame.mixer.Sound("sample_0.wav"), loops=-1)
pygame.mixer.Channel(1).play(pygame.mixer.Sound("sample_1.wav"), loops=-1)
pygame.mixer.Channel(2).play(pygame.mixer.Sound("sample_2.wav"), loops=-1)
...

My understanding of how pygame mixer works is that it acts as a frontend to SDL_mixer, which as far as I can tell, should do the chunking / buffering automatically when passed a file to open.

Is this the intended operation? Does pygame load the whole file into memory anyway, and if so is that intentional and what is the purpose of the buffer?

Is it intended for the user to split audio into buffers manually? It doesn't seem that way but pygame documentation is pretty vague..

1 Answers

Okay so it turns out SDL_mixer library supports streaming from files for Music objects only, and regular Sound objects are always loaded into memory.

Related