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

WkikiConversation #5

Closed
wants to merge 1 commit into from
Closed
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
Add files via upload
  • Loading branch information
ankxyz authored Sep 3, 2016
commit 47ec9b0bf58d33fd2592e2a68802f25a6aa4584a
147 changes: 147 additions & 0 deletions WikiConversation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import pyaudio
import wave
import audioop
import time


import soundfile


import argparse
import base64
import json

from googleapiclient import discovery
import httplib2
from oauth2client.client import GoogleCredentials


import wikipedia


from gtts import gTTS


import pyglet


import sys


FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1024

SPEECH_DISCOVERY_URL = ('https://{api}.googleapis.com/$discovery/rest?'
'version={apiVersion}')


#records audio from microphone from first "loud" sound until silence
def record(file_name=None, min_amplitude=500):
audio = pyaudio.PyAudio()
was_speech = False #detect first "loud" sound
silence_count = 0 #counter of chunks with silence
print ("recording...")
frames = []
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
while True:
data = stream.read(CHUNK) #read next chunk and add it to array
amplitude = audioop.rms(data, 2)
print('amplitude=',amplitude)
if amplitude >= min_amplitude:
was_speech = True #if speech (sounds) was
silence_count = 0 #wait for "long" silence
if was_speech:
frames.append(data)
if was_speech and amplitude < min_amplitude:
silence_count +=1
if silence_count >= 30: #break if detected "long" silence
break

print("finished recording")

# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
if file_name is None:
file_name = str(time.time()) + '.wav'
waveFile = wave.open(file_name, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
return file_name


def convert_to_flac(wave_file):
data, samplerate = soundfile.read(wave_file)
flac_file = wave_file[:-4] + '.flac'
soundfile.write(flac_file, data, samplerate)
print('samplerate=', samplerate)
return flac_file


def google_speech_connect(flac_file, lang='ru-RU'):
credentials = GoogleCredentials.get_application_default().create_scoped(
['https://www.googleapis.com/auth/cloud-platform'])
http = httplib2.Http()
credentials.authorize(http)

service = discovery.build('speech', 'v1beta1', http=http,
discoveryServiceUrl=SPEECH_DISCOVERY_URL)
with open(flac_file, 'rb') as speech:
speech_content = base64.b64encode(speech.read())

service_request = service.speech().syncrecognize(
body={
'config': {
'encoding': 'FLAC',
'sampleRate': RATE,
'languageCode': lang
},
'audio': {
'content': speech_content.decode('UTF-8')
}
})
response = service_request.execute()
return response


def get_text(response):
return response['results'][0]['alternatives'][0]['transcript']


def get_wiki_page(text,lang='ru'):
wikipedia.set_lang(lang)
return wikipedia.summary(text)


def tts(wiki_page, lang='ru'):
gtts = gTTS(wiki_page, lang=lang)
mp3_file_name = str(time.time()) + '.mp3'
gtts.save(mp3_file_name)
return mp3_file_name


def play_mp3(mp3_file):
player = pyglet.media.Player()
player.queue(pyglet.media.load(mp3_file))
player.play()
#music = pyglet.resource.media(mp3_file)
#music.play()
print('Если хотите закончить прослушивание, нажмите q')
c = sys.stdin.read(1)
if c == 'q':
player.delete()


def main():
play_mp3(tts(get_wiki_page(get_text(google_speech_connect(convert_to_flac(record()))))))

#main()