tSIP: playing audio file after answering the call

Script playing wave automatically at the beginning of answered call (for incoming calls only) and then switching to normal call. Small modification of playing audio on demand example.

Create wave file (L16 mono, sampling rate is not important for current tSIP version) with announcement and half a second of silence at the end. Copy file to application directory, update file name and length (not counting extra silence) in seconds in this script. Assign script to "on call state" event, enable auto answer, configure account, etc.

-- put in application folder, L16 uncompressed
local audio_file_name = "announcement_01.wav"	
-- seconds, wave file must be slightly longer than this
local audio_file_length = 2.9   

local callState = GetCallState()
if callState ~= 6 or IsCallIncoming() == 0 then
    return
end

print("Incoming call is answered\n") 

function play(audio_file_name, audio_file_length)
    Beep(800, 300)  -- audible feedback
    local btnId = 15
    SetButtonCaption(btnId, "Playing...");	-- show also on button
    SwitchAudioSource("aufile", audio_file_name)
    -- checking if callee did not hang up
    for i=1, audio_file_length*10, 1
    do
        Sleep(100)  -- ms, hence *10 above
        call_state = GetCallState()
        if call_state == 0 then
            -- CALL_STATE_CLOSED
            print("End of call\n")
            break
        end
    end
    -- switching back to sound card (empty string as argument = default device)
    -- replace it with e.g. USB device name if non-default audio input is used
    SwitchAudioSource("winwave", "")
    Beep(1200, 300) -- audible feedback
    SetButtonCaption(btnId, "Playing done");
end

play(audio_file_name, audio_file_length)

Note: checking exact length of each audio file and/or adding silence at the end to make sure that "Sleep" is shorter than file length is annoying (although sometimes audio file can be used to limit total call length this way). Later tSIP version added new option, not terminating call at the end of the file:
tSIP: disconnect call on audio error
This was used e.g. in the "Lenny" example.

Back to howto list.