Compare commits

..

1 Commits

Author SHA1 Message Date
Drew Bednar c26621e45f Working bytes stream 3 years ago

@ -1,7 +1,3 @@
# learn_audio
A repo dedicated to audio experiments
# Requirements
One the Ubuntu raspberrypi 20.04 server I needed to have `sudo apt-get install libasound2-dev` installed before run `pip install -r requirements.txt`.
A repo dedicated to audio experiments

Binary file not shown.

@ -1,2 +0,0 @@
black
pip-tools

@ -1,41 +0,0 @@
#
# This file is autogenerated by pip-compile with python 3.8
# To update, run:
#
# pip-compile dev_requirements.in
#
black==22.10.0
# via -r dev_requirements.in
build==0.9.0
# via pip-tools
click==8.1.3
# via
# black
# pip-tools
mypy-extensions==0.4.3
# via black
packaging==21.3
# via build
pathspec==0.10.2
# via black
pep517==0.13.0
# via build
pip-tools==6.10.0
# via -r dev_requirements.in
platformdirs==2.5.4
# via black
pyparsing==3.0.9
# via packaging
tomli==2.0.1
# via
# black
# build
# pep517
typing-extensions==4.4.0
# via black
wheel==0.38.4
# via pip-tools
# The following packages are considered to be unsafe in a requirements file:
# pip
# setuptools

@ -1,6 +1,8 @@
import simpleaudio as sa
import sys
filename = './data/hello.wav'
filename = sys.argv[1]
'
wave_obj = sa.WaveObject.from_wave_file(filename)
play_obj = wave_obj.play()
play_obj.wait_done() # Wait until sound has finished playing
play_obj.wait_done() # Wait until sound has finished playing

1
say

@ -1 +0,0 @@

@ -12,7 +12,7 @@ wave_file_path = sys.argv[1]
if not wave_file_path.endswith('.wav'):
raise ValueError("File must have .wav file extension.")
(HOST,PORT)=('localhost',9000)
(HOST,PORT)=('0.0.0.0',9000)
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect((HOST,PORT))
with open(wave_file_path, 'rb') as wave_file:

@ -3,7 +3,7 @@ import time
import simpleaudio as sa
HOST = "127.0.0.1"
HOST = "0.0.0.0"
PORT = 9000
def save_file(client_socket):
@ -18,9 +18,11 @@ def save_file(client_socket):
def play_file(conn):
# you could consider sa.play_buffer ... I don't know if it would be more performant.
try:
wave_object = sa.WaveObject.from_wave_file(conn.makefile(mode="rb"))
play_obj = wave_object.play()
play_obj = sa.play_buffer(conn.makefile(mode="rb").read(), 1,2,22050)
play_obj.wait_done()
#wave_object = sa.WaveObject.from_wave_file(conn.makefile(mode="rb"))
#play_obj = wave_object.play()
#play_obj.wait_done()
except Exception as e:
print(f"Hit an error: {e} skipping playback")
return
@ -44,4 +46,4 @@ with socket.socket() as server_sock:
client_sock, client_addr = server_sock.accept()
print(f"New connection from {client_addr}.")
with client_sock as conn:
play_file(conn)
play_file(conn)

@ -1,3 +0,0 @@
pyaudio
SpeechRecognition
gTTs

@ -1,26 +0,0 @@
#
# This file is autogenerated by pip-compile with python 3.8
# To update, run:
#
# pip-compile requirement.in
#
certifi==2022.9.24
# via requests
charset-normalizer==2.1.1
# via requests
click==8.1.3
# via gtts
gtts==2.2.4
# via -r requirement.in
idna==3.4
# via requests
pyaudio==0.2.12
# via -r requirement.in
requests==2.28.1
# via gtts
six==1.16.0
# via gtts
speechrecognition==3.8.1
# via -r requirement.in
urllib3==1.26.12
# via requests

@ -1,102 +0,0 @@
import webbrowser
import typing
import os
import sys
from abc import ABC, abstractmethod
import speech_recognition as sr
class Action(ABC):
action_key: str
@abstractmethod
def action_callback(self, action_text) -> typing.Any:
pass
def register_va(self, virtual_assisant):
self._va = virtual_assisant
def sans_action_key(self, action_text):
return action_text[len(self.action_key)+1:]
class BrowserAction(Action):
action_key = "open browser"
def action_callback(self, action_text):
domain = self.sans_action_key(action_text)
url = f"https://{domain}"
print(f"Opening: {url}")
self._va.("Opening {domain}")
webbrowser.open(url)
class EchoAction(Action):
action_key = "echo"
def action_callback(self, action_text) -> typing.Any:
print(action_text)
self._va.text_to_speech(self.sans_action_key(action_text))
class StopAction(Action):
action_key = "stop listening"
def action_callback(self, action_text) -> typing.Any:
print("Stopping Virtual Assistant...")
sys.exit(0)
class VirtualAssistant:
def __init__(self, timeout: int = 3, actions: typing.List[Action]=[]):
self.listener = sr.Recognizer()
self.timeout = timeout
self.action_register = []
for action in actions:
self.register_action(action)
def register_action(self, action: Action):
"""Adds new Action instances to the Virtual Assistant's action register."""
self.action_register.append(action)
action.register_va(self)
def parse_action(self, text: str):
"""Checks the VA command register and executes callbacks when found."""
for action in self.action_register:
if text.lower().startswith(action.action_key):
action.action_callback(text)
return
print(f"No actions taken on: {text}")
def speech_to_text(self) -> str:
""""""
try:
with sr.Microphone() as mic:
print("Arni is listening...")
self.listener.adjust_for_ambient_noise(mic)
utterance = self.listener.listen(mic, timeout=self.timeout)
content = self.listener.recognize_google(utterance)
return content
except (sr.UnknownValueError, sr.RequestError, sr.WaitTimeoutError):
return ""
def text_to_speech(self, text: str):
print(f"Saying: {text}")
cmd = f'gtts-cli --nocheck "{text}" | mpg123 -q -'
print(cmd)
os.system(cmd)
def main():
print("Creating virtual assistant...")
va = VirtualAssistant(actions=[BrowserAction(),EchoAction(), StopAction()])
# breakpoint()
while True:
content = va.speech_to_text()
va.parse_action(content)
if __name__ == "__main__":
main()
Loading…
Cancel
Save