[Python] Localize sound source only to human voice with ReSpeaker

What is sound source localization?

Sound source localization is where the sound is heard, and imagine stereophonic sound or binaural recording. By localizing the sound source for stereophonic sound, you can feel that the instrument can be heard from behind.

This time, we will use ReSpeaker Mic Array v2.0.

image.png

Basically all the usage and other options are written here, but I will briefly summarize them.

Implementation of sound source localization

environment

ubuntu 18.04 python3


Environment


sudo apt-get update
sudo pip install pyusb click
git clone https://github.com/respeaker/usb_4_mic_array.git
cd usb_4_mic_array
sudo python dfu.py --download 6_channels_firmware.bin

I think the usb_4_mic_array folder has been created. Create and execute the following code under this.

DOA.py


from tuning import Tuning
import usb.core
import usb.util
import time
 
dev = usb.core.find(idVendor=0x2886, idProduct=0x0018)
 
if dev:
    Mic_tuning = Tuning(dev)
    print(Mic_tuning.direction)
    while True:
        try:
            print(Mic_tuning.direction)
            time.sleep(1)
        except KeyboardInterrupt:
            break

Output result ↓

~/usb_4_mic_array$ python3 DOA.py 
122
121
120
122

If you get an error

When I ran this code ImportError: No module named usb.core When it comes out


sudo apt-get install python-usb python3-usb

To execute.

Also, if permission denied, udev will give you device access.

sudo touch /etc/udev/rules.d/10-any_name_is_ok.rules
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="2886", ATTR{idProduct}=="0018", MODE="0666", GROUP="plugdev"' | sudo tee 10-any_name_is_ok.rules
sudo gpasswd -a username plugdev
sudo chmod a+r /etc/udev/rules.d/10-any_name_is_ok.rules
sudo udevadm control --reload-rules
udevadm trigger

After this, it will be reflected when you restart.


Respond only to human voice

There are many options for ReSpeaker. [Optional Parameter List (Frequently Asked Questions Q1)] (https://wiki.seeedstudio.com/ReSpeaker_Mic_Array_v2.0/#faq)

As an example, we will use SPEECHDETECTED to implement sound source localization with only human voice. 0 = false (no speech detected) 1 = true (speech detected) So, modify DOA.py as follows.

voise_angle.py


from tuning import Tuning
import usb.core
import usb.util
import time
 
dev = usb.core.find(idVendor=0x2886, idProduct=0x0018)
 
if dev:
    Mic_tuning = Tuning(dev)
    while True:
        try:
            if Mic_tuning.read('SPEECHDETECTED') == 1:
                 print(Mic_tuning.direction)
                 time.sleep(1)
        except KeyboardInterrupt:
            break

With this, you can prevent the angle from being output even if you clap your hand, for example.


Caution

Since these codes use usb_4_mic_array / Tuning.py, you need to put Tuning.py in a folder or write the function of Tuning.py in the code to execute it outside this folder.

Here is an example of rewriting so that it can be used in any folder.

get_angle.py


from time import sleep

import usb
import usb.core
import usb.util
import struct

dev = usb.core.find(idVendor=0x2886,idProduct=0x0018)
TIMEOUT = 100000

# PARAMETERS for sound localization
PARAMETERS = {
    'DOAANGLE': (21, 0, 'int', 359, 0, 'ro', 'DOA angle. Current value. Orientation depends on build configuration.'),
    'SPEECHDETECTED': (19, 22, 'int', 1, 0, 'ro', 'Speech detection status.', '0 = false (no speech detected)',
                       '1 = true (speech detected)'),
    }

def read(param_name):
    try:
        data = PARAMETERS[param_name]
    except KeyError:
        return

    id = data[0]

    cmd = 0x80 | data[1]
    if data[2] == 'int':
        cmd |= 0x40

    length = 8

    response = dev.ctrl_transfer(
        usb.util.CTRL_IN | usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_RECIPIENT_DEVICE,
        0, cmd, id, length, TIMEOUT)

    response = struct.unpack(b'ii', response.tostring())

    if data[2] == 'int':
        result = response[0]
    else:
        result = response[0] * (2. ** response[1])

    return result

# Find angular
if dev:

    while True:
        if read('SPEECHDETECTED') == 1:
            print(read('DOAANGLE'))
            sleep(1)

With this, you can localize the sound source under any folder.


Bonus: LED control

Do the following wherever you like (either at home or in usb_4_mic_array)

python


git clone https://github.com/respeaker/pixel_ring.git
cd pixel_ring
sudo python setup.py install
sudo python examples/usb_mic_array.py

When you do this, How to wake up for 3 seconds How to shine think for 3 seconds speak 6 seconds to shine off How to shine for 3 seconds To do. Please refer to pixel_ring / pixel_ring / usb_pixel_ring_v2.py for other ways of lighting.

Recommended Posts

[Python] Localize sound source only to human voice with ReSpeaker
Python to remember only with hello, worlds
Voice analysis with python
Voice analysis with python
I tried to make a real-time sound source separation mock with Python machine learning
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Switch python to 2.7 with alternatives
Write to csv with Python
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
Get the source of the page to load infinitely with python.
How to change Django's SQLite3 uploaded to python anywhere with GUI only
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Try to get CloudWatch metrics with re: dash python data source
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Install Python from source with Ansible
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
Easily post to twitter with Python 3
I want to debug with Python
Let's analyze voice with Python # 1 FFT
Challenge Fizz Buzz! Problems with Python in 5 patterns (only now up to 398)
Source code of sound source separation (machine learning practice series) learned with Python
Try to reproduce color film with Python
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
Convert memo at once with Python 2to3
HTML email with image to send with python
Memo to ask for KPI with python
Add a Python data source with Redash
Output color characters to pretty with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
I wanted to solve ABC160 with Python
Connect to MySQL with Python within Docker
How to work with BigQuery in Python
[Introduction to Python] Let's use foreach with Python
Single pixel camera to experience with Python
[Python] Introduction to CNN with Pytorch MNIST
Let's make a voice slowly with Python
Convert FX 1-minute data to 5-minute data with Python
I want to analyze logs with Python
How to do portmanteau test with python
I want to play with aws with python