Difference between revisions of "ECE434 Project - Bluetooth Speaker"

From eLinux.org
Jump to: navigation, search
Line 181: Line 181:
We've included a video demoing our [https://youtu.be/UzPf0LYgQ5s/ visualizer] in action. This visualizer can handle a variety of sampling rates without being underrun, syncs to the music within a reasonable margin of error (a user-adjustable margin of error, at that), and goes dark whenever the user presses CTRL+C to quit playback.  
We've included a video demoing our [https://youtu.be/UzPf0LYgQ5s/ visualizer] in action. This visualizer can handle a variety of sampling rates without being underrun, syncs to the music within a reasonable margin of error (a user-adjustable margin of error, at that), and goes dark whenever the user presses CTRL+C to quit playback.  
We've also included our [https://www.hackster.io/james-werne/led-matrix-audio-visualizer-0975da/ hackster.io] page -- see the link below to follow the project as we (or other programmers) release updates.

Revision as of 18:01, 17 November 2020

thumb‎ Embedded Linux Class by Mark A. Yoder

Team members: James Werne Neil Roy

Grading Template

I'm using the following template to grade. Each slot is 10 points. 0 = Missing, 5=OK, 10=Wow!

00 Executive Summary
00 Packaging
00 Installation Instructions 
00 User Instructions
00 Highlights
00 Theory of Operation
00 Work Breakdown
00 Future Work/Conclusions
00 Hackster.io
00 Demo/Poster
00 Late
Comments: Not yet graded.

Score:  00/100

(Inline Comment)

Executive Summary


This project will use a BeagleBone Green Wireless to connect to a Bluetooth speaker. The BeagleBone will then take music (either .mp3s or .wavs) on the computer and send the music to the speaker. There is also a program included that lights up a WS2812b programmable LED strip to respond to the music.

We currently have a BeagleBone Green Wireless and a Bluetooth speaker. We've been able to connect to the Bluetooth Speaker and play .mp3s (using mplayer) and .wavs (using aplay). We've written code to have an LED matrix visually respond to the music being played.

One issue that we consistently run into is with getting the LEDs to start at the same time as the music. We've also only been able to apply the visualizer to .wavs; all other audio formats have to be converted to .wav beforehand.

Overall, we accomplished all of our original goals for the project such as playing music from the Bone over Bluetooth and creating an LED visualizer, however there is still room for improvement and many more things we wish we could do if we had more time.


This project uses a Beaglebone Green Wireless, a WS2812b programmable LED strip, and a Bluetooth speaker (any will work), and any cables required to interface the Beaglebone with a linux device. We recommend using a current-limiting resistor (220Ω works well) when connecting the WS2812b to the GPIO ports on the Beaglebone. You can find the WS2812b in the following link:


There are no additional hardware requirements, but feel free to design an enclosure and cover the outside with programmable LEDs.

Installation Instructions

To install the project, ssh into your Beaglebone and follow the instructions below:

1) Make sure you're connected to the internet. If you want to use wifi, we recommend following the instructions provided on the following site:


2) Ensure you have git installed on your device, and fork the repo linked to the following read-only git site:


In particular, you can run the following command:

bone$ git clone https://github.com/wernejm/ECE434_Project

3) Navigate to the project directory. Run the following to install pexpect, which is a necessary package for the visualizer:

bone$ ./install.sh

Note: read through install.sh and see if you need to install any additional packages. There are several included in the install.sh file that have been commented out. These are used in an mp3-to-wav conversion program that we've included in this project. If your audio files are all already in the .wav format, there is no need to edit the install file.

Once you've done the above three steps, you can proceed to the user instructions to learn how to play music and/or run the audio visualizer code.

User Instructions

We've outlined the key functions of our project below, as well as instructions on how to use each of them.

1: Ensure you've installed all of the necessary packages (see installation instructions above).

2: The next step is to connect to your bluetooth speaker. It's important to make sure that the Bluetooth Daemon is running on your Beaglebone, so be sure to run "sudo systemctl start bluetooth" if you haven't already.

First, run the following commands to turn on the Bluetooth Agent and to enter scan mode:

[bluetooth]# agent on
Agent is already registered
[bluetooth]# scan on
Discovery started

A list of MAC addresses will be continuously printed to the terminal. Once you see your device, run:

[bluetooth]# scan off 
Discovery stopped

Now take note of the MAC address associated with your device. Once you've done this, you'll want to pair with the device, then trust it. Lastly, you'll connect to the device. To do these three things, run the following:

[bluetooth]# pair [MAC address]
[bluetooth]# trust [MAC address]
[bluetooth]# connect [MAC address]
Attempting to connect to [MAC address]
[CHG] Device [MAC address] Connected: yes
Connection successful
[CHG] Device [MAC address] ServicesResolved: yes

