dmfaber1 / TunerPi

Raspberry Pi Guitar Tuner
1 stars 0 forks source link

Test PyAudio #4

Open dmfaber1 opened 3 years ago

dmfaber1 commented 3 years ago

PyAudio seems like one of the best tools to analyze an audio stream using Python: https://pypi.org/project/PyAudio/

This is just a python interface to PortAudio, which needs to be installed to actually use PyAudio: http://www.portaudio.com/

PortAudio is cross platform, so it is a good audio stack to utilize, as we can test on Windows, and deploy to RaspberryPi (Linux). Installing PortAudio on Linux is pretty straightforward (http://files.portaudio.com/docs/v19-doxydocs/compile_linux.html):

sudo apt-get install libasound-dev
./configure && make
sudo make install

On Windows (http://files.portaudio.com/docs/v19-doxydocs/compile_windows_mingw.html), it is a bit more complicated, because you will need to install a compiler (http://mingw-w64.org/doku.php). Or we can just try using the WSL again.

Once PortAudio is installed, PyAudio can be installed: pip install PyAudio

Example of a simple Spectrum Analyzer using PyAudio: https://gist.github.com/netom/8221b3588158021704d5891a4f9c0edd

faberke611 commented 3 years ago

Do you know which PyAudio I should download?

faberke611 commented 3 years ago

Trying Python. Opens in: "The prompt >>> on the last line indicates that you are now in an interactive Python interpeter session, also called the “Python shell”. This tutorial are intended to be run in a shell (also called a terminal or console)."

How do I run in shell (also called a terminal or console)? Ctrl-Z or exit() just closes the window.

dmfaber1 commented 3 years ago

Do you know which PyAudio I should download?

So now that you have the Ubuntu WSL working, this opens up more possibilities. The preferred way would be to download the Linux one, and use the Ubuntu WSL to build and run it. But I am not sure that Windows shares the Audio Devices with the Ubuntu WSL. If it doesn't, than the Linux one doesn't really help us and we will have to use the Windows one, which requires the more complicated mingw compilation. Let me try this one out first so I know what would work better.

Trying Python. Opens in: "The prompt >>> on the last line indicates that you are now in an interactive Python interpeter session, also called the “Python shell”. This tutorial are intended to be run in a shell (also called a terminal or console)." How do I run in shell (also called a terminal or console)? Ctrl-Z or exit() just closes the window.

So if you create a .py file with python code inside it, you can just call python that_file.py to execute it in the shell. Just opening up powershell should let you do this, I believe the Windows Python installation set up all the links for this to work.

I will also create a new issue for helpful stuff for learning Python, specifically using VSCode to make modifying and running python code super easy.

dmfaber1 commented 3 years ago

So to go through the steps we did to test out the portaudio on your laptop, but on your raspberry pi:

  1. Open SSH connection to raspberry pi. If you haven't done so already you will have to update the /boot files on the sd card for your home wifi.
  2. Download portaudio. cd ~/Downloads wget http://files.portaudio.com/archives/pa_stable_candidate_v190700_20210307.tgz If you don't have wget installed, just install it first sudo apt update && sudo apt install wget
  3. Extract the portaudio download file tar -xzf pa_stable_candidate_v190700_20210307.tgz
  4. Install tools needed to compile portaudio sudo apt update sudo apt install libasound-dev sudo apt install build-essential
  5. Compile portaudio cd portaudio ./configure && make
  6. Install the portaudio libraries to raspberry pi sudo make install
  7. Install Python3 and PyAudio sudo apt install python3 sudo apt install pip3 pip3 install PyAudio
  8. Test out PyAudio. This won't work yet, but I'll work on creating some test cases for PyAudio. I'll check them into the git repository, then you should be able to scp them to the raspberry pi and try it out. python3 testPyAudio.py sample.wav
faberke611 commented 3 years ago

Executed #1 to 7. Everything went smoothly

dmfaber1 commented 3 years ago

I pushed some code - mostly just copy pasted examples. So if you copy test_pyaudio.py and Ring01.wav to your raspberry pi, you should be able to run python3 test_pyaudio.py Ring01.wav. So this should play the ring noise if you have a sound device connected. But really the first thing is just to see what happens when run, ie does it run with no errors?

Do you have a usb headset/mic? That would be the next interesting thing, if it uses the default audio device correctly or if we need to tweak some code when opening the audio stream.

The other thing I pushed was some guys code for doing just what we are trying https://www.instructables.com/Raspberry-Pi-Guitar-Tuner/. I cleaned it up a bit so it hopefully can be used. Would be interesting to see what happens.