Adjusting system volume using OF

Hi,

I’m trying to create a slider and adjust the system volume on OSX.
Basically I’d like to have spotify, youtube or whatever playing in the background and be able to use an OF application to adjust the global volume.

I’ve been reading about http://openframeworks.cc/documentation/sound/ofSoundStream.html and then also looked at this Howto use ofSoundStream.setDeviceID()? but was wondering if there’s maybe a simpler way to do it, without an additional virtual audio device.

Thanks for the help!

you can adjust the global volume using apple script – ie:

osascript -e “set Volume 10”

to do this in OF, you can do:

system("osascript -e “set Volume 2"”);

system calls code as you would from the command line

2 Likes

Thank you, zach, that’s perfect!

hey, … and how can I ‘insert’ a variable into this appleScript?
I mean, on this case, what if I want to convert an int to string to use just a line for every volume changing values?
I tried something like this:

std::string systemAppleScript; //all the script
std::string systemVolumeValueStr; //just the string for the variable value

systemVolumeValueStr = ofToString(systemVolumeValue); //systemVolumeValue is the changing value
systemAppleScript = "osascript -e \"set Volume " + systemVolumeValueStr + "\"";
cout << "systemAppleScript: " << systemAppleScript << endl;

system("systemAppleScript");

Obviously this code doesn’t works because system is not receiving the content of the string. This is the console output in Xcode:

systemVolumeValue: -10.1
systemAppleScript: osascript -e "set Volume -10.1"
sh: systemAppleScript: command not found

Hi moebiussurfing,

I’ve used something like this:

    string cmd = "osascript -e \"set Volume "+ofToString(new_volume)+"\"";
    system(cmd.c_str());
    cout << cmd.c_str() << "\n";

hope this helps!

1 Like

thanks! it worked! but I was trying to make a continuous volume controller with a “kind-of-slider”, and it’s very slow the command until the execution of the line ends. So I’ll try to make a shortcut (keystroke) or something…

EDIT: I have seen some addon to keyboard emulate or maybe i’ll try to use Osculator.

glad it worked, at least the system commands.

I’ve tried getting arduino and volume control to work together with a proximity sensor. I ended up just ‘easing’ the value. So for example I get 1 from arduino and the volume is at 5, it gradualy goes to 1 in OF, worked slow but fine. Not super precise as you can see :slightly_smiling:

new_volume = (new_volume * 0.75) + (old_volume * 0.25);

Please let us know how you do it in the end, since it’s useful and interesting to know.

I guess this stopped working with macOS 10.14

**2019-11-14 19:37:59.333803-0700 osascript[71827:1453809] MessageTracer: load_domain_whitelist_search_tree:73: Search tree file's format version number (0) is not supported**

**2019-11-14 19:37:59.333963-0700 osascript[71827:1453809] MessageTracer: Falling back to default whitelist**

**2019-11-14 19:37:59.369565-0700 osascript[71827:1453809] skipped scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" because it is not SIP-protected.**

**2019-11-14 19:37:59.369602-0700 osascript[71827:1453809] skipped scripting addition "/Library/ScriptingAdditions/SASyphonInjector.osax" because it is not SIP-protected.**

**2019-11-14 19:37:59.385655-0700 osascript[71827:1453809] skipped scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" because it is not SIP-protected.**

**2019-11-14 19:37:59.385695-0700 osascript[71827:1453809] skipped scripting addition "/Library/ScriptingAdditions/SASyphonInjector.osax" because it is not SIP-protected.**

**2019-11-14 19:37:59.464226-0700 osascript[71827:1453809] Error loading /Library/Audio/Plug-Ins/HAL/JackRouter.plugin/Contents/MacOS/JackRouter: dlopen(/Library/Audio/Plug-Ins/HAL/JackRouter.plugin/Contents/MacOS/JackRouter, 0x0106): code signature in (/Library/Audio/Plug-Ins/HAL/JackRouter.plugin/Contents/MacOS/JackRouter) not valid for use in process: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.**

**2019-11-14 19:37:59.464277-0700 osascript[71827:1453809] Cannot find function pointer New_JackRouterPlugIn for factory <CFUUID 0x600000223980> 7CB18864-927D-48B5-904C-CCFBCFBC7ADD in CFBundle/CFPlugIn 0x102f01f80 </Library/Audio/Plug-Ins/HAL/JackRouter.plugin> (bundle, not loaded)**