As of Nsound-0.8.1, playback through the soundcard is now supported on platforms that have libao or libportaudio.
Audio playback can be as simple as:
import Nsound as ns
a = ns.AudioStream("california.wav")
a >> ns.AudioPlayback(a.getSamplerate(), a.getNChannels(), 16)
The Nsound.AudioPlayback constructor:
Not all combinations of sample_rate, n_channels and bits_per_sample will work. Most platforms should work with
Other combinations have not been tested.
The audio backend to use can be selected. By default, the audio backend is automatically seleceted based on the backends that were available at compile time in the following order:
Following the Matplotlib convention, a call to Nsound.use() can select the audio backend to use.
The backend to use; currently recognized backends:
- “portaudio” or “libportaudio”
- “ao” or “libao”
The backend must be selected before initializing the AudioPlayback class:
ns.use("portaudio")
pb = ns.AudioPlayback(44100.0, 2, 16)
buffer_or_audio_stream >> pb
See src/examples/example1.py and src/examples/example4.py for AudioPlayback example usage.
The Nsound.AudioPlayback class is not thread safe, so only one thread can use the class at a time. Only blocking calls are currently implemented, with memory being allocated and floating point numbers being casted to integer types on a call by call basis.
If you are on Linux and you are using libportaudio, you may see a message like:
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
Audio will still be played to the sound card (at least on Nick’s Ubuntu 10.04 box.) The error happens because Bluetooth is disabled but ALSA is still configured to use Bluetooth services, to fix this, remove the bluez-alsa package:
$ sudo apt-get purge bluez-alsa
The error messsage should now go away, but be careful with this solution if you use Bluetooth devices for audio.