Page 1 of 1

How to set sound playback volume?

Posted: Wed Dec 04, 2024 11:40 pm
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.

Re: How to set sound playback volume?

Posted: Thu Dec 05, 2024 7:27 am
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.

Re: How to set sound playback volume?

Posted: Thu Dec 05, 2024 11:20 am
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.

Re: How to set sound playback volume?

Posted: Fri Dec 06, 2024 2:50 am
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.

Re: How to set sound playback volume?

Posted: Sat Dec 07, 2024 11:46 pm
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

Re: How to set sound playback volume?

Posted: Thu Jan 30, 2025 10:18 am
by Fred
See Caronte3D answer, that's the correct way to play a sound and setting the volume already work with PlaySound()