diff --git a/.gitignore b/.gitignore index 0dbf2f2..1ff74a5 100644 --- a/.gitignore +++ b/.gitignore @@ -168,3 +168,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +# project-specific +audio/ diff --git a/README.md b/README.md index 3ac59f0..a55a830 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # dinnerbell -Teaching my cat to tell time with Pavlovian conditioning \ No newline at end of file +Teaching my cat to tell time with Pavlovian conditioning + +# Set-up + +Install dependencies: + +``` +echo +``` + +and run: + +python dinnerbell.py diff --git a/dinnerbell.py b/dinnerbell.py new file mode 100755 index 0000000..9e91a42 --- /dev/null +++ b/dinnerbell.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 + +import os +import time +import datetime +import random +import subprocess +import argparse + +DEFAULT_WATCH_DIRECTORY = "audio" + +def play_audio(file_path): + try: + subprocess.run(["ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet", file_path]) + except Exception as e: + print(f"Error playing {file_path}: {e}") + +def play_audio_for_current_time(watch_dir): + current_time = datetime.datetime.now().strftime('%H:%M') + file_path = os.path.join(watch_dir, f"{current_time}.wav") + folder_path = os.path.join(watch_dir, current_time) + + if os.path.isfile(file_path): + print(f"Playing '{file_path}'") + play_audio(file_path) + elif os.path.isdir(folder_path): + wav_files = [f for f in os.listdir(folder_path) if f.lower().endswith('.wav')] + if wav_files: + selected_file = os.path.join(folder_path, random.choice(wav_files)) + print(f"Playing '{selected_file}'") + play_audio(selected_file) + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--directory', default=DEFAULT_WATCH_DIRECTORY, help='Folder to read audio files from') + parser.add_argument('--daemon', action='store_true', help='Run in daemon mode') + args = parser.parse_args() + + while True: + play_audio_for_current_time(args.directory) + now = datetime.datetime.now() + seconds_until_next_minute = 60 - now.second + if not args.daemon: + break + print(f"Using audio from path '{args.directory}'") + time.sleep(seconds_until_next_minute) diff --git a/dinnerbell.service b/dinnerbell.service new file mode 100644 index 0000000..40c4bb9 --- /dev/null +++ b/dinnerbell.service @@ -0,0 +1,15 @@ +[Unit] +Description=Dinnerbell Python script +After=network.target sound.target + +[Service] +Type=simple +ExecStart=/usr/bin/dinnerbell --daemon --directory=${AUDIO_DIR} +Environment=AUDIO_DIR=/etc/dinnerbell/audio +Restart=on-failure +User=nobody +Group=nogroup +WorkingDirectory=/opt/dinnerbell/ + +[Install] +WantedBy=multi-user.target diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..80ca702 --- /dev/null +++ b/install.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +mkdir -p /opt/dinnerbell/ && \ +/usr/bin/cp "$SCRIPT_DIR"/dinnerbell.py /opt/dinnerbell/dinnerbell.py && \ +chmod +x /opt/dinnerbell/dinnerbell.py && \ +ln -sf /opt/dinnerbell/dinnerbell.py /usr/bin/dinnerbell && \ +mkdir -p /etc/dinnerbell/audio/ && \ +chmod 777 /etc/dinnerbell/audio/ && \ +/usr/bin/cp "$SCRIPT_DIR"/dinnerbell.service /etc/systemd/system/dinnerbell.service && \ +systemctl daemon-reload && \ +systemctl restart playclock.service