-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (70 loc) · 2.34 KB
/
main.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
### Packages ###
import os, random, time
import RPI.GPIO as GPIO
import simpleaudio as sa
### Config ###
# Path to wave files
audio_path = './'
# GPIO pin number the switch is connected to
switch_gpio_pin_number = 18
### Setup GPIO ###
# Setting GPIO mode to Broadcom
GPIO.setmode(GPIO.BCM)
# Set the GPIO pin to input and pull up
GPIO.setup(switch_gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
### Functions ###
def get_wave_files(path: str):
# List of files in a given path
files = os.listdir(path)
# New empty list to populate with .wav files
wave_files = []
# For every file in given path
for file in files:
# If the file is type .wav
if file.endswith('.wav'):
# Append to wav file list
wave_files.append(path + file)
# Return list of wav files in a given path
return wave_files
def pick_random_clip(clips: list):
# Get the length of a list of clips
number_of_clips = len(clips)
# If the length is greater than 0
if number_of_clips > 0:
# Use random to pick a random clip
random_int = random.randint(1, len(clips))
# Minus 1 from the int to get the index
random_index = random_int - 1
# Return random clip
return clips[random_index]
else:
# Return null if clips list was empty
return null
def play_clip(clip_path):
try:
# Use simple audio to load the wave file
wave_object = sa.WaveObject.from_wave_file(clip_path)
# Use simple audio to play the wave file
play_object = wave_object.play()
# Wait for the wave file to finish playing
play_object.wait_done()
except FileNotFoundError:
# Return an error if the wave file was not found
print('Wav File ' + file_name + ' does not exist')
def switch_press():
# Get a list of wave files from a given path
wave_files = get_wave_files(audio_path)
# Select a wave file from the path at random
selected_wave_file = pick_random_clip(wave_files)
# Play the randomly selected wave file
play_clip(selected_wave_file)
### Main ###
while True:
# Get the state of the switch via GPIO
switch_state = GPIO.input(switch_gpio_pin_number)
# If the state is False, the switch is in the closed position (Pressed)
if switch_state == False:
# Trigger the switch press function to play a random wave file
switch_press()
# Set a grace period before it can be triggered again
time.sleep(1)