Skip to content

Commit 4270648

Browse files
committed
feat: add ArraySound class as ArrayView dependency
This simple implementation plays a constant sound that is only implemented on bubble sort as the arithmetic average of the two values being compared.
1 parent 8712c98 commit 4270648

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

levels/bubble_sort.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ func get_effect(i):
4848
if i >= _end:
4949
return EFFECTS.DIMMED
5050
return EFFECTS.NONE
51+
52+
func get_frac():
53+
return (array.frac(_index) + array.frac(_index + 1)) / 2.0

project.godot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ _global_script_classes=[ {
1414
"language": "GDScript",
1515
"path": "res://models/array_model.gd"
1616
}, {
17+
"base": "Node",
18+
"class": "ArraySound",
19+
"language": "GDScript",
20+
"path": "res://views/array_sound.gd"
21+
}, {
1722
"base": "HBoxContainer",
1823
"class": "ArrayView",
1924
"language": "GDScript",
@@ -86,6 +91,7 @@ _global_script_classes=[ {
8691
} ]
8792
_global_script_class_icons={
8893
"ArrayModel": "",
94+
"ArraySound": "",
8995
"ArrayView": "",
9096
"BinaryTreeModel": "",
9197
"BogoSort": "",

views/array_sound.gd

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class_name ArraySound
2+
extends Node
3+
4+
const SAMPLE_HZ = 44100
5+
const MIN_HZ = 110
6+
const MAX_HZ = 440
7+
8+
var frac: float
9+
var player = AudioStreamPlayer.new()
10+
var _phase = 0.0
11+
var _playback: AudioStreamGeneratorPlayback
12+
13+
func _fill_buffer(pulse_hz):
14+
var increment = pulse_hz / SAMPLE_HZ
15+
for i in range(_playback.get_frames_available()):
16+
_playback.push_frame(Vector2.ONE * sin(_phase * TAU))
17+
_phase = fmod(_phase + increment, 1.0)
18+
19+
func _process(delta):
20+
_fill_buffer(MIN_HZ + (MAX_HZ - MIN_HZ) * frac)
21+
22+
func _init():
23+
add_child(player)
24+
player.stream = AudioStreamGenerator.new()
25+
player.stream.buffer_length = 0.05
26+
player.stream.mix_rate = SAMPLE_HZ
27+
_playback = player.get_stream_playback()
28+
player.play()

views/array_view.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ var _rects = []
1616
var _positions = []
1717
var _pointer = Polygon2D.new()
1818
var _pointer_size: int
19+
var _sound = ArraySound.new()
1920
onready var _separation = 128 / _level.array.size
2021

2122
func _init(level):
2223
_level = level
2324
add_child(_level) # NOTE: This is necessary for it to read input
2425
add_child(_tween) # NOTE: This is necessary for it to animate
2526
add_child(_pointer)
27+
add_child(_sound)
2628
_pointer.hide()
2729

2830
func _ready():
@@ -65,6 +67,7 @@ func _ready():
6567
_pointer.show()
6668

6769
func _process(delta):
70+
_sound.frac = _level.get_frac()
6871
if _pointer.visible:
6972
var pointed = _level.get_pointer()
7073
var height = rect_size.y - _pointer_size * 2
@@ -76,6 +79,7 @@ func _process(delta):
7679

7780
func _on_ComparisonSort_done():
7881
set_process(false)
82+
_sound.player.stop()
7983
_pointer.hide()
8084
for i in range(_rects.size()):
8185
_rects[i].color = ComparisonSort.EFFECTS.NONE

0 commit comments

Comments
 (0)