Page 1 of 1

How to get sound volume?

Posted: Fri Jul 21, 2017 9:48 pm
by codeit
Hi i have a volume control that i am setting up on my mp3 player and so far i can change the volume fine
but i can't find any info on getting the current volume for when my app boots up so i can set the volume control to
where it is meant to be any help on this would be great thanks.

Code: Select all

Procedure Sound_SetVolume(SoundObject, Volume.f)
  CocoaMessage(0, SoundObject, "setVolume:@", @Volume)
EndProcedure

Re: How to get sound volume?

Posted: Fri Jul 21, 2017 10:57 pm
by Wolfram

Code: Select all

  volume =CocoaMessage(0, SoundObject, "volume")

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 5:15 am
by wilbert
Volume if a float.
To get none integer values, you have to pass a pointer to the variable as the first CocoaMessage procedure argument.

Code: Select all

CocoaMessage(@Volume.f, SoundObject, "volume")
Debug Volume

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 10:55 am
by codeit
Cheers for the input guys but this ok if you already have an mp3 playing
but what i am looking for is a way to obtain the volume before playing so as
to set the volume control at run time i.e system volume etc ...

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 11:28 am
by wilbert
Sound volume is relative to the system volume. It doesn't change it.
If you want to control the system volume, maybe this gives you some ideas.
https://stackoverflow.com/questions/895 ... lume-level

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 12:09 pm
by codeit
Thanks wilbert but what language is that in?
Sorry but it sounds like the right thing but i don't understand
it and not sure how to convert to PureBasic :cry:

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 6:01 pm
by wilbert
codeit wrote:Thanks wilbert but what language is that in?
Sorry but it sounds like the right thing but i don't understand
it and not sure how to convert to PureBasic :cry:
I believe it's C.

It's still available but deprecated since OSX 10.11.
I don't know if there's a better alternative.

Code: Select all

#kAudioHardwarePropertyDefaultOutputDevice = $644F7574; 'dOut'
#kAudioHardwareServiceDeviceProperty_VirtualMasterVolume = $766D7663; 'vmvc'
#kAudioObjectPropertyElementMaster = 0
#kAudioObjectPropertyScopeGlobal = $676C6F62; 'glob'
#kAudioObjectPropertyScopeOutput = $6F757470; 'outp'
#kAudioDevicePropertyScopeOutput = $6F757470; 'outp'
#kAudioObjectSystemObject = 1

Structure AudioObjectPropertyAddress
  mSelector.l
  mScope.l
  mElement.l
EndStructure

ImportC "-framework CoreAudio"
  AudioObjectGetPropertyData(inObjectID, *inAddress, inQualifierDataSize, *inQualifierData, *ioDataSize, *outData)
EndImport

ImportC "-framework AudioToolbox"
  AudioHardwareServiceGetPropertyData(inObjectID, *inAddress, inQualifierDataSize, *inQualifierData, *ioDataSize, *outData)
EndImport  


Define propertyAddress.AudioObjectPropertyAddress
Define.l dataSize, outputDeviceID
Define.f volume

; >> Get deviceID of default output device <<

propertyAddress\mSelector = #kAudioHardwarePropertyDefaultOutputDevice
propertyAddress\mScope = #kAudioObjectPropertyScopeGlobal
propertyAddress\mElement = #kAudioObjectPropertyElementMaster

dataSize = 4
If AudioObjectGetPropertyData(#kAudioObjectSystemObject, @propertyAddress, 0, #Null, @dataSize, @outputDeviceID)
  Debug "error getting default output device"
EndIf

; >> Get virtual master volume <<

propertyAddress\mSelector = #kAudioHardwareServiceDeviceProperty_VirtualMasterVolume
propertyAddress\mScope = #kAudioObjectPropertyScopeOutput
propertyAddress\mElement = #kAudioObjectPropertyElementMaster

dataSize = 4
If AudioHardwareServiceGetPropertyData(outputDeviceID, @propertyAddress, 0, #Null, @dataSize, @volume)
  Debug "error getting virtual master volume"
EndIf

Debug volume

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 7:04 pm
by codeit
Thanks wilbert you sure know your stuff.
I can't see me adding all that code to my project just to be able to obtain the volume value.
I bet if it was Windows it would be one API call job done.
Dam what to do ...... cut down at the last post :shock:

Thanks anyway wilbert

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 7:15 pm
by wilbert
I'm still wondering why you want this.
It's very uncommon for an app to display the system volume unless maybe you want to detect if the user has muted the audio.
Even an app like iTunes shows the volume for the sound that is playing.

Re: How to get sound volume?

Posted: Sat Jul 22, 2017 9:52 pm
by codeit
Having just spent time playing with a different audio player i noticed that the volume is not the master
but a separate volume so this leads me to a question

on other media player on the Mac when the volume is adjusted it stays at that set volume
for every track but on mine when the track changes the volume goes back to default and like vlc
it seems to save the volume state as it comes back on with the same volume strange
In Windows you have the registry and ini files to save things in but not sure with the imac.
at the moment i am using this procedure to change the volume

Code: Select all

Procedure Sound_SetVolume(SoundObject, Volume.f)
  CocoaMessage(0, SoundObject, "setVolume:@", @Volume)
EndProcedure


This works ok but if i shutdown my app and start it again the volume is back to default.

Any ideas, help, pointers would be great thanks for your time.

Re: How to get sound volume?

Posted: Sun Jul 23, 2017 5:26 am
by wilbert
codeit wrote:In Windows you have the registry and ini files to save things in but not sure with the imac.
The normal way for a Mac app is to use NSUserDefaults.
http://www.purebasic.fr/english/viewtop ... 04#p404904
But it's also possible to work with PureBasic commands like CreatePreferences, OpenPreferences etc. to store things in a preference file.