audio_stream_player.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*************************************************************************/
  2. /* audio_stream_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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.h"
  31. #include "core/engine.h"
  32. void AudioStreamPlayer::_mix_to_bus(const AudioFrame *p_frames, int p_amount) {
  33. int bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus);
  34. AudioFrame *targets[4] = { nullptr, nullptr, nullptr, nullptr };
  35. if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {
  36. targets[0] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, 0);
  37. } else {
  38. switch (mix_target) {
  39. case MIX_TARGET_STEREO: {
  40. targets[0] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, 0);
  41. } break;
  42. case MIX_TARGET_SURROUND: {
  43. for (int i = 0; i < AudioServer::get_singleton()->get_channel_count(); i++) {
  44. targets[i] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, i);
  45. }
  46. } break;
  47. case MIX_TARGET_CENTER: {
  48. targets[0] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, 1);
  49. } break;
  50. }
  51. }
  52. for (int c = 0; c < 4; c++) {
  53. if (!targets[c]) {
  54. break;
  55. }
  56. for (int i = 0; i < p_amount; i++) {
  57. targets[c][i] += p_frames[i];
  58. }
  59. }
  60. }
  61. void AudioStreamPlayer::_mix_internal(bool p_fadeout) {
  62. //get data
  63. AudioFrame *buffer = mix_buffer.ptrw();
  64. int buffer_size = mix_buffer.size();
  65. if (p_fadeout) {
  66. // Short fadeout ramp
  67. buffer_size = MIN(buffer_size, 128);
  68. }
  69. stream_playback->mix(buffer, pitch_scale, buffer_size);
  70. //multiply volume interpolating to avoid clicks if this changes
  71. float target_volume = p_fadeout ? -80.0 : volume_db;
  72. float vol = Math::db2linear(mix_volume_db);
  73. float vol_inc = (Math::db2linear(target_volume) - vol) / float(buffer_size);
  74. for (int i = 0; i < buffer_size; i++) {
  75. buffer[i] *= vol;
  76. vol += vol_inc;
  77. }
  78. //set volume for next mix
  79. mix_volume_db = target_volume;
  80. _mix_to_bus(buffer, buffer_size);
  81. }
  82. void AudioStreamPlayer::_mix_audio() {
  83. if (use_fadeout) {
  84. _mix_to_bus(fadeout_buffer.ptr(), fadeout_buffer.size());
  85. use_fadeout = false;
  86. }
  87. if (!stream_playback.is_valid() || !active.is_set() ||
  88. (stream_paused && !stream_paused_fade)) {
  89. return;
  90. }
  91. if (stream_paused) {
  92. if (stream_paused_fade && stream_playback->is_playing()) {
  93. _mix_internal(true);
  94. stream_paused_fade = false;
  95. }
  96. return;
  97. }
  98. if (setstop.is_set()) {
  99. _mix_internal(true);
  100. stream_playback->stop();
  101. setstop.clear();
  102. }
  103. if (setseek.get() >= 0.0 && !stop_has_priority.is_set()) {
  104. if (stream_playback->is_playing()) {
  105. //fade out to avoid pops
  106. _mix_internal(true);
  107. }
  108. stream_playback->start(setseek.get());
  109. setseek.set(-1.0); //reset seek
  110. mix_volume_db = volume_db; //reset ramp
  111. }
  112. stop_has_priority.clear();
  113. _mix_internal(false);
  114. }
  115. void AudioStreamPlayer::_notification(int p_what) {
  116. if (p_what == NOTIFICATION_ENTER_TREE) {
  117. AudioServer::get_singleton()->add_callback(_mix_audios, this);
  118. if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
  119. play();
  120. }
  121. }
  122. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  123. if (!active.is_set() || (setseek.get() < 0 && !stream_playback->is_playing())) {
  124. active.clear();
  125. set_process_internal(false);
  126. emit_signal("finished");
  127. }
  128. }
  129. if (p_what == NOTIFICATION_EXIT_TREE) {
  130. AudioServer::get_singleton()->remove_callback(_mix_audios, this);
  131. }
  132. if (p_what == NOTIFICATION_PAUSED) {
  133. if (!can_process()) {
  134. // Node can't process so we start fading out to silence
  135. set_stream_paused(true);
  136. }
  137. }
  138. if (p_what == NOTIFICATION_UNPAUSED) {
  139. set_stream_paused(false);
  140. }
  141. }
  142. void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {
  143. // Instancing audio streams can cause large memory allocations, do it prior to AudioServer::lock.
  144. Ref<AudioStreamPlayback> pre_instanced_playback;
  145. if (p_stream.is_valid()) {
  146. pre_instanced_playback = p_stream->instance_playback();
  147. }
  148. AudioServer::get_singleton()->lock();
  149. if (active.is_set() && stream_playback.is_valid() && !stream_paused) {
  150. //changing streams out of the blue is not a great idea, but at least
  151. //lets try to somehow avoid a click
  152. AudioFrame *buffer = fadeout_buffer.ptrw();
  153. int buffer_size = fadeout_buffer.size();
  154. stream_playback->mix(buffer, pitch_scale, buffer_size);
  155. //multiply volume interpolating to avoid clicks if this changes
  156. float target_volume = -80.0;
  157. float vol = Math::db2linear(mix_volume_db);
  158. float vol_inc = (Math::db2linear(target_volume) - vol) / float(buffer_size);
  159. for (int i = 0; i < buffer_size; i++) {
  160. buffer[i] *= vol;
  161. vol += vol_inc;
  162. }
  163. use_fadeout = true;
  164. }
  165. mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
  166. if (stream_playback.is_valid()) {
  167. stream_playback.unref();
  168. stream.unref();
  169. active.clear();
  170. setseek.set(-1);
  171. setstop.clear();
  172. }
  173. if (p_stream.is_valid()) {
  174. stream = p_stream;
  175. stream_playback = pre_instanced_playback;
  176. }
  177. AudioServer::get_singleton()->unlock();
  178. if (p_stream.is_valid() && stream_playback.is_null()) {
  179. stream.unref();
  180. }
  181. }
  182. Ref<AudioStream> AudioStreamPlayer::get_stream() const {
  183. return stream;
  184. }
  185. void AudioStreamPlayer::set_volume_db(float p_volume) {
  186. volume_db = p_volume;
  187. }
  188. float AudioStreamPlayer::get_volume_db() const {
  189. return volume_db;
  190. }
  191. void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {
  192. ERR_FAIL_COND(p_pitch_scale <= 0.0);
  193. pitch_scale = p_pitch_scale;
  194. }
  195. float AudioStreamPlayer::get_pitch_scale() const {
  196. return pitch_scale;
  197. }
  198. void AudioStreamPlayer::play(float p_from_pos) {
  199. if (stream_playback.is_valid()) {
  200. //mix_volume_db = volume_db; do not reset volume ramp here, can cause clicks
  201. setseek.set(p_from_pos);
  202. stop_has_priority.clear();
  203. active.set();
  204. set_process_internal(true);
  205. }
  206. }
  207. void AudioStreamPlayer::seek(float p_seconds) {
  208. if (stream_playback.is_valid()) {
  209. setseek.set(p_seconds);
  210. }
  211. }
  212. void AudioStreamPlayer::stop() {
  213. if (stream_playback.is_valid() && active.is_set()) {
  214. setstop.set();
  215. stop_has_priority.set();
  216. }
  217. }
  218. bool AudioStreamPlayer::is_playing() const {
  219. if (stream_playback.is_valid()) {
  220. return active.is_set() && !setstop.is_set(); //&& stream_playback->is_playing();
  221. }
  222. return false;
  223. }
  224. float AudioStreamPlayer::get_playback_position() {
  225. if (stream_playback.is_valid()) {
  226. float ss = setseek.get();
  227. if (ss >= 0.0) {
  228. return ss;
  229. }
  230. return stream_playback->get_playback_position();
  231. }
  232. return 0;
  233. }
  234. void AudioStreamPlayer::set_bus(const StringName &p_bus) {
  235. //if audio is active, must lock this
  236. AudioServer::get_singleton()->lock();
  237. bus = p_bus;
  238. AudioServer::get_singleton()->unlock();
  239. }
  240. StringName AudioStreamPlayer::get_bus() const {
  241. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  242. if (AudioServer::get_singleton()->get_bus_name(i) == bus) {
  243. return bus;
  244. }
  245. }
  246. return "Master";
  247. }
  248. void AudioStreamPlayer::set_autoplay(bool p_enable) {
  249. autoplay = p_enable;
  250. }
  251. bool AudioStreamPlayer::is_autoplay_enabled() {
  252. return autoplay;
  253. }
  254. void AudioStreamPlayer::set_mix_target(MixTarget p_target) {
  255. mix_target = p_target;
  256. }
  257. AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const {
  258. return mix_target;
  259. }
  260. void AudioStreamPlayer::_set_playing(bool p_enable) {
  261. if (p_enable) {
  262. play();
  263. } else {
  264. stop();
  265. }
  266. }
  267. bool AudioStreamPlayer::_is_active() const {
  268. return active.is_set();
  269. }
  270. void AudioStreamPlayer::set_stream_paused(bool p_pause) {
  271. if (p_pause != stream_paused) {
  272. stream_paused = p_pause;
  273. stream_paused_fade = p_pause;
  274. }
  275. }
  276. bool AudioStreamPlayer::get_stream_paused() const {
  277. return stream_paused;
  278. }
  279. void AudioStreamPlayer::_validate_property(PropertyInfo &property) const {
  280. if (property.name == "bus") {
  281. String options;
  282. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  283. if (i > 0) {
  284. options += ",";
  285. }
  286. String name = AudioServer::get_singleton()->get_bus_name(i);
  287. options += name;
  288. }
  289. property.hint_string = options;
  290. }
  291. }
  292. void AudioStreamPlayer::_bus_layout_changed() {
  293. _change_notify();
  294. }
  295. Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {
  296. return stream_playback;
  297. }
  298. void AudioStreamPlayer::_bind_methods() {
  299. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);
  300. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);
  301. ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);
  302. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);
  303. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);
  304. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);
  305. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));
  306. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);
  307. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);
  308. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);
  309. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);
  310. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);
  311. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);
  312. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);
  313. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);
  314. ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);
  315. ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);
  316. ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer::_set_playing);
  317. ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer::_is_active);
  318. ClassDB::bind_method(D_METHOD("_bus_layout_changed"), &AudioStreamPlayer::_bus_layout_changed);
  319. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);
  320. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);
  321. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);
  322. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  323. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24"), "set_volume_db", "get_volume_db");
  324. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
  325. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
  326. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  327. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  328. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
  329. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  330. ADD_SIGNAL(MethodInfo("finished"));
  331. BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);
  332. BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);
  333. BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);
  334. }
  335. AudioStreamPlayer::AudioStreamPlayer() {
  336. mix_volume_db = 0;
  337. pitch_scale = 1.0;
  338. volume_db = 0;
  339. autoplay = false;
  340. setseek.set(-1);
  341. stream_paused = false;
  342. stream_paused_fade = false;
  343. mix_target = MIX_TARGET_STEREO;
  344. fadeout_buffer.resize(512);
  345. use_fadeout = false;
  346. AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");
  347. }
  348. AudioStreamPlayer::~AudioStreamPlayer() {
  349. }