Guest User

It's Lenny in Lua

a guest
Aug 19th, 2014
2,539
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. It's Lenny in Lua
  3. --]]
  4.  
  5. local path = '/usr/local/freeswitch/sounds/lenny/';
  6. local counter_mainloop = 1;
  7. local counter_noinput  = 1;
  8. local counter_consec_noinput = 0;
  9.  
  10. function get_filename (aType, aNumber)
  11.    local file = path .. aType .. '/' .. aNumber .. '.raw';
  12.    local fd = io.open(file, "r");
  13.    if fd == nil then
  14.       file = path .. aType .. '/' .. aNumber .. '.gsm';
  15.    end
  16.    io.close(fd);
  17.    return file;
  18. end
  19.  
  20. function wait_for_silence(aTimeoutIncrement)
  21.    if aTimeoutIncrement == nil then
  22.       aTimeoutIncrement = 2000;
  23.    end
  24.    freeswitch.consoleLog( 'DEBUG', "ENTER wait_for_silence " .. aTimeoutIncrement .. "\n" );
  25.    if session:ready() ~= true then
  26.       return false;
  27.    end
  28.    session:setVariable("wait_for_silence_timeout" , "");
  29.    session:setVariable("wait_for_silence_listenhits", "0");
  30.    session:setVariable("wait_for_silence_silence_hits", "0" );
  31.    session:execute( "wait_for_silence", "300 30 5 " .. aTimeoutIncrement);
  32.  
  33.    local timeout = tonumber(session:getVariable("wait_for_silence_timeout"));
  34.    local speech  = tonumber(session:getVariable("wait_for_silence_listenhits"));
  35.    local silence = tonumber(session:getVariable("wait_for_silence_silence_hits"));
  36.  
  37.    freeswitch.consoleLog( 'DEBUG', "Speech : " .. speech .. " Silence : " .. silence .. "\n" );
  38.    if speech > 20 then
  39.       wait_for_silence( aTimeoutIncrement );
  40.       return true;
  41.    else
  42.       return false;
  43.    end
  44. end
  45.  
  46. function play_next_mainloop ()
  47.    session:execute( "playback", get_filename( 'mainloop', counter_mainloop ) );
  48.    counter_mainloop = counter_mainloop + 1;
  49.    local fd = io.open( get_filename( 'mainloop', counter_mainloop ) , "r");
  50.    if fd == nil then
  51.       counter_mainloop = 1;
  52.    end
  53.    counter_consec_noinput = 0;
  54.    io.close(fd);
  55.    return 2000;
  56. end
  57.  
  58. function play_next_noinput ()
  59.    session:execute("playback", get_filename( 'mainloop', counter_mainloop ) );
  60.    counter_noinput = counter_noinput + 1;
  61.    counter_consec_noinput = counter_consec_noinput + 1;
  62.    local fd = io.open( get_filename( 'noinputloop', counter_noinput ) , "r");
  63.    if fd ~= nil then
  64.       counter_noinput = 1;
  65.    end
  66.    if counter_consec_noinput > 3 then
  67.       counter_mainloop = 1;
  68.    end
  69.    io.close(fd);
  70.    return 3200;
  71. end
  72.  
  73. session:answer();
  74. session:execute( "playback", get_filename( 'greeting', 1 ) );
  75. local SilenceTimeout = 4000;
  76. while session:ready() == true do
  77.    if wait_for_silence( SilenceTimeout ) ~= true then
  78.       SilenceTimeout = play_next_noinput();
  79.    else
  80.       SilenceTimeout = play_next_mainloop();
  81.    end
  82. end
RAW Paste Data