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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
To play sound given array input_array of 16 bit samples. This is modified example from pyadio documentation page
or use the sounddevice module. Install using
pip install sounddevice
, but you need this first:sudo apt-get install libportaudio2
absolute basic:
A few more options:
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:
There's also pyglet, pygame, and many others..
Here's a snippet of code taken from this stackoverflow answer, with an added example to play a numpy array (scipy loaded sound file):
Basically you can just call the tobytes() method; the returned bytearray then can be played.
P.S. this method is supa fast
I think you may look this list http://wiki.python.org/moin/PythonInMusic It list many useful tools for working with sound.