Skip to content

Commit 64eeb04

Browse files
authored
Merge pull request #76373 from MewPurPur/add-AudioStreamGenerator-example
2 parents 921d231 + 9d77caa commit 64eeb04

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

doc/classes/AudioStreamGenerator.xml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<class name="AudioStreamGenerator" inherits="AudioStream" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
33
<brief_description>
4-
Audio stream that generates sounds procedurally.
4+
An audio stream with utilities for procedural sound generation.
55
</brief_description>
66
<description>
7-
This audio stream does not play back sounds, but expects a script to generate audio data for it instead. See also [AudioStreamGeneratorPlayback].
7+
[AudioStreamGenerator] is a type of audio stream that does not play back sounds on its own; instead, it expects a script to generate audio data for it. See also [AudioStreamGeneratorPlayback].
8+
Here's a sample on how to use it to generate a sine wave:
9+
[codeblock]
10+
var playback # Will hold the AudioStreamGeneratorPlayback.
11+
@onready var sample_hz = $AudioStreamPlayer.stream.mix_rate
12+
var pulse_hz = 440.0 # The frequency of the sound wave.
13+
14+
func _ready():
15+
$AudioStreamPlayer.play()
16+
playback = $AudioStreamPlayer.get_stream_playback()
17+
fill_buffer()
18+
19+
func fill_buffer():
20+
var phase = 0.0
21+
var increment = pulse_hz / sample_hz
22+
var frames_available = playback.get_frames_available()
23+
24+
for i in range(frames_available):
25+
playback.push_frame(Vector2.ONE * sin(phase * TAU))
26+
phase = fmod(phase + increment, 1.0)
27+
[/codeblock]
28+
In the example above, the "AudioStreamPlayer" node must use an [AudioStreamGenerator] as its stream. The [code]fill_buffer[/code] function provides audio data for approximating a sine wave.
829
See also [AudioEffectSpectrumAnalyzer] for performing real-time audio spectrum analysis.
930
[b]Note:[/b] Due to performance constraints, this class is best used from C# or from a compiled language via GDExtension. If you still want to use this class from GDScript, consider using a lower [member mix_rate] such as 11,025 Hz or 22,050 Hz.
1031
</description>
1132
<tutorials>
1233
<link title="Audio Generator Demo">https://godotengine.org/asset-library/asset/526</link>
13-
<link title="Godot 3.2 will get new audio features">https://godotengine.org/article/godot-32-will-get-new-audio-features</link>
1434
</tutorials>
1535
<members>
1636
<member name="buffer_length" type="float" setter="set_buffer_length" getter="get_buffer_length" default="0.5">

0 commit comments

Comments
 (0)