Your speaker should now be connected.

3: Next, we will configure the asound configuration for the alsa device. If you don't have any preferred audio settings configured for your BeagleBone, simply run the following:

bone$ cd ECE434_Project
bone$ nano asound.conf

Using the same MAC address as above, go through and replace the [ENTER MAC ADDRESS HERE] prompts with your speaker's MAC address. Additionally, you may replace the [ENTER DEVICE NAME HERE] prompt with the name of your speaker. Once you've done these, save and exit the editor, then run the following:

bone$ cp asound.conf /etc 

Note that if you do not wish to alter your asound.conf file, then use our code as a reference and adjust your config file as necessary.

4: If you only want to play audio, skip to step 8. If you want to use the visualizer, follow along with the next few steps.

Modify Rpmsg.pru0.c as necessary. If you're using the WS2812b, change the following to configure the GPIOs from the default Pin 30 to whichever pins you wish to use:

#define CHAN_NAME                       "rpmsg-pru"
#define CHAN_DESC                       "Channel 30"
#define CHAN_PORT                       30

Our LED matrix is a 10x10 grid. If you have a different number of LEDs (note that you will have to make other serious modifications to the visualizer code if this is the case), you can modify the string length by changing the following line:

#define STR_LEN 100 

From here, your code should be ready to run.

5: Compile the program. Run the following:

bone$ ./setup.sh

This will configure the correct GPIOs to use the PRU and prepare the LED to read values.

6: Select an audio file you'd like to play. First, convert the file to a .wav. We wrote a script to convert mp3s to wavs, which can be run using the following command:

bone$ ./mp3towav.py

Follow the prompts to convert the file. Remember the name of your output file.

7: Now open visualizer.py. Define "MUSIC" as the name of the .wav audio file you wish to play:

MUSIC = 'mario.wav'

Save and close this file.

8: If you only with to play the audio (without the visualizer), select an .mp3 or a .wav file and run the following:

bone$ ./playmusic.sh <audio file name> 

If you wish to use the visualizer, run the following instead:

bone$ ./visualizer.py

Now sit back and enjoy.


We've included a video demoing our visualizer in action. This visualizer can handle a variety of sampling rates without being underrun, syncs to the music within a reasonable margin of error (a user-adjustable margin of error, at that), and goes dark whenever the user presses CTRL+C to quit playback.

We've also included our hackster.io page -- see the link below to follow the project as we (or other programmers) release updates.

Theory of Operation

The first thing that our code does is read in the music file, and takes the left channel and converts it to a 1-D array. Then, it checks the sampling rate of the file. If it is too large (>24000), then it is downsampled by using every other value in the array.

Next, a subprocess is created to play the music on the bluetooth speaker. Simultaneously, the code is calculating the FFT of the subset of music being played at that time, and converting it to a format that can be displayed on the LEDs, so that the music and visualization is synchronized. The synchronization was done using the length in time of the music file, calculated from the samping rate, and the number of times we iterate through the loop, to calculate the delay time in each loop iteration of the FFT portion of the code to make sure that the FFT was not going faster than the audio was being played. This is why the original input array was downsampled if the FFT calculations were going to take too long.


Work Breakdown

Both partners worked together on most of the major tasks. We both worked on getting the BeagleBone to connect to the Bluetooth speaker to play music. James primarily handled mplayer, while Neil focused on aplay. We worked together to display patterns on the programmable LED. We also worked together on applying the FFT to our music, which ultimately led to us being able to develop a real-time equalizer pattern. The project involved a considerable amount of pair programming, so the technical work was shared fairly. In general, though, Neil focused more on the technical work, while James focused a little more on updating the eLinux and hackster.io pages.

Future Work

We were interested in taking this project in several different directions. Below are a few of our suggestions:

  • Increasing the number of audio file formats that properly work with our playmusic.sh function
  • Work to interface the program with popular streaming services, like Spotify, Youtube Music, and so on
  • Make use of the BeagleBone's wireless capabilities and find ways to select/play music on the Bone remotely, perhaps using Flask or other related software
  • Changing the way the code runs so that the user is able to specify the audio file when running the command rather than changing the code every time


Our initial goals for this project was just to be able to play music over Bluetooth to a speaker and be able to make an LED strip or matrix react to the music in real-time somehow. We did successfully meet our goals, but there is still some room for improvement.

Ease of use can definitely be improved a little bit in terms of selecting which audio file you want to select. Another thing that could be added is getting the code to work with MP3 files as well. If we were to expand our project some more, we could probably automate some of the setup such as connecting to the speaker via Bluetooth and we could also try to incorporate audio streaming services such as Spotify, Apple Music, etc. Overall, our we successfully accomplished everything that we had originally set out to do for our project.

thumb‎ Embedded Linux Class by Mark A. Yoder