Setting the system volume?

Member
Posts: 194
Joined: 2009.02
Post: #1
How would one set the system volume using cocoa, or even carbon?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
please don't!
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #3
OneSadCookie Wrote:please don't!

lol, I don't plan on using it in an actual game.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #4
At the risk of dealing a deathblow to OSC:
Code:
// setting system volume
- (void)setVolume:(float)volume {
    AudioDeviceID device;

    UInt32 size = sizeof(device);
    OSStatus err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
    if(err != noErr) {
        NSLog(@"audio-volume error get device");
        return;
    }
    
    // try set master-channel (0) volume
    Boolean canSetVolume = false;
    
    size = sizeof(canSetVolume);
    err = AudioDeviceGetPropertyInfo(device, 0, false, kAudioDevicePropertyVolumeScalar, &size, &canSetVolume);
    if(err == noErr && canSetVolume) {
        size = sizeof(volume);
        err = AudioDeviceSetProperty(device, NULL, 0, false, kAudioDevicePropertyVolumeScalar, size, &volume);
    } else {
        // try seperate channes
        UInt32 channels[2];
        
        size = sizeof(channels);
        err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
        if(err!=noErr) {
            NSLog(@"error getting channel-numbers");
            return;
        }
        
        size = sizeof(float);
        err = AudioDeviceSetProperty(device, 0, channels[0], false, kAudioDevicePropertyVolumeScalar, size, &volume);
        if(noErr != err) {
            NSLog(@"error setting volume of channel %d", channels[0]);
        }
        
        err = AudioDeviceSetProperty(device, 0, channels[1], false, kAudioDevicePropertyVolumeScalar, size, &volume);
        if(noErr!=err) {
            NSLog(@"error setting volume of channel %d", channels[1]);
        }
    }
}

- (float)volume {
    AudioDeviceID device;
    
    // get device
    UInt32 size = sizeof(device);
    OSStatus err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
    if(err != noErr) {
        NSLog(@"audio-volume error get device");
        return 0.0;
    }
    
    // try get master volume (channel 0)
    float masterVolume;
    
    size = sizeof(masterVolume);
    err = AudioDeviceGetProperty(device, 0, 0, kAudioDevicePropertyVolumeScalar, &size, &masterVolume);
    if(err == noErr) {
        return masterVolume;
    } else {
        // otherwise, try seperate channels
        UInt32 channels[2];
        float channelVolume[2];
        
        size = sizeof(channels);
        err = AudioDeviceGetProperty(device, 0, 0, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
        if(err != noErr) {
            NSLog(@"error getting channel-numbers");
        }
        
        size = sizeof(float);
        err = AudioDeviceGetProperty(device, channels[0], 0, kAudioDevicePropertyVolumeScalar, &size, &channelVolume[0]);
        if(noErr != err) {
            NSLog(@"error getting volume of channel %d", channels[0]);
        }
        
        err = AudioDeviceGetProperty(device, channels[1], 0, kAudioDevicePropertyVolumeScalar, &size, &channelVolume[1]);
        if(noErr != err) {
            NSLog(@"error getting volume of channel %d", channels[1]);
        }
        
        return (channelVolume[0] + channelVolume[1]) / 2.00;
    }
}
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #5
Thanks Josh, the only thing I've noticed is that when I have my program continually setting the system volume to the max, it works until the user turns the volume down to 0 which seems to be the same as mute, in which case the mute must override whatever the system volume is set to. How could I manually toggle mute to overcome this?
Quote this message in a reply
Member
Posts: 144
Joined: 2009.11
Post: #6
NelsonMandella Wrote:Thanks Josh, the only thing I've noticed is that when I have my program continually setting the system volume to the max, it works until the user turns the volume down to 0 which seems to be the same as mute, in which case the mute must override whatever the system volume is set to. How could I manually toggle mute to overcome this?

I shudder to think of what you could possibly be making that would require this.

Everyone's favourite forum lurker!
https://github.com/NSError
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #7
Nevermind, I figured it out.

cmiller Wrote:I shudder to think of what you could possibly be making that would require this.

Nothing to nefarious, I was just writing a little program to randomly play sounds, at a very low volume, which would make it seem like the computer was totally coming apart, electrical currents, vacuums, jackhammers, and even a rubber ducky. I installed it in my roommates computer and it's working beautifully. And volume control was key because he had to believe the sounds were actually coming from the hardware.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #8
LOLLOL

¡Muy nefario!
Quote this message in a reply
Member
Posts: 144
Joined: 2009.11
Post: #9
NelsonMandella Wrote:Nevermind, I figured it out.



Nothing to nefarious, I was just writing a little program to randomly play sounds, at a very low volume, which would make it seem like the computer was totally coming apart, electrical currents, vacuums, jackhammers, and even a rubber ducky. I installed it in my roommates computer and it's working beautifully. And volume control was key because he had to believe the sounds were actually coming from the hardware.

I can haz copy plz? (That's just funny enough for me to put in my funny folder for lolz later on)

Everyone's favourite forum lurker!
https://github.com/NSError
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #10
cmiller Wrote:I can haz copy plz? (That's just funny enough for me to put in my funny folder for lolz later on)


Sure, what's your email?
Quote this message in a reply
Member
Posts: 144
Joined: 2009.11
Post: #11
Hmm, I was hoping you could put it on Github or something...

Failing that, lordSaurontheGreat (at) http://gmail.com should work with attachments up to 20MB.

(Don't worry, that poor address is already in public in far too many places; I have another address for personal use, so those who've already started formulating a reply which goes something like "you shouldn't publicly disclose your email like that" can rest easy.)

Everyone's favourite forum lurker!
https://github.com/NSError
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #12
cmiller Wrote:(Don't worry, that poor address is already in public in far too many places; I have another address for personal use, so those who've already started formulating a reply which goes something like "you shouldn't publicly disclose your email like that" can rest easy.)

On the contrary, we have to be able to communicate, so if you gotta put out the address, then you gotta put out the address. I haven't seen anyone get smacked for sharing an email address in public here for years. Seems like the consensus is that as long as you're at least mildly creative (like you were), and everyone switches it up a little, then that's enough to trip up the email harvesting bots.
Quote this message in a reply
Member
Posts: 144
Joined: 2009.11
Post: #13
AnotherJake Wrote:On the contrary, we have to be able to communicate, so if you gotta put out the address, then you gotta put out the address. I haven't seen anyone get smacked for sharing an email address in public here for years. Seems like the consensus is that as long as you're at least mildly creative (like you were), and everyone switches it up a little, then that's enough to trip up the email harvesting bots.

I'm not convinced. That email is the one I use for all bulk-rate traffic; I have one on my fsdev.net domain that I use for personal and professional stuff. So far the spammers haven't found it! Ninja

Everyone's favourite forum lurker!
https://github.com/NSError
Quote this message in a reply
Post Reply