-
Notifications
You must be signed in to change notification settings - Fork 12
/
audio_ctrl.py
133 lines (106 loc) · 3 KB
/
audio_ctrl.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pygame
import os
import random
import threading
import time
import yaml
import pyttsx3
usb_connected = False
curpath = os.path.realpath(__file__)
thisPath = os.path.dirname(curpath)
with open(thisPath + '/config.yaml', 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
current_path = os.path.abspath(os.path.dirname(__file__))
try:
pygame.mixer.init()
pygame.mixer.music.set_volume(config['audio_config']['default_volume'])
usb_connected = True
print('audio usb connected')
except:
usb_connected = False
print('audio usb not connected')
play_audio_event = threading.Event()
min_time_bewteen_play = config['audio_config']['min_time_bewteen_play']
engine = pyttsx3.init()
engine.setProperty('rate', config['audio_config']['speed_rate'])
def play_audio(input_audio_file):
if not usb_connected:
return
try:
pygame.mixer.music.load(input_audio_file)
pygame.mixer.music.play()
except:
play_audio_event.clear()
return
while pygame.mixer.music.get_busy():
pass
time.sleep(min_time_bewteen_play)
play_audio_event.clear()
def play_random_audio(input_dirname, force_flag):
if not usb_connected:
return
if play_audio_event.is_set() and not force_flag:
return
audio_files = [f for f in os.listdir(current_path + "/sounds/" + input_dirname) if f.endswith((".mp3", ".wav"))]
audio_file = random.choice(audio_files)
play_audio_event.set()
audio_thread = threading.Thread(target=play_audio, args=(current_path + "/sounds/" + input_dirname + "/" + audio_file,))
audio_thread.start()
def play_audio_thread(input_file):
if not usb_connected:
return
if play_audio_event.is_set():
return
play_audio_event.set()
audio_thread = threading.Thread(target=play_audio, args=(input_file,))
audio_thread.start()
def play_file(audio_file):
if not usb_connected:
return
audio_file = current_path + "/sounds/" + audio_file
play_audio_thread(audio_file)
def get_mixer_status():
if not usb_connected:
return
return pygame.mixer.music.get_busy()
def set_audio_volume(input_volume):
if not usb_connected:
return
input_volume = float(input_volume)
if input_volume > 1:
input_volume = 1
elif input_volume < 0:
input_volume = 0
pygame.mixer.music.set_volume(input_volume)
def set_min_time_between(input_time):
if not usb_connected:
return
global min_time_bewteen_play
min_time_bewteen_play = input_time
def play_speech(input_text):
if not usb_connected:
return
engine.say(input_text)
engine.runAndWait()
play_audio_event.clear()
def play_speech_thread(input_text):
if not usb_connected:
return
if play_audio_event.is_set():
return
play_audio_event.set()
speech_thread = threading.Thread(target=play_speech, args=(input_text,))
speech_thread.start()
def stop():
if not usb_connected:
return
pygame.mixer.music.stop()
play_audio_event.clear()
if __name__ == '__main__':
# while True:
# print(1)
# engine.say("this is a test")
# engine.runAndWait()
# time.sleep(1)
play_audio_thread("/home/ws/ugv_rpi/sounds/others/Boomopera_-_You_Rock_Full_Length.mp3")
time.sleep(100)