audio_stream_player_3d.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "core/engine.h"
  32. #include "scene/3d/area.h"
  33. #include "scene/3d/camera.h"
  34. #include "scene/3d/listener.h"
  35. #include "scene/main/viewport.h"
  36. void AudioStreamPlayer3D::_mix_audio() {
  37. if (!stream_playback.is_valid() || !active ||
  38. (stream_paused && !stream_paused_fade_out)) {
  39. return;
  40. }
  41. bool started = false;
  42. if (setseek >= 0.0) {
  43. stream_playback->start(setseek);
  44. setseek = -1.0; //reset seek
  45. started = true;
  46. }
  47. //get data
  48. AudioFrame *buffer = mix_buffer.ptrw();
  49. int buffer_size = mix_buffer.size();
  50. if (stream_paused_fade_out) {
  51. // Short fadeout ramp
  52. buffer_size = MIN(buffer_size, 128);
  53. }
  54. // Mix if we're not paused or we're fading out
  55. if ((output_count > 0 || out_of_range_mode == OUT_OF_RANGE_MIX)) {
  56. float output_pitch_scale = 0.0;
  57. if (output_count) {
  58. //used for doppler, not realistic but good enough
  59. for (int i = 0; i < output_count; i++) {
  60. output_pitch_scale += outputs[i].pitch_scale;
  61. }
  62. output_pitch_scale /= float(output_count);
  63. } else {
  64. output_pitch_scale = 1.0;
  65. }
  66. stream_playback->mix(buffer, pitch_scale * output_pitch_scale, buffer_size);
  67. }
  68. //write all outputs
  69. for (int i = 0; i < output_count; i++) {
  70. Output current = outputs[i];
  71. //see if current output exists, to keep volume ramp
  72. bool found = false;
  73. for (int j = i; j < prev_output_count; j++) {
  74. if (prev_outputs[j].viewport == current.viewport) {
  75. if (j != i) {
  76. SWAP(prev_outputs[j], prev_outputs[i]);
  77. }
  78. found = true;
  79. break;
  80. }
  81. }
  82. bool interpolate_filter = !started;
  83. ;
  84. if (!found) {
  85. //create new if was not used before
  86. if (prev_output_count < MAX_OUTPUTS) {
  87. prev_outputs[prev_output_count] = prev_outputs[i]; //may be owned by another viewport
  88. prev_output_count++;
  89. }
  90. prev_outputs[i] = current;
  91. interpolate_filter = false;
  92. }
  93. //mix!
  94. int buffers = AudioServer::get_singleton()->get_channel_count();
  95. for (int k = 0; k < buffers; k++) {
  96. AudioFrame target_volume = stream_paused_fade_out ? AudioFrame(0.f, 0.f) : current.vol[k];
  97. AudioFrame vol_prev = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol[k];
  98. AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size);
  99. AudioFrame vol = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : current.vol[k];
  100. if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.bus_index, k))
  101. continue; //may have been deleted, will be updated on process
  102. AudioFrame *target = AudioServer::get_singleton()->thread_get_channel_mix_buffer(current.bus_index, k);
  103. current.filter.set_mode(AudioFilterSW::HIGHSHELF);
  104. current.filter.set_sampling_rate(AudioServer::get_singleton()->get_mix_rate());
  105. current.filter.set_cutoff(attenuation_filter_cutoff_hz);
  106. current.filter.set_resonance(1);
  107. current.filter.set_stages(1);
  108. current.filter.set_gain(current.filter_gain);
  109. if (interpolate_filter) {
  110. current.filter_process[k * 2 + 0] = prev_outputs[i].filter_process[k * 2 + 0];
  111. current.filter_process[k * 2 + 1] = prev_outputs[i].filter_process[k * 2 + 1];
  112. current.filter_process[k * 2 + 0].set_filter(&current.filter, false);
  113. current.filter_process[k * 2 + 1].set_filter(&current.filter, false);
  114. current.filter_process[k * 2 + 0].update_coeffs(buffer_size);
  115. current.filter_process[k * 2 + 1].update_coeffs(buffer_size);
  116. for (int j = 0; j < buffer_size; j++) {
  117. AudioFrame f = buffer[j] * vol;
  118. current.filter_process[k * 2 + 0].process_one_interp(f.l);
  119. current.filter_process[k * 2 + 1].process_one_interp(f.r);
  120. target[j] += f;
  121. vol += vol_inc;
  122. }
  123. } else {
  124. current.filter_process[k * 2 + 0].set_filter(&current.filter);
  125. current.filter_process[k * 2 + 1].set_filter(&current.filter);
  126. current.filter_process[k * 2 + 0].update_coeffs();
  127. current.filter_process[k * 2 + 1].update_coeffs();
  128. for (int j = 0; j < buffer_size; j++) {
  129. AudioFrame f = buffer[j] * vol;
  130. current.filter_process[k * 2 + 0].process_one(f.l);
  131. current.filter_process[k * 2 + 1].process_one(f.r);
  132. target[j] += f;
  133. vol += vol_inc;
  134. }
  135. }
  136. if (current.reverb_bus_index >= 0) {
  137. if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.reverb_bus_index, k))
  138. continue; //may have been deleted, will be updated on process
  139. AudioFrame *rtarget = AudioServer::get_singleton()->thread_get_channel_mix_buffer(current.reverb_bus_index, k);
  140. if (current.reverb_bus_index == prev_outputs[i].reverb_bus_index) {
  141. AudioFrame rvol_inc = (current.reverb_vol[k] - prev_outputs[i].reverb_vol[k]) / float(buffer_size);
  142. AudioFrame rvol = prev_outputs[i].reverb_vol[k];
  143. for (int j = 0; j < buffer_size; j++) {
  144. rtarget[j] += buffer[j] * rvol;
  145. rvol += rvol_inc;
  146. }
  147. } else {
  148. AudioFrame rvol = current.reverb_vol[k];
  149. for (int j = 0; j < buffer_size; j++) {
  150. rtarget[j] += buffer[j] * rvol;
  151. }
  152. }
  153. }
  154. }
  155. prev_outputs[i] = current;
  156. }
  157. prev_output_count = output_count;
  158. //stream is no longer active, disable this.
  159. if (!stream_playback->is_playing()) {
  160. active = false;
  161. }
  162. output_ready = false;
  163. stream_paused_fade_in = false;
  164. stream_paused_fade_out = false;
  165. }
  166. float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const {
  167. float att = 0;
  168. switch (attenuation_model) {
  169. case ATTENUATION_INVERSE_DISTANCE: {
  170. att = Math::linear2db(1.0 / ((p_distance / unit_size) + CMP_EPSILON));
  171. } break;
  172. case ATTENUATION_INVERSE_SQUARE_DISTANCE: {
  173. float d = (p_distance / unit_size);
  174. d *= d;
  175. att = Math::linear2db(1.0 / (d + CMP_EPSILON));
  176. } break;
  177. case ATTENUATION_LOGARITHMIC: {
  178. att = -20 * Math::log(p_distance / unit_size + CMP_EPSILON);
  179. } break;
  180. default: {
  181. ERR_PRINT("Unknown attenuation type");
  182. break;
  183. }
  184. }
  185. att += unit_db;
  186. if (att > max_db) {
  187. att = max_db;
  188. }
  189. return att;
  190. }
  191. void _update_sound() {
  192. }
  193. void AudioStreamPlayer3D::_notification(int p_what) {
  194. if (p_what == NOTIFICATION_ENTER_TREE) {
  195. velocity_tracker->reset(get_global_transform().origin);
  196. AudioServer::get_singleton()->add_callback(_mix_audios, this);
  197. if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
  198. play();
  199. }
  200. }
  201. if (p_what == NOTIFICATION_EXIT_TREE) {
  202. AudioServer::get_singleton()->remove_callback(_mix_audios, this);
  203. }
  204. if (p_what == NOTIFICATION_PAUSED) {
  205. if (!can_process()) {
  206. // Node can't process so we start fading out to silence
  207. set_stream_paused(true);
  208. }
  209. }
  210. if (p_what == NOTIFICATION_UNPAUSED) {
  211. set_stream_paused(false);
  212. }
  213. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  214. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  215. velocity_tracker->update_position(get_global_transform().origin);
  216. }
  217. }
  218. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  219. //update anything related to position first, if possible of course
  220. if (!output_ready) {
  221. Vector3 linear_velocity;
  222. //compute linear velocity for doppler
  223. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  224. linear_velocity = velocity_tracker->get_tracked_linear_velocity();
  225. }
  226. Ref<World> world = get_world();
  227. ERR_FAIL_COND(world.is_null());
  228. int new_output_count = 0;
  229. Vector3 global_pos = get_global_transform().origin;
  230. int bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus);
  231. //check if any area is diverting sound into a bus
  232. PhysicsDirectSpaceState *space_state = PhysicsServer::get_singleton()->space_get_direct_state(world->get_space());
  233. PhysicsDirectSpaceState::ShapeResult sr[MAX_INTERSECT_AREAS];
  234. int areas = space_state->intersect_point(global_pos, sr, MAX_INTERSECT_AREAS, Set<RID>(), area_mask, false, true);
  235. Area *area = NULL;
  236. for (int i = 0; i < areas; i++) {
  237. if (!sr[i].collider)
  238. continue;
  239. Area *tarea = Object::cast_to<Area>(sr[i].collider);
  240. if (!tarea)
  241. continue;
  242. if (!tarea->is_overriding_audio_bus() && !tarea->is_using_reverb_bus())
  243. continue;
  244. area = tarea;
  245. break;
  246. }
  247. List<Camera *> cameras;
  248. world->get_camera_list(&cameras);
  249. for (List<Camera *>::Element *E = cameras.front(); E; E = E->next()) {
  250. Camera *camera = E->get();
  251. Viewport *vp = camera->get_viewport();
  252. if (!vp->is_audio_listener())
  253. continue;
  254. bool listener_is_camera = true;
  255. Spatial *listener_node = camera;
  256. Listener *listener = vp->get_listener();
  257. if (listener) {
  258. listener_node = listener;
  259. listener_is_camera = false;
  260. }
  261. Vector3 local_pos = listener_node->get_global_transform().orthonormalized().affine_inverse().xform(global_pos);
  262. float dist = local_pos.length();
  263. Vector3 area_sound_pos;
  264. Vector3 listener_area_pos;
  265. if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) {
  266. area_sound_pos = space_state->get_closest_point_to_object_volume(area->get_rid(), listener_node->get_global_transform().origin);
  267. listener_area_pos = listener_node->get_global_transform().affine_inverse().xform(area_sound_pos);
  268. }
  269. if (max_distance > 0) {
  270. float total_max = max_distance;
  271. if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) {
  272. total_max = MAX(total_max, listener_area_pos.length());
  273. }
  274. if (total_max > max_distance) {
  275. continue; //can't hear this sound in this listener
  276. }
  277. }
  278. float multiplier = Math::db2linear(_get_attenuation_db(dist));
  279. if (max_distance > 0) {
  280. multiplier *= MAX(0, 1.0 - (dist / max_distance));
  281. }
  282. Output output;
  283. output.bus_index = bus_index;
  284. output.reverb_bus_index = -1; //no reverb by default
  285. output.viewport = vp;
  286. float db_att = (1.0 - MIN(1.0, multiplier)) * attenuation_filter_db;
  287. if (emission_angle_enabled) {
  288. Vector3 listenertopos = global_pos - listener_node->get_global_transform().origin;
  289. float c = listenertopos.normalized().dot(get_global_transform().basis.get_axis(2).normalized()); //it's z negative
  290. float angle = Math::rad2deg(Math::acos(c));
  291. if (angle > emission_angle)
  292. db_att -= -emission_angle_filter_attenuation_db;
  293. }
  294. output.filter_gain = Math::db2linear(db_att);
  295. Vector3 flat_pos = local_pos;
  296. flat_pos.y = 0;
  297. flat_pos.normalize();
  298. unsigned int cc = AudioServer::get_singleton()->get_channel_count();
  299. if (cc == 1) {
  300. // Stereo pair
  301. float c = flat_pos.x * 0.5 + 0.5;
  302. output.vol[0].l = 1.0 - c;
  303. output.vol[0].r = c;
  304. } else {
  305. Vector3 listenertopos = global_pos - listener_node->get_global_transform().origin;
  306. float c = listenertopos.normalized().dot(get_global_transform().basis.get_axis(2).normalized()); //it's z negative
  307. float angle = Math::rad2deg(Math::acos(c));
  308. float av = angle * (flat_pos.x < 0 ? -1 : 1) / 180.0;
  309. if (cc >= 1) {
  310. // Stereo pair
  311. float fl = Math::abs(1.0 - Math::abs(-0.8 - av));
  312. float fr = Math::abs(1.0 - Math::abs(0.8 - av));
  313. output.vol[0].l = fl;
  314. output.vol[0].r = fr;
  315. }
  316. if (cc >= 2) {
  317. // Center pair
  318. float center = 1.0 - Math::sin(Math::acos(c));
  319. output.vol[1].l = center;
  320. output.vol[1].r = center;
  321. }
  322. if (cc >= 3) {
  323. // Side pair
  324. float sleft = Math::abs(1.0 - Math::abs(-0.4 - av));
  325. float sright = Math::abs(1.0 - Math::abs(0.4 - av));
  326. output.vol[2].l = sleft;
  327. output.vol[2].r = sright;
  328. }
  329. if (cc >= 4) {
  330. // Rear pair
  331. float rleft = Math::abs(1.0 - Math::abs(-0.2 - av));
  332. float rright = Math::abs(1.0 - Math::abs(0.2 - av));
  333. output.vol[3].l = rleft;
  334. output.vol[3].r = rright;
  335. }
  336. }
  337. for (unsigned int k = 0; k < cc; k++) {
  338. output.vol[k] *= multiplier;
  339. }
  340. bool filled_reverb = false;
  341. int vol_index_max = AudioServer::get_singleton()->get_speaker_mode() + 1;
  342. if (area) {
  343. if (area->is_overriding_audio_bus()) {
  344. //override audio bus
  345. StringName bus_name = area->get_audio_bus();
  346. output.bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus_name);
  347. }
  348. if (area->is_using_reverb_bus()) {
  349. filled_reverb = true;
  350. StringName bus_name = area->get_reverb_bus();
  351. output.reverb_bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus_name);
  352. float uniformity = area->get_reverb_uniformity();
  353. float area_send = area->get_reverb_amount();
  354. if (uniformity > 0.0) {
  355. float distance = listener_area_pos.length();
  356. float attenuation = Math::db2linear(_get_attenuation_db(distance));
  357. //float dist_att_db = -20 * Math::log(dist + 0.00001); //logarithmic attenuation, like in real life
  358. float center_val[3] = { 0.5f, 0.25f, 0.16666f };
  359. AudioFrame center_frame(center_val[vol_index_max - 1], center_val[vol_index_max - 1]);
  360. if (attenuation < 1.0) {
  361. //pan the uniform sound
  362. Vector3 rev_pos = listener_area_pos;
  363. rev_pos.y = 0;
  364. rev_pos.normalize();
  365. if (cc >= 1) {
  366. // Stereo pair
  367. float c = rev_pos.x * 0.5 + 0.5;
  368. output.reverb_vol[0].l = 1.0 - c;
  369. output.reverb_vol[0].r = c;
  370. }
  371. if (cc >= 3) {
  372. // Center pair + Side pair
  373. float xl = Vector3(-1, 0, -1).normalized().dot(rev_pos) * 0.5 + 0.5;
  374. float xr = Vector3(1, 0, -1).normalized().dot(rev_pos) * 0.5 + 0.5;
  375. output.reverb_vol[1].l = xl;
  376. output.reverb_vol[1].r = xr;
  377. output.reverb_vol[2].l = 1.0 - xr;
  378. output.reverb_vol[2].r = 1.0 - xl;
  379. }
  380. if (cc >= 4) {
  381. // Rear pair
  382. // FIXME: Not sure what math should be done here
  383. float c = rev_pos.x * 0.5 + 0.5;
  384. output.reverb_vol[3].l = 1.0 - c;
  385. output.reverb_vol[3].r = c;
  386. }
  387. for (int i = 0; i < vol_index_max; i++) {
  388. output.reverb_vol[i] = output.reverb_vol[i].linear_interpolate(center_frame, attenuation);
  389. }
  390. } else {
  391. for (int i = 0; i < vol_index_max; i++) {
  392. output.reverb_vol[i] = center_frame;
  393. }
  394. }
  395. for (int i = 0; i < vol_index_max; i++) {
  396. output.reverb_vol[i] = output.vol[i].linear_interpolate(output.reverb_vol[i] * attenuation, uniformity);
  397. output.reverb_vol[i] *= area_send;
  398. }
  399. } else {
  400. for (int i = 0; i < vol_index_max; i++) {
  401. output.reverb_vol[i] = output.vol[i] * area_send;
  402. }
  403. }
  404. }
  405. }
  406. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  407. Vector3 listener_velocity;
  408. if (listener_is_camera) {
  409. listener_velocity = camera->get_doppler_tracked_velocity();
  410. }
  411. Vector3 local_velocity = listener_node->get_global_transform().orthonormalized().basis.xform_inv(linear_velocity - listener_velocity);
  412. if (local_velocity == Vector3()) {
  413. output.pitch_scale = 1.0;
  414. } else {
  415. float approaching = local_pos.normalized().dot(local_velocity.normalized());
  416. float velocity = local_velocity.length();
  417. float speed_of_sound = 343.0;
  418. output.pitch_scale = speed_of_sound / (speed_of_sound + velocity * approaching);
  419. output.pitch_scale = CLAMP(output.pitch_scale, (1 / 8.0), 8.0); //avoid crazy stuff
  420. }
  421. } else {
  422. output.pitch_scale = 1.0;
  423. }
  424. if (!filled_reverb) {
  425. for (int i = 0; i < vol_index_max; i++) {
  426. output.reverb_vol[i] = AudioFrame(0, 0);
  427. }
  428. }
  429. outputs[new_output_count] = output;
  430. new_output_count++;
  431. if (new_output_count == MAX_OUTPUTS)
  432. break;
  433. }
  434. output_count = new_output_count;
  435. output_ready = true;
  436. }
  437. //start playing if requested
  438. if (setplay >= 0.0) {
  439. setseek = setplay;
  440. active = true;
  441. setplay = -1;
  442. //do not update, this makes it easier to animate (will shut off otherwise)
  443. ///_change_notify("playing"); //update property in editor
  444. }
  445. //stop playing if no longer active
  446. if (!active) {
  447. set_physics_process_internal(false);
  448. //do not update, this makes it easier to animate (will shut off otherwise)
  449. //_change_notify("playing"); //update property in editor
  450. emit_signal("finished");
  451. }
  452. }
  453. }
  454. void AudioStreamPlayer3D::set_stream(Ref<AudioStream> p_stream) {
  455. AudioServer::get_singleton()->lock();
  456. mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
  457. if (stream_playback.is_valid()) {
  458. stream_playback.unref();
  459. stream.unref();
  460. active = false;
  461. setseek = -1;
  462. }
  463. if (p_stream.is_valid()) {
  464. stream = p_stream;
  465. stream_playback = p_stream->instance_playback();
  466. }
  467. AudioServer::get_singleton()->unlock();
  468. if (p_stream.is_valid() && stream_playback.is_null()) {
  469. stream.unref();
  470. }
  471. }
  472. Ref<AudioStream> AudioStreamPlayer3D::get_stream() const {
  473. return stream;
  474. }
  475. void AudioStreamPlayer3D::set_unit_db(float p_volume) {
  476. unit_db = p_volume;
  477. }
  478. float AudioStreamPlayer3D::get_unit_db() const {
  479. return unit_db;
  480. }
  481. void AudioStreamPlayer3D::set_unit_size(float p_volume) {
  482. unit_size = p_volume;
  483. }
  484. float AudioStreamPlayer3D::get_unit_size() const {
  485. return unit_size;
  486. }
  487. void AudioStreamPlayer3D::set_max_db(float p_boost) {
  488. max_db = p_boost;
  489. }
  490. float AudioStreamPlayer3D::get_max_db() const {
  491. return max_db;
  492. }
  493. void AudioStreamPlayer3D::set_pitch_scale(float p_pitch_scale) {
  494. ERR_FAIL_COND(p_pitch_scale <= 0.0);
  495. pitch_scale = p_pitch_scale;
  496. }
  497. float AudioStreamPlayer3D::get_pitch_scale() const {
  498. return pitch_scale;
  499. }
  500. void AudioStreamPlayer3D::play(float p_from_pos) {
  501. if (stream_playback.is_valid()) {
  502. active = true;
  503. setplay = p_from_pos;
  504. output_ready = false;
  505. set_physics_process_internal(true);
  506. }
  507. }
  508. void AudioStreamPlayer3D::seek(float p_seconds) {
  509. if (stream_playback.is_valid()) {
  510. setseek = p_seconds;
  511. }
  512. }
  513. void AudioStreamPlayer3D::stop() {
  514. if (stream_playback.is_valid()) {
  515. active = false;
  516. set_physics_process_internal(false);
  517. setplay = -1;
  518. }
  519. }
  520. bool AudioStreamPlayer3D::is_playing() const {
  521. if (stream_playback.is_valid()) {
  522. return active; // && stream_playback->is_playing();
  523. }
  524. return false;
  525. }
  526. float AudioStreamPlayer3D::get_playback_position() {
  527. if (stream_playback.is_valid()) {
  528. return stream_playback->get_playback_position();
  529. }
  530. return 0;
  531. }
  532. void AudioStreamPlayer3D::set_bus(const StringName &p_bus) {
  533. //if audio is active, must lock this
  534. AudioServer::get_singleton()->lock();
  535. bus = p_bus;
  536. AudioServer::get_singleton()->unlock();
  537. }
  538. StringName AudioStreamPlayer3D::get_bus() const {
  539. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  540. if (AudioServer::get_singleton()->get_bus_name(i) == bus) {
  541. return bus;
  542. }
  543. }
  544. return "Master";
  545. }
  546. void AudioStreamPlayer3D::set_autoplay(bool p_enable) {
  547. autoplay = p_enable;
  548. }
  549. bool AudioStreamPlayer3D::is_autoplay_enabled() {
  550. return autoplay;
  551. }
  552. void AudioStreamPlayer3D::_set_playing(bool p_enable) {
  553. if (p_enable)
  554. play();
  555. else
  556. stop();
  557. }
  558. bool AudioStreamPlayer3D::_is_active() const {
  559. return active;
  560. }
  561. void AudioStreamPlayer3D::_validate_property(PropertyInfo &property) const {
  562. if (property.name == "bus") {
  563. String options;
  564. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  565. if (i > 0)
  566. options += ",";
  567. String name = AudioServer::get_singleton()->get_bus_name(i);
  568. options += name;
  569. }
  570. property.hint_string = options;
  571. }
  572. }
  573. void AudioStreamPlayer3D::_bus_layout_changed() {
  574. _change_notify();
  575. }
  576. void AudioStreamPlayer3D::set_max_distance(float p_metres) {
  577. ERR_FAIL_COND(p_metres < 0.0);
  578. max_distance = p_metres;
  579. }
  580. float AudioStreamPlayer3D::get_max_distance() const {
  581. return max_distance;
  582. }
  583. void AudioStreamPlayer3D::set_area_mask(uint32_t p_mask) {
  584. area_mask = p_mask;
  585. }
  586. uint32_t AudioStreamPlayer3D::get_area_mask() const {
  587. return area_mask;
  588. }
  589. void AudioStreamPlayer3D::set_emission_angle_enabled(bool p_enable) {
  590. emission_angle_enabled = p_enable;
  591. update_gizmo();
  592. }
  593. bool AudioStreamPlayer3D::is_emission_angle_enabled() const {
  594. return emission_angle_enabled;
  595. }
  596. void AudioStreamPlayer3D::set_emission_angle(float p_angle) {
  597. ERR_FAIL_COND(p_angle < 0 || p_angle > 90);
  598. emission_angle = p_angle;
  599. update_gizmo();
  600. }
  601. float AudioStreamPlayer3D::get_emission_angle() const {
  602. return emission_angle;
  603. }
  604. void AudioStreamPlayer3D::set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db) {
  605. emission_angle_filter_attenuation_db = p_angle_attenuation_db;
  606. }
  607. float AudioStreamPlayer3D::get_emission_angle_filter_attenuation_db() const {
  608. return emission_angle_filter_attenuation_db;
  609. }
  610. void AudioStreamPlayer3D::set_attenuation_filter_cutoff_hz(float p_hz) {
  611. attenuation_filter_cutoff_hz = p_hz;
  612. }
  613. float AudioStreamPlayer3D::get_attenuation_filter_cutoff_hz() const {
  614. return attenuation_filter_cutoff_hz;
  615. }
  616. void AudioStreamPlayer3D::set_attenuation_filter_db(float p_db) {
  617. attenuation_filter_db = p_db;
  618. }
  619. float AudioStreamPlayer3D::get_attenuation_filter_db() const {
  620. return attenuation_filter_db;
  621. }
  622. void AudioStreamPlayer3D::set_attenuation_model(AttenuationModel p_model) {
  623. ERR_FAIL_INDEX(p_model, 3);
  624. attenuation_model = p_model;
  625. }
  626. AudioStreamPlayer3D::AttenuationModel AudioStreamPlayer3D::get_attenuation_model() const {
  627. return attenuation_model;
  628. }
  629. void AudioStreamPlayer3D::set_out_of_range_mode(OutOfRangeMode p_mode) {
  630. ERR_FAIL_INDEX((int)p_mode, 2);
  631. out_of_range_mode = p_mode;
  632. }
  633. AudioStreamPlayer3D::OutOfRangeMode AudioStreamPlayer3D::get_out_of_range_mode() const {
  634. return out_of_range_mode;
  635. }
  636. void AudioStreamPlayer3D::set_doppler_tracking(DopplerTracking p_tracking) {
  637. if (doppler_tracking == p_tracking)
  638. return;
  639. doppler_tracking = p_tracking;
  640. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  641. set_notify_transform(true);
  642. velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
  643. if (is_inside_tree()) {
  644. velocity_tracker->reset(get_global_transform().origin);
  645. }
  646. } else {
  647. set_notify_transform(false);
  648. }
  649. }
  650. AudioStreamPlayer3D::DopplerTracking AudioStreamPlayer3D::get_doppler_tracking() const {
  651. return doppler_tracking;
  652. }
  653. void AudioStreamPlayer3D::set_stream_paused(bool p_pause) {
  654. if (p_pause != stream_paused) {
  655. stream_paused = p_pause;
  656. stream_paused_fade_in = stream_paused ? false : true;
  657. stream_paused_fade_out = stream_paused ? true : false;
  658. }
  659. }
  660. bool AudioStreamPlayer3D::get_stream_paused() const {
  661. return stream_paused;
  662. }
  663. Ref<AudioStreamPlayback> AudioStreamPlayer3D::get_stream_playback() {
  664. return stream_playback;
  665. }
  666. void AudioStreamPlayer3D::_bind_methods() {
  667. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer3D::set_stream);
  668. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer3D::get_stream);
  669. ClassDB::bind_method(D_METHOD("set_unit_db", "unit_db"), &AudioStreamPlayer3D::set_unit_db);
  670. ClassDB::bind_method(D_METHOD("get_unit_db"), &AudioStreamPlayer3D::get_unit_db);
  671. ClassDB::bind_method(D_METHOD("set_unit_size", "unit_size"), &AudioStreamPlayer3D::set_unit_size);
  672. ClassDB::bind_method(D_METHOD("get_unit_size"), &AudioStreamPlayer3D::get_unit_size);
  673. ClassDB::bind_method(D_METHOD("set_max_db", "max_db"), &AudioStreamPlayer3D::set_max_db);
  674. ClassDB::bind_method(D_METHOD("get_max_db"), &AudioStreamPlayer3D::get_max_db);
  675. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer3D::set_pitch_scale);
  676. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer3D::get_pitch_scale);
  677. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer3D::play, DEFVAL(0.0));
  678. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer3D::seek);
  679. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer3D::stop);
  680. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer3D::is_playing);
  681. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer3D::get_playback_position);
  682. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer3D::set_bus);
  683. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer3D::get_bus);
  684. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer3D::set_autoplay);
  685. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer3D::is_autoplay_enabled);
  686. ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer3D::_set_playing);
  687. ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer3D::_is_active);
  688. ClassDB::bind_method(D_METHOD("set_max_distance", "metres"), &AudioStreamPlayer3D::set_max_distance);
  689. ClassDB::bind_method(D_METHOD("get_max_distance"), &AudioStreamPlayer3D::get_max_distance);
  690. ClassDB::bind_method(D_METHOD("set_area_mask", "mask"), &AudioStreamPlayer3D::set_area_mask);
  691. ClassDB::bind_method(D_METHOD("get_area_mask"), &AudioStreamPlayer3D::get_area_mask);
  692. ClassDB::bind_method(D_METHOD("set_emission_angle", "degrees"), &AudioStreamPlayer3D::set_emission_angle);
  693. ClassDB::bind_method(D_METHOD("get_emission_angle"), &AudioStreamPlayer3D::get_emission_angle);
  694. ClassDB::bind_method(D_METHOD("set_emission_angle_enabled", "enabled"), &AudioStreamPlayer3D::set_emission_angle_enabled);
  695. ClassDB::bind_method(D_METHOD("is_emission_angle_enabled"), &AudioStreamPlayer3D::is_emission_angle_enabled);
  696. ClassDB::bind_method(D_METHOD("set_emission_angle_filter_attenuation_db", "db"), &AudioStreamPlayer3D::set_emission_angle_filter_attenuation_db);
  697. ClassDB::bind_method(D_METHOD("get_emission_angle_filter_attenuation_db"), &AudioStreamPlayer3D::get_emission_angle_filter_attenuation_db);
  698. ClassDB::bind_method(D_METHOD("set_attenuation_filter_cutoff_hz", "degrees"), &AudioStreamPlayer3D::set_attenuation_filter_cutoff_hz);
  699. ClassDB::bind_method(D_METHOD("get_attenuation_filter_cutoff_hz"), &AudioStreamPlayer3D::get_attenuation_filter_cutoff_hz);
  700. ClassDB::bind_method(D_METHOD("set_attenuation_filter_db", "db"), &AudioStreamPlayer3D::set_attenuation_filter_db);
  701. ClassDB::bind_method(D_METHOD("get_attenuation_filter_db"), &AudioStreamPlayer3D::get_attenuation_filter_db);
  702. ClassDB::bind_method(D_METHOD("set_attenuation_model", "model"), &AudioStreamPlayer3D::set_attenuation_model);
  703. ClassDB::bind_method(D_METHOD("get_attenuation_model"), &AudioStreamPlayer3D::get_attenuation_model);
  704. ClassDB::bind_method(D_METHOD("set_out_of_range_mode", "mode"), &AudioStreamPlayer3D::set_out_of_range_mode);
  705. ClassDB::bind_method(D_METHOD("get_out_of_range_mode"), &AudioStreamPlayer3D::get_out_of_range_mode);
  706. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &AudioStreamPlayer3D::set_doppler_tracking);
  707. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &AudioStreamPlayer3D::get_doppler_tracking);
  708. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer3D::set_stream_paused);
  709. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer3D::get_stream_paused);
  710. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer3D::get_stream_playback);
  711. ClassDB::bind_method(D_METHOD("_bus_layout_changed"), &AudioStreamPlayer3D::_bus_layout_changed);
  712. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  713. ADD_PROPERTY(PropertyInfo(Variant::INT, "attenuation_model", PROPERTY_HINT_ENUM, "Inverse,InverseSquare,Log"), "set_attenuation_model", "get_attenuation_model");
  714. ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_db", PROPERTY_HINT_RANGE, "-80,80"), "set_unit_db", "get_unit_db");
  715. ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_size", PROPERTY_HINT_RANGE, "0.1,100,0.1"), "set_unit_size", "get_unit_size");
  716. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_db", PROPERTY_HINT_RANGE, "-24,6"), "set_max_db", "get_max_db");
  717. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,32,0.01"), "set_pitch_scale", "get_pitch_scale");
  718. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
  719. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  720. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  721. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_distance", PROPERTY_HINT_EXP_RANGE, "0,4096,1,or_greater"), "set_max_distance", "get_max_distance");
  722. ADD_PROPERTY(PropertyInfo(Variant::INT, "out_of_range_mode", PROPERTY_HINT_ENUM, "Mix,Pause"), "set_out_of_range_mode", "get_out_of_range_mode");
  723. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  724. ADD_PROPERTY(PropertyInfo(Variant::INT, "area_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_area_mask", "get_area_mask");
  725. ADD_GROUP("Emission Angle", "emission_angle");
  726. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emission_angle_enabled"), "set_emission_angle_enabled", "is_emission_angle_enabled");
  727. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_angle_degrees", PROPERTY_HINT_RANGE, "0.1,90,0.1"), "set_emission_angle", "get_emission_angle");
  728. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_angle_filter_attenuation_db", PROPERTY_HINT_RANGE, "-80,0,0.1"), "set_emission_angle_filter_attenuation_db", "get_emission_angle_filter_attenuation_db");
  729. ADD_GROUP("Attenuation Filter", "attenuation_filter_");
  730. ADD_PROPERTY(PropertyInfo(Variant::REAL, "attenuation_filter_cutoff_hz", PROPERTY_HINT_RANGE, "50,50000,1"), "set_attenuation_filter_cutoff_hz", "get_attenuation_filter_cutoff_hz");
  731. ADD_PROPERTY(PropertyInfo(Variant::REAL, "attenuation_filter_db", PROPERTY_HINT_RANGE, "-80,0,0.1"), "set_attenuation_filter_db", "get_attenuation_filter_db");
  732. ADD_GROUP("Doppler", "doppler_");
  733. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  734. BIND_ENUM_CONSTANT(ATTENUATION_INVERSE_DISTANCE);
  735. BIND_ENUM_CONSTANT(ATTENUATION_INVERSE_SQUARE_DISTANCE);
  736. BIND_ENUM_CONSTANT(ATTENUATION_LOGARITHMIC);
  737. BIND_ENUM_CONSTANT(OUT_OF_RANGE_MIX);
  738. BIND_ENUM_CONSTANT(OUT_OF_RANGE_PAUSE);
  739. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED);
  740. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP);
  741. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP);
  742. ADD_SIGNAL(MethodInfo("finished"));
  743. }
  744. AudioStreamPlayer3D::AudioStreamPlayer3D() {
  745. unit_db = 0;
  746. unit_size = 1;
  747. attenuation_model = ATTENUATION_INVERSE_DISTANCE;
  748. max_db = 3;
  749. pitch_scale = 1.0;
  750. autoplay = false;
  751. setseek = -1;
  752. active = false;
  753. output_count = 0;
  754. prev_output_count = 0;
  755. max_distance = 0;
  756. setplay = -1;
  757. output_ready = false;
  758. area_mask = 1;
  759. emission_angle = 45;
  760. emission_angle_enabled = false;
  761. emission_angle_filter_attenuation_db = -12;
  762. attenuation_filter_cutoff_hz = 5000;
  763. attenuation_filter_db = -24;
  764. out_of_range_mode = OUT_OF_RANGE_MIX;
  765. doppler_tracking = DOPPLER_TRACKING_DISABLED;
  766. stream_paused = false;
  767. stream_paused_fade_in = false;
  768. stream_paused_fade_out = false;
  769. velocity_tracker.instance();
  770. AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");
  771. set_disable_scale(true);
  772. }
  773. AudioStreamPlayer3D::~AudioStreamPlayer3D() {
  774. }