Search code examples
pythonwav

Playing a sound from a wave form stored in an array


I'm currently experimenting with generating sounds in Python, and I'm curious how I can take a n array representing a waveform (with a sample rate of 44100 hz), and play it. I'm looking for pure Python here, rather than relying on a library that supports more than just .wav format.


Solution

  • You should use a library. Writing it all in pure python could be many thousands of lines of code, to interface with the audio hardware!

    With a library, e.g. audiere, it will be as simple as this:

    import audiere
    ds = audiere.open_device()
    os = ds.open_array(input_array, 44100)
    os.play()
    

    There's also pyglet, pygame, and many others..


    Edit: audiere module mentioned above appears no longer maintained, but my advice to rely on a library stays the same. Take your pick of a current project here:

    https://wiki.python.org/moin/Audio/

    https://pythonbasics.org/python-play-sound/

    The reason there's not many high-level stdlib "batteries included" here is because interactions with the audio hardware can be very platform-dependent.