[PYTHON] Media programming with Raspberry Pi (preparation for audio)

Introduction

When I tried to do real-time sound processing using pyaudio + scipy + numpy on Raspberry Pi, it took a lot of time and effort, so I summarized it in the article. This article is a preparatory part, and it is up to the point where you install python 3.7.6 on Raspberry Pi and install and use numpy, scipy and pyaudio under that environment.

Using the environment maintained in this document, I will describe the software that pitch shifts in real time with Raspberry Pi in python, but I will introduce it in another article.

NOOBS 3.3.0 This time, I will install NOOBS 3.3.0 newly and describe the procedure immediately after that.

Installation of necessary libraries, etc.

Since it is necessary for pyaudio, install the following libraries etc .:

sudo apt-get install portaudio19-dev python-pyaudio libatlas-base-dev

To avoid getting _ctypes related errors in scipy, install the following libraries (you need to apt-get install before installing python 3.7.6 with pyenv which you will install later):

sudo apt-get install libffi-dev

Install the following libraries to install pyenv:

sudo apt-get install libbz2-dev libreadline-dev libsqlite3-dev

Install pyenv

Run the following command to install pyenv:

git clone https://github.com/yyuu/pyenv.git ~/.pyenv

Since pyenv cannot be executed as it is, add the following contents to ~ / .profile to set environment variables and initialize pyenv. The editor can be anything, but if you want to use the nano editor installed by default, use nano ~ / .profile.

~/.profile


export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

For the nano editor, type the above and press Ctrl-O and you will be asked if you want to write to a file. Check the file name and press the enter key to write. Then press Ctrl-X to exit the nano editor.

After writing the above contents in ~ / .profie,

source ~/.profile

As, the content just written (environment variable setting) is reflected. Whether or not it is reflected well is

pyenv --version

If the version information is displayed, it is OK.

Note that you only need to execute source ~ / .profile once (from the next time onward, it will be executed automatically when you launch Raspberry Pi and log in).

Install python 3.7.6

I'm using pyenv to install python 3.7.6, but the build-time options for python are important, so I'll configure that. Specifically, you can set the following contents in the environment variable PYTHON_CONFIGURE_OPTS, but in case of failure, write it to the file once just in case. For now, let's write the following to a file called setup.sh:

setup.sh


export PYTHON_CONFIGURE_OPTS="--enable-ipv6\
--enable-unicode=ucs4\
--enable-shared\
--with-dbmliborder=bdb:gdbm\
--with-system-expat\
--with-system-ffi\
--with-fpectl"

If you can write to the file,

source setup.sh

As, update the environment variables. To check if the environment variables have been updated correctly

echo $PYTHON_CONFIGURE_OPTS

As a result, it is OK if the contents described in setup.sh are displayed together on one line.

If you want to proceed with the following steps as it is, it is enough to perform the above work once. However, if you finish the work here and continue tomorrow, etc., please turn on the power again and then try again from source setup.sh (because the information of the set environment variables will be lost when the power is turned off). is).

Creating an experimental directory

Use pyenv to create a python 3.7.6 environment only under a certain directory. Here, create a directory called test, and make the environment python 3.7.6 only in that directory.

mkdir test
cd test

Installation of Python 3.7.6

Run python in the experimental test directory so that python 3.7.6 runs. First, install 3.7.6 using pyenv, and then configure the python to use in this directory.

Install python 3.7.6 with the following command (it will take some time):

pyenv install 3.7.6

If you can install it successfully,

pyenv versions

As a result, if the message 3.7.6 appears, it is successful.

Change Python in the experimental directory to 3.7.6

The default python installed on the Raspberry Pi (or rather, on the Raspbian I just installed) is 2.7.16. There are 2 and 3 versions of Python, so make sure to use ver. 3.7.6 in the experimental directory and 2.7.16 (the system standard version) elsewhere.

The point is that the test directory is set to use 3.7.6, which is

pyenv local 3.7.6

Execute the command to complete.

If you try python -V in the test directory, you'll see the string 3.7.6 (if you don't see it, the setup has failed). As a test, if you move to another directory such as your home directory and use python -V, you will see 2.7.16.

pip upgrade

pip is a python package manager that you will use to install various modules that you will use later. If you install Python with pyenv, pip may also revert to the old one, so just in case, run the following command to keep pip up to date. To do this, run a pair of commands:

pip install --upgrade pip

Install numpy, scipy, pyaudio

Now you are ready to install the various modules! I'd like to say that, but there is one more module that needs to be installed, so I'll install it. After this step, we will use pip to install the module:

pip install pybind11

After the installation is completed successfully

pip install numpy
pip install scipy
pip install pyaudio

As the goal of this article, install each package. If it succeeds, start python as a test and

import numpy
import scipy
import pyaudio

And make sure that each module can be imported without any problem.

in conclusion

This document has shown you how to install numpy and scipy, which can be a bit tedious to install. I also showed how to install pyaudio and prepared an environment where real-time sound processing can be developed with Raspberry Pi.

Next, I would like to make a real-time pitch shifter with Raspberry Pi using this environment. Since it is software that changes the pitch in real time, when you input music etc., the pitch will be shifted and played, and the music will be very deaf. Stay tuned.

Recommended Posts

Media programming with Raspberry Pi (preparation for audio)
Try fishing for smelt with Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Mutter plants with Raspberry Pi
Update Python for Raspberry Pi to 3.7 or later with pyenv
Check! Get sensor data via Bluetooth with Raspberry Pi ~ Preparation
I made a resource monitor for Raspberry Pi with a spreadsheet
[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Raspberry pi initial setting (for myself)
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi
Programming for humans with a well-defined __repr__
Measure SIM signal strength with Raspberry Pi
Ask for Pi with the bc command
Hello World with Raspberry Pi + Minecraft Pi Edition
Build a Tensorflow environment with Raspberry Pi [2020]
Get BITCOIN LTP information with Raspberry PI
Improved motion sensor made with Raspberry Pi
Try Object detection with Raspberry Pi 4 + Coral
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Infer Custom Vision model with Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
Preparation for scraping with python [Chocolate flavor]
Inkbird IBS-TH1 value logged with Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I tried to create a button for Slack with Raspberry Pi + Tact Switch
Discord bot with python raspberry pi zero with [Notes]
I tried L-Chika with Raspberry Pi 4 (Python edition)
Enjoy electronic work with GPIO on Raspberry Pi
MQTT RC car with Arduino and Raspberry Pi
Power on / off your PC with raspberry pi
Use Majoca Iris elongated LCD with Raspberry Pi
CSV output of pulse data with Raspberry Pi (CSV output)
Observe the Geminids meteor shower with Raspberry Pi 4
Get CPU information of Raspberry Pi with Python
Beginning cross-compilation for Raspberry Pi Zero on Ubuntu
Play with your Ubuntu desktop on your Raspberry Pi 4
Get temperature and humidity with DHT11 and Raspberry Pi
Stock investment analysis app made with Raspberry Pi
Logging Inkbird IBS-TH1 mini values with Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Measure CPU temperature of Raspberry Pi with Python
Raspberry Pi backup
File sharing server made with Raspberry Pi that can be used for remote work
Record temperature and humidity with systemd on Raspberry Pi
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot