pyVoIP icon indicating copy to clipboard operation
pyVoIP copied to clipboard

Example for receive a call and record it

Open xyc0815 opened this issue 2 years ago • 3 comments

I try to record the call. For this I use this part in the callback function answer

        print("+++++++++++++++++  Get Audio from caller and write file")
        w = wave.open('test_wave.wav', 'wb')
        w.setparams((1, 16 // 8, 8000, 0, 'NONE', 'NONE'))
        while call.state == CallState.ANSWERED:
            data = call.read_audio()
            #print(data)
            w.writeframesraw(data)
        w.close()

This writes a wav file, but I only get is very noisy. I think the parameters in setparams a not right. But I don't know what to set here.

xyc0815 avatar Nov 03 '22 20:11 xyc0815

Are you sure that you receive 2 Bytes = 16 Bit? Normally you will get 8 Bit = 1 Byte. It worked for me, and the saved audio-file is totally okay.

My Code:

    w = wave.open('test_wave.wav', 'wb')
    w.setnchannels(1)
    w.setsampwidth(8 // 8) # 8 Bit = 1 Byte
    w.setframerate(8000)
    w.close

lo-hei avatar Nov 08 '22 14:11 lo-hei

That's correct, PCMU/A is 8 bit

tayler6000 avatar Nov 08 '22 14:11 tayler6000

Thanks, sometimes the right solution is so easy. It works for me too.

xyc0815 avatar Nov 17 '22 09:11 xyc0815

This code works, recording is permanently. How can i stop recording by time or (will be better) by silent in channel?

w = wave.open('test_wave.wav', 'wb')
w.setnchannels(1)
w.setsampwidth(8 // 8)
w.setframerate(8000)

while call.state == CallState.ANSWERED:
    data = call.read_audio()
    w.writeframesraw(data)
w.close()
call.hangup()

Koshkodav avatar Jan 25 '23 07:01 Koshkodav

You could probably do something like:

if data != b"\x80" * len(data):
    w.writeframes(data)

tayler6000 avatar Feb 04 '23 00:02 tayler6000

I'm trying to record a few seconds of a certain part in a call. However, I can't even get a simple recording to work properly...

I want to save a recording as a .wav file. If I'm using: data = call.read_audio(blocking=False) it starts to produce an multiple hour long .wav file that's pretty large (+ 200Mb) and does not even contain any audio... When I'm using data = call.read_audio(blocking=True) it spits out a 365kb file with 0 seconds...

What am I missing here?

from pyVoIP.VoIP import VoIPPhone, InvalidStateError, CallState
import time
import wave


# ACCOUNT DATA:
SIP_IP = 'pbx.easybell.de'
SIP_Port = 5060
SIP_Username = 'placeholder'
SIP_Password = 'placeholder'
myIP = 'placeholder'

def answer(call):
    w = wave.open('test_wave.wav', 'wb')
    w.setnchannels(1)
    w.setsampwidth(2)
    w.setsampwidth(8 // 8)
    w.setframerate(8000)
    call.answer()

    while call.state == CallState.ANSWERED:
        print("recording start")
        data = call.read_audio(blocking=False)
        w.writeframesraw(data)
    w.close()
    print("recording saved")


if __name__ == "__main__":
    phone = VoIPPhone(SIP_IP, SIP_Port, SIP_Username, SIP_Password, myIP=myIP, callCallback=answer)
    phone.start()
    input('Press enter to disable the phone')
    phone.stop()

TipsTricksMore avatar Feb 09 '23 13:02 TipsTricksMore

I'm trying to record a few seconds of a certain part in a call. ...

>     while call.state == CallState.ANSWERED:
>         print("recording start")
>         data = call.read_audio(blocking=False)

Don't use print() in the function answer(). If you need logging then use recording into a file. Parameter "blocking" better left by default.

Koshkodav avatar Feb 10 '23 03:02 Koshkodav

@TipsTricksMore, definitely heed what @Koshkodav said about blocking. But I think your problem is you don't want to be using writeframesraw, if I recall correctly, that does not increase the frame counter in the wav file so it thinks you haven't added any data when you have. But yeah, if you set blocking to false it's going to add a TON of empty sound in between packets.

tayler6000 avatar Feb 10 '23 05:02 tayler6000

w = wave.open('test_wave.wav', 'wb')
w.setnchannels(1)
w.setsampwidth(2)
w.setsampwidth(8 // 8)
w.setframerate(8000)

call.answer()
while call.state == CallState.ANSWERED:
  data = call.read_audio(blocking=False)
  if data != b"\x80" * len(data):
      w.writeframesraw(data)

w.close()
call.hangup()

I am getting only a 0-sec file what am I doing wrong? I am not receiving any audio from another side I don't know why. Any suggestions

PraveenChordia avatar Mar 30 '23 13:03 PraveenChordia

I believe the issue you are having is the writeframesraw call. Per the Python documentation writeframesraw does not increment the frames counter in the wav file. You should use writeframes instead.

tayler6000 avatar May 09 '23 02:05 tayler6000