Sunteți pe pagina 1din 7

#for PushButton Part

import RPi.GPIO as GPIO

from time import sleep

import lcddriver

import pyaudio

import wave

import time

import datetime

import speech_recognition as sr

import sys

button1 = 23

button2 = 20

led1 = 24

led2 = 21

GPIO.setmode(GPIO.BCM)

GPIO.setup(button1,GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button to GPIO23

GPIO.setup(button2,GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button to GPIO20

GPIO.setup(led1,GPIO.OUT) #LED to GPIO24


GPIO.setup(led2,GPIO.OUT) #LED to GPIO21

boolpush = False

GPIO.output(led1,False)

GPIO.output(led2,False)

def good_rec1(arecx, speakerx):

form_1 = pyaudio.paInt16 # 16-bit resolution

chans = 1 # 1 channel

samp_rate = 44100 # 44.1kHz sampling rate

chunk = 4096 # 2^12 samples for buffer

record_secs = 5 # seconds to record

dev_index = 2 # device index found by p.get_device_info_by_index(ii)

wav_output_filename = 'test1.wav' # name of .wav file

audio = pyaudio.PyAudio() # create pyaudio instantiation

# create pyaudio stream

stream = audio.open(format = form_1,rate = samp_rate,channels = chans,input_device_index =


dev_index,input = True,frames_per_buffer=chunk)

print("recording")

frames = []

# loop through stream and append audio chunks to frame array

for ii in range(0,int((samp_rate/chunk)*record_secs)):

data = stream.read(chunk)
frames.append(data)

print("recording")

# stop the stream, close it, and terminate the pyaudio instantiation

#time.sleep(7)

stream.stop_stream()

stream.close()

audio.terminate()

#Save to Audio MP3

timestr = time.strftime("%Y%m%d-%H%M%S")

#wav_output_filename = "aRec1" + timestr + ".wav"

wav_output_filename = arecx + timestr + ".wav"

# save the audio frames as .wav file

wavefile = wave.open(wav_output_filename,'wb')

wavefile.setnchannels(chans)

wavefile.setsampwidth(audio.get_sample_size(form_1))

wavefile.setframerate(samp_rate)

wavefile.writeframes(b''.join(frames))

wavefile.close()

r=sr.Recognizer()

with sr.AudioFile(wav_output_filename) as source:

audio=r.listen(source)
try:

print("Output:"+r.recognize_google(audio))

f= open("xnotes.txt","a+")

#f.write("Speaker 1: " + r.recognize_google(audio) + "." + " [Date :" +


datetime.datetime.now().ctime() + "]" + "\r\n")

f.write(speakerx + ":" + r.recognize_google(audio) + "." + " [Date :" +


datetime.datetime.now().ctime() + "]" + "\r\n")

#f.write("Speaker 1: " + r.recognize_google(audio) + ".")

f.close()

GPIO.output(led1,False)

GPIO.output(led2,False)

except Exception:

print("Error")

GPIO.output(led1,False)

GPIO.output(led2,False)

try:

while True:

#counter = 0

#while counter < 10:


#button_state = GPIO.input(23)

#if button_state == False:

if GPIO.input(button1)== 0:

print("Button 1 was Pressed:")

sleep(.1)

GPIO.output(led1,True)

GPIO.output(led2,False)

display = lcddriver.lcd()

# Main body of code

#while True:

print('Speak Now 1')

display.lcd_display_string("Speak Now ", 1) # Write line of text to first line of display

print('processing')

display.lcd_display_string("Processing", 1) # Write line of text to first line of display

try:

print('Speaker 1.')

display.lcd_display_string("[REC] Speaker 1", 2) # Write line of text to second line of display

############################################

#good_rec1()

good_rec1("aRec1","Speaker 1")
except Exception as e: print(e)

#except:

#print('Cant understand you.')

if GPIO.input(button2)==0:

#print("Button 2 was Pressed:")

#sleep(.1)

GPIO.output(led1,False)

GPIO.output(led2,True)

display = lcddriver.lcd()

# Main body of code

#while True:

print('Speak Now 2')

display.lcd_display_string("Speak Now ", 1) # Write line of text to first line of display

print('processing')

display.lcd_display_string("Processing", 1) # Write line of text to first line of display

try:

print('Speaker 2.')

display.lcd_display_string("[REC] Speaker 2", 2) # Write line of text to second line of display

#good_rec1()

good_rec1("aRec2","Speaker 2")

except Exception as e: print(e)


finally:

GPIO.cleanup()

S-ar putea să vă placă și