#!/bin/bash







WAV=big.wav

sox WAV/8_Channel_ID.wav WAV/Nums_7dot1_24_48000.wav 	\
	${WAV}

DATAFILE=datas.out
INCFILE=essai.inc
POVDIM=" -w1024 -h768 "


# ---------------------------------------------
make n-pistes
error=$?
if [ $error -ne 0 ]
then
	echo "error make n-pistes = " $error
	exit 1
fi


./n-pistes -v -i ${WAV} > ${DATAFILE}
printf "\tplotting\n" ; ./plot.sh 

# ---------------------------------------------

make downmixer
error=$?
if [ $error -ne 0 ]
then
	echo "error make downmixer = " $error
	exit 1
fi

./downmixer -v -i ${WAV}

# ---------------------------------------------

make essai
error=$?
if [ $error -ne 0 ]
then
	echo error make essai = $error
	exit 1
fi

NBFRAMES=$(wc -l ${DATAFILE} | cut -d ' ' -f 1)

echo "IL Y A " $NBFRAMES " FRAMES"

DEBUT=0 

for idx in $(seq ${DEBUT} $(( NBFRAMES - 1 )) ) ; do

	printf "\nxxxxxxxxxxxxxxxxxxxxxxxxxx %d\n" $idx

	./essai -f $idx > ${INCFILE}

	outpng=$(printf "out/%05d.png" $idx)
	echo pov output to $outpng

	povray -iessai.pov			\
		${POVDIM} +q9 +a -d -v -o${outpng} 2> err.txt

	if [ $? -ne 0 ] ; then
		echo "erreur raytracing"
		tail err.txt
		echo ; sleep 5 ; break
		exit 2
	fi
	echo "eol"

done

# ---------------------------------------------

WAV="downmixed.wav"

ffmpeg 	-y -r 25			\
	-nostdin			\
	-loglevel error			\
	-f image2 -i out/%05d.png	\
	-i $WAV -ac 2			\
	-c:v libx264			\
	-pix_fmt yuv420p		\
	essai-8pistes.mp4

echo ENCODING DONE

echo "     end of " $0
