audio_stream_player_3d.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /**************************************************************************/
  2. /* audio_stream_player_3d.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_3d.h"
  31. #include "audio_stream_player_3d.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "scene/3d/audio_listener_3d.h"
  34. #include "scene/3d/camera_3d.h"
  35. #include "scene/3d/physics/area_3d.h"
  36. #include "scene/3d/velocity_tracker_3d.h"
  37. #include "scene/audio/audio_stream_player_internal.h"
  38. #include "scene/main/viewport.h"
  39. #include "servers/audio/audio_stream.h"
  40. // Based on "A Novel Multichannel Panning Method for Standard and Arbitrary Loudspeaker Configurations" by Ramy Sadek and Chris Kyriakakis (2004)
  41. // Speaker-Placement Correction Amplitude Panning (SPCAP)
  42. class Spcap {
  43. private:
  44. struct Speaker {
  45. Vector3 direction;
  46. real_t effective_number_of_speakers = 0; // precalculated
  47. mutable real_t squared_gain = 0; // temporary
  48. };
  49. Vector<Speaker> speakers;
  50. public:
  51. Spcap(unsigned int speaker_count, const Vector3 *speaker_directions) {
  52. speakers.resize(speaker_count);
  53. Speaker *w = speakers.ptrw();
  54. for (unsigned int speaker_num = 0; speaker_num < speaker_count; speaker_num++) {
  55. w[speaker_num].direction = speaker_directions[speaker_num];
  56. w[speaker_num].squared_gain = 0.0;
  57. w[speaker_num].effective_number_of_speakers = 0.0;
  58. for (unsigned int other_speaker_num = 0; other_speaker_num < speaker_count; other_speaker_num++) {
  59. w[speaker_num].effective_number_of_speakers += 0.5 * (1.0 + w[speaker_num].direction.dot(w[other_speaker_num].direction));
  60. }
  61. }
  62. }
  63. unsigned int get_speaker_count() const {
  64. return (unsigned int)speakers.size();
  65. }
  66. Vector3 get_speaker_direction(unsigned int index) const {
  67. return speakers.ptr()[index].direction;
  68. }
  69. void calculate(const Vector3 &source_direction, real_t tightness, unsigned int volume_count, real_t *volumes) const {
  70. const Speaker *r = speakers.ptr();
  71. real_t sum_squared_gains = 0.0;
  72. for (unsigned int speaker_num = 0; speaker_num < (unsigned int)speakers.size(); speaker_num++) {
  73. real_t initial_gain = 0.5 * powf(1.0 + r[speaker_num].direction.dot(source_direction), tightness) / r[speaker_num].effective_number_of_speakers;
  74. r[speaker_num].squared_gain = initial_gain * initial_gain;
  75. sum_squared_gains += r[speaker_num].squared_gain;
  76. }
  77. for (unsigned int speaker_num = 0; speaker_num < MIN(volume_count, (unsigned int)speakers.size()); speaker_num++) {
  78. volumes[speaker_num] = sqrtf(r[speaker_num].squared_gain / sum_squared_gains);
  79. }
  80. }
  81. };
  82. //TODO: hardcoded main speaker directions for 2, 3.1, 5.1 and 7.1 setups - these are simplified and could also be made configurable
  83. static const Vector3 speaker_directions[7] = {
  84. Vector3(-1.0, 0.0, -1.0).normalized(), // front-left
  85. Vector3(1.0, 0.0, -1.0).normalized(), // front-right
  86. Vector3(0.0, 0.0, -1.0).normalized(), // center
  87. Vector3(-1.0, 0.0, 1.0).normalized(), // rear-left
  88. Vector3(1.0, 0.0, 1.0).normalized(), // rear-right
  89. Vector3(-1.0, 0.0, 0.0).normalized(), // side-left
  90. Vector3(1.0, 0.0, 0.0).normalized(), // side-right
  91. };
  92. void AudioStreamPlayer3D::_calc_output_vol(const Vector3 &source_dir, real_t tightness, Vector<AudioFrame> &output) {
  93. unsigned int speaker_count = 0; // only main speakers (no LFE)
  94. switch (AudioServer::get_singleton()->get_speaker_mode()) {
  95. case AudioServer::SPEAKER_MODE_STEREO:
  96. speaker_count = 2;
  97. break;
  98. case AudioServer::SPEAKER_SURROUND_31:
  99. speaker_count = 3;
  100. break;
  101. case AudioServer::SPEAKER_SURROUND_51:
  102. speaker_count = 5;
  103. break;
  104. case AudioServer::SPEAKER_SURROUND_71:
  105. speaker_count = 7;
  106. break;
  107. }
  108. Spcap spcap(speaker_count, speaker_directions); //TODO: should only be created/recreated once the speaker mode / speaker positions changes
  109. real_t volumes[7];
  110. spcap.calculate(source_dir, tightness, speaker_count, volumes);
  111. switch (AudioServer::get_singleton()->get_speaker_mode()) {
  112. case AudioServer::SPEAKER_SURROUND_71:
  113. output.write[3].left = volumes[5]; // side-left
  114. output.write[3].right = volumes[6]; // side-right
  115. [[fallthrough]];
  116. case AudioServer::SPEAKER_SURROUND_51:
  117. output.write[2].left = volumes[3]; // rear-left
  118. output.write[2].right = volumes[4]; // rear-right
  119. [[fallthrough]];
  120. case AudioServer::SPEAKER_SURROUND_31:
  121. output.write[1].right = 1.0; // LFE - always full power
  122. output.write[1].left = volumes[2]; // center
  123. [[fallthrough]];
  124. case AudioServer::SPEAKER_MODE_STEREO:
  125. output.write[0].right = volumes[1]; // front-right
  126. output.write[0].left = volumes[0]; // front-left
  127. break;
  128. }
  129. }
  130. void AudioStreamPlayer3D::_calc_reverb_vol(Area3D *area, Vector3 listener_area_pos, Vector<AudioFrame> direct_path_vol, Vector<AudioFrame> &reverb_vol) {
  131. reverb_vol.resize(4);
  132. reverb_vol.write[0] = AudioFrame(0, 0);
  133. reverb_vol.write[1] = AudioFrame(0, 0);
  134. reverb_vol.write[2] = AudioFrame(0, 0);
  135. reverb_vol.write[3] = AudioFrame(0, 0);
  136. float uniformity = area->get_reverb_uniformity();
  137. float area_send = area->get_reverb_amount();
  138. if (uniformity > 0.0) {
  139. float distance = listener_area_pos.length();
  140. float attenuation = Math::db_to_linear(_get_attenuation_db(distance));
  141. // Determine the fraction of sound that would come from each speaker if they were all driven uniformly.
  142. float center_val[3] = { 0.5f, 0.25f, 0.16666f };
  143. int channel_count = AudioServer::get_singleton()->get_channel_count();
  144. AudioFrame center_frame(center_val[channel_count - 1], center_val[channel_count - 1]);
  145. if (attenuation < 1.0) {
  146. //pan the uniform sound
  147. Vector3 rev_pos = listener_area_pos;
  148. rev_pos.y = 0;
  149. rev_pos.normalize();
  150. // Stereo pair.
  151. float c = rev_pos.x * 0.5 + 0.5;
  152. reverb_vol.write[0].left = 1.0 - c;
  153. reverb_vol.write[0].right = c;
  154. if (channel_count >= 3) {
  155. // Center pair + Side pair
  156. float xl = Vector3(-1, 0, -1).normalized().dot(rev_pos) * 0.5 + 0.5;
  157. float xr = Vector3(1, 0, -1).normalized().dot(rev_pos) * 0.5 + 0.5;
  158. reverb_vol.write[1].left = xl;
  159. reverb_vol.write[1].right = xr;
  160. reverb_vol.write[2].left = 1.0 - xr;
  161. reverb_vol.write[2].right = 1.0 - xl;
  162. }
  163. if (channel_count >= 4) {
  164. // Rear pair
  165. // FIXME: Not sure what math should be done here
  166. reverb_vol.write[3].left = 1.0 - c;
  167. reverb_vol.write[3].right = c;
  168. }
  169. for (int i = 0; i < channel_count; i++) {
  170. reverb_vol.write[i] = reverb_vol[i].lerp(center_frame, attenuation);
  171. }
  172. } else {
  173. for (int i = 0; i < channel_count; i++) {
  174. reverb_vol.write[i] = center_frame;
  175. }
  176. }
  177. for (int i = 0; i < channel_count; i++) {
  178. reverb_vol.write[i] = direct_path_vol[i].lerp(reverb_vol[i] * attenuation, uniformity);
  179. reverb_vol.write[i] *= area_send;
  180. }
  181. } else {
  182. for (int i = 0; i < 4; i++) {
  183. reverb_vol.write[i] = direct_path_vol[i] * area_send;
  184. }
  185. }
  186. }
  187. float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const {
  188. float att = 0;
  189. switch (attenuation_model) {
  190. case ATTENUATION_INVERSE_DISTANCE: {
  191. att = Math::linear_to_db(1.0 / ((p_distance / unit_size) + CMP_EPSILON));
  192. } break;
  193. case ATTENUATION_INVERSE_SQUARE_DISTANCE: {
  194. float d = (p_distance / unit_size);
  195. d *= d;
  196. att = Math::linear_to_db(1.0 / (d + CMP_EPSILON));
  197. } break;
  198. case ATTENUATION_LOGARITHMIC: {
  199. att = -20 * Math::log(p_distance / unit_size + CMP_EPSILON);
  200. } break;
  201. case ATTENUATION_DISABLED:
  202. break;
  203. default: {
  204. ERR_PRINT("Unknown attenuation type");
  205. break;
  206. }
  207. }
  208. att += internal->volume_db;
  209. if (att > max_db) {
  210. att = max_db;
  211. }
  212. return att;
  213. }
  214. void AudioStreamPlayer3D::_notification(int p_what) {
  215. internal->notification(p_what);
  216. switch (p_what) {
  217. case NOTIFICATION_ENTER_TREE: {
  218. velocity_tracker->reset(get_global_transform().origin);
  219. AudioServer::get_singleton()->add_listener_changed_callback(_listener_changed_cb, this);
  220. } break;
  221. case NOTIFICATION_EXIT_TREE: {
  222. AudioServer::get_singleton()->remove_listener_changed_callback(_listener_changed_cb, this);
  223. } break;
  224. case NOTIFICATION_TRANSFORM_CHANGED: {
  225. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  226. velocity_tracker->update_position(get_global_transform().origin);
  227. }
  228. } break;
  229. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  230. // Update anything related to position first, if possible of course.
  231. Vector<AudioFrame> volume_vector;
  232. if (setplay.get() > 0 || (internal->active.is_set() && last_mix_count != AudioServer::get_singleton()->get_mix_count()) || force_update_panning) {
  233. force_update_panning = false;
  234. volume_vector = _update_panning();
  235. }
  236. if (setplayback.is_valid() && setplay.get() >= 0) {
  237. internal->active.set();
  238. HashMap<StringName, Vector<AudioFrame>> bus_map;
  239. bus_map[_get_actual_bus()] = volume_vector;
  240. AudioServer::get_singleton()->start_playback_stream(setplayback, bus_map, setplay.get(), actual_pitch_scale, linear_attenuation, attenuation_filter_cutoff_hz);
  241. setplayback.unref();
  242. setplay.set(-1);
  243. }
  244. if (!internal->stream_playbacks.is_empty() && internal->active.is_set()) {
  245. internal->process();
  246. }
  247. internal->ensure_playback_limit();
  248. } break;
  249. }
  250. }
  251. // Interacts with PhysicsServer3D, so can only be called during _physics_process
  252. Area3D *AudioStreamPlayer3D::_get_overriding_area() {
  253. //check if any area is diverting sound into a bus
  254. Ref<World3D> world_3d = get_world_3d();
  255. ERR_FAIL_COND_V(world_3d.is_null(), nullptr);
  256. Vector3 global_pos = get_global_transform().origin;
  257. PhysicsDirectSpaceState3D *space_state = PhysicsServer3D::get_singleton()->space_get_direct_state(world_3d->get_space());
  258. PhysicsDirectSpaceState3D::ShapeResult sr[MAX_INTERSECT_AREAS];
  259. PhysicsDirectSpaceState3D::PointParameters point_params;
  260. point_params.position = global_pos;
  261. point_params.collision_mask = area_mask;
  262. point_params.collide_with_bodies = false;
  263. point_params.collide_with_areas = true;
  264. int areas = space_state->intersect_point(point_params, sr, MAX_INTERSECT_AREAS);
  265. for (int i = 0; i < areas; i++) {
  266. if (!sr[i].collider) {
  267. continue;
  268. }
  269. Area3D *tarea = Object::cast_to<Area3D>(sr[i].collider);
  270. if (!tarea) {
  271. continue;
  272. }
  273. if (!tarea->is_overriding_audio_bus() && !tarea->is_using_reverb_bus()) {
  274. continue;
  275. }
  276. return tarea;
  277. }
  278. return nullptr;
  279. }
  280. // Interacts with PhysicsServer3D, so can only be called during _physics_process.
  281. StringName AudioStreamPlayer3D::_get_actual_bus() {
  282. Area3D *overriding_area = _get_overriding_area();
  283. if (overriding_area && overriding_area->is_overriding_audio_bus() && !overriding_area->is_using_reverb_bus()) {
  284. return overriding_area->get_audio_bus_name();
  285. }
  286. return internal->bus;
  287. }
  288. // Interacts with PhysicsServer3D, so can only be called during _physics_process.
  289. Vector<AudioFrame> AudioStreamPlayer3D::_update_panning() {
  290. Vector<AudioFrame> output_volume_vector;
  291. output_volume_vector.resize(4);
  292. for (AudioFrame &frame : output_volume_vector) {
  293. frame = AudioFrame(0, 0);
  294. }
  295. if (!internal->active.is_set() || internal->stream.is_null()) {
  296. return output_volume_vector;
  297. }
  298. Vector3 linear_velocity;
  299. //compute linear velocity for doppler
  300. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  301. linear_velocity = velocity_tracker->get_tracked_linear_velocity();
  302. }
  303. Vector3 global_pos = get_global_transform().origin;
  304. Ref<World3D> world_3d = get_world_3d();
  305. ERR_FAIL_COND_V(world_3d.is_null(), output_volume_vector);
  306. HashSet<Camera3D *> cameras = world_3d->get_cameras();
  307. cameras.insert(get_viewport()->get_camera_3d());
  308. PhysicsDirectSpaceState3D *space_state = PhysicsServer3D::get_singleton()->space_get_direct_state(world_3d->get_space());
  309. for (Camera3D *camera : cameras) {
  310. if (!camera) {
  311. continue;
  312. }
  313. Viewport *vp = camera->get_viewport();
  314. if (!vp) {
  315. continue;
  316. }
  317. if (!vp->is_audio_listener_3d()) {
  318. continue;
  319. }
  320. bool listener_is_camera = true;
  321. Node3D *listener_node = camera;
  322. AudioListener3D *listener = vp->get_audio_listener_3d();
  323. if (listener) {
  324. listener_node = listener;
  325. listener_is_camera = false;
  326. }
  327. Vector3 local_pos = listener_node->get_global_transform().orthonormalized().affine_inverse().xform(global_pos);
  328. float dist = local_pos.length();
  329. Vector3 area_sound_pos;
  330. Vector3 listener_area_pos;
  331. Area3D *area = _get_overriding_area();
  332. if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) {
  333. area_sound_pos = space_state->get_closest_point_to_object_volume(area->get_rid(), listener_node->get_global_transform().origin);
  334. listener_area_pos = listener_node->get_global_transform().affine_inverse().xform(area_sound_pos);
  335. }
  336. if (max_distance > 0) {
  337. float total_max = max_distance;
  338. if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) {
  339. total_max = MAX(total_max, listener_area_pos.length());
  340. }
  341. if (dist > total_max || total_max > max_distance) {
  342. if (!was_further_than_max_distance_last_frame) {
  343. HashMap<StringName, Vector<AudioFrame>> bus_volumes;
  344. for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  345. // So the player gets muted and mostly stops mixing when out of range.
  346. AudioServer::get_singleton()->set_playback_bus_volumes_linear(playback, bus_volumes);
  347. }
  348. was_further_than_max_distance_last_frame = true; // Cache so we don't set the volume over and over.
  349. }
  350. continue; //can't hear this sound in this listener
  351. }
  352. }
  353. was_further_than_max_distance_last_frame = false;
  354. float multiplier = Math::db_to_linear(_get_attenuation_db(dist));
  355. if (max_distance > 0) {
  356. multiplier *= MAX(0, 1.0 - (dist / max_distance));
  357. }
  358. float db_att = (1.0 - MIN(1.0, multiplier)) * attenuation_filter_db;
  359. if (emission_angle_enabled) {
  360. Vector3 listenertopos = global_pos - listener_node->get_global_transform().origin;
  361. float c = listenertopos.normalized().dot(get_global_transform().basis.get_column(2).normalized()); //it's z negative
  362. float angle = Math::rad_to_deg(Math::acos(c));
  363. if (angle > emission_angle) {
  364. db_att -= -emission_angle_filter_attenuation_db;
  365. }
  366. }
  367. linear_attenuation = Math::db_to_linear(db_att);
  368. for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  369. AudioServer::get_singleton()->set_playback_highshelf_params(playback, linear_attenuation, attenuation_filter_cutoff_hz);
  370. }
  371. // Bake in a constant factor here to allow the project setting defaults for 2d and 3d to be normalized to 1.0.
  372. float tightness = cached_global_panning_strength * 2.0f;
  373. tightness *= panning_strength;
  374. _calc_output_vol(local_pos.normalized(), tightness, output_volume_vector);
  375. for (unsigned int k = 0; k < 4; k++) {
  376. output_volume_vector.write[k] = multiplier * output_volume_vector[k];
  377. }
  378. HashMap<StringName, Vector<AudioFrame>> bus_volumes;
  379. if (area) {
  380. if (area->is_overriding_audio_bus()) {
  381. //override audio bus
  382. bus_volumes[area->get_audio_bus_name()] = output_volume_vector;
  383. }
  384. if (area->is_using_reverb_bus()) {
  385. StringName reverb_bus_name = area->get_reverb_bus_name();
  386. Vector<AudioFrame> reverb_vol;
  387. _calc_reverb_vol(area, listener_area_pos, output_volume_vector, reverb_vol);
  388. bus_volumes[reverb_bus_name] = reverb_vol;
  389. }
  390. } else {
  391. bus_volumes[internal->bus] = output_volume_vector;
  392. }
  393. for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  394. AudioServer::get_singleton()->set_playback_bus_volumes_linear(playback, bus_volumes);
  395. }
  396. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  397. Vector3 listener_velocity;
  398. if (listener_is_camera) {
  399. listener_velocity = camera->get_doppler_tracked_velocity();
  400. }
  401. Vector3 local_velocity = listener_node->get_global_transform().orthonormalized().basis.xform_inv(linear_velocity - listener_velocity);
  402. if (local_velocity != Vector3()) {
  403. float approaching = local_pos.normalized().dot(local_velocity.normalized());
  404. float velocity = local_velocity.length();
  405. float speed_of_sound = 343.0;
  406. float doppler_pitch_scale = internal->pitch_scale * speed_of_sound / (speed_of_sound + velocity * approaching);
  407. doppler_pitch_scale = CLAMP(doppler_pitch_scale, (1 / 8.0), 8.0); //avoid crazy stuff
  408. actual_pitch_scale = doppler_pitch_scale;
  409. } else {
  410. actual_pitch_scale = internal->pitch_scale;
  411. }
  412. } else {
  413. actual_pitch_scale = internal->pitch_scale;
  414. }
  415. for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  416. AudioServer::get_singleton()->set_playback_pitch_scale(playback, actual_pitch_scale);
  417. if (playback->get_is_sample()) {
  418. Ref<AudioSamplePlayback> sample_playback = playback->get_sample_playback();
  419. if (sample_playback.is_valid()) {
  420. AudioServer::get_singleton()->update_sample_playback_pitch_scale(sample_playback, actual_pitch_scale);
  421. }
  422. }
  423. }
  424. }
  425. return output_volume_vector;
  426. }
  427. void AudioStreamPlayer3D::set_stream(Ref<AudioStream> p_stream) {
  428. internal->set_stream(p_stream);
  429. }
  430. Ref<AudioStream> AudioStreamPlayer3D::get_stream() const {
  431. return internal->stream;
  432. }
  433. void AudioStreamPlayer3D::set_volume_db(float p_volume) {
  434. ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN.");
  435. internal->volume_db = p_volume;
  436. }
  437. float AudioStreamPlayer3D::get_volume_db() const {
  438. return internal->volume_db;
  439. }
  440. void AudioStreamPlayer3D::set_unit_size(float p_volume) {
  441. unit_size = p_volume;
  442. update_gizmos();
  443. }
  444. float AudioStreamPlayer3D::get_unit_size() const {
  445. return unit_size;
  446. }
  447. void AudioStreamPlayer3D::set_max_db(float p_boost) {
  448. max_db = p_boost;
  449. }
  450. float AudioStreamPlayer3D::get_max_db() const {
  451. return max_db;
  452. }
  453. void AudioStreamPlayer3D::set_pitch_scale(float p_pitch_scale) {
  454. internal->set_pitch_scale(p_pitch_scale);
  455. }
  456. float AudioStreamPlayer3D::get_pitch_scale() const {
  457. return internal->pitch_scale;
  458. }
  459. void AudioStreamPlayer3D::play(float p_from_pos) {
  460. Ref<AudioStreamPlayback> stream_playback = internal->play_basic();
  461. if (stream_playback.is_null()) {
  462. return;
  463. }
  464. setplayback = stream_playback;
  465. setplay.set(p_from_pos);
  466. // Sample handling.
  467. if (stream_playback->get_is_sample()) {
  468. Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback();
  469. sample_playback->offset = p_from_pos;
  470. sample_playback->bus = _get_actual_bus();
  471. AudioServer::get_singleton()->start_sample_playback(sample_playback);
  472. }
  473. }
  474. void AudioStreamPlayer3D::seek(float p_seconds) {
  475. if (is_playing()) {
  476. stop();
  477. play(p_seconds);
  478. }
  479. }
  480. void AudioStreamPlayer3D::stop() {
  481. setplay.set(-1);
  482. internal->stop_basic();
  483. }
  484. bool AudioStreamPlayer3D::is_playing() const {
  485. if (setplay.get() >= 0) {
  486. return true; // play() has been called this frame, but no playback exists just yet.
  487. }
  488. return internal->is_playing();
  489. }
  490. float AudioStreamPlayer3D::get_playback_position() {
  491. return internal->get_playback_position();
  492. }
  493. void AudioStreamPlayer3D::set_bus(const StringName &p_bus) {
  494. internal->bus = p_bus; // This will be pushed to the audio server during the next physics timestep, which is fast enough.
  495. }
  496. StringName AudioStreamPlayer3D::get_bus() const {
  497. return internal->get_bus();
  498. }
  499. void AudioStreamPlayer3D::set_autoplay(bool p_enable) {
  500. internal->autoplay = p_enable;
  501. }
  502. bool AudioStreamPlayer3D::is_autoplay_enabled() const {
  503. return internal->autoplay;
  504. }
  505. void AudioStreamPlayer3D::_set_playing(bool p_enable) {
  506. internal->set_playing(p_enable);
  507. }
  508. void AudioStreamPlayer3D::_validate_property(PropertyInfo &p_property) const {
  509. internal->validate_property(p_property);
  510. }
  511. void AudioStreamPlayer3D::set_max_distance(float p_metres) {
  512. ERR_FAIL_COND(p_metres < 0.0);
  513. max_distance = p_metres;
  514. update_gizmos();
  515. }
  516. float AudioStreamPlayer3D::get_max_distance() const {
  517. return max_distance;
  518. }
  519. void AudioStreamPlayer3D::set_area_mask(uint32_t p_mask) {
  520. area_mask = p_mask;
  521. }
  522. uint32_t AudioStreamPlayer3D::get_area_mask() const {
  523. return area_mask;
  524. }
  525. void AudioStreamPlayer3D::set_emission_angle_enabled(bool p_enable) {
  526. emission_angle_enabled = p_enable;
  527. update_gizmos();
  528. }
  529. bool AudioStreamPlayer3D::is_emission_angle_enabled() const {
  530. return emission_angle_enabled;
  531. }
  532. void AudioStreamPlayer3D::set_emission_angle(float p_angle) {
  533. ERR_FAIL_COND(p_angle < 0 || p_angle > 90);
  534. emission_angle = p_angle;
  535. update_gizmos();
  536. }
  537. float AudioStreamPlayer3D::get_emission_angle() const {
  538. return emission_angle;
  539. }
  540. void AudioStreamPlayer3D::set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db) {
  541. emission_angle_filter_attenuation_db = p_angle_attenuation_db;
  542. }
  543. float AudioStreamPlayer3D::get_emission_angle_filter_attenuation_db() const {
  544. return emission_angle_filter_attenuation_db;
  545. }
  546. void AudioStreamPlayer3D::set_attenuation_filter_cutoff_hz(float p_hz) {
  547. attenuation_filter_cutoff_hz = p_hz;
  548. }
  549. float AudioStreamPlayer3D::get_attenuation_filter_cutoff_hz() const {
  550. return attenuation_filter_cutoff_hz;
  551. }
  552. void AudioStreamPlayer3D::set_attenuation_filter_db(float p_db) {
  553. attenuation_filter_db = p_db;
  554. }
  555. float AudioStreamPlayer3D::get_attenuation_filter_db() const {
  556. return attenuation_filter_db;
  557. }
  558. void AudioStreamPlayer3D::set_attenuation_model(AttenuationModel p_model) {
  559. ERR_FAIL_INDEX((int)p_model, 4);
  560. attenuation_model = p_model;
  561. update_gizmos();
  562. }
  563. AudioStreamPlayer3D::AttenuationModel AudioStreamPlayer3D::get_attenuation_model() const {
  564. return attenuation_model;
  565. }
  566. void AudioStreamPlayer3D::set_doppler_tracking(DopplerTracking p_tracking) {
  567. if (doppler_tracking == p_tracking) {
  568. return;
  569. }
  570. doppler_tracking = p_tracking;
  571. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  572. set_notify_transform(true);
  573. velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
  574. if (is_inside_tree()) {
  575. velocity_tracker->reset(get_global_transform().origin);
  576. }
  577. } else {
  578. set_notify_transform(false);
  579. }
  580. }
  581. AudioStreamPlayer3D::DopplerTracking AudioStreamPlayer3D::get_doppler_tracking() const {
  582. return doppler_tracking;
  583. }
  584. void AudioStreamPlayer3D::set_stream_paused(bool p_pause) {
  585. internal->set_stream_paused(p_pause);
  586. }
  587. bool AudioStreamPlayer3D::get_stream_paused() const {
  588. return internal->get_stream_paused();
  589. }
  590. bool AudioStreamPlayer3D::has_stream_playback() {
  591. return internal->has_stream_playback();
  592. }
  593. Ref<AudioStreamPlayback> AudioStreamPlayer3D::get_stream_playback() {
  594. return internal->get_stream_playback();
  595. }
  596. void AudioStreamPlayer3D::set_max_polyphony(int p_max_polyphony) {
  597. internal->set_max_polyphony(p_max_polyphony);
  598. }
  599. int AudioStreamPlayer3D::get_max_polyphony() const {
  600. return internal->max_polyphony;
  601. }
  602. void AudioStreamPlayer3D::set_panning_strength(float p_panning_strength) {
  603. ERR_FAIL_COND_MSG(p_panning_strength < 0, "Panning strength must be a positive number.");
  604. panning_strength = p_panning_strength;
  605. }
  606. float AudioStreamPlayer3D::get_panning_strength() const {
  607. return panning_strength;
  608. }
  609. AudioServer::PlaybackType AudioStreamPlayer3D::get_playback_type() const {
  610. return internal->get_playback_type();
  611. }
  612. void AudioStreamPlayer3D::set_playback_type(AudioServer::PlaybackType p_playback_type) {
  613. internal->set_playback_type(p_playback_type);
  614. }
  615. bool AudioStreamPlayer3D::_set(const StringName &p_name, const Variant &p_value) {
  616. return internal->set(p_name, p_value);
  617. }
  618. bool AudioStreamPlayer3D::_get(const StringName &p_name, Variant &r_ret) const {
  619. return internal->get(p_name, r_ret);
  620. }
  621. void AudioStreamPlayer3D::_get_property_list(List<PropertyInfo> *p_list) const {
  622. internal->get_property_list(p_list);
  623. }
  624. void AudioStreamPlayer3D::_bind_methods() {
  625. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer3D::set_stream);
  626. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer3D::get_stream);
  627. ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer3D::set_volume_db);
  628. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer3D::get_volume_db);
  629. ClassDB::bind_method(D_METHOD("set_unit_size", "unit_size"), &AudioStreamPlayer3D::set_unit_size);
  630. ClassDB::bind_method(D_METHOD("get_unit_size"), &AudioStreamPlayer3D::get_unit_size);
  631. ClassDB::bind_method(D_METHOD("set_max_db", "max_db"), &AudioStreamPlayer3D::set_max_db);
  632. ClassDB::bind_method(D_METHOD("get_max_db"), &AudioStreamPlayer3D::get_max_db);
  633. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer3D::set_pitch_scale);
  634. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer3D::get_pitch_scale);
  635. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer3D::play, DEFVAL(0.0));
  636. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer3D::seek);
  637. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer3D::stop);
  638. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer3D::is_playing);
  639. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer3D::get_playback_position);
  640. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer3D::set_bus);
  641. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer3D::get_bus);
  642. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer3D::set_autoplay);
  643. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer3D::is_autoplay_enabled);
  644. ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer3D::_set_playing);
  645. ClassDB::bind_method(D_METHOD("set_max_distance", "meters"), &AudioStreamPlayer3D::set_max_distance);
  646. ClassDB::bind_method(D_METHOD("get_max_distance"), &AudioStreamPlayer3D::get_max_distance);
  647. ClassDB::bind_method(D_METHOD("set_area_mask", "mask"), &AudioStreamPlayer3D::set_area_mask);
  648. ClassDB::bind_method(D_METHOD("get_area_mask"), &AudioStreamPlayer3D::get_area_mask);
  649. ClassDB::bind_method(D_METHOD("set_emission_angle", "degrees"), &AudioStreamPlayer3D::set_emission_angle);
  650. ClassDB::bind_method(D_METHOD("get_emission_angle"), &AudioStreamPlayer3D::get_emission_angle);
  651. ClassDB::bind_method(D_METHOD("set_emission_angle_enabled", "enabled"), &AudioStreamPlayer3D::set_emission_angle_enabled);
  652. ClassDB::bind_method(D_METHOD("is_emission_angle_enabled"), &AudioStreamPlayer3D::is_emission_angle_enabled);
  653. ClassDB::bind_method(D_METHOD("set_emission_angle_filter_attenuation_db", "db"), &AudioStreamPlayer3D::set_emission_angle_filter_attenuation_db);
  654. ClassDB::bind_method(D_METHOD("get_emission_angle_filter_attenuation_db"), &AudioStreamPlayer3D::get_emission_angle_filter_attenuation_db);
  655. ClassDB::bind_method(D_METHOD("set_attenuation_filter_cutoff_hz", "degrees"), &AudioStreamPlayer3D::set_attenuation_filter_cutoff_hz);
  656. ClassDB::bind_method(D_METHOD("get_attenuation_filter_cutoff_hz"), &AudioStreamPlayer3D::get_attenuation_filter_cutoff_hz);
  657. ClassDB::bind_method(D_METHOD("set_attenuation_filter_db", "db"), &AudioStreamPlayer3D::set_attenuation_filter_db);
  658. ClassDB::bind_method(D_METHOD("get_attenuation_filter_db"), &AudioStreamPlayer3D::get_attenuation_filter_db);
  659. ClassDB::bind_method(D_METHOD("set_attenuation_model", "model"), &AudioStreamPlayer3D::set_attenuation_model);
  660. ClassDB::bind_method(D_METHOD("get_attenuation_model"), &AudioStreamPlayer3D::get_attenuation_model);
  661. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &AudioStreamPlayer3D::set_doppler_tracking);
  662. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &AudioStreamPlayer3D::get_doppler_tracking);
  663. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer3D::set_stream_paused);
  664. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer3D::get_stream_paused);
  665. ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer3D::set_max_polyphony);
  666. ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer3D::get_max_polyphony);
  667. ClassDB::bind_method(D_METHOD("set_panning_strength", "panning_strength"), &AudioStreamPlayer3D::set_panning_strength);
  668. ClassDB::bind_method(D_METHOD("get_panning_strength"), &AudioStreamPlayer3D::get_panning_strength);
  669. ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer3D::has_stream_playback);
  670. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer3D::get_stream_playback);
  671. ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer3D::set_playback_type);
  672. ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer3D::get_playback_type);
  673. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  674. ADD_PROPERTY(PropertyInfo(Variant::INT, "attenuation_model", PROPERTY_HINT_ENUM, "Inverse,Inverse Square,Logarithmic,Disabled"), "set_attenuation_model", "get_attenuation_model");
  675. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,80,suffix:dB"), "set_volume_db", "get_volume_db");
  676. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "unit_size", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater"), "set_unit_size", "get_unit_size");
  677. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_db", PROPERTY_HINT_RANGE, "-24,6,suffix:dB"), "set_max_db", "get_max_db");
  678. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
  679. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing");
  680. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  681. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  682. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "0,4096,0.01,or_greater,suffix:m"), "set_max_distance", "get_max_distance");
  683. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
  684. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "panning_strength", PROPERTY_HINT_RANGE, "0,3,0.01,or_greater"), "set_panning_strength", "get_panning_strength");
  685. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  686. ADD_PROPERTY(PropertyInfo(Variant::INT, "area_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_area_mask", "get_area_mask");
  687. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type");
  688. ADD_GROUP("Emission Angle", "emission_angle");
  689. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emission_angle_enabled"), "set_emission_angle_enabled", "is_emission_angle_enabled");
  690. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_angle_degrees", PROPERTY_HINT_RANGE, "0.1,90,0.1,degrees"), "set_emission_angle", "get_emission_angle");
  691. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_angle_filter_attenuation_db", PROPERTY_HINT_RANGE, "-80,0,0.1,suffix:dB"), "set_emission_angle_filter_attenuation_db", "get_emission_angle_filter_attenuation_db");
  692. ADD_GROUP("Attenuation Filter", "attenuation_filter_");
  693. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attenuation_filter_cutoff_hz", PROPERTY_HINT_RANGE, "1,20500,1,suffix:Hz"), "set_attenuation_filter_cutoff_hz", "get_attenuation_filter_cutoff_hz");
  694. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attenuation_filter_db", PROPERTY_HINT_RANGE, "-80,0,0.1,suffix:dB"), "set_attenuation_filter_db", "get_attenuation_filter_db");
  695. ADD_GROUP("Doppler", "doppler_");
  696. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  697. BIND_ENUM_CONSTANT(ATTENUATION_INVERSE_DISTANCE);
  698. BIND_ENUM_CONSTANT(ATTENUATION_INVERSE_SQUARE_DISTANCE);
  699. BIND_ENUM_CONSTANT(ATTENUATION_LOGARITHMIC);
  700. BIND_ENUM_CONSTANT(ATTENUATION_DISABLED);
  701. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED);
  702. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP);
  703. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP);
  704. ADD_SIGNAL(MethodInfo("finished"));
  705. }
  706. AudioStreamPlayer3D::AudioStreamPlayer3D() {
  707. internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer3D::play), callable_mp(this, &AudioStreamPlayer3D::stop), true));
  708. velocity_tracker.instantiate();
  709. set_disable_scale(true);
  710. cached_global_panning_strength = GLOBAL_GET("audio/general/3d_panning_strength");
  711. }
  712. AudioStreamPlayer3D::~AudioStreamPlayer3D() {
  713. memdelete(internal);
  714. }