Page 1 of 1

Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 3:54 am
by Quin
In my app, I want to gradually fade the volume of a sound until it goes completely silent. I found the MiniAudio function ma_sound_set_fade_in_milliseconds, but can't figure out how to wrap it in PB. Has anyone does this and/or can provide any tips?
Thanks!

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 3:56 am
by BarryG
Quin wrote: Fri Mar 15, 2024 3:54 amIn my app, I want to gradually fade the volume of a sound
Since it's a sound in your own app, why not use the SoundVolume() command?

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 4:51 am
by Quin
From what I could see, that doesn't provide an easy way to gradually change the volume like MiniAudio seems to, because there's no way to query the current volume :(

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 5:33 am
by idle
Hi Quin,

I'm not sure how Freds implemented the sounds in miniaudio and how to get the sound pointer back, also the included lib is considerably cut down and lacks some of the functions to load a sound so you can connect it to the effects node graph.
I have been working on an an example with my pbcex tool and while it works as an executable I can't turn it into a static lib which is odd.

edit:

Well actually I have managed to do it with miniaudiox64.dll and then importing it and wrapping it in a dll, so the functions are exposed in a PB friendly wrapper. It will take me a while to clean up the mess but I will make a zip post it

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 6:19 am
by BarryG
Quin wrote: Fri Mar 15, 2024 4:51 amFrom what I could see, that doesn't provide an easy way to gradually change the volume like MiniAudio seems to, because there's no way to query the current volume :(
But it's your app, so you've set the volume and would know the current volume level. Or are you talking about controlling the volume of another app?

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 6:28 am
by idle
here's a x64 test to play mp3 with optional delay decay and the fade
https://www.dropbox.com/scl/fi/j7a1ftav ... oi1wr&dl=0

Code: Select all

engine = ma_init() 
sound = ma_LoadSound(engine,"E:\Music\motat.mp3",0.6,0.25) 
ma_sound_set_fade_in_milliseconds(sound,1.0,0.1,15000)
ma_PlaySound(sound)

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 8:07 am
by RASHAD
Maybe
#1 :

Code: Select all

 InitSound()           ; Initialize Sound system
 UseOGGSoundDecoder()  ; Use ogg files

 LoadSound(0, GetTemporaryDirectory()+"01AlicesRestaurantMassacree.ogg")
 
 length = SoundLength(0,#PB_Sound_Millisecond)
 interval = Length / 1000
 volume = 100
 PlaySound(0)
 Repeat
  For i = 1 To interval
   SoundVolume(0, volume)
   Delay(1000)
   volume = volume - 5
   If volume = 0
    Break
   EndIf
  Next 
Until volume = 0 
 FreeSound(0) ; The sound is freed
End
#2 :
Simple and direct

Code: Select all

InitSound()           ; Initialize Sound system
UseOGGSoundDecoder()  ; Use ogg files

LoadSound(0, GetTemporaryDirectory()+"01AlicesRestaurantMassacree.ogg")

volume = 100
PlaySound(0)
Repeat
  SoundVolume(0, volume)
  Delay(100)
  volume - 1
Until volume <= 0 
FreeSound(0) ; The sound is freed
End

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 9:21 am
by Fred
As BarryG said you are the code here so you know the sound volume you put for your sounds, then you can easily change it to 0 gradually like Rashad shown.

Re: Fading a sound's volume using MiniAudio

Posted: Fri Mar 15, 2024 2:29 pm
by Quin
Thanks everyone! Here's what I came up with, in case it's ever useful to anyone :)

Code: Select all

; Simple procedure to fade a sound out over a given duration using MiniAudio.
; Written by Quin on Thursday, March 14, 2024

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf

;{ Declares
Declare FadeSound(Sound, InitialVolume = 100, TargetVolume = 0, SweepDuration = 20)
;} End declares

;{ Procedures
Procedure FadeSound(Sound, InitialVolume = 100, TargetVolume = 0, SweepDuration = 20)
  Protected I
  For I = InitialVolume To TargetVolume Step -5
    SoundVolume(Sound, I)
    Delay(SweepDuration)
  Next I
EndProcedure
;} End procedures

;{ Test
CompilerIf #PB_Compiler_IsMainFile
  InitSound()
  UseOGGSoundDecoder()
  LoadSound(0, "C:\Program Files\PureBasic\Examples\3D\Data\Roar.ogg")
  PlaySound(0)
  FadeSound(0)
CompilerEndIf
;} End test

Re: Fading a sound's volume using MiniAudio

Posted: Sat Mar 16, 2024 10:59 pm
by idle
I made an wrapper dll to ma_sound windows x64 only
https://www.purebasic.fr/english/viewtopic.php?t=83822