Monitor stereo-example

Description

The following example will show how the Monitor application can be used to record phone conversations in stereo, ie. each party of the phone call represents one channel of a stereo recording. The recording will be compressed in MP3.
This example requires the support of options within the Monitor application, version 1.0.7 is ok, but also earlier versions might work.

Alternative

As of Asterisk 1.2, MixMonitor is available and mixes the wav files internally. if you just want the file mixed rather than two separate files, and don't need the stereo, use MixMonitor. If you want the file in two channels (left == in, right == out) and/or converted to mp3, continue reading.

Example

In extensions.conf the variable MONITOR_EXEC is defined, this script will be used to combine *-in.wav and *-out.wav to a stereo MP3-file. In context [incoming] the variable CALLFILENAME is defined, the fileaname shall consist of the calling party number plus a time stamp seperated by year and month. The prefix "incoming" shall indicate that this is an incoming call and store the file in the incoming folder.

extensions.conf

[globals]
; script to be executed when monitoring has been finished
MONITOR_EXEC=/usr/local/bin/2wav2mp3

; uncomment this line if you are using Ogg Vorbis
;MONITOR_EXEC=/usr/local/bin/2wav2ogg

[incoming]
exten => s,1,SetVar(CALLFILENAME=incoming/${TIMESTAMP:0:4}/${TIMESTAMP:4:2}/${TIMESTAMP}-${CALLERIDNUM})
exten => s,2,Monitor(wav,${CALLFILENAME},m)
(...)


SoX v14

SoX 14.0.1 supports merging channels natively. You can do:


$SOX -M $LEFT.wav $RIGHT.wav $OUT.mp3



MP3 Script

The shell-script uses sox and soxmix to create a stereo-file out of two mono files. Newer versions of sox(mix) support mp3, but using an external encoder gives more flexibilty (eg. variable bit rate encoding, id3-tags). You can also find this script at zlabinger.at/asterisk.



#!/bin/sh
# 2wav2mp3 - create stereo mp3 out of two mono wav-files
# source files will be deleted
#
# 2005 05 23 dietmar zlabinger http://www.zlabinger.at/asterisk
#
# usage: 2wav2mp3 <wave1> <wave2> <mp3>
# designed for Asterisk Monitor(file,format,option) where option is "e" and
# the variable
# MONITOR_EXEC/usr/local/bin/2wav2mp3


# location of SOX (SOXMIX no longer needed)
# (set according to your system settings, eg. /usr/bin)
SOX=/usr/local/bin/sox
# lame is only required when sox does not support liblame
#LAME=/usr/local/bin/lame


# command line variables
LEFT="$1"
RIGHT="$2"
OUT="$3"

#test if input files exist
test ! -r $LEFT && exit 21
test ! -r $RIGHT && exit 22

# convert mono to stereo, adjust balance to -1/1
# left channel
$SOX $LEFT -c 2 $LEFT-tmp.wav pan -1
# right channel
$SOX $RIGHT -c 2 $RIGHT-tmp.wav pan 1

# combine and compress
# this requires sox to be built with mp3-support.
# To see if there is support for Mp3 run sox -h and
# look for it under the list of supported file formats as "mp3".
$SOX -m -v 1 $LEFT-tmp.wav -v 1 $RIGHT-tmp.wav $OUT.mp3

# in case an old version of sox is used, encoding
# can be done afterwards
#$SOXMIX -v 1 $LEFT-tmp.wav -v 1 $RIGHT-tmp.wav -v 1 $OUT.wav
#$LAME -S -V7 -B24 --tt $OUT --add-id3v2 $OUT.wav $OUT.mp3


#remove temporary files
test -w $LEFT-tmp.wav && rm $LEFT-tmp.wav
test -w $RIGHT-tmp.wav && rm $RIGHT-tmp.wav
test -w $OUT.wav && rm $OUT.wav

#remove input files if successfull
test -r $OUT.mp3 && rm $LEFT $RIGHT
# eof


Ogg Vorbis Script

This shell-script also uses sox and soxmix to create a stereo-file out of two mono files. This version of the script has been modified to output Ogg Vorbis encoded files instead of mp3s. This method uses less CPU and creates slightly larger files than the mp3s. It also does not require anything other than sox and soxmix.


#!/bin/sh -v
# 2wav2ogg - create stereo ogg out of two mono wav-files
# source files will be deleted
#
# 2005 05 23 dietmar zlabinger http://www.zlabinger.at/asterisk
# 2006 09 26 Modified by Cherebrum & utsl to do ogg instead of mp3
#
# usage: 2wav2ogg <wave1> <wave2> <ogg>
# designed for Asterisk Monitor(file,format,option) where option is "e" and
# the variable
# MONITOR_EXEC/usr/local/bin/2wav2ogg


# location of SOX and SOXMIX
# (set according to your system settings, eg. /usr/bin)
SOX=/usr/bin/sox
SOXMIX=/usr/bin/soxmix


# command line variables
LEFT="$1"
RIGHT="$2"
OUT=`echo $3|sed s/.wav//`

echo $1 $2 $3 >>/tmp/2wav2ogg.log

#test if input files exist
test ! -r $LEFT && exit
test ! -r $RIGHT && exit

# convert mono to stereo, adjust balance to -1/1
# left channel
/usr/bin/nice -n 10 $SOX $LEFT -c 2 -t wav $LEFT-tmp.wav pan -1
# right channel
/usr/bin/nice -n 10 $SOX $RIGHT -c 2 -t wav $RIGHT-tmp.wav pan 1


# mix and encode to ogg vorbis
/usr/bin/nice -n 10 $SOXMIX -v 1 -t wav $LEFT-tmp.wav -v 1 -t wav $RIGHT-tmp.wav -v 1 -t vorbis $OUT.ogg

#remove temporary files
test -w $LEFT-tmp.wav && rm $LEFT-tmp.wav
test -w $RIGHT-tmp.wav && rm $RIGHT-tmp.wav
test -w $OUT.wav && rm $OUT.wav

#remove input files if successfull
test -r $OUT.ogg && rm $LEFT $RIGHT
# eof

Remarks

While recording phone conversations can be very useful for debugging purposes, it can be rather problematic in a production environment - seek legal advice first. ..

When listening to the recording with headsets, a reduced channel seperation (pan -0.5/0.5) might be apropriate. The original wav-format was limited to max 2 GBytes,
extremely long phonecalls might break this format and cause this script to fail. The temporary wav-files are twice as big as the original files. In case these limitations becomes problematic, recording could be done in GSM-FR (ca. 13kBit/s) and not in uncompressed PCM. While MP3 is a good compromise in between portable format, good quality and good compression, different formats like Ogg Vorbis might be more effective. Another option is to leave recordings in good quality for a shorter period (eg. one week) and recompress them later to very low data-rates (eg. 2.4 kBit/s).



References



Created by: dzlabing, Last modification: Fri 24 of Oct, 2008 (13:52 UTC) by atis


Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+

Page Changes | Comments

 

Featured -

Search: