#!/bin/sh
# pdmtest - record from the PDM digital mics on the P1 header

#   pdmtest                 10s, 2 channels (one data line) -> /tmp/pdmtest.wav
#   pdmtest 30              30s, 2 channels
#   pdmtest 30 4            30s, 4 channels (two data lines; +2ch per SDI line)

# Enable PDM first:  novaconfig -> interfaces -> pdm, then reboot.

die() { echo "pdmtest: $*" >&2; exit 1; }

DUR="${1:-10}"
CH="${2:-2}"
case "$DUR" in ''|*[!0-9]*) die "duration must be in seconds: $DUR" ;; esac
case "$CH"  in ''|*[!0-9]*) die "channels must be a number: $CH" ;; esac

# pdm-mics is a separate capture card; its number shifts with the acodec, so
# address it by its stable ALSA id 'pdmmics' (from the 'pdm-mics' card name).
grep -q pdm-mics /proc/asound/cards 2>/dev/null \
	|| die "no pdm-mics card - enable it: novaconfig -> interfaces -> pdm, then reboot"

OUT=$(mktemp /tmp/pdmtest.XXXXXX.wav) || die "mktemp failed"
echo "recording PDM: ${CH}ch S32_LE 48kHz for ${DUR}s -> $OUT"
arecord -D hw:pdmmics,0 -r 48000 -f S32_LE -c "$CH" -d "$DUR" "$OUT" || die "arecord failed"

SIZE=$(wc -c < "$OUT")
echo "done: $OUT ($SIZE bytes)   play: aplay -D plughw:rockchiprk3308a $OUT"
