Mixing Your Recordings Your Way With Asterisk

Quality Assurance with Asterisk AudioUsing the Asterisk Monitor application is a great way to record your calls. Sometimes, though, you have specific requirements for how the recordings are mixed, processed, or otherwise handled. Asterisk does give you a way to do this. An example that came up recently was that the Q/A department needed to be able to distinguish the two sides of the call. By default, Monitor records both the inbound and outbound audio, then mixes the two channels together into a mono recording. If the caller and call center agent had similar voices, this was tough to do. If there were audio or voice problems, it would be hard to tell which side was having the issue. They were looking for a way to have their call center software for Asterisk do this.

Monitor itself uses sox to mix the two channels into one. Back in the day, it was soxmix, but that functionality was merged into sox years ago with the -m flag. It mixes then deletes the two separate audio files (helpfully labelled -in and -out). If you want to have something else happen, you do have options. The variable MONITOR_EXEC will be checked before mixing. If it is set, whatever command is there will be run with 3 arguments: the names of the two audio files to be mixed, and the filename to mix the audio into (the same as the legs of the call, minus the -in or out part). Note that Asterisk will not delete the original two legs of the recording in this case. You are responsible for the cleanup.

So how do we get Asterisk to mix our recording into stereo? Since the -M flag mixes the audio into stereo, you could set MONITOR_EXEC to “/usr/bin/sox -M“. That works, but it leaves the two legs lying around. Something a little more complex is needed. One approach is to create a shell script that does what you need. If you create a file mix-stereo.sh in /root and make it executable, you can give it the contents:

#!/bin/bash

SOX="/usr/bin/sox -M"
RM="/bin/rm"

IN="$1"
OUT="$2"
DESTINATION="$3" 
 
$SOX -M $IN $OUT $DESTINATION && $RM $IN $OUT

then set MONITOR_EXEC=”/root/mix-stereo.sh”

When your call is done, you should find a single stereo recording for the call in /var/spool/asterisk/monitor

If you need mp3 recordings, and your version of sox supports it (and you’re comfortable with the licensing and patent issues surrounding encoding mp3s), you could do something like this instead:

#!/bin/bash

SOX="/usr/bin/sox"
IN="$1"
OUT="$2"
DESTINATION=${3%.*}
EXTEN="mp3"

$SOX -M $IN $OUT $DESTINATION.$EXTEN

You could also use something similar to transcode your recordings to any other format. To generate test files for mixing, what I occasionally do is set

MONITOR_EXEC=/bin/true

This returns a success result to Asterisk, while doing nothing. You can then go into /var/spool/asterisk/monitor and pull out the -in and -out files for experimentation. Since you can call it with the same arguments asterisk does, you can run the script with:

/root/mix-stereo.sh randomname-in.wav randomanme-out.wav randomname.wav

with randomname replacing the actual name of the files. Do keep a copy of the in and out files so you can test the output and rerun if needed.