summaryrefslogtreecommitdiff
path: root/scripts/gen_spectrograms.sh
blob: 7eb6bdc9e64ade2dab24a501b44acbbb7effaaa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# This script converts all .wav and .aif files from assets/originals
# into .spec files in assets/final using the spectool utility.

set -euo pipefail

# --- Configuration ---
PROJECT_ROOT=$(git rev-parse --show-toplevel)
SOURCE_DIR="${PROJECT_ROOT}/assets/originals"
DEST_DIR="${PROJECT_ROOT}/assets/final"
SPECTOOL_PATH="${PROJECT_ROOT}/build/spectool"
TEMP_WAV_DIR=$(mktemp -d)

# --- Pre-flight Checks ---
if ! command -v ffmpeg &> /dev/null; then
    echo "Error: ffmpeg is not installed or not in your PATH."
    echo "Please install it to handle .aif files."
    exit 1
fi

if [ ! -f "${SPECTOOL_PATH}" ]; then
    echo "Error: spectool not found at ${SPECTOOL_PATH}"
    echo "Please build the project first (e.g., cmake -S . -B build && cmake --build build)"
    exit 1
fi

echo "Source directory: ${SOURCE_DIR}"
echo "Destination directory: ${DEST_DIR}"
echo "Using spectool: ${SPECTOOL_PATH}"

# --- Processing ---
process_file() {
    local input_file="$1"
    local base_name
    base_name=$(basename "$input_file")
    
    local output_name
    output_name=$(echo "${base_name%.*}" | tr '[:lower:]' '[:upper:]' | tr ' ' '_')
    local spec_file="${DEST_DIR}/${output_name}.spec"

    local file_to_process="$input_file"

    # Convert .aif to .wav if necessary
    if echo "${base_name}" | tr '[:upper:]' '[:lower:]' | grep -q ".aif$"; then
        echo "Converting AIF to WAV: ${base_name}"
        temp_wav="${TEMP_WAV_DIR}/${output_name}.wav"
        ffmpeg -i "$input_file" -y "$temp_wav" &> /dev/null
        file_to_process="$temp_wav"
    fi

    echo "Generating spectrogram: ${base_name} -> ${output_name}.spec"
    "${SPECTOOL_PATH}" analyze "$file_to_process" "$spec_file"
}

# Find and process all audio files
find "$SOURCE_DIR" -type f \( -name "*.wav" -o -name "*.aif" \) | while read -r audio_file; do
    process_file "$audio_file"
done

# --- Cleanup ---
rm -rf "$TEMP_WAV_DIR"

echo "Spectrogram generation complete."