2
0

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