How to do audio output selection for MediaPlayer control?

Post your Gambas programming questions here.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

How to do audio output selection for MediaPlayer control?

Post by Cedron »

I'm working on improving my music player some.

Issue #1:

There seems to be a little bit of strange behavior while working on a Raspberry Pi 4. Every third or fourth play or so the playback halts. The event handler reports an event type 1. I was able to code a work around (will be included when I post it in the project showcase) that stops the player, resets the URL, then starts the player again when this event occurs. The only consequence is the song gets listed twice in the history. Rather than fixing that, I let it be so I could track occurrences.


Issue #2:

The media player does not honor the default audio out system setting. Strangely it works opposite on the Pi4 than my Mint laptop. When I plug in an external usb sound device it takes precedence on the laptop while on the Pi4 the HDMI output takes precedence.

So, I looked at the wiki at the media player stuff and am a bit puzzled at how to put it all together. I was hoping somebody has already done this and knows the proper way to do it.

I'd like to add a settings tab to the player that lets the output channel be selected on the fly. This requires being able to read the available outputs and then being able to select one.

Does anyone know how to do this?
.... and carry a big stick!
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

I don't know if I understand correctly ...
However, in general if you want to identify the audio device, to which to direct the audio, in my opinion you have to use the ".Audio.Output" Property of the "MediaPlayer" Class, assigning it the GStreamer plugin - for example - "alsasink" through the "MediaControl" Class and specifying the audio-device.

I show a practical example:
Public Sub Main()
 
  Dim mp As MediaPlayer
  
  With mp = New MediaPlayer
    .URL = Media.URL("/path/of/audio/file")
    .Audio.Output = New MediaControl(mp, "alsasink")
 ' It specifies audio-device (in my sistem is "hw:1,0,0"):
    .Audio.Output["device"] = "hw:1"   ' or more in detail "1,0,0".  But it could also be '"plughw:1,0,0" 
    .Play
  End With
  
  Repeat       ' It allows "MediaPlayer" to return the duration value of the audio file
    Wait 0.01
  Until mp.Duration > 0.0
  
  Wait mp.Duration

  mp.Close

End
Last edited by vuott on Tuesday 6th October 2020 7:29am, edited 1 time in total.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

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

Post by Cedron »

Thanks vuott. I got your example to work, but I can't get it to direct to either output by switching the value from "hw:0" or "hw:1"

hw:0 plays through the laptops internal speaker, not the HDMI.


This code snippet:
 
        myPlayer.Play()

        myInput = myPlayer.Input
        myOutput = myPlayer.Audio.Output
        
        Print myInput.Name, myInput.Type
        Print myOutput.Name, myOutput.Type
Generates this output:

Code: Select all

source  filesrc
audiosink       autoaudiosink
I've been doing some searching on ALSA, and found the following:

Code: Select all

