Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Basic]

What is the Sound device of the Python module?

I think Pyaudio is the acoustic signal processing module that can be used in Python, but it's hard to use personally! Also, once it became unusable during development on Windows, Pyaudio has a light trauma, and I would like to introduce a Sound device that can control the audio interface based on ASIO.

Good points of sound device

・ Can control ASIO devices -Easy input / output channel mapping (microphone and speaker distribution) ・ Easy control of WASAPI and core audio ・ Easy to write and easy to use ・ Simultaneous recording / playback is super easy (I'm glad that it is very easy for people who are doing acoustic engineering to measure impulse response)

Items covered in this article

In this article, I will write about the preparation before using the sound device, the initial setting method for using it, and the basic playback method, recording method, simultaneous recording / playback method, and streaming method on the sound device.

For the channel mapping method (input from your favorite microphone and output from your favorite speaker in a multi-channel environment) and the detailed setting method of ASIO, WASAPI, Core Audio, etc., see the second part Sound signal processing module that can be used with Python Sounddevice ASIO [ Application] I would like to write.

Installation

(1) In Linux, macOS, and Anaconda environments, it can be installed from conda-forge.

(2) You can install from PyPl using pip.

If you installed Sounddevice on macOS or Windows using pip, the PortAudio library will be downloaded automatically. When installing on other platforms, please install each library.

How to use

import

import sounddevice as sd
import numpy as np #NumPy is basically required. Please import.
import wave #Import if you want to work with audio files

Since sounddevice handles data with a basic NumPy array, it is necessary to import NumPy. However, there is also a way to handle data without using NumPy, which will be described later.

Initial setting

With sounddevice, you can also preset the sample rate and number of channels that can be used by default, and the audio interface to be used. Of course, you can set it each time without setting it. In this article, I will describe both the case where the default setting is set and the case where it is not set, but for the time being, I will write how to set the default.

Sample rate setting

default_fs.py


fs = 44100 #Usually 41000Hz or 48000Hz
sd.default.samplerate = fs

Setting the number of channels

The number of channels to output is automatically set from the data input to the function, but you need to specify the number of input channels yourself.

default_ch.py


channels = 2
sd.default.channels = channels
#sd.default.channels = 1,You can also set the number of channels as 2 respectively

Setting of equipment used

First, get the device ID of the audio system installed or connected to your environment.

default_system.py


sd.query_devices()

When I typed it in the command prompt, I got the following output in my environment:

> 0 Built-in Microphone, Core Audio (2 in, 0 out)
< 1 Built-in Output, Core Audio (0 in, 2 out)
2 Pro Tools Equipment Set, Core Audio (2 in, 2 out)

Currently the input is set to ID 0 and the output is set to ID 1. If you have a machine connected to an ASIO device

3 〇〇Audio system, ASIO (24 in, 24 out)

Is output as. After confirming the device ID you want to use, set as follows.

default_system.py


sd.default.device = 3
# sd.default.device = [4, 5]You can also set each input / output as.

Setting the data type to record

When recording, it is recorded with'float32'by default, but if you want to record with a different data type, set as follows.

default_dtype.py


sd.default.dtype = 'float64'

Reset all default settings

Cancels the set initial settings

default_reset.py


sd.default.reset()

Playback

When executed, the function will return immediately but will continue to play in the background.

playback.py


sd.play(myarray,fs) 
# myarray :NumPy array holding audio data
# fs :Sample rate

Arguments can be reduced if the sample rate is initialized.

playback.py


sd.play(myarray)

Here's what the other options look like.

playback.py


sd.play(data,
        samplerate=None,
        mapping=None, #Channel mapping settings (explained in the second part)
        blocking=False, #True to prevent the function from returning (waits during playback)
        loop=False, #Loop play with True
        **kwargs) 

Stop playing (stop)

Stop playing the sound.

stop.py


sd.stop()

Recording

Record the acquired audio data in a NumPy array. By default, array data is recorded as'float32'. When executed, the function returns immediately, but continues recording in the background.

rec.py


duration = 10.5 #Playback time[Seconds]
recdata = sd.rec(int(duration * fs), samplerate = fs,channels = 2)
#Number of taps, sample rate, number of channels

You can reduce the number of arguments when you make the initial settings.

rec.py


recdata = sd.rec(int(duration * fs))

If you want to know if the recording is complete, use the following function. This function returns immediately if the recording is complete, and returns after the recording is complete if it is not. It can also be used when you want to move on to the next phase after recording is complete.

rec.py


sd.wait()

Other options are as follows

rec.py


recdata = sd.rec(frames=None,#Option: Not required if the number of recorded frames out is specified
                 samplerate=None,
                 channels=None,#Number of channels Not required if mapping or out is specified
                 dtype=None,#Data type setting
                 out=None,#Write the recorded data to an arbitrary array
                 mapping=None,#Channel mapping settings (explained in the second part)
                 blocking=False,#True to wait while recording
                 **kwargs)

Simultaneous Playback and Recording

You can play and record the Array at the same time. The number of output channels is automatically set from the given data, but you can set the number of input channels yourself.

playrec.py


recdata = sd.playrec(myarray,fs,channels = 2)

If the default sample rate and the number of channels are set by default, the description can be omitted as usual.

playrec.py


recdata = sd.playrec(myarray)

Click here for other options

playrec.py


recdata = sd.playrec(data,
                     samplerate=None,
                     channels=None,#If there is no setting of the number of output channels, it is automatically determined from the data
                     dtype=None,#Data type setting
                     out=None,#Write the recorded data to an arbitrary array
                     input_mapping=None,#Input channel mapping settings
                     output_mapping=None,#Output channel mapping settings
                     blocking=False,#True to wait while recording
                     **kwargs)

Streaming (Callback stream)

The sound captured by the microphone is sent to the speaker as it is.

stream.py


duration = 5.5  # seconds

def callback(indata, outdata, frames, time, status):
    if status:
        print(status)
    outdata[:] = indata

with sd.Stream(channels=2, callback=callback):
    sd.sleep(int(duration * 1000))   

Also note that sd.sleep here can freeze the next call at any time, but it's not very accurate.

sleep.py


sd.sleep(msec)

So far for the time being

I think this is enough for easy usage. For the channel mapping method (input from your favorite microphone and output from your favorite speaker in a multi-channel environment) and the detailed setting method of ASIO, WASAPI, Core Audio, etc., see the second part Sound signal processing module that can be used with Python Sounddevice ASIO [ Application] I would like to write.

reference

Python sound device official Sound signal processing module that can be used with Python Sounddevice ASIO [Application]

Recommended Posts

Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Basic]
Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Application]
File types that can be used with Go
Basic algorithms that can be used in competition pros
Python knowledge notes that can be used with AtCoder
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Python standard module that can be used on the command line
Easy padding of data that can be used in natural language processing
Mathematical optimization that can be used for free work with Python + PuLP
Until youtube-dl can be used with Synology (DS120j)
I made a familiar function that can be used in statistics with Python
No module named'distutils.util' cannot be used with get-pip.py
Linux command that can be used from today if you know it (Basic)
List packages that can be updated with pip
"Gazpacho", a scraping module that can be used more easily than Beautiful Soup
Convert images from FlyCapture SDK to a form that can be used with openCV
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
File sharing server made with Raspberry Pi that can be used for remote work
Japanese can be used with Python in Docker environment
Color list that can be set with tkinter (memorial)
ANTs image registration that can be used in 5 minutes
[Django] About users that can be used on template
Limits that can be analyzed at once with MeCab
Format summary of formats that can be serialized with gensim
It seems that Skeleton Tracking can be done with RealSense
Basic knowledge of DNS that can not be heard now
pd.tseries.offsets.DateOffset can be quite slow if not used with caution
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
Scripts that can be used when using bottle in Python
Acoustic signal processing starting with Python-Let's make a stereophonic system
I investigated the pretreatment that can be done with PyCaret
Let's make a diagram that can be clicked with IPython
Understand the probabilities and statistics that can be used for progress management with a python program
About the matter that torch summary can be really used when building a model with Pytorch
[Python] Make a graph that can be moved around with Plotly
Until torch-geometric can be used only with Windows (or Mac) CPU
Make a Spinbox that can be displayed in Binary with Tkinter
A timer (ticker) that can be used in the field (can be used anywhere)
I made a shuffle that can be reset (reverted) with Python
Make a currency chart that can be moved around with Plotly (2)
Python standard input summary that can be used in competition pro
Comparison of 4 styles that can be passed to seaborn with set_context
Make a Spinbox that can be displayed in HEX with Tkinter
Make a currency chart that can be moved around with Plotly (1)