Introduction
Build your own music player/visualiser with the Raspberry Pi and Sense HAT.
First we will show you how to do so using Scratch 3.0, then in the second part of this guide, we will show you how to create it with more advanced programming using Python.
Complete this guide to turn your Raspberry Pi 4 + Sense HAT into a music player.
First we will show you how to do so using Scratch 3.0, then in the second part of this guide, we will show you how to create it with more advanced programming using Python.
Complete this guide to turn your Raspberry Pi 4 + Sense HAT into a music player.
Tools
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
import time import os import pygame from sense_hat import SenseHat import glob
-
import time import os import pygame from sense_hat import SenseHat import glob # Initialise pygame mixer pygame.mixer.init()
-
import time import os import pygame from sense_hat import SenseHat import glob # Initialise pygame mixer pygame.mixer.init() # Create SenseHat object sense = SenseHat()
-
import time import os import pygame from sense_hat import SenseHat import glob # Initialise pygame mixer pygame.mixer.init() # Create SenseHat object sense = SenseHat() R = [255, 0, 0] O = [255, 165, 0] Y = [255, 255, 0] G = [0, 128, 0] B = [0, 0, 255] I = [75, 0, 130] V = [139, 0, 255] N = [0, 0, 0]
-
import time import os import pygame from sense_hat import SenseHat import glob # Initialise pygame mixer pygame.mixer.init() # Create SenseHat object sense = SenseHat() R = [255, 0, 0] O = [255, 165, 0] Y = [255, 255, 0] G = [0, 128, 0] B = [0, 0, 255] I = [75, 0, 130] V = [139, 0, 255] N = [0, 0, 0] rainbow = [ R, R, R, R, R, R, R, R, O, O, O, O, O, O, O, O, Y, Y, Y, Y, Y, Y, Y, Y, G, G, G, G, G, G, G, G, B, B, B, B, B, B, B, B, I, I, I, I, I, I, I, I, V, V, V, V, V, V, V, V, N, N, N, N, N, N, N, N ]
-
-
import time import os import pygame from sense_hat import SenseHat import glob # Initialise pygame mixer pygame.mixer.init() # Create SenseHat object sense = SenseHat() R = [255, 0, 0] O = [255, 165, 0] Y = [255, 255, 0] G = [0, 128, 0] B = [0, 0, 255] I = [75, 0, 130] V = [139, 0, 255] N = [0, 0, 0] rainbow = [ R, R, R, R, R, R, R, R, O, O, O, O, O, O, O, O, Y, Y, Y, Y, Y, Y, Y, Y, G, G, G, G, G, G, G, G, B, B, B, B, B, B, B, B, I, I, I, I, I, I, I, I, V, V, V, V, V, V, V, V, N, N, N, N, N, N, N, N ] # Get a list of all the music files path_to_music = "/home/pi/Music" os.chdir(path_to_music) music_files = glob.glob("*.mp3") music_files.sort()
-
import time import os import pygame from sense_hat import SenseHat import glob # Initialise pygame mixer pygame.mixer.init() # Create SenseHat object sense = SenseHat() R = [255, 0, 0] O = [255, 165, 0] Y = [255, 255, 0] G = [0, 128, 0] B = [0, 0, 255] I = [75, 0, 130] V = [139, 0, 255] N = [0, 0, 0] rainbow = [ R, R, R, R, R, R, R, R, O, O, O, O, O, O, O, O, Y, Y, Y, Y, Y, Y, Y, Y, G, G, G, G, G, G, G, G, B, B, B, B, B, B, B, B, I, I, I, I, I, I, I, I, V, V, V, V, V, V, V, V, N, N, N, N, N, N, N, N ] # Get a list of all the music files path_to_music = "/home/pi/Music" os.chdir(path_to_music) music_files = glob.glob("*.mp3") music_files.sort() volume = 1.0 current_track = 0 no_tracks = len(music_files) while True: pygame.mixer.music.load(music_files[current_track]) pygame.mixer.music.set_volume(volume) pygame.mixer.music.play() start_time = 0.0 sense.set_pixels(rainbow) while pygame.mixer.music.get_busy(): pygame.time.Clock().tick()
-
while True: pygame.mixer.music.load(music_files[current_track]) pygame.mixer.music.set_volume(volume) pygame.mixer.music.play() start_time = 0.0 sense.set_pixels(rainbow) while pygame.mixer.music.get_busy(): pygame.time.Clock().tick() for x in sense.stick.get_events(): if x.direction == 'right': current_track = current_track + 1 if current_track >= no_tracks: current_track = 0 pygame.mixer.music.stop() pygame.mixer.music.load(music_files[current_track]) pygame.mixer.music.play() start_time = 0.0 if x.direction == 'left': current_track = current_track - 1 if current_track < 0: current_track = 0 pygame.mixer.music.stop() pygame.mixer.music.load(music_files[current_track]) pygame.mixer.music.play()
-
while True: pygame.mixer.music.load(music_files[current_track]) pygame.mixer.music.set_volume(volume) pygame.mixer.music.play() start_time = 0.0 sense.set_pixels(rainbow) while pygame.mixer.music.get_busy(): pygame.time.Clock().tick() for x in sense.stick.get_events(): if x.direction == 'right': current_track = current_track + 1 if current_track >= no_tracks: current_track = 0 pygame.mixer.music.stop() pygame.mixer.music.load(music_files[current_track]) pygame.mixer.music.play() start_time = 0.0 if x.direction == 'left': current_track = current_track - 1 if current_track < 0: current_track = 0 pygame.mixer.music.stop() pygame.mixer.music.load(music_files[current_track]) pygame.mixer.music.play() if x.direction == 'up': volume = volume + 0.05 if volume >- 1.0: volume = 1.0 pygame.mixer.music.set_volume(volume) if x.direction == 'down': volume = volume - 0.05 if volume < 0.0: volume = 0.0 pygame.mixer.music.set_volume(volume)
-