tSIP: playing audio file on demand during call

In this setup softphone is configured with few buttons playing different audio files on demand during the call to second party.

Step 1: prepare announcement audio files - uncompressed wav files, 16 bits per sample, mono, 8000 samples per second. Each audio file should contain few extra second of silence at the end as at the moment (tSIP 0.1.62) switching audio source must happen before end of the file, otherwise call would be disconnected. Audacity would work:
audacity
Make sure to export audio to correct format. Example files: announcement_01.wav, announcement_02.wav. Note actual length of the files, not counting extra seconds of silence at the end - after this time audio source would switch back to default (e.g. microphone).

Script is splitted into two files - common part (to make things easier to manage) and script handling particular button (possibly multiple scripts for multiple buttons).

Common part (just placed in scripts directory):

-- btn_announcement_common.lua
local M = {} -- public interface

function M.play(audio_file_name, audio_file_length)
    Beep(800, 300)  -- audible feedback
    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
end

return M

Script assigned to button:

-- btn_announcement_01.lua

-- button-specific section
local audio_file_name = "announcement_01.wav"
local audio_file_length = 2.9   -- seconds, approximate
-- end of button-specific section

local btn_announcement = require("scripts.btn_announcement_common")
btn_announcement.play(audio_file_name, audio_file_length)

Buttons can be used directly, by assigning hotkeys to them or with command line /tsip=PROGRAMMABLE_BTN_xxx parameter.

Update for tSIP 0.3.4.1

With newer tSIP version it is little bit easier to setup:

tSIP playing audio files during call
tSIP_playing_wav.7z - preconfigured portable example with 4 buttons playing wave files

Back to howto list.