How to set sound playback volume?

Just starting out? Need help? Post your questions and find answers here.
lanciamarcos
Posts: 3
Joined: Thu Nov 28, 2024 6:26 am

How to set sound playback volume?

Post by lanciamarcos »

Hi all,

I'm a new user, enjoying SpiderBasic so far.

Just curious how the volume of a sound can be altered in code.

Here's what I've been working with, starting from the documentation and modifying it based on what I'm reading.

I'm not able to get the volume to change, whether it's a .wav or .mp3 file.

Code: Select all

  InitSound()

  Procedure Loading(Type, Filename$, ObjectId)
    Debug Filename$ + " loaded (id = " + ObjectId + ")"
    
    PlaySound(0) ; play the sound
  EndProcedure
    
  Procedure LoadingError(Type, Filename$)
    Debug Filename$ + ": loading error"
  EndProcedure
    
  ; Register the loading event before calling any resource load command
  BindEvent(#PB_Event_Loading, @Loading())
  BindEvent(#PB_Event_LoadingError, @LoadingError())
  
  ; This works and plays fine
  If LoadSound(0, "raf-self-control.mp3") ; Be sure this sound is available (the source file should be saved in the SpiderBasic/Examples drawer)
    
    ; This doesn't seem to adjust the volume
    ;PlaySound(0,#PB_Sound_Loop,50)
    
    ; This also doesn't seem to do anything
    SoundVolume(0, 0)
    
  EndIf
Thanks for any feedback.
User avatar
Caronte3D
Posts: 187
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: How to set sound playback volume?

Post by Caronte3D »

You must wait to the sound file to be loaded.
If you put the: SoundVolume(0, 0) after PlaySound(0) (inside Loading procedure), it works as expected.
Fred
Site Admin
Posts: 1820
Joined: Mon Feb 24, 2014 10:51 am

Re: How to set sound playback volume?

Post by Fred »

That said it would be better if it could work before the sound start or you can have a small time where it play with the wrong volume. I will investigate this.
lanciamarcos
Posts: 3
Joined: Thu Nov 28, 2024 6:26 am

Re: How to set sound playback volume?

Post by lanciamarcos »

Caronte3D wrote: Thu Dec 05, 2024 7:27 am You must wait to the sound file to be loaded.
If you put the: SoundVolume(0, 0) after PlaySound(0) (inside Loading procedure), it works as expected.
Ah? Like this? I couldn't get it to work unless I removed the PlaySound(). I tried with PlaySound(0) etc. (Windows 11 if it matters)

Code: Select all

  ; This example plays nothing, until you comment out PlaySound()
  If LoadSound(0, "raf-self-control.mp3") ; Be sure this sound is available (the source file should be saved in the SpiderBasic/Examples drawer)
    
    ; Adding this back in results in no sound at all
    PlaySound(0,#PB_Sound_Loop,20)
    
    ; This also doesn't seem to do anything by itself.
    ; In combination with PlaySound(), there is no audio even at 100 volume.
    ; Without PlaySound(), it does not set the volume.
    SoundVolume(0, 20)
    
  EndIf
If any workarounds are available I would like to try them out.
That said it would be better if it could work before the sound start or you can have a small time where it play with the wrong volume. I will investigate this.
Cool, thank you Fred.
User avatar
Caronte3D
Posts: 187
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: How to set sound playback volume?

Post by Caronte3D »

As you can see, the volume goes from 0 to 100 and vice versa:

Code: Select all

  InitSound()

  Procedure Loading(Type, Filename$, ObjectId)
    Debug Filename$ + " loaded (id = " + ObjectId + ")"
    PlaySound(0) ; play the sound
  EndProcedure
    
  Procedure LoadingError(Type, Filename$)
    Debug Filename$ + ": loading error"
  EndProcedure
  
  Procedure WaveVolume()
    Static volume=0, incre=0
    If volume=100 : incre=-1 : EndIf
    If volume=0 : incre=1 : EndIf
    volume+incre
    SoundVolume(0, volume)
  EndProcedure
  
  ; Register the loading event before calling any resource load command
  BindEvent(#PB_Event_Loading, @Loading())
  BindEvent(#PB_Event_LoadingError, @LoadingError())
  
  
  ; This works and plays fine
  If LoadSound(0, "./raf-self-control.mp3") ; Be sure this sound is available (the source file should be saved in the SpiderBasic/Examples drawer)
    AddTimer(0, 40) ; First timer every 100 seconds
    BindEvent(#PB_Event_Timer, @WaveVolume())
  EndIf
Fred
Site Admin
Posts: 1820
Joined: Mon Feb 24, 2014 10:51 am

Re: How to set sound playback volume?

Post by Fred »

See Caronte3D answer, that's the correct way to play a sound and setting the volume already work with PlaySound()
Post Reply