123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- :github_url: hide
- .. DO NOT EDIT THIS FILE!!!
- .. Generated automatically from Godot engine sources.
- .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
- .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/AudioStreamGenerator.xml.
- .. _class_AudioStreamGenerator:
- AudioStreamGenerator
- ====================
- **Inherits:** :ref:`AudioStream<class_AudioStream>` **<** :ref:`Resource<class_Resource>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
- An audio stream with utilities for procedural sound generation.
- .. rst-class:: classref-introduction-group
- Description
- -----------
- **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 :ref:`AudioStreamGeneratorPlayback<class_AudioStreamGeneratorPlayback>`.
- Here's a sample on how to use it to generate a sine wave:
- .. tabs::
- .. code-tab:: gdscript
- var playback # Will hold the AudioStreamGeneratorPlayback.
- @onready var sample_hz = $AudioStreamPlayer.stream.mix_rate
- var pulse_hz = 440.0 # The frequency of the sound wave.
-
- func _ready():
- $AudioStreamPlayer.play()
- playback = $AudioStreamPlayer.get_stream_playback()
- fill_buffer()
-
- func fill_buffer():
- var phase = 0.0
- var increment = pulse_hz / sample_hz
- var frames_available = playback.get_frames_available()
-
- for i in range(frames_available):
- playback.push_frame(Vector2.ONE * sin(phase * TAU))
- phase = fmod(phase + increment, 1.0)
- .. code-tab:: csharp
- [Export] public AudioStreamPlayer Player { get; set; }
-
- private AudioStreamGeneratorPlayback _playback; // Will hold the AudioStreamGeneratorPlayback.
- private float _sampleHz;
- private float _pulseHz = 440.0f; // The frequency of the sound wave.
-
- public override void _Ready()
- {
- if (Player.Stream is AudioStreamGenerator generator) // Type as a generator to access MixRate.
- {
- _sampleHz = generator.MixRate;
- Player.Play();
- _playback = (AudioStreamGeneratorPlayback)Player.GetStreamPlayback();
- FillBuffer();
- }
- }
-
- public void FillBuffer()
- {
- double phase = 0.0;
- float increment = _pulseHz / _sampleHz;
- int framesAvailable = _playback.GetFramesAvailable();
-
- for (int i = 0; i < framesAvailable; i++)
- {
- _playback.PushFrame(Vector2.One * (float)Mathf.Sin(phase * Mathf.Tau));
- phase = Mathf.PosMod(phase + increment, 1.0);
- }
- }
- In the example above, the "AudioStreamPlayer" node must use an **AudioStreamGenerator** as its stream. The ``fill_buffer`` function provides audio data for approximating a sine wave.
- See also :ref:`AudioEffectSpectrumAnalyzer<class_AudioEffectSpectrumAnalyzer>` for performing real-time audio spectrum analysis.
- \ **Note:** 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 :ref:`mix_rate<class_AudioStreamGenerator_property_mix_rate>` such as 11,025 Hz or 22,050 Hz.
- .. rst-class:: classref-introduction-group
- Tutorials
- ---------
- - `Audio Generator Demo <https://godotengine.org/asset-library/asset/526>`__
- .. rst-class:: classref-reftable-group
- Properties
- ----------
- .. table::
- :widths: auto
- +---------------------------+-------------------------------------------------------------------------+-------------+
- | :ref:`float<class_float>` | :ref:`buffer_length<class_AudioStreamGenerator_property_buffer_length>` | ``0.5`` |
- +---------------------------+-------------------------------------------------------------------------+-------------+
- | :ref:`float<class_float>` | :ref:`mix_rate<class_AudioStreamGenerator_property_mix_rate>` | ``44100.0`` |
- +---------------------------+-------------------------------------------------------------------------+-------------+
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Property Descriptions
- ---------------------
- .. _class_AudioStreamGenerator_property_buffer_length:
- .. rst-class:: classref-property
- :ref:`float<class_float>` **buffer_length** = ``0.5``
- .. rst-class:: classref-property-setget
- - void **set_buffer_length** **(** :ref:`float<class_float>` value **)**
- - :ref:`float<class_float>` **get_buffer_length** **(** **)**
- The length of the buffer to generate (in seconds). Lower values result in less latency, but require the script to generate audio data faster, resulting in increased CPU usage and more risk for audio cracking if the CPU can't keep up.
- .. rst-class:: classref-item-separator
- ----
- .. _class_AudioStreamGenerator_property_mix_rate:
- .. rst-class:: classref-property
- :ref:`float<class_float>` **mix_rate** = ``44100.0``
- .. rst-class:: classref-property-setget
- - void **set_mix_rate** **(** :ref:`float<class_float>` value **)**
- - :ref:`float<class_float>` **get_mix_rate** **(** **)**
- The sample rate to use (in Hz). Higher values are more demanding for the CPU to generate, but result in better quality.
- In games, common sample rates in use are ``11025``, ``16000``, ``22050``, ``32000``, ``44100``, and ``48000``.
- According to the `Nyquist-Shannon sampling theorem <https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem>`__, there is no quality difference to human hearing when going past 40,000 Hz (since most humans can only hear up to ~20,000 Hz, often less). If you are generating lower-pitched sounds such as voices, lower sample rates such as ``32000`` or ``22050`` may be usable with no loss in quality.
- .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
- .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
- .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
- .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
- .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
- .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
- .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
|