11PIO ?= pio
22SAMPLES_DIR ?= samples
3+ TRIM_START_DB := -45
4+ TRIM_START_MS := 10
35
46ifneq ($(filter convert-samples,$(MAKECMDGOALS ) ) ,)
57CONVERT_SAMPLES_ARGS := $(filter-out convert-samples,$(MAKECMDGOALS ) )
@@ -60,6 +62,11 @@ convert-samples:
6062 @[ -d " $( SAMPLES_DIR) " ] || { echo " Missing directory: $( SAMPLES_DIR) " ; exit 1; }
6163 @set -e; \
6264 target_peak_db=" -1.0" ; \
65+ trim_db=" $( TRIM_START_DB) " ; \
66+ trim_ms=" $( TRIM_START_MS) " ; \
67+ trim_s=" $$ (awk -v ms=" $$ trim_ms" 'BEGIN { printf " %.3f" , (ms / 1000.0) }')" ; \
68+ trim_filter=" silenceremove=start_periods=1:start_duration=$$ trim_s:start_threshold=$$ {trim_db}dB:detection=peak," ; \
69+ trim_desc=" trim=$$ trim_db dB/$$ trim_ms ms" ; \
6370 count=" $$ (find " $(SAMPLES_DIR ) " -maxdepth 1 -type f \( -iname '*.wav' \) | wc -l)" ; \
6471 if [ " $$ count" -eq 0 ]; then \
6572 echo " No .wav files found in $( SAMPLES_DIR) " ; \
@@ -69,12 +76,12 @@ convert-samples:
6976 max_vol=" $$ (ffmpeg -nostdin -hide_banner -i " $$ f" -af volumedetect -f null - 2>&1 | awk -F'max_volume: ' '/max_volume:/ {split($$ 2, a, " dB" ); print a[1]}' | tail -n 1)" ; \
7077 if [ -z " $$ max_vol" ] || [ " $$ max_vol" = " -inf" ]; then \
7178 gain_db=" 0.000" ; \
72- echo " Converting + normalizing: $$ f (max=unknown, gain=$$ gain_db dB, target=$$ target_peak_db dBFS)" ; \
79+ echo " Converting + normalizing: $$ f (max=unknown, gain=$$ gain_db dB, target=$$ target_peak_db dBFS, $$ trim_desc )" ; \
7380 else \
7481 gain_db=" $$ (awk -v target=" $$ target_peak_db" -v max=" $$ max_vol" 'BEGIN { printf " %.3f" , (target - max) }')" ; \
75- echo " Converting + normalizing: $$ f (max=$$ max_vol dB, gain=$$ gain_db dB, target=$$ target_peak_db dBFS)" ; \
82+ echo " Converting + normalizing: $$ f (max=$$ max_vol dB, gain=$$ gain_db dB, target=$$ target_peak_db dBFS, $$ trim_desc )" ; \
7683 fi ; \
77- ffmpeg -nostdin -hide_banner -loglevel error -y -i " $$ f" -af " volume=$$ {gain_db}dB" -ac 1 -ar 44100 -c:a pcm_s16le " $$ tmp" ; \
84+ ffmpeg -nostdin -hide_banner -loglevel error -y -i " $$ f" -af " $$ {trim_filter} volume=$$ {gain_db}dB" -ac 1 -ar 44100 -c:a pcm_s16le " $$ tmp" ; \
7885 mv " $$ tmp" " $$ f" ; \
7986 done ; \
8087 fi
0 commit comments