forked from pimoroni/inventorhatmini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay_sound_pygame.py
More file actions
34 lines (24 loc) · 766 Bytes
/
play_sound_pygame.py
File metadata and controls
34 lines (24 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pygame
from inventorhatmini import InventorHATMini
"""
Play a WAV audio file from your Inventor HAT Mini, using PyGame!
Press "User" to play the sound.
Note: WAV files need to be signed 16-bit
"""
# Create a new InventorHATMini
board = InventorHATMini(init_leds=False)
# Initialise PyGame so we can play sounds with it
pygame.init()
# Load the sound from file
ahoy = pygame.mixer.Sound("./ahoy.wav")
print("Press the 'User' button on your Inventor HAT Mini to play the sound.")
# Loop forever
while True:
if board.switch_pressed():
# Play the sound
channel = ahoy.play()
print("Playing ... ", end="")
# Wait for the sound to finish playing
while channel.get_busy():
pass
print("Done")