audio_stream_player_3d.cpp 32 KB

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