#!/bin/bash
# Version : 0.2
#  Author : Mats Karlsson @ Rebtel
#  Date   : 2006-05-09
#  Scoop  : Convert voice prompts from wav format to Asterisk (www.asterisk.org)
#           native formats.
#           Converts to G.729, G.711 Alaw/Ulaw, SLN and GSM.
#
# Requirements:
#         res_conv (http://redice.krisk.org/)
#         Asterisk (Created on version 1.2.7.1)
#         G.729 codec installed in Asterisk
#         sox
#
# Reference:
#   http://redice.krisk.org/
#
# Thanx to Kristian Kielhofner for the help and inspiration to create this script.
#
# Modified by Kristian Kielhofner <kris@krisk.org> to support multiple formats
# more easily

SourceDir=/root/sounds			# Copy wav files to this directory
DestinationDir=/root/sounds/conv	# /var/lib/asterisk/sounds/rebtel
Formats="g729 alaw ulaw gsm ilbc"	# Convert to these formats (seperate by spaces)

# Convert Wav to SLN and resample wav's to 8KHz that is demanded by Asterisk

for X in $SourceDir/*.wav
do
	Y=`basename $X .wav`
	echo "Converts $Y"
	sox $SourceDir/$Y.wav -t raw -r 8000 -s -w -c 1 $DestinationDir/$Y.sln resample -ql
done


for X in $DestinationDir/*.sln
do
	Y=`basename $X .sln`
	echo "Converts $Y"

	for i in $Formats
	do
		# Convert SLN to $FORMAT with help of asterisk and convert (se ref)
		asterisk -rx "convert $DestinationDir/$Y.sln $DestinationDir/$Y.$i"
	done
done

