#!/bin/bash 

pcapfile=$1
rtpfolder=`dirname $pcapfile`

rtpbreak -g -d $rtpfolder -r $pcapfile

streams=(`find $rtpfolder -type f -name *.raw | sort -t'.' -n -k3`)

for ((i=0; i<${#streams[*]}; i++))
do
    firstfilename=`basename ${streams[$i]} '.raw'`
    sox -r8000 -c1 -t ul ${streams[$i]} -t wav $rtpfolder/$firstfilename.wav
    j=$i
    let i=$i+1
    if [ "$i" -lt "${#streams[*]}" ];
    then
        secondfilename=`basename ${streams[$i]} '.raw'`
        sox -r8000 -c1 -t ul ${streams[$i]} -t wav $rtpfolder/$secondfilename.wav
        # Uncomment to join consecutive files together, could be wrong if a stream is missing
        #sox -m $rtpfolder/$firstfilename.wav $rtpfolder/$secondfilename.wav $rtpfolder/joint-$firstfilename-$i.wav
    fi

done
