Song Recognition Device

by xharrison991 in Circuits > Raspberry Pi

5 Views, 0 Favorites, 0 Comments

Song Recognition Device

WhatsApp Image 2025-04-28 at 2.34.27 AM (2).jpeg

This song recognition device was created for the course Intro to Mechatronics at the University of Kansas. This device works similarly to Shazam, where a played song is recorded and compared to a universal music database to try and identify the song playing. When this device detects a song, it states its name, artist, and the album it comes from.

Supplies

  1. Raspberry Pi 4 with at least 4 GB of RAM (Or any other wifi compatible Pi with sufficient RAM)
  2. USB Microphone
  3. USB Speaker
  4. USB Keyboard
  5. USB Mouse (Setup Only)
  6. Compatible HDMI cable (Setup only)
  7. Monitor (Setup only)
  8. Raspberry Pi power cable
  9. 32 GB MicroSD
  10. Raspberry Pi case (optional)
  11. Internet access (vital)

Construction

The construction of the device is straightforward. The Raspberry Pi will have its USB ports plugged into the speaker, microphone, keyboard, and mouse. The MicroSD is inserted into the Pi. The Raspberry Pi is plugged into the wall for power. During setup, the Pi should be connected to a monitor to make the process much easier. Once setup is completed, the USB mouse and monitor can be disconnected.

Setup

pi.png

1) Once the Raspberry Pi is powered and connected to a monitor, it can then connect to Wifi. Once the Pi has connected to the internet, it will download its operating system to the microSD if it is not already there.


2) Once the operating system has been downloaded, open the coding terminal on the desktop. Type in the following code to install the necessary libraries needed for the device (including text-to-speech, audio recording, and keyboard access).

-(System Packages)

sudo apt update

sudo apt install espeak alsa-utils python3-pip

-(Python Libraries)

pip3 install requests keyboard --break-system-packages


3) To have access to a music database, an account for the website https://audd.io/ needs to be created. This website will give access to an API token necessary for the device to function.


4) Run the following code to detect what ports the speaker and microphone are connected to. These numbers will be used for the final code.

arecord -l

aplay -l

Code

1) Create a script for the code by typing the following into the terminal.

nano /home/yourusername/recognize_song.py


2) Copy and paste the following code into the now-opened script.

import os # Allows running shell commands (for espeak and aplay)

import requests # Used to send audio to Audd.io API

import keyboard # Detects keyboard input (spacebar press)

import time # Used for sleep/delays


# === CONFIGURATION ===


API_TOKEN = "YOUR_API_KEY_HERE" # Replace this with your actual Audd.io API token


MIC_CARD = 3 # Replace with your actual mic card number (from `arecord -l`)

USB_SPEAKER_CARD = 2 # Replace with your speaker card number (from `aplay -l`)

RECORD_SECONDS = 8 # How long to record audio each time


# === FUNCTION: Recognize a song using the Audd.io API ===


def recognize_song(api_token, file_name):

url = "https://api.audd.io/" # Audd.io API endpoint

data = {

'api_token': api_token, # Your API key

'return': 'apple_music,spotify' # Ask API to return Spotify & Apple Music info

}

files = {

'file': open(file_name, 'rb'), # Audio file to upload

}


print("🎶 Uploading file...")

response = requests.post(url, data=data, files=files)

return response.json() # Parse the result into a Python dictionary


# === MAIN LOOP ===


while True:

# Prompt user with voice to press the spacebar

print("📻 Waiting for SPACEBAR to record...")

os.system(

f'espeak -a 200 -w temp.wav "Press the spacebar when you are ready to record." '

f'&& aplay -D plughw:{USB_SPEAKER_CARD},0 temp.wav'

)


# Wait for user to press the spacebar key

keyboard.wait('space')


# Record audio from the USB microphone

print(f"🎙️ Recording {RECORD_SECONDS} seconds...")

os.system(

f'arecord -D plughw:{MIC_CARD},0 -f cd -t wav -d {RECORD_SECONDS} music_sample.wav'

)


# Send the recorded audio to Audd.io for recognition

result = recognize_song(API_TOKEN, "music_sample.wav")


# === Check if a song was successfully recognized ===

if result['status'] == 'success' and result['result']:

song = result['result'] # Extract song details

title = song['title']

artist = song['artist']

album = song.get('album', 'Unknown') # Use "Unknown" if album is missing


# Print song info to terminal

print(f"Title: {title}\nArtist: {artist}\nAlbum: {album}")


# Speak the result out loud through USB speaker

os.system(

f'espeak -a 200 -w temp.wav "This song is {title} by {artist} from the album {album}" '

f'&& aplay -D plughw:{USB_SPEAKER_CARD},0 temp.wav'

)


else:

# If no match found, speak an error message

os.system(

f'espeak -a 200 -w temp.wav "Sorry, I could not recognize the song." '

f'&& aplay -D plughw:{USB_SPEAKER_CARD},0 temp.wav'

)


# Small pause before starting next loop iteration

time.sleep(1)


3) Enable autologin to the terminal once the Raspberry Pi is turned on. This eliminates the need for a monitor once setup is complete.

sudo raspi-config


4) Type the following commands into the terminal to guarantee the code runs on Pi startup.

nano /home/yourusername/.bashrc

sudo python3 /home/yourusername/recognize_song.py

sudo reboot




Use

Video Demonstration: https://photos.app.goo.gl/Kr7a3t6svFJRCyAj9

1) Plug in the Raspberry Pi.

2) The speaker will announce that it is awaiting the user to press the space key on the keyboard to begin recording a song.

3) Once space is pressed, the microphone records for 8 seconds. After 8 seconds, the recorded music is sent and compared with a musical database. If a song is detected, the speaker will announce its name, artist, and album. If a song is not detected, the speaker will say no song was found.

4) The device resets and once again asks for the spacebar to be pressed for coding to begin.