This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
richtier
committed
Jan 4, 2019
1 parent
fb24bc9
commit 7f8ee2e
Showing
9 changed files
with
142 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import argparse | ||
import io | ||
import time | ||
|
||
from pydub import AudioSegment | ||
from pydub.playback import play | ||
import pyaudio | ||
|
||
from avs_client import AlexaVoiceServiceClient | ||
from avs_client.avs_client import helpers | ||
|
||
|
||
def main(client_id, secret, refresh_token): | ||
alexa_client = AlexaVoiceServiceClient( | ||
client_id=client_id, | ||
secret=secret, | ||
refresh_token=refresh_token, | ||
) | ||
|
||
p = pyaudio.PyAudio() | ||
|
||
def callback(in_data, frame_count, time_info, status): | ||
input_buffer.write(in_data) | ||
return (in_data, pyaudio.paContinue) | ||
|
||
stream = p.open( | ||
rate=16000, | ||
channels=1, | ||
format=pyaudio.paInt16, | ||
input=True, | ||
stream_callback=callback, | ||
frames_per_buffer=128, | ||
start=False | ||
) | ||
|
||
dialog_request_id = helpers.generate_unique_id() | ||
|
||
try: | ||
print('listening. Press CTRL + C to exit.') | ||
input_buffer = io.BytesIO() | ||
stream.start_stream() | ||
print('Say something to Alexa.') | ||
alexa_client.connect() | ||
while True: | ||
directives = alexa_client.send_audio_file( | ||
input_buffer, | ||
dialog_request_id=dialog_request_id | ||
) | ||
stream.stop_stream() | ||
if directives: | ||
print('Alexa\'s turn.') | ||
for directive in directives: | ||
if directive.name in ['Speak', 'Play']: | ||
output_buffer = io.BytesIO(directive.audio_attachment) | ||
track = AudioSegment.from_mp3(output_buffer) | ||
play(track) | ||
input_buffer = io.BytesIO() | ||
stream.start_stream() | ||
print('Your turn. Say something.') | ||
time.sleep(1) | ||
finally: | ||
stream.stop_stream() | ||
stream.close() | ||
p.terminate() | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
'-c', '--client-id', help='AVS client ID', required=True | ||
) | ||
parser.add_argument( | ||
'-s', '--client-secret', help='AVS client secret', required=True | ||
) | ||
parser.add_argument( | ||
'-r', '--refresh-token', help='AVS refresh token', required=True | ||
) | ||
parsed = parser.parse_args() | ||
main( | ||
client_id=parsed.client_id, | ||
secret=parsed.client_secret, | ||
refresh_token=parsed.refresh_token, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters