From fbeb365932534488294dacb401427f9f36d69eae Mon Sep 17 00:00:00 2001 From: Pi User Date: Wed, 1 Oct 2025 23:29:38 +1300 Subject: [PATCH] pipenv: add sound-run; sound.py: --input-device flag --- Pipfile | 1 + src/sound.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Pipfile b/Pipfile index 69d4df9..69dabc5 100644 --- a/Pipfile +++ b/Pipfile @@ -34,3 +34,4 @@ flash-esp32 = "bash -c 'source $HOME/esp/esp-idf/export.sh && cd esp32 && idf.py watch-esp32 = "watchfiles 'bash -c \"source $HOME/esp/esp-idf/export.sh && cd esp32 && idf.py -p ${ESPPORT:-/dev/ttyACM0} -b ${ESPSPEED:-460800} flash monitor\"' esp32/main" send-json = "python test/send_json.py" send-net = "python test/test_networking.py" +sound-run = "python src/sound.py" diff --git a/src/sound.py b/src/sound.py index 7c2a0b8..6e4643e 100644 --- a/src/sound.py +++ b/src/sound.py @@ -5,6 +5,7 @@ import aubio import numpy as np from time import sleep import json +import argparse import socket import time import logging # Added logging import @@ -24,7 +25,7 @@ SOUND_CONTROL_HOST = "127.0.0.1" SOUND_CONTROL_PORT = 65433 class SoundBeatDetector: - def __init__(self, tcp_host: str, tcp_port: int): + def __init__(self, tcp_host: str, tcp_port: int, *, input_device: int | None = None): self.tcp_host = tcp_host self.tcp_port = tcp_port self.tcp_socket = None @@ -34,7 +35,7 @@ class SoundBeatDetector: self.bufferSize = 512 self.windowSizeMultiple = 2 - self.audioInputDeviceIndex = 7 + self.audioInputDeviceIndex = 7 if input_device is None else int(input_device) self.audioInputChannels = 1 self.pa = pyaudio.PyAudio() @@ -196,11 +197,15 @@ class SoundBeatDetector: # Removed async def run(self) if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Sound beat detector") + parser.add_argument("--input-device", type=int, help="Audio input device index to use") + args = parser.parse_args() + # TCP Server Configuration (should match midi.py) MIDI_TCP_HOST = "127.0.0.1" MIDI_TCP_PORT = 65432 - sound_detector = SoundBeatDetector(MIDI_TCP_HOST, MIDI_TCP_PORT) + sound_detector = SoundBeatDetector(MIDI_TCP_HOST, MIDI_TCP_PORT, input_device=args.input_device) logging.info("Starting SoundBeatDetector...") try: sound_detector.start_stream()