#!/sbin/sh
#################################################
# Voice convert & make Seqsfile Bankfile
#################################################

### define ###
aiffpath=$FOXVOICE_ROOT/aiff
aifcpath=$FOXVOICE_ROOT/Aifc
seqspath=$FOXVOICE_ROOT/Seqs
bankpath=$FOXVOICE_ROOT/Banks
template=$FOXVOICE_ROOT/template.bcc
codebook=$FOXVOICE_ROOT/lib/aiff/fox_voice_codebook.tbl
awkpath=$FOXVOICE_ROOT/lib/awk/

### main ###
for x
do
    if [ ! $x = 99 ]
    then
        aiffvoicepath=$aiffpath/voice$x
    aifcvoicepath=$aifcpath/voice$x
    if [ ! -d $aifcvoicepath ]
    then
	mkdir $aifcvoicepath
    fi

    ls -1 $aiffvoicepath/fox_$x[0-9][0-9][0-9].y.44.c4.aiff > list
#    sed -n '/06025/p;/06027/p;/06029/p;/06041/p;/06042/p;/17130/p;/17140/p;/17300/p;/17350/p;/16120/p;/16130/p;/16135/p;/16140/p;/16150/p;/16160/p;/16165/p;/16185/p;/20084/p;' list > list_cat$x
#    sed '/06025/d;/06027/d;/06029/d;/06041/d;/06042/d;/17130/d;/17140/d;/17300/d;/17350/d;/16120/d;/16130/d;/16135/d;/16140/d;/16150/d;/16160/d;/16165/d;/16185/d;/20084/d;' list > list_not_cat
    rm $aiffvoicepath/fox_$x[0-9][0-9][0-9]*.y.[1-9].c4.aiff
    rm $aifcvoicepath/*.aifc
    exefiles command='src3 -n $ 7000' list
#    exefiles command='src3 -n $ 9000' list_cat$x
    
    ls -1 $aiffvoicepath/fox_$x[0-9][0-9][0-9].y.[1-9].c4.aiff > list
    exefiles command='exc $ $ 400 -a' list
    aiffdivide.fox -c1000 -t0.1 $aiffvoicepath/fox_$x[0-9][0-9][0-9].y.[1-9].c4.aiff @cn4
    aifcup -C2 $codebook -d $aifcvoicepath $aiffvoicepath/fox_$x*.y.[1-9].c4.aiff
    
    ## seqs
    musfile=$seqspath/fox_voice$x.mus
    if [ ! -f $musfile ]
    then
	echo $0: $musfile が見つかりません。
	exit
    fi
    echo "シーケンス登録中"
    cp $musfile $musfile.bak
    nawk -f $seqspath/fox_voice.awk -f $seqspath/fox_voice$x.awk aiffdivide.seqs > $musfile
    nawk -v path=$awkpath -v scene=$x -v musfile=$musfile \
    '{
		scene=sprintf("%02d", scene);
		idx=index($0,"fox_");
		num=substr($0,idx+6,3);
		label=substr($0,idx+4,5);
		label=sprintf("_v_%s",label);
#	awk=sprintf("tableset num=%d label=%s %s > tmp.mus",num,label,musfile);
		awk=sprintf("tableset %s %s", scene,num);
		system(awk);
    }' list
    if [ $x = 00 ]
    then
	sed 's/_v_novox/_v_comed/' $musfile | sed 's/_v_novox/_v_clear/' | sed 's/_v_novox/_v_test1/' | sed 's/_v_novox/_v_comst/' > tmp.mus
	echo end
	mv tmp.mus $musfile
    fi
    
#    sed '/^_v_[012][0-9][0-9][0-9][0-9]$/,$d' $seqspath/fox_voice$x.mus > tmp.seqs
#    cat tmp.seqs aiffdivide.seqs > $seqspath/fox_voice$x.mus
    sed '/^_v_000[2345]0$/{N;P;d;}' $seqspath/fox_voice$x.mus > tmp.seqs
    cp tmp.seqs $seqspath/fox_voice$x.mus
    sed '/^_v_210[12356]0$/{N;P;d;}' $seqspath/fox_voice$x.mus > tmp.seqs
    cp tmp.seqs $seqspath/fox_voice$x.mus
    sed '/^_v_2022[0123]/{N;a\
    call, _v_danger
    }' $seqspath/fox_voice$x.mus > tmp.seqs
    cp tmp.seqs $seqspath/fox_voice$x.mus
    echo "シーケンス登録完了"


    ## bank
    bankfile=$bankpath/fox_voice$x.bcc
    if [ -f $bankfile ]
    then
        mv $bankfile $bankfile.bak
    fi
    echo "バンク登録中"
    nawk -v scene=$x '/\/\+SCENE\+\//{sub(/\/\+SCENE\+\//,scene)} \
		  /\/\+DATE\+\//{printf(";       ");system("date");next} \
		  /\/\+WAVELIST\+\//{exit} \
		    {print $0}' $template > $bankfile
    echo "@WAVELIST\n @PATH \"../Aifc/voice$x/\"" >> $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/banklist.awk ]
    then
	nawk -v scene=$x -v path=$awkpath -v bankfile=$bankfile \
	'/fox_/{
        	idx=index($0,"fox_");
		num=substr($0,idx+6,3);
        	label=substr($0,idx+4,5);
        	label=sprintf("_INST_%s",label);
        	banklist=sprintf("nawk -v num=%d -v label=%s -f %s/banklist.awk %s > tmp.bcc",num,label,path,bankfile);
        	mv=sprintf("mv tmp.bcc %s",bankfile);
        	system(banklist);
        	system(mv);
	}' list
    else
	echo $0: banklist.awk が見つかりません。
	exit
    fi
    if [ -r $awkpath/banksort.awk ]
    then
	nawk -f $awkpath/banksort.awk $bankfile > tmp.bcc
	mv tmp.bcc $bankfile
    fi
    echo "バンク登録完了"
    
    ## def
    makedefine $x $bankpath $seqspath
    
    rm list tmp.seqs aiffdivide.inst aiffdivide.seqs aiffdivide.samp
    echo "終了しました。"

    fi
done
