Sourav Roy
Sourav Roy

Reputation: 407

how to play background music in Asterisk AGI while some process is Going on in background to remove the silence during execution

I am using python 2 in asterisk 2 there is a section where code listen to callers audio and process the audio. During the process there is a silence of 15 sec before the audio is played. I want to add a music during the processing of audio. Is there a way to do this. the extension.config is like this

[autoattendant2]
exten => 5555,1,same=>n,Answer() 
same=> n, AGI(/root/code_base/Queue/code2.py)
same=>n,hangup()
   

Below is the python code

#!/usr/bin/env python2
import sys
import re
import time
import random
import subprocess
import requests
import json
from datetime import datetime

    
    env = {}
    tests = 0;
    while 1:
       line = sys.stdin.readline().strip()
       if line == '':
          break
       key,data = line.split(':')
       if key[:4] <> 'agi_':
          #skip input that doesn't begin with agi_
          sys.stderr.write("Did not work!\n");
          sys.stderr.flush()
          continue
       key = key.strip()
       data = data.strip()
       if key <> '':
          env[key] = data
    
    sys.stderr.write("AGI Environment Dump:\n");
    sys.stderr.flush()
    for key in env.keys():
       sys.stderr.write(" -- %s = %s\n" % (key, env[key]))
       sys.stderr.flush()
    
    def checkresult (params):
       params = params.rstrip()
       if re.search('^200',params):
          result = re.search('result=(\d+)',params)
          if (not result):
             sys.stderr.write("FAIL ('%s')\n" % params)
             sys.stderr.flush()
             return -1
          else:
             result = result.group(1)
             #debug("Result:%s Params:%s" % (result, params))
             sys.stderr.write("PASS (%s)\n" % result)
             sys.stderr.flush()
             return result
       else:
          sys.stderr.write("FAIL (unexpected result '%s')\n" % params)
          sys.stderr.flush()
          return -2
    
    def change_file(path, cid):
    # one of the process example  
       filename = 'complain{0}'.format(cid)
       #filename = 
       input_file = path + '/' + filename + '.gsm'
       output_file = path + '/' + filename + '.wav'
       #command = "sox {} -r 8000 -c 1 {}".format(input_file, output_file)
       command = "sox {} -b 16 -r 44100 -c 1  {} trim 0 7 vol 2".format(input_file, output_file)
       subprocess.call(command, shell=True)
       pbcheck = requests.get("http://127.0.0.1:8000/STT_complaint/", params = {"address" : output_file, "lang" : language, "cid":cid, "phone":callerid, "start_time":now})
       res = pbcheck.content
       res2 = res.replace('"', "")  
         
       return res2
    def run_cmd(cmd):
    #This runs the general command 
       sys.stderr.write(cmd)
       sys.stderr.flush()
       sys.stdout.write(cmd)
       sys.stdout.flush()
       result = sys.stdin.readline().strip()
       checkresult(result)
    
    
    #language = "ben"
# asking problem recorded audio
    cmd_streaming = "STREAM FILE /root/code_base/recorded_voices/{0}/plz_tell_problem \"\"\n".format(language, language)
    run_cmd(cmd_streaming)
# listening to caller / recording caller voice    
    cmd_record = "RECORD FILE {0}/complain{1} gsm 1234 {2} s=3 \"\"\n".format(fixed_path, cid, 15)
    run_cmd(cmd_record)
  #processing audio
    processed_output = change_file(path , cid) # while this is executing ad giving output i want to play wait_music and then stop to run 
    # while processing play this 
    cmd_streaming = "STREAM FILE /root/code_base/recorded_voices/wait_music  \"\"\n".format(language, language)
    run_cmd(cmd_streaming)
    # once output received (processed_output) play next audio

    cmd_streaming = "STREAM FILE /root/code_base/recorded_voices/next_instruction  \"\"\n")
    run_cmd(cmd_streaming)

Upvotes: 0

Views: 1263

Answers (1)

arheops
arheops

Reputation: 15247

For that asterisk AGI have the special command "SET MUSIC ON"

https://wiki.asterisk.org/wiki/display/AST/Asterisk+18+AGICommand_set+music

Set waiting tone for asterisk agi function processing

Upvotes: 1

Related Questions