FreeSWITCH AMD with mod_audio_stream and one Lua script
LineVaults FreeSWITCH AMD is a Lua script, linevaults_amd.lua, that streams the answered leg through mod_audio_stream and writes the verdict back as channel variables. Your dialplan reads AMDSTATUS as HUMAN, MACHINE or FALLBACK, with AMDCAUSE beside it, in about 2 seconds.
1. Background
FreeSWITCH has no counterpart to Asterisk's AMD(). The usual options are mod_avmd, which listens for the voicemail beep at the end of a greeting rather than deciding who answered; third-party modules built on timing rules; or detection offered by a carrier on its own calls. LineVaults gives FreeSWITCH systems the same detection VICIdial users get: it tells a live "Hello?" from a recording, identifies call-progress tones such as busy, SIT, fax and ringback, and spots recorded greetings played to many numbers. The technology page describes each stage.
2. Components
| Component | Source | Role |
|---|---|---|
mod_lua | Ships with FreeSWITCH | Runs the script |
mod_audio_stream | Open-source project, installed by you | Sends the call's audio to a WebSocket and raises events for the replies |
linevaults_amd.lua | LineVaults, download | Starts and stops the stream, waits for the verdict, sets the variables; no other dependencies |
| API key | Your dashboard | Sent as a request header on the WebSocket handshake |
License note: mod_audio_stream is maintained independently of LineVaults, and we don't distribute it. Its community edition is licensed AGPL-3.0 with a linking exception for FreeSWITCH; review those terms against your deployment.
3. Call sequence
- AnswerThe callee picks up and the dialplan runs
linevaults_amd.lua. - StreamThe script starts
mod_audio_stream: caller audio, 8 kHz mono, over TLS. - DecideAbout 2 s of audio, or up to 3.2 s for an unclear call, then one JSON verdict.
- EventThe verdict comes back to FreeSWITCH as a
mod_audio_stream::jsonevent. - RouteThe script sets the variables, stops the stream and returns to the dialplan.
4. Installation
4.1 Load mod_audio_stream
Follow the module's own README to get it onto the server; the project provides packages for current Debian releases running SignalWire's FreeSWITCH builds. Load it into the running switch, and add it to the module list so it survives a restart:
fs_cli -x "load mod_audio_stream"
# autoload_configs/modules.conf.xml, so it loads at startup:
# <load module="mod_audio_stream"/>
4.2 Place the script
cd /usr/share/freeswitch/scripts # /etc/freeswitch/scripts on some installs
curl -fsSLO https://www.linevaults.com/assets/linevaults_amd.lua
4.3 Create a key
Open a free account and create a key on the WS keys page. One key per FreeSWITCH server keeps each server's calls apart in the call logs.
5. Dialplan
Answered outbound calls enter a detection extension, and a transfer branches on the outcome. Add this to dialplan/default.xml or your own context:
<extension name="amd_check">
<condition field="destination_number" expression="^amd_check$">
<action application="lua" data="linevaults_amd.lua wss://amd.linevaults.com/ YOUR_KEY 4500"/>
<action application="transfer" data="route_${AMDSTATUS} XML default"/>
</condition>
</extension>
<extension name="route_human">
<condition field="destination_number" expression="^route_HUMAN$">
<action application="transfer" data="agents XML default"/> <!-- your agent queue -->
</condition>
</extension>
<extension name="route_machine">
<condition field="destination_number" expression="^route_MACHINE$">
<action application="hangup"/> <!-- or play a voicemail message first -->
</condition>
</extension>
<extension name="route_fallback">
<condition field="destination_number" expression="^route_FALLBACK$">
<action application="transfer" data="agents XML default"/> <!-- or the detection you used before -->
</condition>
</extension>
Channel variables in action data are expanded when the action runs, so route_${AMDSTATUS} resolves after the script has finished: route_HUMAN, route_MACHINE or route_FALLBACK. For finer routing, branch on AMDCAUSE as well:
| Variable | Values |
|---|---|
AMDSTATUS | HUMAN, MACHINE, FALLBACK |
AMDCAUSE (HUMAN / MACHINE) | HUMAN, MACHINE, SCREENING, SILENCE, TONE, BUSY, UNKNOWN |
AMDCAUSE (FALLBACK) | AUTH, SUSPENDED, QUOTA, CONCURRENCY, CPS, NOSERVER |
6. Placing outbound calls
Originate each call with the detection extension as its destination. FreeSWITCH enters that extension at the moment of pickup, so streaming starts with the first word:
originate {origination_caller_id_number=15550002222}sofia/gateway/trunk_a/15557654321 amd_check XML default
Dialers driving FreeSWITCH over the event socket can send the same command with bgapi originate.
7. Verification
| Check | Expected result |
|---|---|
fs_cli -x "uuid_audio_stream" | The module's usage text, not "command not found" |
| Dial a mobile you own and leave it unanswered until voicemail picks up | A console line starting linevaults_amd: with MACHINE |
| Call it again and answer | HUMAN |
| Open the dashboard's call logs | Two entries, each with its cause, elapsed time and a playable clip of what was analyzed |
8. Failure modes
The script follows the same rule as every LineVaults client: FALLBACK only before anything has been streamed, a machine verdict for anything that goes wrong afterwards.
| Condition | AMDSTATUS / AMDCAUSE |
|---|---|
mod_audio_stream not loaded | FALLBACK / NOSERVER |
| Connection error, or no connection within 1 second | FALLBACK / NOSERVER |
| Key refused, account suspended, plan limit reached | FALLBACK / AUTH, SUSPENDED, QUOTA, CONCURRENCY or CPS |
| Timeout or disconnect after audio started | MACHINE / UNKNOWN |
FALLBACK answers are never billed. Full definitions are in the AMD WebSocket API reference.
9. Notes
FusionPBX and other FreeSWITCH-based platforms
The two prerequisites are the same everywhere: the module must load, and you must be able to create an extension. FusionPBX lets you create dialplan entries in its web interface.
Exactly which audio leaves the server
One channel, the called party's voice, at 8 kHz and 16 bits per sample. Streaming begins at pickup and is shut off the moment a verdict comes back, which is normally between 2 and 3.2 seconds in.
How the key travels
The script puts it in STREAM_EXTRA_HEADERS, so mod_audio_stream presents it in the TLS-protected handshake as a Bearer token. The stream URL never contains it.
Using mod_avmd alongside
Yes, the two do different jobs. Once LineVaults has said MACHINE, mod_avmd can wait for the beep so a recorded message starts at the right moment.
A different streaming module
Any component able to push 8 kHz, 16-bit PCM over a WebSocket can talk to the service; the AMD WebSocket API reference defines the messages. Asterisk systems use the EAGI client instead.
Put a verdict on every answered call
Run it on one campaign next to your current AMD and compare. If the service can't help, your dialer falls back to Asterisk AMD() on its own.