Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realizar a Limpeza de áudio de input #12

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ soundfile
streamlit
spacy_streamlit
flask
numpy
sounddevice
python_speech_features
soundfile
64 changes: 52 additions & 12 deletions src/Servidor/reconhecimentoAudio.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,76 @@
import speech_recognition as sr
import numpy as np
import sounddevice as sd
import soundfile as sf


#TODO: limpeza de audio
def trataAudio(audio, taxa_amostragem, duracao):

passa_alta = False
amplitude = 0.3

duracao_amostragem = int(duracao * taxa_amostragem)

sinal_entrada = audio

freq_corte = np.geomspace(20000, 20, sinal_entrada.shape[0])
passaAlta_saida = np.zeros_like(sinal_entrada)
dn_1 = 0

for n in range(sinal_entrada.shape[0]):
freq_quebra = freq_corte[n]

tan = np.tan(np.pi * freq_quebra / taxa_amostragem)
a1 = (tan - 1) / (tan + 1)

passaAlta_saida[n] = a1 * sinal_entrada[n] + dn_1
dn_1 = sinal_entrada[n] - a1 * passaAlta_saida[n]

if passa_alta:
passaAlta_saida *= -1

audio_tratado = sinal_entrada + passaAlta_saida
audio_tratado *= 0.5
audio_tratado *= amplitude

return audio_tratado


def reconhecerAudio(audio, sample_rate=44100, sample_width=2):
rec = sr.Recognizer()

audiodata = sr.AudioData(audio, sample_rate, sample_width)

texto = rec.recognize_google(audiodata, language="pt-BR")
sd.play(audio, sample_rate)
sd.wait()

try:
rec.adjust_for_ambient_noise(audio)
audiodata = sr.AudioData(audio, sample_rate, sample_width)
texto = rec.recognize_google(audiodata, language="pt-BR", show_all=False)
except:
return None

return texto


if __name__ == "__main__":
from sys import path
from os.path import join, dirname, realpath

#adiciona o caminho da função de gravar áudio, feito somente para
#reutilizar código na situação de debugging,
#não é uma prática boa fora desse contexto!
# adiciona o caminho da função de gravar áudio, feito somente para
# reutilizar código na situação de debugging,
# não é uma prática boa fora desse contexto!
path.append(join(dirname(realpath(__file__)), "../Cliente"))

from gravarAudio import gravarAudioArquivo
import os

gravarAudioArquivo("exemplo.wav")
arq = open("exemplo.wav", "rb")
audio = arq.read()
data = sf.SoundFile('exemplo.wav')
duracao = data.frames / data.samplerate
data, taxa_amostragem = sf.read('exemplo.wav')

texto = reconhecerAudio(audio)
audio_tratado = trataAudio(np.array(data), taxa_amostragem, duracao)
texto = reconhecerAudio(audio_tratado)
print(texto)

arq.close()
os.remove("exemplo.wav")
os.remove("exemplo.wav")