2
0

audio_stream_player_2d.cpp 17 KB

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