Skip to content

Raspberry Pi application that detects music with ML, identifies it using Shazam, and shows the song information on an e-ink display

License

Notifications You must be signed in to change notification settings

maurocastermans/now-playing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

25 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽถ Now-playing

Now-playing is a Python application for the Raspberry Pi that listens for background music, identifies the song, and displays the song information on an e-ink display.

This project was born out of a personal need: I love listening to vinyl, but since it's analog, I could never see what track was playing. The same goes for filmsโ€”I'd often reach for my phone to Shazam a song. Now, I just glance at my display.

Example Image 1 Example Image 2

๐Ÿš€ Features

  • Detects music using a local YAMNet ML model
  • When music is detected, identifies the song with ShazamIO
  • Displays song title, artist, and album cover on an e-ink display
  • Pressing Button A on the e-ink display adds the current song to a Spotify playlist with Spotipy
  • When no music is detected for a while, the display switches to a screensaver mode that shows the weather

โœจ What's New?

This project builds on and refactors several previous works (see LICENSE):

All credits for the original idea go to them. While they laid the groundwork, this version focuses on clean code, modularity, and extensibility.

โ™ป๏ธ Improvements

  • Simplified, readable logic with meaningful function names
  • Clear separation of concerns with dedicated services (e.g., DisplayService has all logic concerning the e-ink display)
  • Application state is handled via a centralized StateManager
  • Type hints added for better clarity and IDE support
  • Configurations via YAML (no more messy INI files)
  • Cleaned up setup script for smoother installation
  • Singleton pattern for Logging and Config
  • Threaded button control for responsiveness
  • Many more...

๐Ÿ“ฆ Installation & Setup

๐Ÿ”ง Required Hardware

๐Ÿฅง Raspberry Pi OS

  1. Flash Raspberry Pi OS Lite to your microSD card using Raspberry Pi Imager
  2. Enable Wi-Fi and SSH (to allow remote access as the OS is headless) in the setup wizard

๐Ÿ” Required Credentials

๐ŸŒฆ๏ธ OpenWeatherMap API

  1. Sign up at OpenWeatherMap
  2. Generate your API key
  3. Safely store it
  4. Go to Google Maps โ†’ Search your location โ†’ Right-click โ†’ Copy coordinates

๐ŸŽต Spotify API

  1. Go to Spotify Developer Dashboard
  2. Click 'Create App' and fill out the form:
    1. App name
    2. App description
    3. Redirect URI = http://localhost:8888/callback
    4. Check 'Web API'
    5. Check the 'Terms of Service'
  3. Click on 'Save'
  4. Safely store your Client ID and Client Secret
  5. Copy the Playlist ID of the playlist you want your songs to be added to

๐ŸŽŸ Spotify Access Token

Since Raspberry Pi OS Lite is headless (no browser), you must authorize Spotify once from a computer:

  1. On your PC or Mac, clone this repo:
  git clone https://github.com/maurocastermans/now-playing
  cd now-playing
  1. Fill in your SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET in spotify_auth_helper.py
  2. Run:
  python3 spotify_auth_helper.py
  1. Follow the browser prompt and allow access to your Spotify account. This will generate a .cache file locally containing a Spotify access token.

โš™๏ธ Installation Script

๐Ÿ“ฅ Download and Run

  wget https://raw.githubusercontent.com/maurocastermans/now-playing/main/setup.sh
  chmod +x setup.sh
  bash ./setup.sh

Afterwards, copy the .cache file to your Raspberry Pi project root. Spotipy will refresh the token automatically using the stored refresh tokenโ€”no need to do this again unless you change accounts.

Should you encounter any issues, check Known Issues

๐Ÿง™ What the Script Does

  • Enables SPI and I2C
  • Updates the system and installs dependencies
  • Sets up a Python virtual environment and installs Python packages
  • Creates config, log, and resources directories
  • Prompts for credentials, your e-ink display size and generates config.yaml
  • Copies and configures a systemd service to autostart on boot
  • Starts the now-playing service

๐Ÿ“‚ Resulting Config

display:
  width: 600 # or 640 (4"), or 800 (7.3")
  height: 448 # or 400 (4"), or 480 (7.3")
  small_album_cover: true
  small_album_cover_px: 250 # or 200 (4"), or 300 (7.3")
  screensaver_image: "resources/default.jpg"
  font_path: "resources/CircularStd-Bold.otf"
  font_size_title: 45
  font_size_subtitle: 35
  offset_left_px: 20
  offset_right_px: 20
  offset_top_px: 0
  offset_bottom_px: 20
  offset_text_shadow_px: 4

weather:
  openweathermap_api_key: "YOUR_API_KEY"
  geo_coordinates: "LAT,LON"

spotify:
  client_id: "YOUR_SPOTIFY_CLIENT_ID"
  client_secret: "YOUR_SPOTIFY_CLIENT_SECRET"
  playlist_id: "YOUR_SPOTIFY_PLAYLIST_ID"

log:
  log_file_path: "log/now_playing.log"

๐Ÿ›  Useful Commands

๐Ÿ“ Edit Configuration

To update your configuration after installation:

  nano config/config.yaml

After editing, restart the service to apply changes:

  sudo systemctl restart now-playing.service

๐Ÿ” Systemd Service

  • Check status:
  sudo systemctl status now-playing.service
  • Start/Stop:
  sudo systemctl stop now-playing.service
  sudo systemctl start now-playing.service
  • Logs:
  journalctl -u now-playing.service
  journalctl -u now-playing.service --follow
  journalctl -u now-playing.service --since today
  journalctl -u now-playing.service -b

๐Ÿงช Manual Python Execution

Now-playing runs in a Python virtual environment (using venv). If you want to run Python code manually:

  sudo systemctl stop now-playing.service
  source venv/bin/activate
  python3 src/now_playing.py

To leave the virtual environment:

  deactivate

๐Ÿ› Known Issues

Low USB Microphone Gain

Some USB microphones have very low default input gain, meaning they only pick up sound when your audio device is extremely close to the mic. This can cause issues with audio detection.

To boost your microphoneโ€™s gain:

  1. Open the audio mixer:
    alsamixer
  1. Select your USB microphone:
    1. Press F6 to open the sound card list
    2. Use the arrow keys to select your USB microphone device
  2. Adjust the input gain:
    1. Press F4 to switch to Capture controls
    2. Increase the gain using the โ†‘ arrow key until it reaches an appropriate level
  3. Save the gain settings (so they persist after reboot):
  sudo alsactl store

GPIO Chip Conflict

If you see:

Woah there, some pins we need are in use!
     Chip Select: (line 8, GPIO8) currently claimed by spi0 CS0

Just recently (16/08/2024), the GPIO Kernel Module in Raspberry PI OS changed

โžก๏ธ Check https://github.com/pimoroni/inky?tab=readme-ov-file#chip-select-line-8-gpio8-currently-claimed-by-spi0-cs0 and follow the instructions

๐Ÿ”ฎ What's Next?

More Button Actions

There are still three unused buttons on the e-ink display. These could be mapped to additional features.

HTML Rendering

The Pimoroni Inky display actually supports rendering HTML, opening up all sorts of design possibilities. This could make the interface:

  • More customizable and visually rich
  • Easier to tweak via CSS/HTML templates
  • Support dynamic layouts or themes

If you have more ideas for new features or you'd like to get involved, feel free to open an issue or submit a PR!

About

Raspberry Pi application that detects music with ML, identifies it using Shazam, and shows the song information on an e-ink display

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published