Skip to content

Latest commit

 

History

History
448 lines (258 loc) · 19.8 KB

class_audiostreamplayer.rst

File metadata and controls

448 lines (258 loc) · 19.8 KB
github_url:hide

AudioStreamPlayer

Inherits: :ref:`Node<class_Node>` < :ref:`Object<class_Object>`

A node for audio playback.

.. rst-class:: classref-introduction-group

Description

The AudioStreamPlayer node plays an audio stream non-positionally. It is ideal for user interfaces, menus, or background music.

To use this node, :ref:`stream<class_AudioStreamPlayer_property_stream>` needs to be set to a valid :ref:`AudioStream<class_AudioStream>` resource. Playing more than one sound at the same time is also supported, see :ref:`max_polyphony<class_AudioStreamPlayer_property_max_polyphony>`.

If you need to play audio at a specific position, use :ref:`AudioStreamPlayer2D<class_AudioStreamPlayer2D>` or :ref:`AudioStreamPlayer3D<class_AudioStreamPlayer3D>` instead.

.. rst-class:: classref-introduction-group

Tutorials

.. rst-class:: classref-reftable-group

Properties

:ref:`bool<class_bool>` :ref:`autoplay<class_AudioStreamPlayer_property_autoplay>` false
:ref:`StringName<class_StringName>` :ref:`bus<class_AudioStreamPlayer_property_bus>` &"Master"
:ref:`int<class_int>` :ref:`max_polyphony<class_AudioStreamPlayer_property_max_polyphony>` 1
:ref:`MixTarget<enum_AudioStreamPlayer_MixTarget>` :ref:`mix_target<class_AudioStreamPlayer_property_mix_target>` 0
:ref:`float<class_float>` :ref:`pitch_scale<class_AudioStreamPlayer_property_pitch_scale>` 1.0
:ref:`PlaybackType<enum_AudioServer_PlaybackType>` :ref:`playback_type<class_AudioStreamPlayer_property_playback_type>` 0
:ref:`bool<class_bool>` :ref:`playing<class_AudioStreamPlayer_property_playing>` false
:ref:`AudioStream<class_AudioStream>` :ref:`stream<class_AudioStreamPlayer_property_stream>`  
:ref:`bool<class_bool>` :ref:`stream_paused<class_AudioStreamPlayer_property_stream_paused>` false
:ref:`float<class_float>` :ref:`volume_db<class_AudioStreamPlayer_property_volume_db>` 0.0
:ref:`float<class_float>` :ref:`volume_linear<class_AudioStreamPlayer_property_volume_linear>`  
.. rst-class:: classref-reftable-group

Methods

:ref:`float<class_float>` :ref:`get_playback_position<class_AudioStreamPlayer_method_get_playback_position>`()
:ref:`AudioStreamPlayback<class_AudioStreamPlayback>` :ref:`get_stream_playback<class_AudioStreamPlayer_method_get_stream_playback>`()
:ref:`bool<class_bool>` :ref:`has_stream_playback<class_AudioStreamPlayer_method_has_stream_playback>`()
|void| :ref:`play<class_AudioStreamPlayer_method_play>`(from_position: :ref:`float<class_float>` = 0.0)
|void| :ref:`seek<class_AudioStreamPlayer_method_seek>`(to_position: :ref:`float<class_float>`)
|void| :ref:`stop<class_AudioStreamPlayer_method_stop>`()
.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Signals

.. rst-class:: classref-signal

finished() :ref:`🔗<class_AudioStreamPlayer_signal_finished>`

Emitted when a sound finishes playing without interruptions. This signal is not emitted when calling :ref:`stop()<class_AudioStreamPlayer_method_stop>`, or when exiting the tree while sounds are playing.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Enumerations

.. rst-class:: classref-enumeration

enum MixTarget: :ref:`🔗<enum_AudioStreamPlayer_MixTarget>`

.. rst-class:: classref-enumeration-constant

:ref:`MixTarget<enum_AudioStreamPlayer_MixTarget>` MIX_TARGET_STEREO = 0

The audio will be played only on the first channel. This is the default.

.. rst-class:: classref-enumeration-constant

:ref:`MixTarget<enum_AudioStreamPlayer_MixTarget>` MIX_TARGET_SURROUND = 1

The audio will be played on all surround channels.

.. rst-class:: classref-enumeration-constant

:ref:`MixTarget<enum_AudioStreamPlayer_MixTarget>` MIX_TARGET_CENTER = 2

The audio will be played on the second channel, which is usually the center.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Property Descriptions

.. rst-class:: classref-property

:ref:`bool<class_bool>` autoplay = false :ref:`🔗<class_AudioStreamPlayer_property_autoplay>`

.. rst-class:: classref-property-setget

If true, this node calls :ref:`play()<class_AudioStreamPlayer_method_play>` when entering the tree.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`StringName<class_StringName>` bus = &"Master" :ref:`🔗<class_AudioStreamPlayer_property_bus>`

.. rst-class:: classref-property-setget

The target bus name. All sounds from this node will be playing on this bus.

Note: At runtime, if no bus with the given name exists, all sounds will fall back on "Master". See also :ref:`AudioServer.get_bus_name()<class_AudioServer_method_get_bus_name>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`int<class_int>` max_polyphony = 1 :ref:`🔗<class_AudioStreamPlayer_property_max_polyphony>`

.. rst-class:: classref-property-setget

The maximum number of sounds this node can play at the same time. Calling :ref:`play()<class_AudioStreamPlayer_method_play>` after this value is reached will cut off the oldest sounds.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`MixTarget<enum_AudioStreamPlayer_MixTarget>` mix_target = 0 :ref:`🔗<class_AudioStreamPlayer_property_mix_target>`

.. rst-class:: classref-property-setget

The mix target channels, as one of the :ref:`MixTarget<enum_AudioStreamPlayer_MixTarget>` constants. Has no effect when two speakers or less are detected (see :ref:`SpeakerMode<enum_AudioServer_SpeakerMode>`).

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` pitch_scale = 1.0 :ref:`🔗<class_AudioStreamPlayer_property_pitch_scale>`

