Asterisk ChatGPT integration

Hi

I am trying to integrate asterisk with ChatGPT allowing someone to call and ask a question. I am using asterisk-external-media to get the sound in and out from the call. So far I was able to get the call sound out and send to ChatGPT. My problem is now in inject the sound back to the asterisk call.

I am using some code based on this project asterisk-dialogflow-rtp-audioserver/DialogFlowConnector.js at master · nimbleape/asterisk-dialogflow-rtp-audioserver · GitHub

To test this, I created a simple sound file where I speak some words. This file was created as 16bit signed sampled at 8KHz. Using SOX I am able to listen to the file with this command “play -b 16 -e signed -c 1 -r 8000 ./t2.sln”

When I try to send this sound to the call with the following code, I can bearably hear my voice because there is a lot of noise. I suspect the problem is the way I create the replyAudio buffer but I am a little lost.

My ARI media format is set to “ulaw”

    _sendAudioToAsterisk() {
        this.log.info('Got audio to play back to asterisk from Custom_connector');

        let audio = fs.readFileSync("t2.sln")

        this._asteriskConfig = {
            format:"sln",
            audioByteSize: 160,
            packetTimestampIncrement: 160,
            rtpPayloadType: 0
        }

        var replyAudio = new Int16Array(audio.length/2);
        for (var i = 0; i < audio.length; i += 2) {
            replyAudio[i / 2] = audio.readInt16BE(i);
        }

        let audioByteSize = this._asteriskConfig.audioByteSize;

        let frames = replyAudio.length / audioByteSize;
        let pos = 0;
        let type = this._asteriskConfig.rtpPayloadType;
        let seq = randomBytes(2).readUInt16BE(0);
        let ssrc = randomBytes(4).readUInt32BE(0);
        let timestamp = 0;

        let timeouts = [];

        for (let i = 0; i < frames+1; i++) {
            timeouts.push(setTimeout(() => {
                let newpos = pos + audioByteSize;
                let buf = replyAudio.slice(pos, newpos);

                timestamp = timestamp !== 0 ? timestamp : Date.now() / 1000;
                let packet = new Packet(Buffer.from(buf), seq, ssrc, timestamp, type);
                seq++;
                timestamp += this._asteriskConfig.packetTimestampIncrement;

                try {
                    this._asteriskAudioStream.outWStream.write(packet.serialize());
                } catch (err) {
                    console.log(err)
                    timeouts.forEach((ref) => {
                        clearTimeout(ref);
                    });
                }
                pos = newpos
            }, i * 20));
        }
    }   

Thanks for any help

I think I found a way that works.
Using a package called wavefile

the code that worked was:

    _sendAudioToAsterisk() {
        this.log.info('Got audio to play back to asterisk from Cust_connector');

        let wav = new WaveFile(fs.readFileSync("./t2.wav"));
        wav.toMuLaw();

        let replyAudio = wav.getSamples()
        // console.log(replyAudio)

        this._asteriskConfig = {
            format:"sln",
            audioByteSize: 160,
            packetTimestampIncrement: 160,
            rtpPayloadType: 0
        }

        let audioByteSize = this._asteriskConfig.audioByteSize;

        let frames = replyAudio.length / audioByteSize;
        let pos = 0;
        let type = this._asteriskConfig.rtpPayloadType;
        let seq = randomBytes(2).readUInt16BE(0);
        let ssrc = randomBytes(4).readUInt32BE(0);
        let timestamp = 0;

        let timeouts = [];

        for (let i = 0; i < frames+1; i++) {
            timeouts.push(setTimeout(() => {
                let newpos = pos + audioByteSize;
                let buf = replyAudio.slice(pos, newpos);

                timestamp = timestamp !== 0 ? timestamp : Date.now() / 1000;
                let packet = new Packet(Buffer.from(buf), seq, ssrc, timestamp, type);
                seq++;
                timestamp += this._asteriskConfig.packetTimestampIncrement;

                try {
                    this._asteriskAudioStream.outWStream.write(packet.serialize());
                } catch (err) {
                    console.log(err)
                    timeouts.forEach((ref) => {
                        clearTimeout(ref);
                    });
                }
                pos = newpos
            }, i * 20));
        }
    }```

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