audio_stream_player_2d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*************************************************************************/
  2. /* audio_stream_player_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_2d.h"
  31. #include "core/engine.h"
  32. #include "scene/2d/area_2d.h"
  33. #include "scene/main/viewport.h"
  34. void AudioStreamPlayer2D::_mix_audio() {
  35. if (!stream_playback.is_valid() || !active.is_set() ||
  36. (stream_paused && !stream_paused_fade_out)) {
  37. return;
  38. }
  39. if (setseek.get() >= 0.0) {
  40. stream_playback->start(setseek.get());
  41. setseek.set(-1.0); //reset seek
  42. }
  43. //get data
  44. AudioFrame *buffer = mix_buffer.ptrw();
  45. int buffer_size = mix_buffer.size();
  46. if (stream_paused_fade_out) {
  47. // Short fadeout ramp
  48. buffer_size = MIN(buffer_size, 128);
  49. }
  50. stream_playback->mix(buffer, pitch_scale, buffer_size);
  51. //write all outputs
  52. int oc = output_count.get();
  53. for (int i = 0; i < oc; i++) {
  54. Output current = outputs[i];
  55. //see if current output exists, to keep volume ramp
  56. bool found = false;
  57. for (int j = i; j < prev_output_count; j++) {
  58. if (prev_outputs[j].viewport == current.viewport) {
  59. if (j != i) {
  60. SWAP(prev_outputs[j], prev_outputs[i]);
  61. }
  62. found = true;
  63. break;
  64. }
  65. }
  66. if (!found) {
  67. //create new if was not used before
  68. if (prev_output_count < MAX_OUTPUTS) {
  69. prev_outputs[prev_output_count] = prev_outputs[i]; //may be owned by another viewport
  70. prev_output_count++;
  71. }
  72. prev_outputs[i] = current;
  73. }
  74. //mix!
  75. AudioFrame target_volume = stream_paused_fade_out ? AudioFrame(0.f, 0.f) : current.vol;
  76. AudioFrame vol_prev = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol;
  77. AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size);
  78. AudioFrame vol = vol_prev;
  79. int cc = AudioServer::get_singleton()->get_channel_count();
  80. if (cc == 1) {
  81. if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.bus_index, 0))
  82. continue; //may have been removed
  83. AudioFrame *target = AudioServer::get_singleton()->thread_get_channel_mix_buffer(current.bus_index, 0);
  84. for (int j = 0; j < buffer_size; j++) {
  85. target[j] += buffer[j] * vol;
  86. vol += vol_inc;
  87. }
  88. } else {
  89. AudioFrame *targets[4];
  90. bool valid = true;
  91. for (int k = 0; k < cc; k++) {
  92. if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.bus_index, k)) {
  93. valid = false; //may have been removed
  94. break;
  95. }
  96. targets[k] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(current.bus_index, k);
  97. }
  98. if (!valid)
  99. continue;
  100. for (int j = 0; j < buffer_size; j++) {
  101. AudioFrame frame = buffer[j] * vol;
  102. for (int k = 0; k < cc; k++) {
  103. targets[k][j] += frame;
  104. }
  105. vol += vol_inc;
  106. }
  107. }
  108. prev_outputs[i] = current;
  109. }
  110. prev_output_count = oc;
  111. //stream is no longer active, disable this.
  112. if (!stream_playback->is_playing()) {
  113. active.clear();
  114. }
  115. output_ready.clear();
  116. stream_paused_fade_in = false;
  117. stream_paused_fade_out = false;
  118. }
  119. void AudioStreamPlayer2D::_notification(int p_what) {
  120. if (p_what == NOTIFICATION_ENTER_TREE) {
  121. AudioServer::get_singleton()->add_callback(_mix_audios, this);
  122. if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
  123. play();
  124. }
  125. }
  126. if (p_what == NOTIFICATION_EXIT_TREE) {
  127. AudioServer::get_singleton()->remove_callback(_mix_audios, this);
  128. }
  129. if (p_what == NOTIFICATION_PAUSED) {
  130. if (!can_process()) {
  131. // Node can't process so we start fading out to silence
  132. set_stream_paused(true);
  133. }
  134. }
  135. if (p_what == NOTIFICATION_UNPAUSED) {
  136. set_stream_paused(false);
  137. }
  138. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  139. //update anything related to position first, if possible of course
  140. if (!output_ready.is_set()) {
  141. List<Viewport *> viewports;
  142. Ref<World2D> world_2d = get_world_2d();
  143. ERR_FAIL_COND(world_2d.is_null());
  144. int new_output_count = 0;
  145. Vector2 global_pos = get_global_position();
  146. int bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus);
  147. //check if any area is diverting sound into a bus
  148. Physics2DDirectSpaceState *space_state = Physics2DServer::get_singleton()->space_get_direct_state(world_2d->get_space());
  149. Physics2DDirectSpaceState::ShapeResult sr[MAX_INTERSECT_AREAS];
  150. int areas = space_state->intersect_point(global_pos, sr, MAX_INTERSECT_AREAS, Set<RID>(), area_mask, false, true);
  151. for (int i = 0; i < areas; i++) {
  152. Area2D *area2d = Object::cast_to<Area2D>(sr[i].collider);
  153. if (!area2d)
  154. continue;
  155. if (!area2d->is_overriding_audio_bus())
  156. continue;
  157. StringName bus_name = area2d->get_audio_bus_name();
  158. bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus_name);
  159. break;
  160. }
  161. world_2d->get_viewport_list(&viewports);
  162. for (List<Viewport *>::Element *E = viewports.front(); E; E = E->next()) {
  163. Viewport *vp = E->get();
  164. if (vp->is_audio_listener_2d()) {
  165. //compute matrix to convert to screen
  166. Transform2D to_screen = vp->get_global_canvas_transform() * vp->get_canvas_transform();
  167. Vector2 screen_size = vp->get_visible_rect().size;
  168. //screen in global is used for attenuation
  169. Vector2 screen_in_global = to_screen.affine_inverse().xform(screen_size * 0.5);
  170. float dist = global_pos.distance_to(screen_in_global); //distance to screen center
  171. if (dist > max_distance)
  172. continue; //can't hear this sound in this viewport
  173. float multiplier = Math::pow(1.0f - dist / max_distance, attenuation);
  174. multiplier *= Math::db2linear(volume_db); //also apply player volume!
  175. //point in screen is used for panning
  176. Vector2 point_in_screen = to_screen.xform(global_pos);
  177. float pan = CLAMP(point_in_screen.x / screen_size.width, 0.0, 1.0);
  178. float l = 1.0 - pan;
  179. float r = pan;
  180. outputs[new_output_count].vol = AudioFrame(l, r) * multiplier;
  181. outputs[new_output_count].bus_index = bus_index;
  182. outputs[new_output_count].viewport = vp; //keep pointer only for reference
  183. new_output_count++;
  184. if (new_output_count == MAX_OUTPUTS)
  185. break;
  186. }
  187. }
  188. output_count.set(new_output_count);
  189. output_ready.set();
  190. }
  191. //start playing if requested
  192. if (setplay.get() >= 0.0) {
  193. setseek.set(setplay.get());
  194. active.set();
  195. setplay.set(-1);
  196. //do not update, this makes it easier to animate (will shut off otherwise)
  197. //_change_notify("playing"); //update property in editor
  198. }
  199. //stop playing if no longer active
  200. if (!active.is_set()) {
  201. set_physics_process_internal(false);
  202. //do not update, this makes it easier to animate (will shut off otherwise)
  203. //_change_notify("playing"); //update property in editor
  204. emit_signal("finished");
  205. }
  206. }
  207. }
  208. void AudioStreamPlayer2D::set_stream(Ref<AudioStream> p_stream) {
  209. AudioServer::get_singleton()->lock();
  210. mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
  211. if (stream_playback.is_valid()) {
  212. stream_playback.unref();
  213. stream.unref();
  214. active.clear();
  215. setseek.set(-1);
  216. }
  217. if (p_stream.is_valid()) {
  218. stream = p_stream;
  219. stream_playback = p_stream->instance_playback();
  220. }
  221. AudioServer::get_singleton()->unlock();
  222. if (p_stream.is_valid() && stream_playback.is_null()) {
  223. stream.unref();
  224. }
  225. }
  226. Ref<AudioStream> AudioStreamPlayer2D::get_stream() const {
  227. return stream;
  228. }
  229. void AudioStreamPlayer2D::set_volume_db(float p_volume) {
  230. volume_db = p_volume;
  231. }
  232. float AudioStreamPlayer2D::get_volume_db() const {
  233. return volume_db;
  234. }
  235. void AudioStreamPlayer2D::set_pitch_scale(float p_pitch_scale) {
  236. ERR_FAIL_COND(p_pitch_scale <= 0.0);
  237. pitch_scale = p_pitch_scale;
  238. }
  239. float AudioStreamPlayer2D::get_pitch_scale() const {
  240. return pitch_scale;
  241. }
  242. void AudioStreamPlayer2D::play(float p_from_pos) {
  243. if (!is_playing()) {
  244. // Reset the prev_output_count if the stream is stopped
  245. prev_output_count = 0;
  246. }
  247. if (stream_playback.is_valid()) {
  248. setplay.set(p_from_pos);
  249. output_ready.clear();
  250. set_physics_process_internal(true);
  251. }
  252. }
  253. void AudioStreamPlayer2D::seek(float p_seconds) {
  254. if (stream_playback.is_valid()) {
  255. setseek.set(p_seconds);
  256. }
  257. }
  258. void AudioStreamPlayer2D::stop() {
  259. if (stream_playback.is_valid()) {
  260. active.clear();
  261. set_physics_process_internal(false);
  262. setplay.set(-1);
  263. }
  264. }
  265. bool AudioStreamPlayer2D::is_playing() const {
  266. if (stream_playback.is_valid()) {
  267. return active.is_set() || setplay.get() >= 0;
  268. }
  269. return false;
  270. }
  271. float AudioStreamPlayer2D::get_playback_position() {
  272. if (stream_playback.is_valid()) {
  273. float ss = setseek.get();
  274. if (ss >= 0.0) {
  275. return ss;
  276. }
  277. return stream_playback->get_playback_position();
  278. }
  279. return 0;
  280. }
  281. void AudioStreamPlayer2D::set_bus(const StringName &p_bus) {
  282. //if audio is active, must lock this
  283. AudioServer::get_singleton()->lock();
  284. bus = p_bus;
  285. AudioServer::get_singleton()->unlock();
  286. }
  287. StringName AudioStreamPlayer2D::get_bus() const {
  288. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  289. if (AudioServer::get_singleton()->get_bus_name(i) == bus) {
  290. return bus;
  291. }
  292. }
  293. return "Master";
  294. }
  295. void AudioStreamPlayer2D::set_autoplay(bool p_enable) {
  296. autoplay = p_enable;
  297. }
  298. bool AudioStreamPlayer2D::is_autoplay_enabled() {
  299. return autoplay;
  300. }
  301. void AudioStreamPlayer2D::_set_playing(bool p_enable) {
  302. if (p_enable)
  303. play();
  304. else
  305. stop();
  306. }
  307. bool AudioStreamPlayer2D::_is_active() const {
  308. return active.is_set();
  309. }
  310. void AudioStreamPlayer2D::_validate_property(PropertyInfo &property) const {
  311. if (property.name == "bus") {
  312. String options;
  313. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  314. if (i > 0)
  315. options += ",";
  316. String name = AudioServer::get_singleton()->get_bus_name(i);
  317. options += name;
  318. }
  319. property.hint_string = options;
  320. }
  321. }
  322. void AudioStreamPlayer2D::_bus_layout_changed() {
  323. _change_notify();
  324. }
  325. void AudioStreamPlayer2D::set_max_distance(float p_pixels) {
  326. ERR_FAIL_COND(p_pixels <= 0.0);
  327. max_distance = p_pixels;
  328. }
  329. float AudioStreamPlayer2D::get_max_distance() const {
  330. return max_distance;
  331. }
  332. void AudioStreamPlayer2D::set_attenuation(float p_curve) {
  333. attenuation = p_curve;
  334. }
  335. float AudioStreamPlayer2D::get_attenuation() const {
  336. return attenuation;
  337. }
  338. void AudioStreamPlayer2D::set_area_mask(uint32_t p_mask) {
  339. area_mask = p_mask;
  340. }
  341. uint32_t AudioStreamPlayer2D::get_area_mask() const {
  342. return area_mask;
  343. }
  344. void AudioStreamPlayer2D::set_stream_paused(bool p_pause) {
  345. if (p_pause != stream_paused) {
  346. stream_paused = p_pause;
  347. stream_paused_fade_in = !p_pause;
  348. stream_paused_fade_out = p_pause;
  349. }
  350. }
  351. bool AudioStreamPlayer2D::get_stream_paused() const {
  352. return stream_paused;
  353. }
  354. Ref<AudioStreamPlayback> AudioStreamPlayer2D::get_stream_playback() {
  355. return stream_playback;
  356. }
  357. void AudioStreamPlayer2D::_bind_methods() {
  358. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer2D::set_stream);
  359. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer2D::get_stream);
  360. ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer2D::set_volume_db);
  361. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer2D::get_volume_db);
  362. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer2D::set_pitch_scale);
  363. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer2D::get_pitch_scale);
  364. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer2D::play, DEFVAL(0.0));
  365. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer2D::seek);
  366. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer2D::stop);
  367. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer2D::is_playing);
  368. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer2D::get_playback_position);
  369. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer2D::set_bus);
  370. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer2D::get_bus);
  371. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer2D::set_autoplay);
  372. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer2D::is_autoplay_enabled);
  373. ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer2D::_set_playing);
  374. ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer2D::_is_active);
  375. ClassDB::bind_method(D_METHOD("set_max_distance", "pixels"), &AudioStreamPlayer2D::set_max_distance);
  376. ClassDB::bind_method(D_METHOD("get_max_distance"), &AudioStreamPlayer2D::get_max_distance);
  377. ClassDB::bind_method(D_METHOD("set_attenuation", "curve"), &AudioStreamPlayer2D::set_attenuation);
  378. ClassDB::bind_method(D_METHOD("get_attenuation"), &AudioStreamPlayer2D::get_attenuation);
  379. ClassDB::bind_method(D_METHOD("set_area_mask", "mask"), &AudioStreamPlayer2D::set_area_mask);
  380. ClassDB::bind_method(D_METHOD("get_area_mask"), &AudioStreamPlayer2D::get_area_mask);
  381. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer2D::set_stream_paused);
  382. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer2D::get_stream_paused);
  383. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer2D::get_stream_playback);
  384. ClassDB::bind_method(D_METHOD("_bus_layout_changed"), &AudioStreamPlayer2D::_bus_layout_changed);
  385. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  386. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24"), "set_volume_db", "get_volume_db");
  387. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
  388. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
  389. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  390. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  391. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_distance", PROPERTY_HINT_EXP_RANGE, "1,4096,1,or_greater"), "set_max_distance", "get_max_distance");
  392. ADD_PROPERTY(PropertyInfo(Variant::REAL, "attenuation", PROPERTY_HINT_EXP_EASING, "attenuation"), "set_attenuation", "get_attenuation");
  393. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  394. ADD_PROPERTY(PropertyInfo(Variant::INT, "area_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_area_mask", "get_area_mask");
  395. ADD_SIGNAL(MethodInfo("finished"));
  396. }
  397. AudioStreamPlayer2D::AudioStreamPlayer2D() {
  398. volume_db = 0;
  399. pitch_scale = 1.0;
  400. autoplay = false;
  401. setseek.set(-1);
  402. prev_output_count = 0;
  403. max_distance = 2000;
  404. attenuation = 1;
  405. setplay.set(-1);
  406. area_mask = 1;
  407. stream_paused = false;
  408. stream_paused_fade_in = false;
  409. stream_paused_fade_out = false;
  410. AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");
  411. }
  412. AudioStreamPlayer2D::~AudioStreamPlayer2D() {
  413. }