Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygame mixer save audio to disk?

Using pygame mixer, I open an audio file and manipulate it. I can't find a way to save the "Sound object" to a local file on disk.

sound_file = "output.mp3"
sound = pygame.mixer.Sound(sound_file)

Is there any way to do this? I have been researching pygame mixer docs but I couldn't find anything related to this.

like image 812
RoundOutTooSoon Avatar asked Dec 06 '25 15:12

RoundOutTooSoon


1 Answers

Your question is almost two years old, but in case people are still looking for an answer: You can save PyGame Sound instances by using the wave module (native Python).

# create a sound from NumPy array of file
snd = pygame.mixer.Sound(my_sound_source)

# open new wave file
sfile = wave.open('pure_tone.wav', 'w')

# set the parameters
sfile.setframerate(SAMPLINGFREQ)
sfile.setnchannels(NCHANNELS)
sfile.setsampwidth(2)

# write raw PyGame sound buffer to wave file
sfile.writeframesraw(snd.get_buffer().raw)

# close file
sfile.close()

More info and examples on GitHub: https://github.com/esdalmaijer/Save_PyGame_Sound.

like image 164
esdalmaijer Avatar answered Dec 08 '25 10:12

esdalmaijer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!