#! /sbin/sh
#################################################
# Aiff convert & make Seqsfile Bankfile
#
# (Please run this program in 'Voice' directory)
#################################################

### define ###
aiffpath=/disk2/inagaki/TFox64/Voice/aiff
aifcpath=/disk2/inagaki/TFox64/Voice/Aifc
seqspath=/disk2/inagaki/TFox64/Voice/Seqs
bankpath=/disk2/inagaki/TFox64/Voice/Banks
template=/disk2/inagaki/TFox64/Voice/template.bcc
codebook=/disk2/inagaki/TFox64/Voice/lib/aiff/fox_voice_codebook.tbl
awkpath=/disk2/inagaki/TFox64/Voice/lib/awk/


### main ###
aiffpath=$aiffpath/voice$1
aifcpath=$aifcpath/voice$1
if [ ! -d $aifcpath ]
then
    mkdir $aifcpath
fi
src3 -n $aiffpath/fox_$1[0-9][0-9][0-9].y.44.c4.aiff 6000
ls -1 $aiffpath/fox_$1[0-9][0-9][0-9].y.6.c4.aiff > list
if [ -f $awkpath/exc.awk ]
then
	nawk -f $awkpath/exc.awk -v n=60 list
else
	echo $0: exc.awk が見つかりません。
	exit
fi
aiffdivide.fox -c3000 -t0.1 $aiffpath/fox_$1[0-9][0-9][0-9].y.6.c4.aiff @cs4
aifcup -C2 $codebook -d $aifcpath $aiffpath/fox_$1*.y.6.c4.aiff

## seqs 
if [ -r $awkpath/tableset.awk ]
then
    nawk -v path=$awkpath -v scene=$1 -v seqspath=$seqspath\
    '{idx=index($0,"fox_");\
    num=substr($0,idx+6,3);\
    printf("Voice seqs %d に %d を登録中\n",scene,num);\
    if (scene < 10){\
	awk=sprintf("nawk -v num=%d -v scene=0%d -f %s/tableset.awk %s/fox_voice0%d.mus > tmp.mus",
		    num,scene,path,seqspath,scene);\
	mv=sprintf("mv tmp.mus %s/fox_voice0%d.mus",seqspath,scene);\
    }\
    else {\
	awk=sprintf("nawk -v num=%d -v scene=%d -f %s/tableset.awk %s/fox_voice%d.mus > tmp.mus",
		    num,scene,path,seqspath,scene);\
	mv=sprintf("mv tmp.mus %s/fox_voice%d.mus",seqspath,scene);\
    }\
    system(awk);\
    system(mv);}' list 
else
    echo $0: tableset.awk が見つかりません。
    exit
fi
sed '/^_v_[01][0-9][0-9][0-9][0-9]$/,$d' $seqspath/fox_voice$1.mus > tmp.seqs
cat tmp.seqs aiffdivide.seqs > $seqspath/fox_voice$1.mus
echo シーケンス登録完了


## bank
bankfile=$bankpath/fox_voice$1.bcc
nawk -v scene=$1 '/\/\+SCENE\+\//{sub(/\/\+SCENE\+\//,scene)} \
		  /\/\+DATE\+\//{printf(";       ");system("date");next} \
		  /\/\+WAVELIST\+\//{exit} \
		    {print $0}' $template > $bankfile
echo "@WAVELIST\n @PATH \"../$aifcpath/" >> $bankfile
cat aiffdivide.samp >> $bankfile
sed '/\/\+WAVELIST\+\//,/\/\+WAVEEND\+\//!d;/\/\+WAVELIST\+\//d;/\/\+WAVEEND\+\//d' $template >> $bankfile
echo "@WAVEEND\n\n" >> $bankfile
sed '/@ENVLIST/,/@ENVEND/!d' $template >> $bankfile
echo "\n@INSTLIST" >> $bankfile
cat aiffdivide.inst >> $bankfile
sed '/\/\+INSTLIST\+\//,/\/\+INSTEND\+\//!d;/\/\+INSTLIST\+\//d;/\/\+INSTEND\+\//d' $template >> $bankfile
echo "@INSTEND\n\n@BANKLIST" >> $bankfile
sed '/\/\+BANKLIST\+\//,/\/\+BANKEND\+\//!d;/\/\+BANKLIST\+\//d;/\/\+BANKEND\+\//d' $template >> $bankfile
echo "@BANKEND" >> $bankfile
if [ -r $awkpath/makebank.awk ]
then
    nawk -v scene=$1 -v path=$AWKLIB -v bankpath=$bankpath -f $awkpath/makebank.awk list
else
    echo $0: makebank.awk が見つかりません。
    exit
fi

## def
makedefine $1 $bankpath

rm list tmp.bcc tmp.seqs aiffdivide.inst aiffdivide.seqs aiffdivide.samp
echo 終了しました。