~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: MID [HDA Intel MID], device 0: ALC269VB Analog [ALC269VB Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: MID [HDA Intel MID], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: MID [HDA Intel MID], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: MiniLink [MiniLink], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0


~ $ cat /proc/asound/modules
 0 snd_hda_intel
 1 snd_usb_audio


~ $ cat /proc/asound/cards
 0 [MID            ]: HDA-Intel - HDA Intel MID
                      HDA Intel MID at 0xd4620000 irq 45
 1 [MiniLink       ]: USB-Audio - MiniLink
                      XITEL MiniLink at usb-0000:00:1d.0-1.3.6, full speed
So, now I'm trying to tie it all together. Still unsuccessful, but better off than I was before.
.... and carry a big stick!
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

Cedron wrote: Tuesday 6th October 2020 12:54am

Code: Select all

source  filesrc
audiosink       autoaudiosink
...I used "alsasink" plugin, not autoaudiosink.

Cedron wrote: Tuesday 6th October 2020 12:54am

Code: Select all

~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: MID [HDA Intel MID], device 0: ALC269VB Analog [ALC269VB Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: MID [HDA Intel MID], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: MID [HDA Intel MID], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: MiniLink [MiniLink], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
  
Did you try with: "hw:0,3,0" or "hw:0,7,0" ?
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

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

Post by Cedron »

Yes. I've tried all sorts of combinations, with two parameters, commas or periods, with or without the "hw". All give me a "Can't set Status" error when the .play is hit.

From looking at /etc/share/alsa/alsa.conf, I've also tried using "subdevice" on another line and get "Unknown property".

On my mint box, I can switch outputs while the player is playing through the sound preferences. This is adequate for my needs, but it doesn't work that way on the Pi4.

Strange.
.... and carry a big stick!
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

Cedron wrote: Tuesday 6th October 2020 1:58pm All give me a "Can't set Status" error when the .play is hit.
My laptop also has the HDMI device, and it is identified with card 0, device 3 and subdevice 0.
So, I tried that code of mine with these instructions:
With mp = New MediaPlayer
  .URL = Media.URL("/path/of/audio/file")
  .Audio.Output = New MediaControl(mp, "alsasink")  ' ...ALSASINK !
  .Audio.Output["device"] = "hw:0,3,0" 
  .Play
End With
Well, I don't hear any sound, as my laptop is now not connected to an external device via HDMI, but the data processing goes on ...by using after .Play() this lines:
Repeat
  Write "\r" & Date(0, 0, 0, 0, 0, 0, mp.Position * 1000)
  Wait 0.001
Until mp.Position >= mp.Duration
and I do not get that error, that you reported.

Cedron wrote: Tuesday 6th October 2020 1:58pm I've also tried using "subdevice" on another line and get "Unknown property".
About GStreamer plugins - used in 2nd argument of MediaControl object - and their properties, you can have a look here:
https://gstreamer.freedesktop.org/docum ... language=c
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

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

Post by Cedron »

It is working now, on both the laptop and Pi4. Not sure what I did wrong while flailing. Thank you very much.

That is a helpful link. The default created by ".play" is obviously "autoalsosink" which allows the system preferences to switch output during playback on my laptop, but not the Pi4. When a media control is explicitly create, per your code, then the playback is immune to the system setting. This is what I wanted.

Now I have to incorporate it into my music player. Do you know of any "pure Gambas" way via the Media* controls of getting the equivalent of the "aplay -l" output?

I do know how to shell, capture, and parse.

Thanks again.

In case anybody hasn't seen it, my music player is available here:

viewtopic.php?f=13&t=873

"It's the best ever, nobody has ever seen anything like it."
.... and carry a big stick!
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

Cedron wrote: Tuesday 6th October 2020 4:46pmDo you know of any "pure Gambas" way via the Media* controls of getting the equivalent of the "aplay -l" output?
Well, I can show you this page, that I wrote, of the WIKI of the Gambas Italian programmers forum.
There you can try two codes, which however use the external functions of ALSA via "Extern ":

https://www.gambas-it.org/wiki/index.ph ... el_sistema

...but if you don't want to use the external ALSA functions, you can easily get those string data from the files in the subfolders of "/proc/asound/ " path.

And more, If you still prefer to use the resources of the gb.media Component, you need to search through the appropriate properties of GStreamer plugins, and use them with the MediaControl Object.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

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

Post by Cedron »

Several years ago I wrote the predecessor to this program in pure C++ using the ALSA library. I was kind of worried I was going to have to port that code to my own C shared library and replace the MediaPlayer.

Thanks to your help, I don't have to do that. The /proc/asound/pcm file, loaded with a single File.Load statement does the trick. I've added a "Settings" tab to the music player and put a list box there with the lines from that file. On a click, I can consruct the proper hw: argument and good to go. The only thing is that it needs to be done before the player is started. That's not a problem for me, but something I need to fix before I post the upgrade. That is going to have to wait for another day.

It still concerns me that the MediaPlayer doesn't honor the system setting on the Pi4. I'm not sure whether it is worth reporting as a bug (It is 3.12.2 after all). Perhaps somebody else who has a Pi4 can verify the same problem.

Many thanks again.
.... and carry a big stick!
GrantXTV
Posts: 17
Joined: Tuesday 31st October 2023 9:20pm

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

Post by GrantXTV »

Hi all,

I have run into an issue whereby I am trying to get audio samples from the Media Viewer or a sound device input.

Option 1
Is to find away to do this from the Media Viewer and to convert it to a lower PCM format of 22.05 kHz, stereo at 8bit sizes, to send to a Byte or an Integer to work with.

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.

The last step would be to send this processed audio back out to sound device, any ideas on how this could done within Gambas?
I have been trying to do something like this for a few weeks now.
Post Reply