#! /bin/bash
# Last edited on 2023-02-23 09:45:09 by stolfi

# Extracts a segment from a ".au" sound file.

# Given 3 numbers {start} {stop} {den}
# the scrip read an ".au" file from stdin and 
# whites to {stdout} the section from position 
# {start/den} to position {stop/den}, where 
# position 0 is the beginning of the file and
# position 1 is the end of the file.

start="$1"; shift
stop="$1"; shift
den="$1"; shift

tmp="/tmp/$$"
tfile="${tmp}.au"

cat > ${tfile}

# get the file's duration: 
dur=`soxi -D ${tfile}`

tstart=`echo "${start} * ${dur} / ${den}" |  bc -lq`
tstart=`printf "%.3f" "${tstart}"`

tstop=`echo "${stop} * ${dur} / ${den}" |  bc -lq`
tstop=`printf "%.3f" "${tstop}"`

echo "${dur}" "${tstart}" "${tstop}" 1>&2

sox -V ${tfile} -t au - trim ${tstart} =${tstop}

rm -f ${tfile}

 
