Trying to integrate with Asterisk EAGI

See original GitHub issue

Hello

I am trying to use this library to listen for commands over the telephone using Asterisk (Virtual PBX)

The idea is to the user call a number and speaks commands, I would use this library to “understand” those commands

Asterisk can execute an external program and pass it the raw sound using EAGI, it uses file descriptor 3 to output the raw sound, but when I run this code under Asterisk it never pass the “with sr.AudioFile(afile) as source:” looks like it enters and never leaves and no exception is thrown.

Here is the code

import speech_recognition as sr
from asterisk.agi import *
import os

agi = AGI()

# instance recognizer
r = sr.Recognizer()

# file descriptor
FD=3

# audio file
afile=os.fdopen(FD, 'rb')

try:
    with sr.AudioFile(afile) as source:
        audio = r.listen(source,3)
except Exception, e :
    agi.exec_command("NOOP","'Error_%s'"%str(e))

The asterisk documentation says it generates ( audio pipe: Linear pcm audio pipe ) on file descriptor 3

I am new to speech recognition so sorry if it is a stupid question.

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Ubericommented, Nov 21, 2016

Hi @bonadio,

I read up on the EAGI spec a bit; it seems like its input file descriptor is an infinite stream, having no EOF markers.

This library doesn’t support raw PCM audio streams directly. However, we can implement it separately:

import speech_recognition as sr
from asterisk.agi import *
import os
agi = AGI()
r = sr.Recognizer()

SAMPLE_WIDTH, SAMPLE_RATE = 2.0, 18000.0 # 16-bit, 48kHz PCM audio

class AudioRawStream(sr.AudioSource):
    def __init__(self, file_object, sample_width, sample_rate, channels = 1, little_endian = True):
        assert hasattr(filename_or_fileobject, "read"), "``file_object`` must be a file-like object"
        self.file_object = file_object
        self.SAMPLE_WIDTH = channels
        self.SAMPLE_RATE = sample_rate
        self.CHUNK = 4096
        self.channels = channels
        self.little_endian = little_endian
        self.stream = None

    def __enter__(self):
        audio_raw_reader = AudioRawStream.AudioRawReader(self.file_object, self.SAMPLE_WIDTH, self.channels)
        self.stream = AudioFile.AudioFileStream(audio_raw_reader, self.little_endian)

    def __exit__(self):
        self.stream = None

    class AudioRawReader(object):
        def __init__(self, file_object, sample_width, channels):
            self.file_object = file_object
            self.sample_width = channels
            self.channels = channels
        def readframes(self, frames): return self.file_object.read(frames * self.sample_width)
        def getnframes(self): raise ValueError("Frame count unsupported - the length of the raw audio is unknown and potentially unbounded.")
        def getsampwidth(self): return self.sample_width
        def getnchannels(self): return self.channels

afile = os.fdopen(3, 'rb')
with AudioRawStream(afile, SAMPLE_WIDTH, SAMPLE_RATE) as source:
    audio = r.listen(source,3)

Please re-open this issue if you have any further questions!

0reactions
TheMasterRootcommented, Aug 11, 2020

hello id like a help. I need to change this code:

import speech_recognition as sr
from asterisk.agi import *
import os
agi = AGI()
r = sr.Recognizer()

SAMPLE_WIDTH, SAMPLE_RATE = 2.0, 18000.0 # 16-bit, 48kHz PCM audio

class AudioRawStream(sr.AudioSource):
    def __init__(self, file_object, sample_width, sample_rate, channels = 1, little_endian = True):
        assert hasattr(filename_or_fileobject, "read"), "``file_object`` must be a file-like object"
        self.file_object = file_object
        self.SAMPLE_WIDTH = channels
        self.SAMPLE_RATE = sample_rate
        self.CHUNK = 4096
        self.channels = channels
        self.little_endian = little_endian
        self.stream = None

    def __enter__(self):
        audio_raw_reader = AudioRawStream.AudioRawReader(self.file_object, self.SAMPLE_WIDTH, self.channels)
        self.stream = AudioFile.AudioFileStream(audio_raw_reader, self.little_endian)

    def __exit__(self):
        self.stream = None

    class AudioRawReader(object):
        def __init__(self, file_object, sample_width, channels):
            self.file_object = file_object
            self.sample_width = channels
            self.channels = channels
        def readframes(self, frames): return self.file_object.read(frames * self.sample_width)
        def getnframes(self): raise ValueError("Frame count unsupported - the length of the raw audio is unknown and potentially unbounded.")
        def getsampwidth(self): return self.sample_width
        def getnchannels(self): return self.channels

afile = os.fdopen(3, 'rb')
with AudioRawStream(afile, SAMPLE_WIDTH, SAMPLE_RATE) as source:
    audio = r.listen(source,3)

to save a diferente file each time that is a silence on speech. i dont nee dto recognize what is talking. some one can helpme?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does EAGI work? - Asterisk Community
Hi everyone! I tried to use speech recognition NodeJS script for my calls with EAGI application. I start it like this:
Read more >
Asterisk AGI: Asterisk Gateway Interface - VoIP-Info
It creates an opportunity to link Asterisk dialplan to an external software that wants to manipulate a specific channel. This provides ...
Read more >
A Technical Introduction to the Asterisk Gateway Interface (AGI)
Now, go ahead and try to run this program yourself. Test it out, understand what is happening, and make it work. One thing...
Read more >
Accessing FD (3) for Asterisk EAGI - Stack Overflow
Are you certain that you have a file descriptor 3? Have you tried just opening the file? – JimB. Apr 11, 2017 at...
Read more >
Chapter 9. The Asterisk Gateway Interface (AGI) - O'Reilly
(Try pressing Ctrl-Alt-F9, and see if you get an Asterisk command-line interface.) ... Otherwise, your AGI may not work as expected, because Asterisk...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found