tSIP: signaling BLF change with audio

Lua "on BLF (dialog-info) change" playing sound when contact(s) state changed to early or confirmed.

function play()
	-- Playing sound asynchronously
	-- note: more powerful (sound device selection) option is using sox tool + ShellExecute
	-- (http://tomeko.net/software/SIPclient/howto/sox.php,
	-- http://tomeko.net/software/SIPclient/howto/local_dtmfs.php)

	local winapi = require("tsip_winapi")

	-- let's use path relative to exe location
	local filename = GetExeName()
	local index = string.find(filename:reverse(), "\\")
	local dir = string.sub(filename, 1, -index)
	local audio_file = dir .. "pluck.wav"

	-- PlaySound is identical to WinAPI function with same name
	local SND_FILENAME = tonumber(0x00020000)
	local SND_ASYNC = tonumber(0x0001)
	winapi.PlaySound(audio_file, 0, SND_FILENAME | SND_ASYNC)
end


local execSourceType = GetExecSourceType()	-- 7 = BLF state change
if execSourceType ~= 7 then
	print("Unexpected context for BLF state script!\n")
	return
end
local contactId = GetExecSourceId()			-- contact id in this context

local number, state = GetBlfState(contactId)

--[[
for state value:
enum dialog_info_status {
	DIALOG_INFO_UNKNOWN = 0,		///< not subscribed / subscription failed
	DIALOG_INFO_TERMINATED,
	DIALOG_INFO_EARLY,
	DIALOG_INFO_CONFIRMED
};
If contactId is out of range (e.g. there is no subscription) then value = -1 is returned.
This is unexpected in script running on event though.
]]

-- number might be used to limit contacts for which audio is played
-- or to assign different audio files to different contacts
-- print(string.format("on_blf_state: number %s state = %d\n", number, state))

if state == 2 or state == 3 then	-- early + confirmed states
	-- let's limit play() frequency
	local currentTime = os.time()
	print ("currentTime: " .. currentTime .. "\n")
	local variableName = "OnBlfStatePlaTime"
	local prevTime, prevTimeIsSet = GetVariable(variableName)
	if prevTimeIsSet ~= 0 then
		print(string.format("prevTime = %s\n", prevTime))
		prevTime = tonumber(prevTime)
		if currentTime - prevTime < 2 then
			-- skip playing
			print("Skipping playing - 2 seconds not passed\n")
			return
		else
			print(string.format("delta = %d\n", currentTime - prevTime))
		end
	else
		print("prevTime not set\n")
	end

	SetVariable(variableName, currentTime)
	play()
end

Back to howto list.


 "Cookie monsters": 7732799    Parse time: 0.001 s