Fading a sound's volume using MiniAudio

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Fading a sound's volume using MiniAudio

Post 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!
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Fading a sound's volume using MiniAudio

Post 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?
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Fading a sound's volume using MiniAudio

Post 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 :(
User avatar
idle
Always Here
Always Here
Posts: 6031
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Fading a sound's volume using MiniAudio

Post 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
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Fading a sound's volume using MiniAudio

Post 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?
User avatar
idle
Always Here
Always Here
Posts: 6031
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Fading a sound's volume using MiniAudio

Post 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)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Fading a sound's volume using MiniAudio

Post 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
Last edited by RASHAD on Fri Mar 15, 2024 3:12 pm, edited 1 time in total.
Egypt my love
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Fading a sound's volume using MiniAudio

Post 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.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Fading a sound's volume using MiniAudio

Post 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
User avatar
idle
Always Here
Always Here
Posts: 6031
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Fading a sound's volume using MiniAudio

Post by idle »

I made an wrapper dll to ma_sound windows x64 only
https://www.purebasic.fr/english/viewtopic.php?t=83822
Post Reply