簡體   English   中英

如何在 Asterisk AGI 中播放背景音樂,同時在后台進行某些進程以消除執行期間的靜音

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

我在星號 2 中使用 python 2 有一段代碼監聽呼叫者音頻並處理音頻。 在此過程中,在播放音頻之前會有 15 秒的靜默。 我想在音頻處理過程中添加音樂。 有沒有辦法做到這一點。 extension.config 是這樣的

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

下面是 python 代碼

#!/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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM