我正在尝试使用 sox 重新采样一系列应该无缝播放的曲目。如果我单独对每个轨道进行重新采样,由于单独的重新采样不太对齐,有时会在轨道边界处发出咔嗒声。该解决方案在概念上似乎很简单:连接所有轨道,将它们重新采样为一个单元,然后再次分割它们。但是,我不确定如何以自动化方式进行此操作。连接步骤很简单(只需将所有文件传递给单个 sox 调用),但如何以与原始曲目相同的持续时间再次分割结果?
答案1
我最终制作了一个脚本来处理这个问题:
#!/usr/bin/env bash
set -o errexit -o pipefail -o nounset
# Can be changed to point to, e.g., a DSD-enabled SoX build.
SOX="sox"
TRACKS=()
while [[ $# -gt 0 ]]; do
case $1 in
-b|--bits)
# Use the specified number of bits-per-sample for the output
BITS="$2"
shift 2
;;
-o|--out)
# Output template for resampled tracks
OUT="$2"
shift 2
;;
-r|--rate)
# The sample rate of the output
RATE="$2"
shift 2
;;
-*|--*)
echo "Unknown option $1" >&2
exit 1
;;
*)
TRACKS+=("$1") # positional arg
shift
;;
esac
done
if [[ -z ${OUT+x} ]]; then
echo "Option --out is required" >&2
exit 1
fi
if [[ -z ${RATE+x} ]]; then
echo "Option --rate is required" >&2
exit 1
fi
if [[ ${#TRACKS[@]} -eq 0 ]]; then
echo "No input files provided" >&2
exit 1
fi
if [[ -n ${BITS+x} ]]; then
BITS_ARG=(-b "$BITS")
else
BITS_ARG=()
fi
if [[ ${#TRACKS[@]} -eq 1 ]]; then
TRIM_ARGS=()
else
# Get lengths of all tracks except the last one
LENGTHS=($("$SOX" --i -D "${TRACKS[@]:0:${#TRACKS[@]}-1}"))
TRIM_ARGS=(trim 0 "${LENGTHS[0]}" : newfile)
for length in "${LENGTHS[@]:1}"; do
TRIM_ARGS+=(: trim 0 "$length" : newfile)
done
fi
# --guard resamples to a temporary file with added headroom, then writes the
# output as close to the original level as possible without clipping.
# Remove to write directly to the output files at the originally level, with the
# possibility of some samples clipping if the input has insufficient headroom.
"$SOX" --guard "${TRACKS[@]}" "${BITS_ARG[@]}" -t wav - rate -v "$RATE" | "$SOX" - "$OUT" "${TRIM_ARGS[@]}"
使用示例:
将 flac 文件重新采样到 48kHz,每个样本具有相同的位数(不复制标签):
$ resample.sh -o output.flac -r 48k ../96kHz/*.flac
将 DSD 音频转换为 48kHz、24 位 FLAC(需要支持 DSD 的 SoX):
$ resample.sh -o output.flac -r 48k -b 24 ../DSD/*.dsf
输出文件将被命名为output001.flac、output002.flac等。
向脚本添加附加选项(例如,在创建 16 位或更少位文件时指定抖动的能力)留给读者作为练习。