#!/bin/bash

#This script runs the python speech filter over files in the raw_recordings directory.
#For the case of wav recordings, it converts them to mp3 and moves succesful files together with a text file showing scores to a staging directory
#2 temp folders were used for testing. remember to uncomment production lines when in production, and comment test lines

#raw_recordings=$1"temp_raw" #testing
raw_recordings=$1"raw_recordings" #production

#score_test_success=$1"temp_success" #testing
score_test_success=$1"score_test_success" #production

score_test_fail=$1"score_test_fail"
raw_path=$raw_recordings"/*.wav"

#find /home/radio/radio/recordings/score_test_success/*.wav -type f -mmin +10  -printf "%f\n" | cut -d. -f -4
FILE_LIST=$(find $raw_path -type f -mmin +10 -printf "%f\n" | cut -d. -f -4 )

for file in $FILE_LIST
do
	echo "speech anylsis for ==>" $file
	raw_wav_file=$raw_recordings"/"$file".wav"
	wav_file=$file".wav"
	sox -r 16k -e unsigned-integer -b 16 -c 1 -t raw $raw_wav_file $wav_file
	mp3_file=$file".mp3"
	#ffmpeg -i $file  temp_audio.wav
	ffmpeg -i $wav_file  $mp3_file
	duration=$(mp3info -p "%m.%02s\n" $mp3_file )
	test_var=$(python /home/radio/radio/pyAudioAnalysis/audioAnalysis.py classifyFile -i $wav_file --model svm --classifier /home/radio/radio/pyAudioAnalysis/data/svmSM | grep speech )
	speech_test=$(echo $test_var | cut -d" " -f 2)
	echo $speech_test","$duration
	if [ "$(echo $speech_test '>' 0.29 | bc -l)" -eq 1 -a "$(echo $duration '>' 3.0 | bc -l)" -eq 1 ]
	then
		echo "$speech_test,$duration" > $mp3_file".txt"
		mv $mp3_file $mp3_file".txt" $score_test_success
		rm $wav_file #production
		rm $raw_wav_file #production
	else
		#echo "$speech_test,$duration" > $mp3_file".txt" #testing
		#mv $mp3_file $mp3_file".txt" $score_test_fail #testing
		rm $raw_wav_file #production
		rm $mp3_file #production
		rm $wav_file #production
	fi
done

