Ooops
October 28, 2020, 04:46:26 AM

Author Topic: [bb] Simple Channel Fading by _PJ_ [ 1+ years ago ]  (Read 1263 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Simple Channel Fading by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Simple Channel Fading
Author : _PJ_
Posted : 1+ years ago

Description : This is a very simple example only. Intended to outline the concept behind fading from one channel to another. There's a lot that this example does NOT deal with such as checking for the end of the sound being played (ChannelPlaying()=False) or any validation of the sound files themselves.

However, it should serve it purpose as a basis for understanding how to maximise the use of sound channel handles to manipulate the playback of various sounds.

The variables of particular interest are OVERLAP#  which is the total duration in milliseconds that the cross-fade from
one channel to the other should take.

Also A_PATH$ and B_PATH$ need to be populated with full filepaths to the music for each channel.


Code :
Code: BlitzBasic
  1. ; THESE ARE REEQUIRED!
  2. Global CHANNEL_PLAYING
  3. Global CHANNEL_HIDDEN
  4.  
  5. Global PLAYING_VOLUME#=1.00
  6. Global HIDDEN_VOLUME#=0.00
  7.  
  8. Global nb_Fading=False
  9. Global FADE_START_TIME=0
  10.  
  11.  
  12. ;EXAMPLE:
  13.  
  14. Graphics 800,600,32,2
  15. SetBuffer BackBuffer()
  16.  
  17. Const OVERLAP#=5850.0
  18.  
  19. Const A_PATH$="[INSERT FILEPATH HERE]"
  20. Const B_PATH$="[INSERT FILEPATH HERE]"
  21.  
  22. Global SOUND_A=LoadSound(A_PATH)
  23. Global SOUND_B=LoadSound(B_PATH)
  24.  
  25. InitialiseChannels(SOUND_A,SOUND_B)
  26.  
  27. While (Not KeyDown(1))
  28.         Cls
  29.        
  30.         If (nb_Fading)
  31.                
  32.                 UpdateFade
  33.                
  34.         Else
  35.                 Text 0,0,"Press Space Bar to Begin Fade In/Out"
  36.                
  37.                 If (KeyHit(57))
  38.                        
  39.                         BeginFade
  40.                        
  41.                 End If
  42.                
  43.         End If 
  44.        
  45.         Flip
  46.        
  47. Wend
  48.  
  49.        
  50.        
  51. Function InitialiseChannels(SOUNDHandle_A,SOUNDHandle_B)       
  52.         CHANNEL_PLAYING=PlaySound(SOUNDHandle_A)
  53.         CHANNEL_HIDDEN=PlaySound(SOUNDHandle_B)
  54.        
  55.         ChannelVolume CHANNEL_PLAYING,PLAYING_VOLUME#
  56.         ChannelVolume CHANNEL_HIDDEN,HIDDEN_VOLUME#
  57.        
  58.         PauseChannel CHANNEL_HIDDEN
  59.        
  60.         ResumeChannel CHANNEL_PLAYING
  61. End Function
  62.  
  63. Function BeginFade()
  64.         FADE_START_TIME=MilliSecs()
  65.         nb_Fading=True
  66.         ResumeChannel CHANNEL_HIDDEN
  67. End Function
  68.  
  69. Function UpdateFade()
  70.        
  71.         Local Elapsed=(MilliSecs()-FADE_START_TIME)
  72.        
  73.         Text 0,20,"Fading..."
  74.        
  75.         If (Elapsed>OVERLAP)
  76.                         ; It's been too long = swap over channels
  77.                
  78.                 Local spare=CHANNEL_PLAYING
  79.                 CHANNEL_PLAYING=CHANNEL_HIDDEN
  80.                 CHANNEL_HIDDEN=spare
  81.                
  82.                 PLAYING_VOLUME#=1.0
  83.                 HIDDEN_VOLUME#=0.0
  84.                
  85.                         ;we're donw with the faded-out channel
  86.                 PauseChannel CHANNEL_HIDDEN
  87.                
  88.                         ; No longer fading because we've all faded out ;)
  89.                 nb_Fading=False
  90.                
  91.         Else
  92.                
  93.                 Local f_VolumeDrop#=Float(Elapsed/OVERLAP)
  94.                
  95.                 PLAYING_VOLUME#=1.0-(f_VolumeDrop#)
  96.                 HIDDEN_VOLUME#=(f_VolumeDrop#)
  97.         End If
  98.        
  99.                 ;Update Volume on channels
  100.        
  101.         ChannelVolume CHANNEL_PLAYING,PLAYING_VOLUME
  102.         ChannelVolume CHANNEL_HIDDEN,HIDDEN_VOLUME
  103.        
  104.        
  105.         Text 0,40,Str(PLAYING_VOLUME)
  106. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal