audio_stream_player_2d.cpp 16 KB

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