.. rst-class:: classref-property-setget

The audio's pitch and tempo, as a multiplier of the :ref:`stream<class_AudioStreamPlayer_property_stream>`'s sample rate. A value of 2.0 doubles the audio's pitch, while a value of 0.5 halves the pitch.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`PlaybackType<enum_AudioServer_PlaybackType>` playback_type = 0 :ref:`🔗<class_AudioStreamPlayer_property_playback_type>`

.. rst-class:: classref-property-setget

Experimental: This property may be changed or removed in future versions.

The playback type of the stream player. If set other than to the default value, it will force that playback type.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`bool<class_bool>` playing = false :ref:`🔗<class_AudioStreamPlayer_property_playing>`

.. rst-class:: classref-property-setget

If true, this node is playing sounds. Setting this property has the same effect as :ref:`play()<class_AudioStreamPlayer_method_play>` and :ref:`stop()<class_AudioStreamPlayer_method_stop>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`AudioStream<class_AudioStream>` stream :ref:`🔗<class_AudioStreamPlayer_property_stream>`

.. rst-class:: classref-property-setget

The :ref:`AudioStream<class_AudioStream>` resource to be played. Setting this property stops all currently playing sounds. If left empty, the AudioStreamPlayer does not work.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`bool<class_bool>` stream_paused = false :ref:`🔗<class_AudioStreamPlayer_property_stream_paused>`

.. rst-class:: classref-property-setget

If true, the sounds are paused. Setting :ref:`stream_paused<class_AudioStreamPlayer_property_stream_paused>` to false resumes all sounds.

Note: This property is automatically changed when exiting or entering the tree, or this node is paused (see :ref:`Node.process_mode<class_Node_property_process_mode>`).

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` volume_db = 0.0 :ref:`🔗<class_AudioStreamPlayer_property_volume_db>`

.. rst-class:: classref-property-setget

Volume of sound, in decibels. This is an offset of the :ref:`stream<class_AudioStreamPlayer_property_stream>`'s volume.

Note: To convert between decibel and linear energy (like most volume sliders do), use :ref:`volume_linear<class_AudioStreamPlayer_property_volume_linear>`, or :ref:`@GlobalScope.db_to_linear()<class_@GlobalScope_method_db_to_linear>` and :ref:`@GlobalScope.linear_to_db()<class_@GlobalScope_method_linear_to_db>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` volume_linear :ref:`🔗<class_AudioStreamPlayer_property_volume_linear>`

.. rst-class:: classref-property-setget

Volume of sound, as a linear value.

Note: This member modifies :ref:`volume_db<class_AudioStreamPlayer_property_volume_db>` for convenience. The returned value is equivalent to the result of :ref:`@GlobalScope.db_to_linear()<class_@GlobalScope_method_db_to_linear>` on :ref:`volume_db<class_AudioStreamPlayer_property_volume_db>`. Setting this member is equivalent to setting :ref:`volume_db<class_AudioStreamPlayer_property_volume_db>` to the result of :ref:`@GlobalScope.linear_to_db()<class_@GlobalScope_method_linear_to_db>` on a value.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Method Descriptions

.. rst-class:: classref-method

:ref:`float<class_float>` get_playback_position() :ref:`🔗<class_AudioStreamPlayer_method_get_playback_position>`

Returns the position in the :ref:`AudioStream<class_AudioStream>` of the latest sound, in seconds. Returns 0.0 if no sounds are playing.

Note: The position is not always accurate, as the :ref:`AudioServer<class_AudioServer>` does not mix audio every processed frame. To get more accurate results, add :ref:`AudioServer.get_time_since_last_mix()<class_AudioServer_method_get_time_since_last_mix>` to the returned position.

Note: This method always returns 0.0 if the :ref:`stream<class_AudioStreamPlayer_property_stream>` is an :ref:`AudioStreamInteractive<class_AudioStreamInteractive>`, since it can have multiple clips playing at once.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`AudioStreamPlayback<class_AudioStreamPlayback>` get_stream_playback() :ref:`🔗<class_AudioStreamPlayer_method_get_stream_playback>`

Returns the latest :ref:`AudioStreamPlayback<class_AudioStreamPlayback>` of this node, usually the most recently created by :ref:`play()<class_AudioStreamPlayer_method_play>`. If no sounds are playing, this method fails and returns an empty playback.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` has_stream_playback() :ref:`🔗<class_AudioStreamPlayer_method_has_stream_playback>`

Returns true if any sound is active, even if :ref:`stream_paused<class_AudioStreamPlayer_property_stream_paused>` is set to true. See also :ref:`playing<class_AudioStreamPlayer_property_playing>` and :ref:`get_stream_playback()<class_AudioStreamPlayer_method_get_stream_playback>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

|void| play(from_position: :ref:`float<class_float>` = 0.0) :ref:`🔗<class_AudioStreamPlayer_method_play>`

Plays a sound from the beginning, or the given from_position in seconds.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

|void| seek(to_position: :ref:`float<class_float>`) :ref:`🔗<class_AudioStreamPlayer_method_seek>`

Restarts all sounds to be played from the given to_position, in seconds. Does nothing if no sounds are playing.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

|void| stop() :ref:`🔗<class_AudioStreamPlayer_method_stop>`

Stops all sounds from this node.