Created
April 11, 2020 15:21
-
-
Save MatteoJoliveau/9fb72d73fedeb2d5cfef90c8925ba42c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
onready var player_a: AudioStreamPlayer = AudioStreamPlayer.new() | |
onready var player_b: AudioStreamPlayer = AudioStreamPlayer.new() | |
export (int) var cross_fade_speed: int = 100 | |
var max_volume: float = 0 | |
var silence_db: float = -80.0 | |
var active_player: AudioStreamPlayer | |
var target_player: AudioStreamPlayer | |
var cross_fading: bool | |
func _ready(): | |
player_a.autoplay = false | |
player_b.autoplay = false | |
player_a.volume_db = silence_db | |
player_b.volume_db = silence_db | |
add_child(player_a) | |
add_child(player_b) | |
active_player = player_a | |
target_player = player_b | |
func _process(delta: float): | |
if !cross_fading: | |
return | |
if active_player.volume_db == silence_db and target_player.volume_db == max_volume: | |
print("Cross fading completed") | |
active_player.stop() | |
prints(active_player, "is the active player") | |
prints(target_player, "is the target player") | |
print("Switching") | |
var tmp = active_player | |
active_player = target_player | |
target_player = tmp | |
prints("Now", active_player, "is the active player") | |
prints("Now", target_player, "is the target player") | |
cross_fading = false | |
return | |
var amount = cross_fade_speed * delta | |
if active_player.volume_db > silence_db: | |
active_player.volume_db -= amount | |
active_player.volume_db = max(active_player.volume_db, silence_db) | |
if target_player.volume_db < max_volume: | |
target_player.volume_db += amount | |
target_player.volume_db = min(target_player.volume_db, max_volume) | |
func play_stream(new_stream: AudioStream): | |
if !active_player.is_playing(): | |
active_player.stream = new_stream | |
active_player.volume_db = max_volume | |
active_player.play() | |
return | |
target_player.stream = new_stream | |
target_player.play() | |
cross_fading = true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment