Skip to content

Commit

Permalink
Merge pull request #28 from slackr31337/client-class
Browse files Browse the repository at this point in the history
Add delete() to stop() for porcupine
  • Loading branch information
slackr31337 authored Aug 13, 2023
2 parents 49f9a20 + f1a079a commit bd101db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"pvporcupine",
"pvrecorder",
"ratecv",
"SIGTERM",
"simpleaudio",
"tomono",
"websockets"
Expand Down
36 changes: 20 additions & 16 deletions voice_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import time
import struct
import signal
import logging
import argparse
import asyncio
Expand All @@ -22,6 +23,7 @@

import aiohttp
import pvporcupine
from pvporcupine import Porcupine
from pvrecorder import PvRecorder
import simpleaudio

Expand Down Expand Up @@ -70,15 +72,16 @@ class PorcupinePipeline:
##########################################
def __init__(self, args: argparse.Namespace):
"""Setup Websocket client and audio pipeline"""


signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)

self._state = State(args=args)
self._state.running = False

self._conn = aiohttp.TCPConnector()
self._event_loop = asyncio.get_event_loop()

self._porcupine = get_porcupine(self._state)

for idx, device in enumerate(PvRecorder.get_audio_devices()):
self._devices[idx] = device
_LOGGER.info("Device %d: %s", idx, device)
Expand All @@ -93,6 +96,7 @@ def __init__(self, args: argparse.Namespace):
if args.show_audio_devices:
sys.exit(0)

self._porcupine = get_porcupine(self._state)
self._audio_thread = threading.Thread(
target=self.read_audio,
daemon=True,
Expand Down Expand Up @@ -138,10 +142,15 @@ def stop(self) -> None:

self._state.recording = False
self._state.running = False
self._websocket = None

self._audio_thread.join(1)

if hasattr(self._porcupine, "delete"):
self._porcupine.delete()

self._porcupine = None
self._websocket = None


##########################################
async def _ping(self):
"""Send Ping to HA"""
Expand Down Expand Up @@ -456,7 +465,7 @@ async def _play_response(self, url: str) -> None:


##########################################
def get_porcupine(state: State) -> pvporcupine:
def get_porcupine(state: State) -> Porcupine:
"""Listen for wake word and send audio to Home-Assistant"""

args = state.args
Expand Down Expand Up @@ -531,9 +540,8 @@ def get_porcupine(state: State) -> pvporcupine:


##########################################
def main() -> None:
"""Main entry point."""

if __name__ == "__main__":

args = get_cli_args()
_LOGGER.setLevel(level=logging.DEBUG if args.debug else logging.INFO)
if args.debug:
Expand All @@ -548,12 +556,8 @@ def main() -> None:
_LOGGER.debug(args)

audio_pipeline = PorcupinePipeline(args)
audio_pipeline.start()


##########################################
if __name__ == "__main__":
with suppress(KeyboardInterrupt):
main()

audio_pipeline.start()

audio_pipeline.stop()
sys.exit(0)

0 comments on commit bd101db

Please sign in to comment.