|
1 | 1 | <?xml version="1.0" encoding="UTF-8" ?>
|
2 | 2 | <class name="AudioStreamGenerator" inherits="AudioStream" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
|
3 | 3 | <brief_description>
|
4 |
| - Audio stream that generates sounds procedurally. |
| 4 | + An audio stream with utilities for procedural sound generation. |
5 | 5 | </brief_description>
|
6 | 6 | <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. |
8 | 29 | See also [AudioEffectSpectrumAnalyzer] for performing real-time audio spectrum analysis.
|
9 | 30 | [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.
|
10 | 31 | </description>
|
11 | 32 | <tutorials>
|
12 | 33 | <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> |
14 | 34 | </tutorials>
|
15 | 35 | <members>
|
16 | 36 | <member name="buffer_length" type="float" setter="set_buffer_length" getter="get_buffer_length" default="0.5">
|
|
0 commit comments