Kitomas

"AudioManager.gd" v1

Oct 24th, 2025 (edited)
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 6.31 KB | Gaming | 0 0
  1. ## Script for the AudioManager singleton
  2. extends Node
  3.  
  4.  
  5.  
  6. ## Volume of Master bus (0.0 -> 1.0, affects all other busses)
  7. var vol_Master: float:
  8.     set(volume): vol_Master = __set_bus_volume(__idx_Master, volume)
  9.  
  10. ## Volume of User Interface SFX bus (0.0 -> 1.0)
  11. var vol_UI: float:
  12.     set(volume): vol_UI = __set_bus_volume(__idx_UI, volume)
  13.  
  14. ## Volume of DSP Effects bus (0.0 -> 1.0, affects Music and SFX busses)
  15. var vol_DSP: float:
  16.     set(volume): vol_DSP = __set_bus_volume(__idx_DSP, volume)
  17.  
  18. ## Volume of Music bus (0.0 -> 1.0)
  19. var vol_Music: float:
  20.     set(volume): vol_Music = __set_bus_volume(__idx_Music, volume)
  21.  
  22. ## Volume of Sound Effects bus (0.0 -> 1.0)
  23. var vol_SFX: float:
  24.     set(volume): vol_SFX = __set_bus_volume(__idx_SFX, volume)
  25.  
  26.  
  27.  
  28.  
  29.  
  30. ## Plays a non-spatial, oneshot sound effect
  31. func play_sfx(stream:AudioStream, volume:float = 1.0) -> void:
  32.     var player := AudioStreamPlayer.new()
  33.  
  34.     player.bus = &"SFX"
  35.     player.stream = stream
  36.     player.volume_linear = vol_Master * vol_SFX * volume
  37.  
  38.     add_child(player)
  39.     player.play()
  40.  
  41.     # Notes for this (in the future, double check if this info is correct):
  42.     # AudioStreamOggVorbis                       [*] Works as expected
  43.     # AudioStreamWAV                             [*] Works for non-looping WAVs
  44.     # AudioStreamMP3                             [*] Works as expected
  45.     # AudioStreamRandomPitch/AudioStreamPlaylist [*] Emits after last entry
  46.     # AudioStreamGenerator                       [ ] Infinite stream, never "finishes"
  47.     # AudioStreamPolyphonic                      [ ] Manages multiple sources
  48.     '''
  49.     player.finished.connect(player.queue_free)
  50.     '''
  51.     # Potential improvement?
  52.     var duration := player.stream.get_length()
  53.     get_tree().create_timer(duration).timeout.connect(player.queue_free)
  54.  
  55.  
  56.  
  57.  
  58.  
  59. func play_music(stream:AudioStream, fade_time_seconds:float = 1.0) -> void:
  60.     # If same track is already playing, return early
  61.     if __player_Music.stream == stream and __player_Music.playing: return
  62.  
  63.     # If a track is already playing, fade-out before playing the next track
  64.     if __player_Music.playing:
  65.         if __tween_Music: __tween_Music.kill()
  66.         __tween_Music = get_tree().create_tween()
  67.         __tween_Music.set_trans(Tween.TRANS_SINE)
  68.         __tween_Music.set_ease(Tween.EASE_IN_OUT)
  69.  
  70.         # Set fade-out tween
  71.         __tween_Music.tween_property(__player_Music, "volume_db", __MUSIC_DB_MIN, fade_time_seconds)
  72.         # 'Fade-in and switch track' is called after fade-out
  73.         __tween_Music.tween_callback(Callable(self, "__switch_music_track").bind(stream, fade_time_seconds))
  74.  
  75.     # Otherwise, simply fade-in
  76.     else:
  77.         __switch_music_track(stream, fade_time_seconds)
  78.  
  79.  
  80.  
  81.  
  82.  
  83. #region Overrides and Signals
  84. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
  85.  
  86. func _ready() -> void:
  87.     # This should be redundant, but just in case
  88.     __clear_all_busses()
  89.  
  90.     # Set up busses
  91.     __idx_UI = __append_bus(&"UI")
  92.     __idx_DSP = __append_bus(&"DSP")
  93.     __idx_Music = __append_bus(&"Music", &"DSP")
  94.     __idx_SFX = __append_bus(&"SFX", &"DSP")
  95.  
  96.     # Initialize volumes (should invoke their setter functions)
  97.     vol_Master = 1.0
  98.     vol_UI = 1.0
  99.     vol_DSP = 1.0
  100.     vol_Music = 1.0
  101.     vol_SFX = 1.0
  102.  
  103.     # Set up music player
  104.     __player_Music = AudioStreamPlayer.new()
  105.     __player_Music.bus = &"Music"
  106.     # (Technically, it's not necessary to add this as a child, given that this
  107.     #  is an autoload script, and there's only one instance of __player_Music,
  108.     #  that's created the moment the game runs. However, there's not much of a
  109.     #  reason *not* to either, and it's idiomatic to do so anyway.)
  110.     add_child(__player_Music)
  111.  
  112.  
  113.  
  114.  
  115.  
  116. #endregion Overrides and Signals
  117. #region Internals
  118. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
  119.  
  120. ## Used to sync relevant resource accesses if multithreading is eventually added
  121. #var __mutex := Mutex.new()
  122.  
  123.  
  124.  
  125. ## Added for consistency, since this is always 0
  126. const __idx_Master: int = 0
  127.  
  128. ## Bus index for user interface sounds (should ignore DSP effects, and is sent straight to Master)
  129. var __idx_UI: int
  130.  
  131. ## Bus index for the DSP (Digital Signal Processing) audio effects' send bus.
  132. ## While both Music and SFX are sent to this bus, its usage may be limited, given
  133. ## that its main purpose is for applying effects that affect both Music and SFX.
  134. var __idx_DSP: int
  135.  
  136. ## Bus index for music; output is sent to DSP
  137. var __idx_Music: int
  138.  
  139. ## Bus index for sound effects; output is sent to DSP
  140. var __idx_SFX: int
  141.  
  142.  
  143.  
  144. ## The decibel level that music fades in from, and fades out to
  145. const __MUSIC_DB_MIN: float = -80.0
  146.  
  147. ## Used for tweening music fade ins/outs
  148. var __tween_Music: Tween
  149.  
  150. ## The stream player used for music
  151. var __player_Music: AudioStreamPlayer
  152.  
  153.  
  154.  
  155.  
  156.  
  157. ## Removes all busses except for Master (which can't be removed anyway),
  158. ## as well as removing any effects from Master.
  159. func __clear_all_busses() -> void:
  160.     # Remove busses
  161.     while AudioServer.bus_count > 1:
  162.         AudioServer.remove_bus(AudioServer.bus_count-1)
  163.  
  164.     # Remove effects
  165.     while AudioServer.get_bus_effect_count(__idx_Master) > 0:
  166.         var last_effect_index: int = AudioServer.get_bus_effect_count(__idx_Master)-1
  167.         AudioServer.remove_bus_effect(__idx_Master, last_effect_index)
  168.  
  169.  
  170.  
  171.  
  172.  
  173. ## Appends a bus to the list, with an optional bus to send to (i.e. target)
  174. func __append_bus(bus_name:String, send_name:StringName = &"Master") -> int:
  175.     # Will be equal to bus_count-1 after adding the new one
  176.     var new_index: int = AudioServer.bus_count
  177.  
  178.     AudioServer.add_bus()
  179.     AudioServer.set_bus_name(new_index, bus_name)
  180.     AudioServer.set_bus_send(new_index, send_name)
  181.  
  182.     return new_index
  183.  
  184.  
  185.  
  186.  
  187.  
  188. func __set_bus_volume(bus_index:int, volume:float) -> float:
  189.     volume = clampf(volume, 0.0, 1.0)
  190.     AudioServer.set_bus_volume_linear(bus_index, volume)
  191.     return volume
  192.  
  193.  
  194.  
  195.  
  196.  
  197. func __switch_music_track(stream:AudioStream, fade_in_seconds: float) -> void:
  198.     __player_Music.stop()
  199.     __player_Music.stream = stream
  200.     __player_Music.volume_db = __MUSIC_DB_MIN # Set decibels to this initially
  201.     __player_Music.play()
  202.  
  203.     if __tween_Music: __tween_Music.kill()
  204.  
  205.     __tween_Music = get_tree().create_tween()
  206.     __tween_Music.set_trans(Tween.TRANS_SINE)
  207.     __tween_Music.set_ease(Tween.EASE_IN_OUT)
  208.  
  209.     __tween_Music.tween_property(__player_Music, "volume_db",
  210.                                  linear_to_db(1.0), fade_in_seconds)
  211.  
  212.  
  213.  
  214.  
  215.  
  216. #endregion Internals
  217.  
  218. # TODO:
  219. # add dsp effects
  220.  
  221. # (EOF)
  222.  
Advertisement
Add Comment
Please, Sign In to add comment