class_audiostreamgenerator.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. var phase = 0.0
  22. func _ready():
  23. $AudioStreamPlayer.play()
  24. playback = $AudioStreamPlayer.get_stream_playback()
  25. fill_buffer()
  26. func fill_buffer():
  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. private double phase = 0.0;
  38. public override void _Ready()
  39. {
  40. if (Player.Stream is AudioStreamGenerator generator) // Type as a generator to access MixRate.
  41. {
  42. _sampleHz = generator.MixRate;
  43. Player.Play();
  44. _playback = (AudioStreamGeneratorPlayback)Player.GetStreamPlayback();
  45. FillBuffer();
  46. }
  47. }
  48. public void FillBuffer()
  49. {
  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/2759>`__
  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. | :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>` | :ref:`mix_rate_mode<class_AudioStreamGenerator_property_mix_rate_mode>` | ``2`` |
  76. +-------------------------------------------------------------------------------------------+-------------------------------------------------------------------------+-------------+
  77. .. rst-class:: classref-section-separator
  78. ----
  79. .. rst-class:: classref-descriptions-group
  80. Enumerations
  81. ------------
  82. .. _enum_AudioStreamGenerator_AudioStreamGeneratorMixRate:
  83. .. rst-class:: classref-enumeration
  84. enum **AudioStreamGeneratorMixRate**: :ref:`🔗<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>`
  85. .. _class_AudioStreamGenerator_constant_MIX_RATE_OUTPUT:
  86. .. rst-class:: classref-enumeration-constant
  87. :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>` **MIX_RATE_OUTPUT** = ``0``
  88. Current :ref:`AudioServer<class_AudioServer>` output mixing rate.
  89. .. _class_AudioStreamGenerator_constant_MIX_RATE_INPUT:
  90. .. rst-class:: classref-enumeration-constant
  91. :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>` **MIX_RATE_INPUT** = ``1``
  92. Current :ref:`AudioServer<class_AudioServer>` input mixing rate.
  93. .. _class_AudioStreamGenerator_constant_MIX_RATE_CUSTOM:
  94. .. rst-class:: classref-enumeration-constant
  95. :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>` **MIX_RATE_CUSTOM** = ``2``
  96. Custom mixing rate, specified by :ref:`mix_rate<class_AudioStreamGenerator_property_mix_rate>`.
  97. .. _class_AudioStreamGenerator_constant_MIX_RATE_MAX:
  98. .. rst-class:: classref-enumeration-constant
  99. :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>` **MIX_RATE_MAX** = ``3``
  100. Maximum value for the mixing rate mode enum.
  101. .. rst-class:: classref-section-separator
  102. ----
  103. .. rst-class:: classref-descriptions-group
  104. Property Descriptions
  105. ---------------------
  106. .. _class_AudioStreamGenerator_property_buffer_length:
  107. .. rst-class:: classref-property
  108. :ref:`float<class_float>` **buffer_length** = ``0.5`` :ref:`🔗<class_AudioStreamGenerator_property_buffer_length>`
  109. .. rst-class:: classref-property-setget
  110. - |void| **set_buffer_length**\ (\ value\: :ref:`float<class_float>`\ )
  111. - :ref:`float<class_float>` **get_buffer_length**\ (\ )
  112. 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.
  113. .. rst-class:: classref-item-separator
  114. ----
  115. .. _class_AudioStreamGenerator_property_mix_rate:
  116. .. rst-class:: classref-property
  117. :ref:`float<class_float>` **mix_rate** = ``44100.0`` :ref:`🔗<class_AudioStreamGenerator_property_mix_rate>`
  118. .. rst-class:: classref-property-setget
  119. - |void| **set_mix_rate**\ (\ value\: :ref:`float<class_float>`\ )
  120. - :ref:`float<class_float>` **get_mix_rate**\ (\ )
  121. The sample rate to use (in Hz). Higher values are more demanding for the CPU to generate, but result in better quality.
  122. In games, common sample rates in use are ``11025``, ``16000``, ``22050``, ``32000``, ``44100``, and ``48000``.
  123. 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.
  124. \ **Note:** **AudioStreamGenerator** is not automatically resampling input data, to produce expected result :ref:`mix_rate_mode<class_AudioStreamGenerator_property_mix_rate_mode>` should match the sampling rate of input data.
  125. \ **Note:** If you are using :ref:`AudioEffectCapture<class_AudioEffectCapture>` as the source of your data, set :ref:`mix_rate_mode<class_AudioStreamGenerator_property_mix_rate_mode>` to :ref:`MIX_RATE_INPUT<class_AudioStreamGenerator_constant_MIX_RATE_INPUT>` or :ref:`MIX_RATE_OUTPUT<class_AudioStreamGenerator_constant_MIX_RATE_OUTPUT>` to automatically match current :ref:`AudioServer<class_AudioServer>` mixing rate.
  126. .. rst-class:: classref-item-separator
  127. ----
  128. .. _class_AudioStreamGenerator_property_mix_rate_mode:
  129. .. rst-class:: classref-property
  130. :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>` **mix_rate_mode** = ``2`` :ref:`🔗<class_AudioStreamGenerator_property_mix_rate_mode>`
  131. .. rst-class:: classref-property-setget
  132. - |void| **set_mix_rate_mode**\ (\ value\: :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>`\ )
  133. - :ref:`AudioStreamGeneratorMixRate<enum_AudioStreamGenerator_AudioStreamGeneratorMixRate>` **get_mix_rate_mode**\ (\ )
  134. Mixing rate mode. If set to :ref:`MIX_RATE_CUSTOM<class_AudioStreamGenerator_constant_MIX_RATE_CUSTOM>`, :ref:`mix_rate<class_AudioStreamGenerator_property_mix_rate>` is used, otherwise current :ref:`AudioServer<class_AudioServer>` mixing rate is used.
  135. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  136. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  137. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  138. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  139. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  140. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  141. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  142. .. |void| replace:: :abbr:`void (No return value.)`