This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

tools.whatsong: init

+64
+17
tools/whatsong/flake-module.nix
··· 1 + { ... }: 2 + { 3 + perSystem = 4 + { pkgs, ... }: 5 + { 6 + packages.whatsong = pkgs.writeShellApplication { 7 + name = "whatsong"; 8 + runtimeInputs = [ 9 + pkgs.sox 10 + pkgs.songrec 11 + pkgs.jq 12 + pkgs.yt-dlp 13 + ]; 14 + text = builtins.readFile ./whatsong.sh; 15 + }; 16 + }; 17 + }
+47
tools/whatsong/whatsong.sh
··· 1 + #!/usr/bin/env bash 2 + set -euo pipefail 3 + 4 + # Recording duration in seconds (default: 5) 5 + DURATION=${1:-5} 6 + 7 + # Temporary file for audio 8 + TEMP_AUDIO=$(mktemp /tmp/whatsong-XXXXXX.wav) 9 + trap 'rm -f "$TEMP_AUDIO"' EXIT 10 + 11 + echo "Listening for $DURATION seconds..." >&2 12 + 13 + # Record audio from default microphone 14 + # songrec needs specific format: 16kHz mono 15 + rec -q -r 16000 -c 1 -b 16 "$TEMP_AUDIO" trim 0 "$DURATION" 2>/dev/null || true 16 + 17 + if [ ! -s "$TEMP_AUDIO" ]; then 18 + echo "Error: No audio recorded" >&2 19 + exit 1 20 + fi 21 + 22 + echo "Identifying song..." >&2 23 + 24 + # Run songrec and get JSON output 25 + RESULT=$(songrec recognize --json "$TEMP_AUDIO" 2>/dev/null) 26 + 27 + if [ -z "$RESULT" ] || [ "$RESULT" = "null" ]; then 28 + echo "Could not identify song" >&2 29 + exit 1 30 + fi 31 + 32 + # Extract artist and title 33 + ARTIST=$(echo "$RESULT" | jq -r '.track.subtitle // empty') 34 + TITLE=$(echo "$RESULT" | jq -r '.track.title // empty') 35 + 36 + if [ -z "$TITLE" ]; then 37 + echo "Could not identify song" >&2 38 + exit 1 39 + fi 40 + 41 + echo "Found: $ARTIST - $TITLE" >&2 42 + 43 + # Search YouTube and get first result 44 + SEARCH_QUERY="$ARTIST $TITLE" 45 + echo "Searching YouTube..." >&2 46 + 47 + yt-dlp --dump-json "ytsearch1:$SEARCH_QUERY" 2>/dev/null | jq -r '.webpage_url'