audio_stream_player_3d.cpp 27 KB

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