#########################################################################
# File Name: Fetch5ch.sh
# Function :extract 6 1 2 8 channel as a new file from 8channel wave file
# Created Time: Thu 22 Mar 2018 03:27:02 PM CST
#########################################################################
#!/bin/bash
ARGS=2;
E_BADQUIT=127;
E_BADPARAM=65;
if [[ $# -ne ${ARGS} ]];then
????????echo -e "Usage: \n\t `(basename $0 .sh)` wave-file-directory out-file-directory"
????????exit ${E_BADPARAM}
fi
wave_dir=$1;
out_dir=$2;
if [[ ! -d ${wave_dir} ]];then
????????echo "Error : can not find path ${wave_dir}"
????????exit ${E_BADPARAM}
fi
if [[ ! -d ${out_dir} ]];then
????????echo "Error : cannot find out directory."
????????exit ${E_BADPARMA}
else
????????is_empty=`ls ${out_dir} | wc -l`
????????if [[ ${is_empty} -ne 0 ]];then
????????????????echo "Warning : ${out_dir} is not empty"
????????fi
fi
find ${wave_dir}/*.wav > tmp
cat tmp | awk 'BEGIN{FS="/"}{print $NF}' > tmp1
rm tmp
# 檢測輸入的wave數(shù)據(jù)的通道數(shù)目,本腳本設(shè)定的是8
function CheckChannel()
{
wave=$1;
num_ch=`soxi ${wave} | grep -w "Channels" | awk 'BEGIN{FS=":"}{print $NF}'`
if [[ ${num_ch} -lt 8? ]];then
????????return 1;
else
????????return 0;
fi
}
# 本腳本從8通道數(shù)據(jù)抽取5個(gè)通道,對5個(gè)通道重新排列
ch1=6;
ch2=1;
ch3=2;
ch4=8;
ch5=7;
while read line;do
????????name="`echo ${line%%.*}`_valid14.wav"
????????CheckChannel "${wave_dir}/${line}"
????????if [[ `echo $?` -ne 0 ]];then
????????????????echo "Error : channels of ${wave_dir}/${line} is less than 8"
????????????????exit ${E_BADQUIT}
????????fi
????????echo $wave_dir/$line
????????if [[ -f ${out_dir}/${name} ]];then
????????????????echo "Error : ${out_dir} have existed ${name}"
????????????????exit ${E_BADPARAM}
????????fi
????????echo $wave_dir/$line
????????sox ${wave_dir}/${line} ${out_dir}/${name} remix ${ch1} ${ch2} ${ch3} ${ch4} ${ch5}
done < tmp1
rm tmp1
exit $?