-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_analysis.py
30 lines (22 loc) · 971 Bytes
/
audio_analysis.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
import speech_recognition as sr
from pydub import AudioSegment
from constants import RESULT_FILENAME
class AudioAnalyzer:
async def process_audio(self):
try:
recognizer = sr.Recognizer()
result_filename = RESULT_FILENAME
audio_file = f'result_audio/{result_filename}.wav'
audio = AudioSegment.from_file(audio_file, format="wav")
audio.export("converted.wav", format="wav")
with sr.AudioFile("converted.wav") as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language='pl-PL')
except sr.UnknownValueError:
text = "You can't recognize the speech."
except sr.RequestError:
text = "Communication error with Google Speech Recognition."
except Exception as e:
text = f"An error occurred: {e}"
return text
audio_analyzer = AudioAnalyzer()