python-3.xasteriskagi

Would it be possible to play an audio directly from memory?


I am currently using Microsoft TTS, I get streaming audio, saved to a file and then send the command to Asterisk in "Stream File". Follow:

....
response = requests.post(url, data=body, headers=headers)
self.print("Got wave response")

with open("{}{}.{}".format(cachedir, filename, self.exten), 'wb') as fd:
    for chunk in response.iter_content(chunk_size=1024):
        self.print("Gravando....")
        fd.write(chunk)
    fd.close()
self.agi.stream_file('{}{}'.format(cachedir, filename))

I would avoid the need to write the file locally, sending the binary to play directly. Or even writing to the file, force the start in the first streaming package. I tried to do this last idea, but I get an Asterisk alert stating that the file is zero bytes.

Any suggestion?


Solution

  • It depend of how OS deal with files.

    In most cases you can use linux pipes, but you still need asterisk correctly work with that, which is not trivial and require high expertise.

    Simple way is just split file in small parts like 1 second fragments and play in asterisk one-by-one.

    Or just use os.fsync() after each write, but it will be much harder to maintain. https://www.tutorialspoint.com/python/os_fsync.htm