Skip to content

Instantly share code, notes, and snippets.

@noidexe
Last active April 13, 2021 23:07
Show Gist options
  • Save noidexe/16fb8d5e45db5829cc898bc2a782717d to your computer and use it in GitHub Desktop.
Save noidexe/16fb8d5e45db5829cc898bc2a782717d to your computer and use it in GitHub Desktop.
User icon.png as Audio
tool
extends AudioStreamPlayer
# "We were so preoccupied with whether or not we could, we didn’t stop to think if we should."
var sample_hz = 8000.0 # Adjust and enjoy the sound of nightmares
var playback: AudioStreamPlayback = null # Actual playback stream, assigned in _init().
var _stream = PoolRealArray()
var stream_pos = 0
var stream_len = 0
func _fill_buffer():
var to_fill = playback.get_frames_available()
while to_fill > 0:
playback.push_frame(Vector2(_stream[stream_pos],_stream[-stream_pos]))
stream_pos = wrapi(stream_pos+1, 0, stream_len)
to_fill -= 1
func _process(_delta):
_fill_buffer()
func _init():
stream = AudioStreamGenerator.new()
stream.mix_rate = sample_hz # Setting mix rate is only possible before play().
load_icon_as_audio()
playback = get_stream_playback()
_fill_buffer() # Prefill, do before play() to avoid delay.
func load_icon_as_audio():
var file = File.new()
file.open("res://icon.png", File.READ)
while not file.eof_reached():
_stream.append( (file.get_8() - 128) /128.0 )
stream_len = _stream.size()
file.close()
@guy-does-things
Copy link

this is the best thing ever

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment