audio_stream_player_2d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /**************************************************************************/
  2. /* audio_stream_player_2d.cpp */
  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. #include "audio_stream_player_2d.h"
  31. #include "audio_stream_player_2d.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "scene/2d/audio_listener_2d.h"
  34. #include "scene/2d/physics/area_2d.h"
  35. #include "scene/audio/audio_stream_player_internal.h"
  36. #include "scene/main/viewport.h"
  37. #include "scene/resources/world_2d.h"
  38. #include "servers/audio/audio_stream.h"
  39. #include "servers/audio_server.h"
  40. void AudioStreamPlayer2D::_notification(int p_what) {
  41. internal->notification(p_what);
  42. switch (p_what) {
  43. case NOTIFICATION_ENTER_TREE: {
  44. AudioServer::get_singleton()->add_listener_changed_callback(_listener_changed_cb, this);
  45. } break;
  46. case NOTIFICATION_EXIT_TREE: {
  47. AudioServer::get_singleton()->remove_listener_changed_callback(_listener_changed_cb, this);
  48. } break;
  49. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  50. // Update anything related to position first, if possible of course.
  51. if (setplay.get() > 0 || (internal->active.is_set() && last_mix_count != AudioServer::get_singleton()->get_mix_count()) || force_update_panning) {
  52. force_update_panning = false;
  53. _update_panning();
  54. }
  55. if (setplayback.is_valid() && setplay.get() >= 0) {
  56. internal->active.set();
  57. AudioServer::get_singleton()->start_playback_stream(setplayback, _get_actual_bus(), volume_vector, setplay.get(), internal->pitch_scale);
  58. setplayback.unref();
  59. setplay.set(-1);
  60. }
  61. if (!internal->stream_playbacks.is_empty() && internal->active.is_set()) {
  62. internal->process();
  63. }
  64. internal->ensure_playback_limit();
  65. } break;
  66. }
  67. }
  68. // Interacts with PhysicsServer2D, so can only be called during _physics_process.
  69. StringName AudioStreamPlayer2D::_get_actual_bus() {
  70. Vector2 global_pos = get_global_position();
  71. //check if any area is diverting sound into a bus
  72. Ref<World2D> world_2d = get_world_2d();
  73. ERR_FAIL_COND_V(world_2d.is_null(), SceneStringName(Master));
  74. PhysicsDirectSpaceState2D *space_state = PhysicsServer2D::get_singleton()->space_get_direct_state(world_2d->get_space());
  75. ERR_FAIL_NULL_V(space_state, SceneStringName(Master));
  76. PhysicsDirectSpaceState2D::ShapeResult sr[MAX_INTERSECT_AREAS];
  77. PhysicsDirectSpaceState2D::PointParameters point_params;
  78. point_params.position = global_pos;
  79. point_params.collision_mask = area_mask;
  80. point_params.collide_with_bodies = false;
  81. point_params.collide_with_areas = true;
  82. int areas = space_state->intersect_point(point_params, sr, MAX_INTERSECT_AREAS);
  83. for (int i = 0; i < areas; i++) {
  84. Area2D *area2d = Object::cast_to<Area2D>(sr[i].collider);
  85. if (!area2d) {
  86. continue;
  87. }
  88. if (!area2d->is_overriding_audio_bus()) {
  89. continue;
  90. }
  91. return area2d->get_audio_bus_name();
  92. }
  93. return internal->bus;
  94. }
  95. // Interacts with PhysicsServer2D, so can only be called during _physics_process
  96. void AudioStreamPlayer2D::_update_panning() {
  97. if (!internal->active.is_set() || internal->stream.is_null()) {
  98. return;
  99. }
  100. Ref<World2D> world_2d = get_world_2d();
  101. ERR_FAIL_COND(world_2d.is_null());
  102. Vector2 global_pos = get_global_position();
  103. HashSet<Viewport *> viewports = world_2d->get_viewports();
  104. volume_vector.resize(4);
  105. volume_vector.write[0] = AudioFrame(0, 0);
  106. volume_vector.write[1] = AudioFrame(0, 0);
  107. volume_vector.write[2] = AudioFrame(0, 0);
  108. volume_vector.write[3] = AudioFrame(0, 0);
  109. StringName actual_bus = _get_actual_bus();
  110. for (Viewport *vp : viewports) {
  111. if (!vp->is_audio_listener_2d()) {
  112. continue;
  113. }
  114. //compute matrix to convert to screen
  115. Vector2 screen_size = vp->get_visible_rect().size;
  116. Vector2 listener_in_global;
  117. Vector2 relative_to_listener;
  118. //screen in global is used for attenuation
  119. AudioListener2D *listener = vp->get_audio_listener_2d();
  120. Transform2D full_canvas_transform = vp->get_global_canvas_transform() * vp->get_canvas_transform();
  121. if (listener) {
  122. listener_in_global = listener->get_global_position();
  123. relative_to_listener = (global_pos - listener_in_global).rotated(-listener->get_global_rotation());
  124. relative_to_listener *= full_canvas_transform.get_scale(); // Default listener scales with canvas size, do the same here.
  125. } else {
  126. listener_in_global = full_canvas_transform.affine_inverse().xform(screen_size * 0.5);
  127. relative_to_listener = full_canvas_transform.xform(global_pos) - screen_size * 0.5;
  128. }
  129. float dist = global_pos.distance_to(listener_in_global); // Distance to listener, or screen if none.
  130. if (dist > max_distance) {
  131. continue; // Can't hear this sound in this viewport.
  132. }
  133. float multiplier = Math::pow(1.0f - dist / max_distance, attenuation);
  134. multiplier *= Math::db_to_linear(internal->volume_db); // Also apply player volume!
  135. float pan = relative_to_listener.x / screen_size.x;
  136. // Don't let the panning effect extend (too far) beyond the screen.
  137. pan = CLAMP(pan, -1, 1);
  138. // Bake in a constant factor here to allow the project setting defaults for 2d and 3d to be normalized to 1.0.
  139. pan *= panning_strength * cached_global_panning_strength * 0.5f;
  140. pan = CLAMP(pan + 0.5, 0.0, 1.0);
  141. float l = 1.0 - pan;
  142. float r = pan;
  143. const AudioFrame &prev_sample = volume_vector[0];
  144. AudioFrame new_sample = AudioFrame(l, r) * multiplier;
  145. volume_vector.write[0] = AudioFrame(MAX(prev_sample[0], new_sample[0]), MAX(prev_sample[1], new_sample[1]));
  146. }
  147. for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  148. AudioServer::get_singleton()->set_playback_bus_exclusive(playback, actual_bus, volume_vector);
  149. }
  150. for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  151. AudioServer::get_singleton()->set_playback_pitch_scale(playback, internal->pitch_scale);
  152. if (playback->get_is_sample() && playback->get_sample_playback().is_valid()) {
  153. Ref<AudioSamplePlayback> sample_playback = playback->get_sample_playback();
  154. AudioServer::get_singleton()->update_sample_playback_pitch_scale(sample_playback, internal->pitch_scale);
  155. }
  156. }
  157. last_mix_count = AudioServer::get_singleton()->get_mix_count();
  158. }
  159. void AudioStreamPlayer2D::set_stream(Ref<AudioStream> p_stream) {
  160. internal->set_stream(p_stream);
  161. }
  162. Ref<AudioStream> AudioStreamPlayer2D::get_stream() const {
  163. return internal->stream;
  164. }
  165. void AudioStreamPlayer2D::set_volume_db(float p_volume) {
  166. ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN.");
  167. internal->volume_db = p_volume;
  168. }
  169. float AudioStreamPlayer2D::get_volume_db() const {
  170. return internal->volume_db;
  171. }
  172. void AudioStreamPlayer2D::set_pitch_scale(float p_pitch_scale) {
  173. internal->set_pitch_scale(p_pitch_scale);
  174. }
  175. float AudioStreamPlayer2D::get_pitch_scale() const {
  176. return internal->pitch_scale;
  177. }
  178. void AudioStreamPlayer2D::play(float p_from_pos) {
  179. Ref<AudioStreamPlayback> stream_playback = internal->play_basic();
  180. if (stream_playback.is_null()) {
  181. return;
  182. }
  183. setplayback = stream_playback;
  184. setplay.set(p_from_pos);
  185. // Sample handling.
  186. if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_valid()) {
  187. Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback();
  188. sample_playback->offset = p_from_pos;
  189. sample_playback->bus = _get_actual_bus();
  190. AudioServer::get_singleton()->start_sample_playback(sample_playback);
  191. }
  192. }
  193. void AudioStreamPlayer2D::seek(float p_seconds) {
  194. internal->seek(p_seconds);
  195. }
  196. void AudioStreamPlayer2D::stop() {
  197. setplay.set(-1);
  198. internal->stop_basic();
  199. }
  200. bool AudioStreamPlayer2D::is_playing() const {
  201. if (setplay.get() >= 0) {
  202. return true; // play() has been called this frame, but no playback exists just yet.
  203. }
  204. return internal->is_playing();
  205. }
  206. float AudioStreamPlayer2D::get_playback_position() {
  207. return internal->get_playback_position();
  208. }
  209. void AudioStreamPlayer2D::set_bus(const StringName &p_bus) {
  210. internal->bus = p_bus; // This will be pushed to the audio server during the next physics timestep, which is fast enough.
  211. }
  212. StringName AudioStreamPlayer2D::get_bus() const {
  213. return internal->get_bus();
  214. }
  215. void AudioStreamPlayer2D::set_autoplay(bool p_enable) {
  216. internal->autoplay = p_enable;
  217. }
  218. bool AudioStreamPlayer2D::is_autoplay_enabled() const {
  219. return internal->autoplay;
  220. }
  221. void AudioStreamPlayer2D::_set_playing(bool p_enable) {
  222. internal->set_playing(p_enable);
  223. }
  224. void AudioStreamPlayer2D::_validate_property(PropertyInfo &p_property) const {
  225. internal->validate_property(p_property);
  226. }
  227. void AudioStreamPlayer2D::set_max_distance(float p_pixels) {
  228. ERR_FAIL_COND(p_pixels <= 0.0);
  229. max_distance = p_pixels;
  230. }
  231. float AudioStreamPlayer2D::get_max_distance() const {
  232. return max_distance;
  233. }
  234. void AudioStreamPlayer2D::set_attenuation(float p_curve) {
  235. attenuation = p_curve;
  236. }
  237. float AudioStreamPlayer2D::get_attenuation() const {
  238. return attenuation;
  239. }
  240. void AudioStreamPlayer2D::set_area_mask(uint32_t p_mask) {
  241. area_mask = p_mask;
  242. }
  243. uint32_t AudioStreamPlayer2D::get_area_mask() const {
  244. return area_mask;
  245. }
  246. void AudioStreamPlayer2D::set_stream_paused(bool p_pause) {
  247. internal->set_stream_paused(p_pause);
  248. }
  249. bool AudioStreamPlayer2D::get_stream_paused() const {
  250. return internal->get_stream_paused();
  251. }
  252. bool AudioStreamPlayer2D::has_stream_playback() {
  253. return internal->has_stream_playback();
  254. }
  255. Ref<AudioStreamPlayback> AudioStreamPlayer2D::get_stream_playback() {
  256. return internal->get_stream_playback();
  257. }
  258. void AudioStreamPlayer2D::set_max_polyphony(int p_max_polyphony) {
  259. internal->set_max_polyphony(p_max_polyphony);
  260. }
  261. int AudioStreamPlayer2D::get_max_polyphony() const {
  262. return internal->max_polyphony;
  263. }
  264. void AudioStreamPlayer2D::set_panning_strength(float p_panning_strength) {
  265. ERR_FAIL_COND_MSG(p_panning_strength < 0, "Panning strength must be a positive number.");
  266. panning_strength = p_panning_strength;
  267. }
  268. float AudioStreamPlayer2D::get_panning_strength() const {
  269. return panning_strength;
  270. }
  271. AudioServer::PlaybackType AudioStreamPlayer2D::get_playback_type() const {
  272. return internal->get_playback_type();
  273. }
  274. void AudioStreamPlayer2D::set_playback_type(AudioServer::PlaybackType p_playback_type) {
  275. internal->set_playback_type(p_playback_type);
  276. }
  277. bool AudioStreamPlayer2D::_set(const StringName &p_name, const Variant &p_value) {
  278. return internal->set(p_name, p_value);
  279. }
  280. bool AudioStreamPlayer2D::_get(const StringName &p_name, Variant &r_ret) const {
  281. return internal->get(p_name, r_ret);
  282. }
  283. void AudioStreamPlayer2D::_get_property_list(List<PropertyInfo> *p_list) const {
  284. internal->get_property_list(p_list);
  285. }
  286. void AudioStreamPlayer2D::_bind_methods() {
  287. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer2D::set_stream);
  288. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer2D::get_stream);
  289. ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer2D::set_volume_db);
  290. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer2D::get_volume_db);
  291. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer2D::set_pitch_scale);
  292. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer2D::get_pitch_scale);
  293. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer2D::play, DEFVAL(0.0));
  294. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer2D::seek);
  295. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer2D::stop);
  296. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer2D::is_playing);
  297. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer2D::get_playback_position);
  298. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer2D::set_bus);
  299. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer2D::get_bus);
  300. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer2D::set_autoplay);
  301. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer2D::is_autoplay_enabled);
  302. ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer2D::_set_playing);
  303. ClassDB::bind_method(D_METHOD("set_max_distance", "pixels"), &AudioStreamPlayer2D::set_max_distance);
  304. ClassDB::bind_method(D_METHOD("get_max_distance"), &AudioStreamPlayer2D::get_max_distance);
  305. ClassDB::bind_method(D_METHOD("set_attenuation", "curve"), &AudioStreamPlayer2D::set_attenuation);
  306. ClassDB::bind_method(D_METHOD("get_attenuation"), &AudioStreamPlayer2D::get_attenuation);
  307. ClassDB::bind_method(D_METHOD("set_area_mask", "mask"), &AudioStreamPlayer2D::set_area_mask);
  308. ClassDB::bind_method(D_METHOD("get_area_mask"), &AudioStreamPlayer2D::get_area_mask);
  309. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer2D::set_stream_paused);
  310. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer2D::get_stream_paused);
  311. ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer2D::set_max_polyphony);
  312. ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer2D::get_max_polyphony);
  313. ClassDB::bind_method(D_METHOD("set_panning_strength", "panning_strength"), &AudioStreamPlayer2D::set_panning_strength);
  314. ClassDB::bind_method(D_METHOD("get_panning_strength"), &AudioStreamPlayer2D::get_panning_strength);
  315. ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer2D::has_stream_playback);
  316. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer2D::get_stream_playback);
  317. ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer2D::set_playback_type);
  318. ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer2D::get_playback_type);
  319. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  320. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
  321. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
  322. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing");
  323. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  324. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  325. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "1,4096,1,or_greater,exp,suffix:px"), "set_max_distance", "get_max_distance");
  326. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attenuation", PROPERTY_HINT_EXP_EASING, "attenuation"), "set_attenuation", "get_attenuation");
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
  328. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "panning_strength", PROPERTY_HINT_RANGE, "0,3,0.01,or_greater"), "set_panning_strength", "get_panning_strength");
  329. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  330. ADD_PROPERTY(PropertyInfo(Variant::INT, "area_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_area_mask", "get_area_mask");
  331. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type");
  332. ADD_SIGNAL(MethodInfo("finished"));
  333. }
  334. AudioStreamPlayer2D::AudioStreamPlayer2D() {
  335. internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer2D::play), callable_mp(this, &AudioStreamPlayer2D::stop), true));
  336. cached_global_panning_strength = GLOBAL_GET("audio/general/2d_panning_strength");
  337. set_hide_clip_children(true);
  338. }
  339. AudioStreamPlayer2D::~AudioStreamPlayer2D() {
  340. memdelete(internal);
  341. }