commit 29fed78d219fb2ac8274171dd9dcd5e157237e33 parent 3662fc304853348adb8583ac81c98392300e8bf3 Author: breadcat <breadcat@users.noreply.github.com> Date: Fri, 17 Jul 2026 10:26:03 +0100 Initial PBX audio script Diffstat:
| M | machines/ilias.nix | | | 1 | + |
| A | scripts/pbx.nix | | | 119 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 120 insertions(+), 0 deletions(-)
diff --git a/machines/ilias.nix b/machines/ilias.nix @@ -39,6 +39,7 @@ ../scripts/duupmove.nix ../scripts/overtid.nix ../scripts/payslips.nix + ../scripts/pbx.nix ../scripts/phone-dump.nix ../scripts/restic.nix ../scripts/seedy.nix diff --git a/scripts/pbx.nix b/scripts/pbx.nix @@ -0,0 +1,119 @@ +{pkgs, ...}: + +let + pbx = pkgs.writeShellScriptBin "pbx" '' + +set -euo pipefail + +usage() { +cat <<EOF +Usage: + $0 <model> <input> [output] [--volume N] + +Examples: + $0 samsung greeting.mp3 + $0 samsung greeting.mp3 output.wav + $0 gamma prompt.flac --volume 0.8 + $0 webex prompt.wav output.wav --volume 0.75 +EOF +exit 1 +} + +VOLUME="" +POSITIONAL=() + +while [[ $# -gt 0 ]]; do + case "$1" in + --volume) + [[ $# -ge 2 ]] || { + echo "--volume requires a value" + exit 1 + } + VOLUME="$2" + shift 2 + ;; + -h|--help) + usage + ;; + *) + POSITIONAL+=("$1") + shift + ;; + esac +done + +set -- "''${POSITIONAL[@]}" + +[[ $# -ge 2 && $# -le 3 ]] || usage + +MODEL=$(tr '[:upper:]' '[:lower:]' <<<"$1") +INPUT="$2" + +if [[ $# -eq 3 ]]; then + OUTPUT="''${3%.*}.wav" +else + OUTPUT="''${INPUT%.*}.wav" +fi + +[[ -f "$INPUT" ]] || { + echo "Input file not found." + exit 1 +} + +COMMON_ARGS=( + -ar 8000 + -ac 1 +) + +case "$MODEL" in + samsung|3cx|webex) + CODEC_ARGS=(-codec:a pcm_s16le -ab 128k) + ;; + lg-emg|lg-ucp|panasonic|gamma) + CODEC_ARGS=(-codec:a pcm_mulaw -ab 64k) + ;; + lg-hosted) + CODEC_ARGS=(-codec:a pcm_s16le -ab 64k) + ;; + *) + echo "Unknown model '$MODEL'" + exit 1 + ;; +esac + +FFMPEG_ARGS=() + +if [[ -n "$VOLUME" ]]; then + FFMPEG_ARGS+=(-af "volume=$VOLUME") +fi + +# Avoid overwriting the input if converting an existing WAV. +if [[ "$(realpath "$INPUT")" == "$(realpath -m "$OUTPUT")" ]]; then + BACKUP="''${INPUT%.wav}.original.wav" + + if [[ -e "$BACKUP" ]]; then + i=1 + while [[ -e "''${INPUT%.wav}.original.''${i}.wav" ]]; do + ((i++)) + done + BACKUP="''${INPUT%.wav}.original.''${i}.wav" + fi + + mv "$INPUT" "$BACKUP" + INPUT="$BACKUP" + + echo "Original renamed to $BACKUP" +fi + +${pkgs.ffmpeg}/bin/ffmpeg -i "$INPUT" \ + "''${CODEC_ARGS[@]}" \ + "''${COMMON_ARGS[@]}" \ + "''${FFMPEG_ARGS[@]}" \ + "$OUTPUT" + +echo "Created $OUTPUT" + + ''; +in { + environment.systemPackages = [pbx]; +}