-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_handler.py
80 lines (62 loc) · 2.63 KB
/
audio_handler.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
import pygame
from rich import print
import os
class AudioHandler:
@staticmethod
def play_audio(file_path):
# Notify the user to wait for the audio to play
print("[yellow]Playing audio file, please wait...[/yellow]")
try:
# Initialize the mixer
pygame.mixer.init()
# Load the sound file
pygame.mixer.music.load(file_path)
# Play the sound file
pygame.mixer.music.play()
# Keep the script running until the audio is finished playing
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
# Stop playback and unload the mixer
pygame.mixer.music.stop()
pygame.mixer.quit()
except:
# Handle error if the audio file is corrupt
print("[red]Error playing audio file. This is probably because the file is corrupt.[/red]")
# Delete the audio file
try:
print("[green]Audio file deleted, continuing recognition.[/green]")
os.remove(file_path)
except PermissionError:
# Handle error if unable to delete the audio file
print("[red]Error deleting audio file. Please delete the file manually.[/red]")
@staticmethod
def test_voice_id(file_path):
# Notify the user to wait for the audio to play
print("[yellow]Playing test file, please wait...[/yellow]")
try:
# Initialize the mixer
pygame.mixer.init()
# Load the sound file
pygame.mixer.music.load(file_path)
# Play the sound file
pygame.mixer.music.play()
# Keep the script running until the audio is finished playing
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
# Stop playback and unload the mixer
pygame.mixer.music.stop()
pygame.mixer.quit()
except:
# Handle error if the audio file cannot be played
print("[red]Error playing audio file. This voice doesn't seem right.[/red]")
return "error"
# Delete the audio file
try:
print("[green]Audio file deleted, continuing recognition.[/green]")
os.remove(file_path)
except PermissionError:
# Handle error if unable to delete the audio file
print("[red]Error deleting audio file. Please delete the file manually.[/red]")
# Tests
if __name__ == "__main__":
AudioHandler.play_audio("test.mp3")