From 942c175b902586ae2791ff2343aa03727ec02f9c Mon Sep 17 00:00:00 2001 From: jango-blockchained Date: Wed, 5 Feb 2025 03:30:15 +0100 Subject: [PATCH] refactor: improve Docker speech container audio configuration and user permissions - Update Dockerfile to enhance audio setup and user management - Modify setup-audio.sh to add robust PulseAudio socket and device checks - Add proper user and directory permissions for audio and model directories - Simplify container startup process and improve audio device detection --- .gitignore | 4 +++- docker/speech/Dockerfile | 37 ++++++++++++++++++------------------ docker/speech/setup-audio.sh | 22 +++++++++++++++++++-- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index cbc5353..9d8ab3a 100644 --- a/.gitignore +++ b/.gitignore @@ -87,4 +87,6 @@ site/ # Python __pycache__/ *.py[cod] -*$py.class \ No newline at end of file +*$py.class + +models/ \ No newline at end of file diff --git a/docker/speech/Dockerfile b/docker/speech/Dockerfile index c28de94..80e50bc 100644 --- a/docker/speech/Dockerfile +++ b/docker/speech/Dockerfile @@ -33,36 +33,35 @@ RUN apt-get update && apt-get install -y \ libasound2 \ libasound2-plugins \ pulseaudio \ - && rm -rf /var/lib/apt/lists/* + pulseaudio-utils \ + libpulse0 \ + libportaudio2 \ + && rm -rf /var/lib/apt/lists/* \ + && mkdir -p /var/run/pulse /var/lib/pulse # Create necessary directories -RUN mkdir -p /models/wake_word /audio +RUN mkdir -p /models/wake_word /audio && \ + chown -R 1000:1000 /models /audio && \ + mkdir -p /home/user/.config/pulse && \ + chown -R 1000:1000 /home/user # Set working directory WORKDIR /app -# Copy the wake word detection script +# Copy the wake word detection script and audio setup script COPY wake_word_detector.py . +COPY setup-audio.sh /setup-audio.sh +RUN chmod +x /setup-audio.sh # Set environment variables ENV WHISPER_MODEL_PATH=/models \ WAKEWORD_MODEL_PATH=/models/wake_word \ PYTHONUNBUFFERED=1 \ - ASR_MODEL=base.en \ - ASR_MODEL_PATH=/models + PULSE_SERVER=unix:/run/user/1000/pulse/native \ + HOME=/home/user -# Add resource limits to Python -ENV PYTHONMALLOC=malloc \ - MALLOC_TRIM_THRESHOLD_=100000 \ - PYTHONDEVMODE=1 +# Run as the host user +USER 1000:1000 -# Add healthcheck -HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ - CMD ps aux | grep '[p]ython' || exit 1 - -# Copy audio setup script -COPY setup-audio.sh /setup-audio.sh -RUN chmod +x /setup-audio.sh - -# Start command -CMD ["/bin/bash", "-c", "/setup-audio.sh && python -u wake_word_detector.py"] \ No newline at end of file +# Start the application +CMD ["/setup-audio.sh"] \ No newline at end of file diff --git a/docker/speech/setup-audio.sh b/docker/speech/setup-audio.sh index e50466d..dc5c033 100755 --- a/docker/speech/setup-audio.sh +++ b/docker/speech/setup-audio.sh @@ -1,7 +1,25 @@ #!/bin/bash -# Wait for PulseAudio to be ready -sleep 2 +# Wait for PulseAudio socket to be available +while [ ! -e /run/user/1000/pulse/native ]; do + echo "Waiting for PulseAudio socket..." + sleep 1 +done + +# Test PulseAudio connection +pactl info || { + echo "Failed to connect to PulseAudio server" + exit 1 +} + +# List audio devices +pactl list sources || { + echo "Failed to list audio devices" + exit 1 +} + +# Start the wake word detector +python /app/wake_word_detector.py # Mute the monitor to prevent feedback pactl set-source-mute alsa_output.pci-0000_00_1b.0.analog-stereo.monitor 1