Skip to content

Commit

Permalink
almost added speakers
Browse files Browse the repository at this point in the history
  • Loading branch information
PasaOpasen committed May 22, 2020
1 parent 1d88132 commit 454a9c6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 27 deletions.
81 changes: 55 additions & 26 deletions FirstTry/speech_recognition_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,47 @@
"""

import speech_recognition as sr
import soundcard as sc
from scipy.io.wavfile import write
import numpy as np

din = sc.all_microphones(include_loopback=True)[1]

def speech_to_text_from_speaker(speaker,time = 100_000,samplerate = 48000,lang = 'ru-RU'):
with speaker.recorder(samplerate=samplerate) as mic:
print('Listen')
dt = mic.record(time)
dt = np.int16(dt * (32767/np.max(np.abs(np.array([dt.min(),dt.max()])))))
print('Okay. Wait')
write("tmp.wav", samplerate, dt)

return speech_to_text_from_wav(lang)


def speech_to_text_from_wav():

def speech_to_text_from_wav(lang = 'ru-RU', file = 'tmp.wav'):

# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()

# Reading Audio file as source
# listening the audio file and store in audio_text variable

with sr.AudioFile('english.wav') as source:
with sr.AudioFile(file) as source:

audio_text = r.listen(source)

# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:

# using google speech recognition
text = r.recognize_google(audio_text)
print('Converting audio transcripts into text ...')
print(text)

except:
print('Sorry.. run again...')
return r.recognize_google(audio_text, language = lang)
except Exception as e:
print(e)
return 'bad result of recognition'


def speech_to_text_from_micro(lang = 'ru-RU'):
r = sr.Recognizer()


# Reading Microphone as source
# listening the speech and store in audio_text variable

Expand All @@ -52,46 +64,63 @@ def speech_to_text_from_micro(lang = 'ru-RU'):
#energy threshold based on the surrounding noise level
r.adjust_for_ambient_noise(source, 0.5)

print("Talk")
print(f"TALK ({lang[:2]})")
audio_text = r.listen(source)
print("Time over, thanks")
print("Okay. Stop talking")
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:
# using google speech recognition
print("Text: "+r.recognize_google(audio_text, language = lang))
except:
print("could not understand audio")

try:
return r.recognize_google(audio_text, language = lang)
except Exception as e:
print(e)
return 'bad result of recognition'


speech_to_text_from_micro()
speech_to_text_from_wav()

speech_to_text_from_speaker(din,time=100000,lang = 'ru-RU')





from scipy.io.wavfile import write
samplerate = 100000; fs = 1000
t = np.linspace(0., 1., samplerate)
amplitude = np.iinfo(np.int16).max
data = amplitude * np.sin(2. * np.pi * fs * t)
write("example.wav", samplerate, data)


import numpy as np
from scipy.io.wavfile import write

data = np.random.uniform(-1,1,44100) # 44100 random samples between -1 and 1
scaled = np.int16(data/np.max(np.abs(data)) * 32767)
write('test.wav', 44100, scaled)


with din.recorder(samplerate=48000) as mic:
print('Listen')
dt = mic.record(100_000)
dt = np.int16(dt/np.max(np.abs(dt)) * 32767)
print('Okay')
write("tmp.wav", 48000, dt)





dt = np.random.rand(50000,500)

%timeit r = np.int16(dt/np.max(np.abs(dt)) * 32767)

%timeit r = np.int16(dt * (32767/np.max(np.abs(dt))))

%timeit r = np.array(dt * (32767/np.max(np.abs(dt))),dtype='int16')








%timeit r = np.int16(dt * (32767/np.max(np.abs(np.array([dt.min(),dt.max()])))))



Expand Down
Binary file added SecondTry/example.wav
Binary file not shown.
28 changes: 27 additions & 1 deletion SecondTry/soundcardtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,38 @@
# alternatively, get a `Recorder` and `Player` object
# and play or record continuously:
with default_mic.recorder(samplerate=48000) as mic, default_speaker.player(samplerate=48000) as sp:
for _ in range(100):
for _ in range(200):
data = mic.record(numframes=1024)
sp.play(data)



import scipy

din = sc.all_microphones(include_loopback=True)[1]
with din.recorder(samplerate=48000) as mic, default_speaker.player(samplerate=48000) as sp:
for _ in range(1):
print('play')
data = mic.record(numframes=48000)
print('stop')
scipy.io.wavfile.write("tmp.wav", 48000, data)
sp.play(data)



















Binary file added SecondTry/test.wav
Binary file not shown.
Binary file added SecondTry/tmp.wav
Binary file not shown.

0 comments on commit 454a9c6

Please sign in to comment.