pythonaudiopython-2.7savepygame

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.


Solution

  • 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.