Soxを使用したギャップレスリサンプリング

Soxを使用したギャップレスリサンプリング

スムーズに再生する必要がある一連のトラックをリサンプリングするために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ビット以下のファイルを作成するときにディザリングを指定する機能)を追加することは、読者の練習問題のままです。

おすすめ記事