Setting sound volume level programmatically

Hi guys. I’m studing C++ and want to little help Haiku.

I’ve found that media keys Volume_Up, Volume_Down and Volume_Mute not working on keyboard under Haiku and that’s not good for system, which created as easy-to-use to endless user.
Okay, I thought, that’s a point where I can help. What can be hard? I’ll write keyboard filter addon, which just will handle media keys (by codes, according to USB-HID) and call some scripts or system functions.

The first trouble is that I found no way to do this simply.
Closing my eyes on DRY, I’ve tried (shame on me) to port MixerControl code from there (thanks to Parodoxon), but it not works, maybe just 'cause it’s not my level. Playing with hey and Scripting Explorer also gave no results — no mans, no ideas how to reach volume slider over desklink.

Guys, where I’m gone wrong? That’s a really huge mess.

To set an audio mixer parameter you have to use BMediaRoster, BParameterWeb and BParameter.

First obtain a media_node reference to the mixer with BMediaRoster::GetAudioMixer(), then use BMediaRoster::ParameterWebFor() using the previously obtained reference to get a BParameterWeb. Then just fetch all the parameters in BParameterWeb until you find one with the id B_MASTER_GAIN. At this point just calling BParameter::SetValue should do the trick. If it doesn’t work, you should check the errors and print them with strerror.

I’ve tried. It caused strange behavior: I lost sound (don’t worry, till reboot only) and all settings in mixer panel turned into chaos.
In the end I’ve found a way to use MixerControl and that works pretty good. I wrote addon, installed it (let’s skip few hours of fails, monkey debug and input losing) and it works.
But there’s only trouble left — when I’m copying addon to filter’s folder it works good, but after restart it not. Looks like I need to await media_server initialization.
In original deskbar addon guys used periodical check with BMessageRunner, but it seems not usable for input filter addon.
So, what is the best way to call some method periodically with a delay in input filter? I saw add_timer, but don’t think its good idea (and maybe it usable for kernel only, don’t know).

Also, really worst point of BeBook is a lack of examples.

Look’s like I’ve solved that problem. Not in good and clean way, but it works now.
After years of C# development few days with C++ really comes as nightmare.

The reason there aren’t much examples in the Be Book is that they were distributed separately, as the “Be sample code”. I don’t know where to get a copy these days.
There are also the “Haiku API examples” here:

1 Like

Right here:

1 Like