Audio Reactive Holiday LEDs (no mic needed)

Published on: December 18, 2020

Welcome to the 20th Raspberry Pi tutorial! This is an update to the Audio Reactive LEDs video from July

One of the most common questions I've had is, can we play music directly on the Pi without a Mic? My answer today is, YES we can! This update brings exactly that.

Although there are some kinks that I'm still working through, the application works as expected. And because there are still a few quirks I'm working through, this has been placed in the no_mic branch of the repository. I will eventually complete it and merge it into master and allow you to switch between mic and no mic. 

Be sure to get the code and more technical specs of this project on GitHub: https://github.com/naztronaut/dancyPi-audio-reactive-led/tree/no_mic

Here's a quick demo of what the end product looks like:

The above demo has sound. Watch the video below for the full demo!

Hardware Setup

Let's talk hardware first. Here is what I used for my project:

Name Notes
Raspberry Pi 2/3/4 You'll see performance issues with the Pi 0 if you use that
WS281x LED can be a strip or in any basic series. You should know the number of LEDs in the series. These lights typically have 3 connections: Power, Ground, and Data
Bluetooth or USB Speaker this will NOT work with a 3.5mm jack speaker. With a 3.5mm speaker, you can either play music, or have the lights dance, not both.
Jumper cables to connect the Pi to the lights
Power  this can be supplied to the Pi and the Pi supplies power to the lights, or you can use two different power sources for the two components
Raspberry Pi Full OS You should install Raspberry Pi Full OS and not the lite version
VNC/GUI Access We'll need to access the GUI of the pi to run the lights. You can either use VNC or just KVM into the Pi


Here's a quick fritz diagram of what the connection looks like:

By default, the project uses GPIO Pin 18 as the data pin with 5v and GND pins being any of the ones provided by the Pi. Find a quick the wiring below (Note: I'm using a Pi0 as the hardware even though I'm using a Pi 4 for the project - just reusing some graphics from another project!):

Fritz Diagram Raspberry Pi connecting to WS2812b LED Strip
Click image to see full resolution

Make sure you install the full Raspberry Pi OS and enable VNC Server if you're not going to access your Pi using KVM. 

Once the hardware is done, we can move onto the Software.

Software Setup

If you did the full RPi OS install, Git should already be installed, but just in case, let's get git, clone the repo, go into our no_mic branch, and run the installer:

    sudo apt install git -y
sudo git clone https://github.com/naztronaut/dancyPi-audio-reactive-led.git
sudo git checkout no_mic
cd dancyPi-audio-reactive-led/python/install

Once you're in the install/ directory, you'll see a few files. The one we're intersted in is install.py. Running this script should install ALL the dependencies that you'll need for this project. These dependencies include numpyscipypyqtgraphpyaudio, and the rpi_ws281x library along with a few others. It also installs pydub to play audio using python. This script will also copy asound.conf and set up vitual audio devices.

So let's run the script with this command:

    sudo python3 install.py

The setup can take anywhere from a few seconds to a few minutes, depending on how many of the dependencies you may already have installed. This script is pretty simple in that it just does a bunch of ssh commands in order for you so that you can run it, walk away, and it'll be done by the time you come back.

Once the script is done running, reboot your pi:

    sudo reboot

After things are rebooted, connect your Bluetooth or USB Speaker (Bluetooth can be connected either through the GUI or through Command line - USB Speaker should be plug n play). But moving forward, switch to the GUI in either VNC viewer OR just use a regular keyboard, monitor, mouse and connect. 

Config.py

Let's take a look at config.py next:

    DEVICE = 'pi'
    LED_PIN = 18
    USE_GUI = False
    DISPLAY_FPS = False
    N_PIXELS = 144
    MIC_RATE = 44100
    FPS = 50

The MIC_RATE for this branch uses 44100 Hz because I found that to be the best one. The Mic version uses 48000Hz.  Although the N_PIXELS variable is not as important, it's still good to get that number correct, because it'll determine where the center is and start the 'dancing' there. The value MUST be divisible by 2.

Running the Software

If everything installed as expected without errors, you should be able to run the project with this simple command:

    sudo python3 visualization.py scroll

Remember to do the above in a shell window in the GUI. Substitute the scroll argument above with any of the following:

  • spectrum
  • scroll
  • energy
  • scroll_in
  • scroll_quad

The last two visualizations are ones that I added as I'm learning numpy. If you have DISPLAY_FPS as True, you should see an FPS value every second or so.

The virtual mic will pick up any and all audio played in the Pi. So it can be done from VLC, a python script, or even if you open YouTube on a browser or stream through Spotify!

If you run into errors, ask in the comments section below or on the GitHub issues forum here: https://github.com/naztronaut/dancyPi-audio-reactive-led/issues

If you want to play music via python, check out the Playing Audio section of the GitHub repo: https://github.com/naztronaut/dancyPi-audio-reactive-led/tree/no_mic#playing-audio

Patreon

I'd like to give a shoutout to my first Patron on Patreon, Jeremy D! I do these projects to learn and teaching is a good way to learn. But if you do want to support me, head on over to Patreon and become a Patron: https://www.patreon.com/nazmus

If you've implemented this project, I'd like to see it! So please share it with me through any of my regular channels. 

Future TODOs

I need to figure out how to make this whole thing headless so that it can be plug and play. I also want to work on having a way to stream from other devices on your network. And lastly, merge this into master and have a way to switch between the two (mic vs no mic) seamlessly. 

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!



Comments: