Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- """
- Simple music player with cross-fade support.
- Make a scene, attach this script to it and put it into autoload.
- Every single song shout be a child of the root node, like this:
- https://imgur.com/a/f3bmzWq
- """
- const FADE_SPEED = 12.0
- var _songs : Array
- var _current_song = null
- func _ready():
- _songs = get_children()
- func _process(delta):
- for song in _songs:
- if song == _current_song:
- song.volume_db = move_towards(song.volume_db, linear2db(1.0), delta * FADE_SPEED)
- elif song.playing:
- song.volume_db = move_towards(song.volume_db, linear2db(0.1), delta * FADE_SPEED)
- if song.volume_db <= linear2db(0.1):
- song.stop()
- func play(song_name, fade = true):
- var new_song = find_node(song_name)
- if _current_song == new_song:
- return
- _current_song = new_song
- if _current_song != null:
- if fade:
- _current_song.volume_db = linear2db(0.1)
- else:
- _current_song.volume_db = linear2db(1.0)
- _current_song.play()
- func stop(fade = true):
- if !fade && _current_song != null:
- _current_song.stop()
- _current_song.volume_db = 0.1
- _current_song = null
- func move_towards(from, to, max_step):
- if to > from:
- from += max_step
- if from > to:
- from = to
- elif to < from:
- from -= max_step
- if from < to:
- from = to
- return from
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement