Skip to content

Commit 8fc061a

Browse files
committed
feat: change sine wave to triangle wave
This gives the sound a more pleasant, lively, chiptune feeling.
1 parent 1b4bcd7 commit 8fc061a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

views/array_sound.gd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extends Node
33

44
const SAMPLE_HZ = 44100
55
const MIN_HZ = 110
6-
const MAX_HZ = 440
6+
const MAX_HZ = 880
77

88
var frac: float
99
var player = AudioStreamPlayer.new()
@@ -13,7 +13,7 @@ var _playback: AudioStreamGeneratorPlayback
1313
func _fill_buffer(pulse_hz):
1414
var increment = pulse_hz / SAMPLE_HZ
1515
for i in range(_playback.get_frames_available()):
16-
_playback.push_frame(Vector2.ONE * sin(_phase * TAU))
16+
_playback.push_frame(Vector2.ONE * triangle(_phase))
1717
_phase = fmod(_phase + increment, 1.0)
1818

1919
func _process(delta):
@@ -26,3 +26,7 @@ func _init():
2626
player.stream.mix_rate = SAMPLE_HZ
2727
_playback = player.get_stream_playback()
2828
player.play()
29+
30+
func triangle(x):
31+
"""Generate a triangle wave from the given phase."""
32+
return 2 / PI * asin(sin(PI * x))

0 commit comments

Comments
 (0)