I'd like to programmatically set volume in Windows, Mac and Ubuntu using C/C++. Command line also can but C/C++ preferred. Thank you in advance!
-
1by volume do yo mean the audio volume? – Alon Dec 17 '09 at 10:25
-
Yes. I want to set the master volume. – Viet Dec 17 '09 at 13:31
-
You're going to get three *very* different answers. I recommend you ask three *separate* questions for this. That way, you're more likely to get responses that answer the *entire* question instead of what you're getting here, which only answer 1/3 of the question. – Rob Kennedy Dec 17 '09 at 15:28
-
Thanks for your suggestion. Actually only MacOS is left. I didn't figure out that I need to nominate the best answer for each question. – Viet Dec 18 '09 at 00:05
3 Answers
Just a hint, In windows the 'philosophy' around volume adjustment has changed between XP and Vista/7. Code that would change the master volume on XP will only change the application specific volume setting in the mixer on Vista and 7.
Here is a good blog post by one of the MS audio dev team regarding this: Larry Osterman's Blog
Here are some codeproject pages that might prove useful:
Also, there are a few powerpoint presentations regarding the new api's here: AMP Summit ppts. The Audio Endpoints in Windows Vista presentation has some good info.
As for OsX and Ubuntu, i have no idea.

- 1,608
- 16
- 31
For Linux using ALSA sound system, you can use following command:
amixer set Master 50%

- 18,889
- 4
- 46
- 89
-
+1 Thanks! Does it have the same effect as adjusting the speaker icon on the Taskbar? This one does not look like C system call but thanks anyway. – Viet Dec 17 '09 at 13:28
-
It is a shell command, you can execute it with system() or fork()/exec(). And yes, it has the same effect as adjusting taskbar volume control. – el.pescado - нет войне Dec 17 '09 at 13:38
-
-
1Then you should look into ALSA library (http://www.alsa-project.org/alsa-doc/alsa-lib/group___mixer.html), which isn't very well documented, or peek into amixer sources;) – el.pescado - нет войне Dec 17 '09 at 14:54
-
For completeness sake, here is the OSX cli version:
osascript -e "set volume output volume (output volume of (get volume settings)+2)"
And on the C side it's more difficult. From everything I found researching this, the easiest way is using one of the readily available objective C answers and wrapping them into a function in an extra object you can call from C/C++.

- 773
- 8
- 15