How to get sound volume?

Mac OSX specific forum
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

How to get sound volume?

Post 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
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: How to get sound volume?

Post by Wolfram »

Code: Select all

  volume =CocoaMessage(0, SoundObject, "volume")
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get sound volume?

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: How to get sound volume?

Post 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 ...
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get sound volume?

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: How to get sound volume?

Post 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:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get sound volume?

Post 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
Last edited by wilbert on Sat Jul 22, 2017 7:12 pm, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: How to get sound volume?

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get sound volume?

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: How to get sound volume?

Post 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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get sound volume?

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply