How to do audio output selection for MediaPlayer control?

Post your Gambas programming questions here.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: How to do audio output selection for MediaPlayer control?

Post by vuott »

GrantXTV wrote: Tuesday 31st October 2023 9:39pm Option 2
If this can not be done, to read in the samples from a sound device at 22.05 kHz, stereo at 8bits to send to a Byte or an Integer to work with.
In general with gb.media Component, to change audio characteristics of a file, during its conversion, the MediaPipeline and MediaFilter Classes should be used.
Below is a practical example, in which we convert an MP3 file to a WAV file having sample rate = 22050 hertz and resolution = 8 bit:
Private bo As Boolean


Public Sub Main()

 Dim pl As MediaPipeline
 Dim src, dcb, con, res, flt, wav, snk As MediaControl
 Dim temporary, audiofile As String
 
 temporaneo = Temp
 audiofile = "/path/of/audio/file/we/want/covert"

 pl = New MediaPipeline As "PLine"
  
 src = New MediaControl(pl, "filesrc")
 src["location"] = audiofile
 dcb = New MediaControl(pl, "decodebin")
 con = New MediaControl(pl, "audioconvert")
 res = New MediaControl(pl, "audioresample")  ' Allows to change the characteristics of the audio file

 flt = New MediaFilter(pl, "audio/x-raw,rate=22050,format=U8")  ' Set the characteristics of the audio file
 wav = New MediaFilter(pl, "wavenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = temporary
 
 src.LinkTo(dcb)
 dcb.LinkLaterTo(con)
 con.LinkTo(res)
 res.LinkTo(flt)
 flt.LinkTo(wav)
 wav.LinkTo(snk)
 
 pl.play

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Duration of file: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
 Write "\r\e[0mElapsed time:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 Copy temporary To "/tmp" &/ File.BaseName(audiofile) & ".wav"

 pl.Close

 Print "\n\e[0mEnd conversion !"

End


Public Sub PLine_End()

 bo = True

End

If we want send audio data to sound card directly:
Private bo As Boolean


Public Sub Main()

 Dim pl As MediaPipeline
 Dim src, dcb, con, res, flt, snk As MediaControl

 pl = New MediaPipeline As "PLine"
  
 src = New MediaControl(pl, "filesrc")
 src["location"] = "/path/of/audio/file/we/want/covert"
 dcb = New MediaControl(pl, "decodebin")
 con = New MediaControl(pl, "audioconvert")
 res = New MediaControl(pl, "audioresample")
 flt = New MediaFilter(pl, "audio/x-raw,rate=11025,format=U8")
 snk = New MediaControl(pl, "autoaudiosink")
 
 src.LinkTo(dcb)
 dcb.LinkLaterTo(con)
 con.LinkTo(res)
 res.LinkTo(flt)
 flt.LinkTo(snk)
 
 pl.play

 While pl.Duration < 1
   Wait 0.01
 Wend
 Print "Duration of file: "; Time(0, 0, 0, pl.Duration * 1000)
 Repeat
 Write "\r\e[0mElapsed time:     \e[31m" & Time(0, 0, 0, pl.Position * 1000)
   Wait 0.01
 Until bo

 pl.Close

End


Public Sub PLine_End()

 bo = True

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
GrantXTV
Posts: 17
Joined: Tuesday 31st October 2023 9:20pm

Re: How to do audio output selection for MediaPlayer control?

Post by GrantXTV »

Thanks for your reply, this is where I at the moment, finding a way to stream out, but not been able to get it to work so far.

Tested on gstreamer
gst-launch-1.0 alsasrc ! audioconvert ! audio/x-raw,rate=48000,depth=8,channels=2,width=16 ! rtpL24pay ! udpsink host=192.168.3.15 port=5001

Public bo As Boolean
Public Sub Form_Open()
bo = True
End

Public Sub send_Click()
Dim pl As MediaPipeline
Dim src, aco, wen, snk, rtp, udp As MediaControl
Dim flt As MediaFilter
pl = New MediaPipeline
src = New MediaControl(pl, "alsasrc")
aco = New MediaControl(pl, "audioconvert")
flt = New MediaFilter(pl, "audio/x-raw,rate=44100,depth=16,channels=2,width=16,signed=true")
rtp = New MediaControl(pl, "rtpL24pay")
udp = New MediaControl(pl, "udpsink host=192.168.3.15 port=5001")
src.LinkTo(aco)
aco.LinkTo(flt)
flt.LinkTo(rtp)
rtp.LinkTo(udp)
pl.Play()
While bo
TextBox1.Text = Str(Time(0, 0, 0, pl.Position * 1000))
Wait 0.01
Wend
pl.Stop()
pl.Close()
Quit
End

Public Sub stop_Click()
bo = False
End

Public Sub Form_Close()
bo = False
End
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: How to do audio output selection for MediaPlayer control?

Post by vuott »

GrantXTV wrote: Monday 1st January 2024 10:15pm udp = New MediaControl(pl, "udpsink host=192.168.3.15 port=5001")
"HOST" and "PORT" are properties of the "udpsink " GStreamer Plugin.
Therefore they should be highlighted and separated.
......
udp = New MediaControl(pl, "udpsink")
udp["host"] = "192.168.3.15"
udp["port"] = 5001
......

GrantXTV wrote: Monday 1st January 2024 10:15pm This is what I tested on gstreamer and it works:
'gst-launch-1.0 alsasrc ! audioconvert ! audio/x-raw,rate=48000,depth=8,channels=2,width=16 ! rtpL24pay ! udpsink host=192.168.3.15 port=5001

What is best way to do this using mediaview, as I have not found a way to get this to work?
If you want to use "gst-launch-1.0 ", you will have to use its corresponding external function "gst_parse_launch()", since it has not been implemented in the Gambas gb.media Component.

Exemplum:
https://www.gambas-it.org/wiki/index.ph ... aunch()%27
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
GrantXTV
Posts: 17
Joined: Tuesday 31st October 2023 9:20pm

Re: How to do audio output selection for MediaPlayer control?

Post by GrantXTV »

Thanks Vuott,

I have tested this code out and it works, but there is an issues with the audio cutting in and out, I may need to add some type of audio buffer.
Any ideas on how I can go about this?

Other than this I can now look at doing the receiver part:
gst-launch-1.0 -v udpsrc port=5001 ! 'application/x-rtp,media=audio,payload=96,clock-rate=44100,channels=2,encoding-name=L24' ! rtpL24depay ! audioconvert ! autoaudiosink sync=false
GrantXTV
Posts: 17
Joined: Tuesday 31st October 2023 9:20pm

Re: How to do audio output selection for MediaPlayer control?

Post by GrantXTV »

I found the issue, I had set the encoder to a sample rate of 44100 and the decoder was set to 48000, it helps to check these things.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: How to do audio output selection for MediaPlayer control?

Post by vuott »

GrantXTV wrote: Tuesday 2nd January 2024 4:14pm Other than this I can now look at doing the receiver part:
gst-launch-1.0 -v udpsrc port=5001 ! 'application/x-rtp,media=audio,payload=96,clock-rate=44100,channels=2,encoding-name=L24' ! rtpL24depay ! audioconvert ! autoaudiosink sync=false
...this does not seem to be difficult.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
GrantXTV
Posts: 17
Joined: Tuesday 31st October 2023 9:20pm

Re: How to do audio output selection for MediaPlayer control?

Post by GrantXTV »

Hi Vuott,

Today's work on the receiver part, "this does not seem to be difficult", but it is not easy, as I have found that issues is with the flt.LinkTo(rtp) part, I am not sure what is going on here.

gst-launch-1.0 udpsrc port=5004 ! application/x-rtp,media=audio,payload=96,clock-rate=41000,depth=8,channels=2,signed=true,encoding-name=L24 ! rtpL24depay ! audioconvert ! autoaudiosink sync=false

This is where I started with testing this script, but translation it to media player as be low:

Public bo As Boolean

Public Sub Form_Open()
bo = True
End

Public Sub Play_Click()

Dim pl As MediaPipeline
Dim src, rtp, aco, als As MediaControl
Dim flt As MediaFilter

pl = New MediaPipeline
src = New MediaControl(pl, "udpsrc")
src["port"] = "5004"
flt = New MediaFilter(pl, "application/x-rtp,media=audio,payload=96,clock-rate=41000,depth=8,channels=2,signed=true,encoding-name=L24")
rtp = New MediaControl(pl, "rtpL24pay")
aco = New MediaControl(pl, "audioconvert")
als = New MediaControl(pl, "autoaudiosink")
als["sync"] = "false"
src.LinkTo(flt)
flt.LinkTo(rtp)
rtp.LinkTo(aco)
aco.LinkTo(als)
pl.Play()
While bo
'Write "\rTempo: " & Str(Time(0, 0, 0, pl.Position * 1000))
TextBox1.Text = Str(Time(0, 0, 0, pl.Position * 1000))
Wait 0.01
Wend
pl.Stop()
pl.Close()
Quit

End

Public Sub stop_Click()
bo = False
End

Public Sub Form_Close()
bo = False
End

Maybe I am missing something?
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: How to do audio output selection for MediaPlayer control?

Post by vuott »

Try like this:

......
flt.LinkLaterTo(rtp)
rtp.LinkLaterTo(aco)
......
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
GrantXTV
Posts: 17
Joined: Tuesday 31st October 2023 9:20pm

Re: How to do audio output selection for MediaPlayer control?

Post by GrantXTV »

Thanks, LinkLaterTo, has got me pass this point, but there is a new issue I get, that is:

** (URL_Player:719423): CRITICAL **: 10:20:16.200: gst_capsfilter_prepare_buf: assertion 'out_caps != NULL' failed

I have been looking around on the internet to see what this could be, but nothing so far, any ideas?
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: How to do audio output selection for MediaPlayer control?

Post by vuott »

Sorry, I don't know.
...but does the code work ?
Is it a simple warning or critical.. fatal error ?
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Post Reply