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.
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
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).
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).
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
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.
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 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
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.
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