-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundClass.py
62 lines (51 loc) · 1.88 KB
/
SoundClass.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
from pygame import mixer # ------ Music player
import glob
from gtts import gTTS
import os
import random
# makes a list of music files
musicList = glob.glob('./Music/*.mp3')
class RadioDevice:
def __init__(self):
mixer.pre_init(22050, -16, 3, 1024)
mixer.init()
self.songIndex = 0
self.numSongs = len(musicList)
mixer.music.load(musicList[self.songIndex])
self.playing = False
self.currSongName = musicList[self.songIndex].replace('./Music/', '').replace('.mp3', '')
self.shutdownNoise = mixer.Sound('./SFX/shutdown.wav')
self.buttonNoise = mixer.Sound('./SFX/beep.wav')
self.clickNoise = mixer.Sound('./SFX/click.wav')
with open('motivation.txt', 'r') as f:
self.motivationList = f.read().splitlines()
def playSong(self):
mixer.music.play()
self.currSongName = musicList[self.songIndex].replace('./Music/', '').replace('.mp3', '')
self.playing = True
def changeSong(self, num):
self.songIndex += num
# Reset to first song if end is reached
if self.songIndex == self.numSongs:
self.songIndex = 0
elif self.songIndex == -1:
self.songIndex = self.numSongs - 1
mixer.music.load(musicList[self.songIndex])
def stopSong(self):
mixer.music.stop()
self.playing = False
self.currSongName = ''
def playShutdown(self):
self.shutdownNoise.play()
def playButtonNoise(self):
self.buttonNoise.play()
def playClickNoise(self):
self.clickNoise.play()
# saves a given string as a .wav and plays it
def playSpeech(self, inputText):
tts = gTTS(inputText, lang='en')
tts.save('tts.wav')
os.system('mpg123 tts.wav')
def playMotivation(self):
randQuote = random.choice(self.motivationList)
self.playSpeech(randQuote)