Skip to content

Commit

Permalink
scripts to start and stop daemons
Browse files Browse the repository at this point in the history
  • Loading branch information
Jef808 committed Feb 21, 2024
1 parent 2036a6d commit 3eacf2a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 22 deletions.
Empty file added echo_crafter/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion echo_crafter/listener/listener_with_wake_word.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def play_sound(wav_file):
"""Play a ding sound to indicate that the wake word was detected."""
subprocess.Popen(["aplay", "-q", str(wav_file)])
subprocess.Popen(["aplay", "-q", wav_file])


def main():
Expand Down
4 changes: 2 additions & 2 deletions echo_crafter/listener/utils/porcupine.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def recorder_context_manager():


class _Microphone:
"""A context manager for the recorder_context_manager."""
"""A class implementing the various microphone actions we use."""

def __init__(self, recorder, porcupine, cheetah):
self.recorder = recorder
Expand Down Expand Up @@ -82,7 +82,7 @@ def process_and_transmit_utterance(self, client):

@contextmanager
def Microphone():
"""Create a Microphone instance and yield it. Delete the instance upon exit."""
"""A context manager taking care of never leaking any resource."""
with recorder_context_manager() as recorder, porcupine_context_manager() as porcupine, cheetah_context_manager() as cheetah:
mic = _Microphone(recorder, porcupine, cheetah)
yield mic
18 changes: 3 additions & 15 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
annotated-types==0.6.0
anyio==3.7.1
certifi==2023.11.17
distro==1.8.0
h11==0.14.0
httpcore==1.0.2
httpx==0.25.2
idna==3.6
openai==1.3.7
PyAudio==0.2.14
pydantic==2.5.2
pydantic_core==2.14.5
sniffio==1.3.0
tqdm==4.66.1
typing_extensions==4.8.0
websocket-client==1.6.4
pvcheetah==2.0.0
pvporcupine==3.0.2
pvrecorder==1.2.2
66 changes: 62 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,72 @@
#!/usr/bin/env python3
from setuptools import setup, find_packages
import platform
import os
from pathlib import Path
from setuptools import setup, find_packages, Command
from setuptools.command.build_py import build_py
import sys

project_root = Path(__file__).parent
venv_dir = os.getenv('VIRTUAL_ENV')

def get_os():
os_name = platform.system()
return 'macOS' if os_name == 'Darwin' else os_name

def is_virtual_env():
return venv_dir is not None

if not is_virtual_env():
print('This script should be run in a virtual environment')
print("Please activate a virtual environment before running this script.")
venv_dir = project_root / '.venv' if (project_root / '.venv').exists() else project_root / 'venv' if (project_root / 'venv').exists() else None
if not venv_dir:
print("You can create a virtual environment using:")
if get_os() == 'Windows':
print("python -m venv venv")
venv_dir = project_root / 'venv'
else:
print("python -m venv .venv")
venv_dir = project_root / '.venv'
print("From the root of the project.")

print("You can activate your virtual environment using:")
if get_os() == 'Windows':
print(f"{venv_dir.relative_to(project_root)}\\Scripts\\activate.bat")
else:
print(f"source {venv_dir.relative_to(project_root)}/bin/activate")
sys.exit(1)

class BuildDocsCommand(Command):
description = 'Build Sphinx documentation'
user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
import subprocess
docs_dir = project_root / 'docs'
if not docs_dir.exists() or not docs_dir.is_dir() or not (docs_dir / 'Makefile').exists():
raise FileNotFoundError('Documentation not found')
venv_activate = project_root / '.venv/bin/activate'
if venv_activate.exists():
subprocess.run(['source', str(venv_activate)], shell=True)
subprocess.run(['make', 'html'], cwd=docs_dir)

setup(
name='echo-crafter',
version='0.1.0',
packages=find_packages(),
cmdclass={
'build_docs': BuildDocsCommand,
'build_py': build_py
},
entry_points={
'console_scripts': [
'microphone_listen = echo_crafter.listener.listener_with_wake_word:main',
'transcripts_collect = echo_crafter.listener.socket_read:main'
'restart_daemons=scripts.restart_daemons:main',
]
}
)
14 changes: 14 additions & 0 deletions start-listener.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env zsh

DIR="$(dirname "$(readlink -f "$0")")"

if pgrep -f "listener_with_wake_word" >/dev/null || pgrep -f "socket_read" >/dev/null; then
echo "The process is already running"
exit 1
else
source "$DIR/.venv/bin/activate"
python "$DIR/echo_crafter/listener/socket_read.py" &
echo "Transcripts collector started"
python "$DIR/echo_crafter/listener/listener_with_wake_word.py" &
echo "Microphone listener started"
fi
15 changes: 15 additions & 0 deletions stop-listener.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env zsh

DIR="$(dirname "$(readlink -f "$0")")"

microphone_listener=$(pgrep -f "listener_with_wake_word") 2>/dev/null
transcripts_collector=$(pgrep -f "socket_read") 2>/dev/null

if [ -n "$microphone_listener" ]; then
kill -15 "$microphone_listener"
echo "Microphone listener stopped"
fi
if [ -n "$transcripts_collector" ]; then
kill -15 "$transcripts_collector"
echo "Transcripts collector stopped"
fi

0 comments on commit 3eacf2a

Please sign in to comment.