Sounddevice Library in Python: Complete Guide to Sound Processing

sounddevice is a Python package that provides an interface to play and record sound using PortAudio, a cross-platform audio I/O library. It allows you to interact with audio streams in real-time, making it useful for a variety of audio applications such as speech recognition, music production, and audio analysis

With sounddevice, you can:

  • Play audio files and streams with low-latency
  • Record audio input from microphones and other devices
  • Monitor audio input/output in real-time
  • Manipulate audio data in real-time using NumPy arrays
  • Control audio parameters such as sample rate, channel count, and format

sounddevice is designed to be easy to use and provides a high-level interface that abstracts away many of the details of working with audio streams.

Step-by-Step Guide: Installing and Utilizing sounddevice in Python for Audio Processing

You can install the sounddevice package in Python using pip, which is the standard package installer for Python. Here are the steps to install and use sounddevice:

  1. Install the sounddevice package using pip by running the following command in your terminal or command prompt:
  2. pip install sounddevice                          
  3. Import the sounddevice module in your Python script:
  4. import sounddevice as sd                          
  5. Use the function to play an audio file or stream. For example, to play a WAV file:
  6. import soundfile as sf
    data, samplerate ='example.wav'), samplerate)                          
  7. Use the sd.rec() function to record audio input from a microphone or other device. For example:
  8. duration = 5  # seconds
    samplerate = sd.query_devices('default')['default_samplerate']
    recording = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=2)
    sd.wait()  # Wait for the recording to complete
    sf.write('recording.wav', recording, samplerate)                          
  9. Use the sd.Stream() class to create an audio stream for real-time processing. For example:
  10. def callback(indata, outdata, frames, time, status):
        if status:
        outdata[:] = indata  # Pass input directly to output
    with sd.Stream(callback=callback):
        sd.sleep(10000)  # Run the stream for 10 seconds                          

These are just a few examples of what you can do with sounddevice. The package provides many more functions and options for working with audio streams. The documentation is available online and provides detailed explanations of each function and option.

Step-by-Step Guide: Recording Voice with Python sounddevice, scipy, and wavio Modules

This code records an audio signal for a specified duration and sampling frequency using the sounddevice library, saves the recorded signal to a WAV file using either the or the wavio.write() function, and stores the resulting file in the current working directory.

To use this code, you would need to have the required Python libraries installed (sounddevice, scipy, and wavio), and you would need to run the code on a computer with a functioning audio input device. Once the code is executed, it will record an audio signal for the specified duration, and save the resulting file in the same directory as the code. The saved file can then be played back or analyzed using various audio processing tools.

This code imports three libraries/modules: sounddevice,, and wavio.

  • sounddevice is a Python library used for recording and playing sound by interacting with the operating system's sound drivers.
  • is a module from the SciPy library used for reading and writing WAV files.
  • wavio is another Python module that can read and write WAV files.
import sounddevice as sd
from import write
import wavio as wv

The code sets the sampling frequency to 44100 Hz and the duration of the recording to 5 seconds. Then, it records an audio signal using sd.rec(), which records an array of samples from the default input device for the specified duration and sampling frequency.

freq = 44100  # Hz
duration = 5  # seconds

The sd.wait() function blocks the program until the recording is finished, ensuring that the recording is complete before the next step.

recording = sd.rec(int(duration * freq), samplerate=freq, channels=2)

Finally, the recorded signal is saved to a WAV file using one of two methods. The first method uses the function, which takes the filename, the sampling frequency, and the recorded signal as arguments. The second method uses the wavio.write() function, which takes the filename, the recorded signal, the sampling frequency, and the sample width as arguments. Both methods produce the same result, which is a WAV file containing the recorded audio signal.

write('recording0.wav', freq, recording)
# or
wv.write('recording1.wav', recording, freq, sampwidth=2)

In summary, by harnessing the power of Python's sounddevice, scipy, and wavio modules, users can confidently embark on recording voice and working with audio data. This project serves as a valuable resource for anyone looking to expand their skills in audio processing using Python.

You can watch the video below: