Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to play audio file? #13

Closed
yaosiqi525 opened this issue Dec 3, 2020 · 11 comments
Closed

How to play audio file? #13

yaosiqi525 opened this issue Dec 3, 2020 · 11 comments

Comments

@yaosiqi525
Copy link

I need play mp3 or wav file, do you know what lib can help me ?
Thanks!!!

@jart
Copy link
Owner

jart commented Dec 3, 2020

There's a command line tool called sox that makes it easy to convert audio files to the format needed by gosip, which is signed linear 16-bit samples at 8khz. For example:

wget https://justine.storage.googleapis.com/numbers.mp3
sox numbers.mp3 -c1 -traw -esigned -b16 -r8000 numbers.s16
play numbers.s16

To get started playing audio files in a telephone calls, you can modify the fone/main.go example so it reads audio from a file rather than your microphone. The ticker.C channel is triggered every 20 milliseconds. Each time that happens, you need to read 320 bytes from the file and pass it along to rtp.Send().

I recommend the audio files be converted to s16 beforehand, so decoding is a one-time cost. If you want to play arbitrary audio formats on the fly, one thing that works great is spawning sox as a subprocess and reading the samples via a pipe. That should minimize the chance of cpu-bound audio decoding disrupting the event loop, which for PSTN calls must behave like real time.

@jart jart closed this as completed Dec 3, 2020
@yaosiqi525
Copy link
Author

Can you provide the param format?
or For example, like "username@host"
main.go need "requestURIString" param

@jart
Copy link
Owner

jart commented Dec 3, 2020

The URI format depends.

If you're calling the PSTN you need to use a service like Flowroute. If you do that they'll give you a prefix. If you wanted to dial a number in New York it'd look something like this:

$ fone sip:12345678*12125559999@sip.flowroute.com

If you want to dial a software telephone, usually they'll speak sip natively and the SIP URI will end up looking more like an email address.

@yaosiqi525
Copy link
Author

me try "go run ./fone/main.go sip:2001@sip-test.pudutech.com"
but it return:
panic: Connection refused
when run "makePulseAudio(C.PA_STREAM_RECORD, requestURIString)"

me running in docker,whether the influence?

@jart
Copy link
Owner

jart commented Dec 3, 2020

I would recommend contacting your local administrator and troubleshooting the problem on your end.

@yaosiqi525
Copy link
Author

me find fone/main.go, but only find use mic
how to read music file? me generated the numbers.s16, but i don't know the way to open it

@jart
Copy link
Owner

jart commented Dec 4, 2020

Open it as a normal file using the Go standard library.

Please note this project supports gosip but we can't provide technical support in general.

@yaosiqi525
Copy link
Author

Thank you very much for your answer

@hoangtuan151
Copy link

hoangtuan151 commented Nov 15, 2022

Thx you @jart for your awesome library!

I'm using the echo_test example code to make a SIP phone call from a Go application to another soft phone. I have FreeSwitch configured, and the Go app successfully send INVITE to soft phone, but I have trouble and really need your help: I can't hear anything from soft phone

Some clues

1/ Here is my modified code
Note: I try so set size=320 (at line 159) but it's not work!

2/ The audio file is in right format:

play ../assets/bot_greeting.wav         
play WARN alsa: can't encode 0-bit Unknown or not applicable

../assets/bot_greeting.wav:

 File Size: 72.2k     Bit Rate: 128k
  Encoding: Signed PCM    
  Channels: 1 @ 16-bit   
Samplerate: 8000Hz       
Replaygain: off         
  Duration: 00:00:04.51  

In:100%  00:00:04.51 [00:00:00.00] Out:36.1k [      |      ] Hd:5.1 Clip:0    
Done.

Note: I also try these but no luck:

  • Using numbers.s16 audio file
  • Ulaw converting: frameout[rtp.HeaderSize+n] = byte(dsp.LinearToUlaw(int64(raw_audio[raw_audio_idx])))

3/ Application log

➜  test git:(dev) ✗ go run .
test:main() begin...
2022/11/15 17:59:55 >>> 10.124.68.213:5080
INVITE sip:1001@10.124.68.213:5080 SIP/2.0
From: Echo Test <sip:10.124.68.213:33684>;tag=ab3072e56f7f
To: <sip:10.124.68.213:5080>
Via: SIP/2.0/UDP 10.124.68.213:33684;branch=z9hG4bK-e83efd2990b5
Contact: <sip:10.124.68.213:33684>
Call-ID: 912685b5-8ce3-469e-a0ce-4537ebe4d35c
CSeq: 17257 INVITE
User-Agent: gosip/1.o
Max-Forwards: 70
Content-Type: application/sdp
Content-Length: 240

v=0
o=- 1929021139 1929021139 IN IP4 10.124.68.213
s=my people call themselves dark angels
c=IN IP4 10.124.68.213
t=0 0
m=audio 49184 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=sendrecv

2022/11/15 17:59:55 <<< 10.124.68.213:5080
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 10.124.68.213:33684;branch=z9hG4bK-e83efd2990b5
From: Echo Test <sip:10.124.68.213:33684>;tag=ab3072e56f7f
To: <sip:10.124.68.213:5080>
Call-ID: 912685b5-8ce3-469e-a0ce-4537ebe4d35c
CSeq: 17257 INVITE
User-Agent: FreeSWITCH-mod_sofia/1.10.5-release+git~20200818T185121Z~25569c1631~64bit
Content-Length: 0


2022/11/15 17:59:58 <<< 10.124.68.213:5080
SIP/2.0 183 Session Progress
Via: SIP/2.0/UDP 10.124.68.213:33684;branch=z9hG4bK-e83efd2990b5
From: Echo Test <sip:10.124.68.213:33684>;tag=ab3072e56f7f
To: <sip:10.124.68.213:5080>;tag=Q0UUBv4N3BSjQ
Call-ID: 912685b5-8ce3-469e-a0ce-4537ebe4d35c
CSeq: 17257 INVITE
Contact: <sip:nobody@10.124.68.213:5080;transport=udp>
User-Agent: FreeSWITCH-mod_sofia/1.10.5-release+git~20200818T185121Z~25569c1631~64bit
Accept: application/sdp
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY
Supported: timer, path, replaces
Allow-Events: talk, hold, conference, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 222
Remote-Party-ID: "1001" <sip:1001@10.124.68.213>;party=calling;privacy=off;screen=no

v=0
o=FreeSWITCH 1668493614 1668493615 IN IP4 10.124.68.213
s=FreeSWITCH
c=IN IP4 10.124.68.213
t=0 0
m=audio 16384 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20

2022/11/15 17:59:58 wanted 200 ok but got:183Session Progress... retry in 5s
2022/11/15 18:00:03 <<< 10.124.68.213:5080
SIP/2.0 200 OK
Via: SIP/2.0/UDP 10.124.68.213:33684;branch=z9hG4bK-e83efd2990b5
From: Echo Test <sip:10.124.68.213:33684>;tag=ab3072e56f7f
To: <sip:10.124.68.213:5080>;tag=Q0UUBv4N3BSjQ
Call-ID: 912685b5-8ce3-469e-a0ce-4537ebe4d35c
CSeq: 17257 INVITE
Contact: <sip:nobody@10.124.68.213:5080;transport=udp>
User-Agent: FreeSWITCH-mod_sofia/1.10.5-release+git~20200818T185121Z~25569c1631~64bit
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY
Supported: timer, path, replaces
Allow-Events: talk, hold, conference, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 222
Remote-Party-ID: "Outbound Call" <sip:1001@10.124.68.213>;party=calling;privacy=off;screen=no

v=0
o=FreeSWITCH 1668493614 1668493615 IN IP4 10.124.68.213
s=FreeSWITCH
c=IN IP4 10.124.68.213
t=0 0
m=audio 16384 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20

2022/11/15 18:00:03 read 72238 bytes from sample audio
2022/11/15 18:00:04 written at audio idx: 8000
2022/11/15 18:00:05 written at audio idx: 16000
2022/11/15 18:00:06 written at audio idx: 24000
2022/11/15 18:00:07 written at audio idx: 32000
2022/11/15 18:00:08 written at audio idx: 40000
2022/11/15 18:00:09 written at audio idx: 48000
2022/11/15 18:00:10 written at audio idx: 56000
2022/11/15 18:00:11 written at audio idx: 64000
2022/11/15 18:00:12 written at audio idx: 72000
2022/11/15 18:00:12 Going to hangup...
2022/11/15 18:00:12 End call !!!
test:main() end!

@hoangtuan151
Copy link

Hi @yaosiqi525 , did you get it work with your audio file?

@tong3jie
Copy link

@yaosiqi525 @hoangtuan151 you can try to get this library(zaf/g711)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants