# Asterisk Audio Stream

**URL:** <https://community.asterisk.org/t/asterisk-audio-stream/101343>\
**Category:** Asterisk Support\
**Created:** [March 5, 2024, 3:21pm UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343 "2024-03-05T15:21:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AfnanMalik](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@AfnanMalik](https://community.asterisk.org/u/AfnanMalik)\
**Post date:** [March 5, 2024, 3:21pm UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343/1 "2024-03-05T15:21:50Z")

</div>

I want to get the audio from call without saving it in audio file and use for Speech to text.  
for example:  
i want to record Bob response direct from call to and convert it into text and then converted text will be use for further processing

---

<div class="post-metadata">

**Author:** ![jsonchan000](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/jsonchan000/32/6979_2.png) [@jsonchan000](https://community.asterisk.org/u/jsonchan000)\
**Post date:** [March 6, 2024, 3:29am UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343/2 "2024-03-06T03:29:50Z")

</div>

[asterisk/asterisk-external-media (github.com)](https://github.com/asterisk/asterisk-external-media)

This project stream audio to a rtp server and send the raw data to google STT.

---

<div class="post-metadata">

**Author:** ![danishhafeez](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@danishhafeez](https://community.asterisk.org/u/danishhafeez)\
**Post date:** [March 6, 2024, 5:43am UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343/3 "2024-03-06T05:43:17Z")

</div>

> [@AfnanMalik](#):
>
> I want to get the audio from call without saving it in audio file and use for Speech to text.  
> for example:  
> i want to record Bob response direct from call to and convert it into text and then converted text will be use for further processing

To achieve this, you would typically use a technique known as real-time speech recognition or live transcription.

here’s a short example in Python using the Google Cloud Speech-to-Text API and the Twilio API for call handling:

```auto
import os
from twilio.rest import Client
from google.cloud import speech_v1p1beta1 as speech

# Twilio credentials
TWILIO_ACCOUNT_SID = 'your_twilio_account_sid'
TWILIO_AUTH_TOKEN = 'your_twilio_auth_token'
TWILIO_PHONE_NUMBER = 'your_twilio_phone_number'

# Google Cloud credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path_to_your_google_credentials.json"

# Initialize Twilio client
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

# Function to transcribe call
def transcribe_call(call_sid):
    call = client.calls(call_sid).fetch()
    audio_url = call.recordings.list()[0].uri
    
    # Initialize Google Speech-to-Text client
    speech_client = speech.SpeechClient()

    # Configure audio settings
    audio = speech.RecognitionAudio(uri=audio_url)
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=8000,
        language_code="en-US",
    )

    # Perform transcription
    response = speech_client.recognize(config=config, audio=audio)

    # Print transcription
    for result in response.results:
        print("Transcript: {}".format(result.alternatives[0].transcript))

# Example usage
call_sid = 'your_call_sid'
transcribe_call(call_sid)

This code assumes you have a call SID from Twilio that you want to transcribe. It uses the Twilio Python library to fetch the call recording URL, then uses the Google Cloud Speech-to-Text API to transcribe the audio from the recording URL into text. Finally, it prints the transcription. You'll need to replace 'your_twilio_account_sid', 'your_twilio_auth_token', 'your_twilio_phone_number', 'path_to_your_google_credentials.json', and 'your_call_sid' with your actual Twilio and Google Cloud credentials and the specific call SID you want to transcribe.

Best regard
Danish Hafeez | QA Assistant
[ICTInnovations](https://www.ictinnovations.com)

```

---

<div class="post-metadata">

**Author:** ![AfnanMalik](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@AfnanMalik](https://community.asterisk.org/u/AfnanMalik)\
**Post date:** [March 6, 2024, 6:55am UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343/4 "2024-03-06T06:55:23Z")

</div>

Is there any other way… like from dialplan or writing AGI.  
Actually i’m trying to get the audio in AGI … Recording audio stream in buffer and then pass it to STT.  
Or any dialplan application that can get audio stream store in buffer rather than saving auido in file by using record, monitor or mixmonitor

---

<div class="post-metadata">

**Author:** ![danishhafeez](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@danishhafeez](https://community.asterisk.org/u/danishhafeez)\
**Post date:** [March 6, 2024, 7:07am UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343/5 "2024-03-06T07:07:48Z")

</div>

Yes, you can capture audio in an Asterisk AGI script without saving it to a file by using a tool like `sox` or `arecord` to capture the audio stream and then passing it to your speech-to-text (STT) engine.

```auto
#!/usr/bin/env python3

import os
import subprocess

def capture_audio(duration):
    # Using arecord to capture audio for the specified duration
    process = subprocess.Popen(["arecord", "-d", str(duration), "-f", "S16_LE", "-r", "16000", "-c", "1", "-t", "raw"], stdout=subprocess.PIPE)
    audio_data, _ = process.communicate()
    return audio_data

def main():
    # Define the duration for capturing audio (in seconds)
    duration = 5

    # Capture audio
    audio_data = capture_audio(duration)

    # Pass the captured audio to your STT engine for processing
    # Replace this line with your code to send the audio data to your STT engine

if __name__ == " __main__":
    main()
You can call this AGI script from your dialplan to capture audio and then process it with your STT engine. Make sure that the necessary permissions are set for executing the AGI script and capturing audio. Additionally, you may need to install the sox or arecord package if it's not already installed on your system.

```

---

<div class="post-metadata">

**Author:** ![AfnanMalik](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@AfnanMalik](https://community.asterisk.org/u/AfnanMalik)\
**Post date:** [March 6, 2024, 7:54am UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343/6 "2024-03-06T07:54:11Z")

</div>

i’m using azure and asterisk is deployed on vm  
ALSA lib confmisc.c:767:(parse\_card) cannot find card ‘0’  
ALSA lib conf.c:4745:(\_snd\_config\_evaluate) function snd\_func\_card\_driver returned error: No such file or directory  
ALSA lib confmisc.c:392:(snd\_func\_concat) error evaluating strings  
ALSA lib conf.c:4745:(\_snd\_config\_evaluate) function snd\_func\_concat returned error: No such file or directory  
ALSA lib confmisc.c:1246:(snd\_func\_refer) error evaluating name  
ALSA lib conf.c:4745:(\_snd\_config\_evaluate) function snd\_func\_refer returned error: No such file or directory  
ALSA lib conf.c:5233:(snd\_config\_expand) Evaluate error: No such file or directory  
ALSA lib pcm.c:2660:(snd\_pcm\_open\_noupdate) Unknown PCM default  
arecord: main:830: audio open error: No such file or directory

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/asterisk/original/2X/4/475d0c889911cfb84ac4881ad6df8b523e3f9105.png) [@system](https://community.asterisk.org/u/system)\
**Post date:** [April 5, 2024, 7:54am UTC](https://community.asterisk.org/t/asterisk-audio-stream/101343/7 "2024-04-05T07:54:53Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
