1/**************************************************************************/
2/* audio_stream_player_2d.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef AUDIO_STREAM_PLAYER_2D_H
32#define AUDIO_STREAM_PLAYER_2D_H
33
34#include "scene/2d/node_2d.h"
35#include "scene/scene_string_names.h"
36#include "servers/audio/audio_stream.h"
37#include "servers/audio_server.h"
38
39class AudioStreamPlayer2D : public Node2D {
40 GDCLASS(AudioStreamPlayer2D, Node2D);
41
42private:
43 enum {
44 MAX_OUTPUTS = 8,
45 MAX_INTERSECT_AREAS = 32
46
47 };
48
49 struct Output {
50 AudioFrame vol;
51 int bus_index = 0;
52 Viewport *viewport = nullptr; //pointer only used for reference to previous mix
53 };
54
55 Vector<Ref<AudioStreamPlayback>> stream_playbacks;
56 Ref<AudioStream> stream;
57
58 SafeFlag active{ false };
59 SafeNumeric<float> setplay{ -1.0 };
60 Ref<AudioStreamPlayback> setplayback;
61
62 Vector<AudioFrame> volume_vector;
63
64 uint64_t last_mix_count = -1;
65 bool force_update_panning = false;
66
67 float volume_db = 0.0;
68 float pitch_scale = 1.0;
69 bool autoplay = false;
70 StringName default_bus = SceneStringNames::get_singleton()->Master;
71 int max_polyphony = 1;
72
73 void _set_playing(bool p_enable);
74 bool _is_active() const;
75
76 StringName _get_actual_bus();
77 void _update_panning();
78 void _bus_layout_changed();
79
80 static void _listener_changed_cb(void *self) { reinterpret_cast<AudioStreamPlayer2D *>(self)->force_update_panning = true; }
81
82 uint32_t area_mask = 1;
83
84 float max_distance = 2000.0;
85 float attenuation = 1.0;
86
87 float panning_strength = 1.0f;
88 float cached_global_panning_strength = 0.5f;
89
90protected:
91 void _validate_property(PropertyInfo &p_property) const;
92 void _notification(int p_what);
93 static void _bind_methods();
94
95public:
96 void set_stream(Ref<AudioStream> p_stream);
97 Ref<AudioStream> get_stream() const;
98
99 void set_volume_db(float p_volume);
100 float get_volume_db() const;
101
102 void set_pitch_scale(float p_pitch_scale);
103 float get_pitch_scale() const;
104
105 void play(float p_from_pos = 0.0);
106 void seek(float p_seconds);
107 void stop();
108 bool is_playing() const;
109 float get_playback_position();
110
111 void set_bus(const StringName &p_bus);
112 StringName get_bus() const;
113
114 void set_autoplay(bool p_enable);
115 bool is_autoplay_enabled();
116
117 void set_max_distance(float p_pixels);
118 float get_max_distance() const;
119
120 void set_attenuation(float p_curve);
121 float get_attenuation() const;
122
123 void set_area_mask(uint32_t p_mask);
124 uint32_t get_area_mask() const;
125
126 void set_stream_paused(bool p_pause);
127 bool get_stream_paused() const;
128
129 void set_max_polyphony(int p_max_polyphony);
130 int get_max_polyphony() const;
131
132 void set_panning_strength(float p_panning_strength);
133 float get_panning_strength() const;
134
135 bool has_stream_playback();
136 Ref<AudioStreamPlayback> get_stream_playback();
137
138 AudioStreamPlayer2D();
139 ~AudioStreamPlayer2D();
140};
141
142#endif // AUDIO_STREAM_PLAYER_2D_H
143