class_audiostreamgenerator.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/AudioStreamGenerator.xml.
  6. .. _class_AudioStreamGenerator:
  7. AudioStreamGenerator
  8. ====================
  9. **Inherits:** :ref:`AudioStream<class_AudioStream>` **<** :ref:`Resource<class_Resource>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. An audio stream with utilities for procedural sound generation.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. **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>`.
  15. Here's a sample on how to use it to generate a sine wave:
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. var playback # Will hold the AudioStreamGeneratorPlayback.
  19. @onready var sample_hz = $AudioStreamPlayer.stream.mix_rate
  20. var pulse_hz = 440.0 # The frequency of the sound wave.
  21. func _ready():
  22. $AudioStreamPlayer.play()
  23. playback = $AudioStreamPlayer.get_stream_playback()
  24. fill_buffer()
  25. func fill_buffer():
  26. var phase = 0.0
  27. var increment = pulse_hz / sample_hz
  28. var frames_available = playback.get_frames_available()
  29. for i in range(frames_available):
  30. playback.push_frame(Vector2.ONE * sin(phase * TAU))
  31. phase = fmod(phase + increment, 1.0)
  32. .. code-tab:: csharp
  33. [Export] public AudioStreamPlayer Player { get; set; }
  34. private AudioStreamGeneratorPlayback _playback; // Will hold the AudioStreamGeneratorPlayback.
  35. private float _sampleHz;
  36. private float _pulseHz = 440.0f; // The frequency of the sound wave.
  37. public override void _Ready()
  38. {
  39. if (Player.Stream is AudioStreamGenerator generator) // Type as a generator to access MixRate.
  40. {
  41. _sampleHz = generator.MixRate;
  42. Player.Play();
  43. _playback = (AudioStreamGeneratorPlayback)Player.GetStreamPlayback();
  44. FillBuffer();
  45. }
  46. }
  47. public void FillBuffer()
  48. {
  49. double phase = 0.0;
  50. float increment = _pulseHz / _sampleHz;
  51. int framesAvailable = _playback.GetFramesAvailable();
  52. for (int i = 0; i < framesAvailable; i++)
  53. {
  54. _playback.PushFrame(Vector2.One * (float)Mathf.Sin(phase * Mathf.Tau));
  55. phase = Mathf.PosMod(phase + increment, 1.0);
  56. }
  57. }
  58. 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.
  59. See also :ref:`AudioEffectSpectrumAnalyzer<class_AudioEffectSpectrumAnalyzer>` for performing real-time audio spectrum analysis.
  60. \ **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.
  61. .. rst-class:: classref-introduction-group
  62. Tutorials
  63. ---------
  64. - `Audio Generator Demo <https://godotengine.org/asset-library/asset/526>`__
  65. .. rst-class:: classref-reftable-group
  66. Properties
  67. ----------
  68. .. table::
  69. :widths: auto
  70. +---------------------------+-------------------------------------------------------------------------+-------------+
  71. | :ref:`float<class_float>` | :ref:`buffer_length<class_AudioStreamGenerator_property_buffer_length>` | ``0.5`` |
  72. +---------------------------+-------------------------------------------------------------------------+-------------+
  73. | :ref:`float<class_float>` | :ref:`mix_rate<class_AudioStreamGenerator_property_mix_rate>` | ``44100.0`` |
  74. +---------------------------+-------------------------------------------------------------------------+-------------+
  75. .. rst-class:: classref-section-separator
  76. ----
  77. .. rst-class:: classref-descriptions-group
  78. Property Descriptions
  79. ---------------------
  80. .. _class_AudioStreamGenerator_property_buffer_length:
  81. .. rst-class:: classref-property
  82. :ref:`float<class_float>` **buffer_length** = ``0.5``
  83. .. rst-class:: classref-property-setget
  84. - void **set_buffer_length** **(** :ref:`float<class_float>` value **)**
  85. - :ref:`float<class_float>` **get_buffer_length** **(** **)**
  86. 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.
  87. .. rst-class:: classref-item-separator
  88. ----
  89. .. _class_AudioStreamGenerator_property_mix_rate:
  90. .. rst-class:: classref-property
  91. :ref:`float<class_float>` **mix_rate** = ``44100.0``
  92. .. rst-class:: classref-property-setget
  93. - void **set_mix_rate** **(** :ref:`float<class_float>` value **)**
  94. - :ref:`float<class_float>` **get_mix_rate** **(** **)**
  95. The sample rate to use (in Hz). Higher values are more demanding for the CPU to generate, but result in better quality.
  96. In games, common sample rates in use are ``11025``, ``16000``, ``22050``, ``32000``, ``44100``, and ``48000``.
  97. 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.
  98. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  99. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  100. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  101. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  102. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  103. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  104. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`