audio_stream_player.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**************************************************************************/
  2. /* audio_stream_player.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/engine.h"
  32. #include "core/math/audio_frame.h"
  33. #include "servers/audio_server.h"
  34. void AudioStreamPlayer::_notification(int p_what) {
  35. switch (p_what) {
  36. case NOTIFICATION_ENTER_TREE: {
  37. if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
  38. play();
  39. }
  40. set_stream_paused(false);
  41. } break;
  42. case NOTIFICATION_INTERNAL_PROCESS: {
  43. Vector<Ref<AudioStreamPlayback>> playbacks_to_remove;
  44. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  45. if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) {
  46. playbacks_to_remove.push_back(playback);
  47. }
  48. }
  49. // Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.
  50. for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) {
  51. stream_playbacks.erase(playback);
  52. }
  53. if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) {
  54. // This node is no longer actively playing audio.
  55. active.clear();
  56. set_process_internal(false);
  57. }
  58. if (!playbacks_to_remove.is_empty()) {
  59. emit_signal(SNAME("finished"));
  60. }
  61. } break;
  62. case NOTIFICATION_EXIT_TREE: {
  63. set_stream_paused(true);
  64. } break;
  65. case NOTIFICATION_PREDELETE: {
  66. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  67. AudioServer::get_singleton()->stop_playback_stream(playback);
  68. }
  69. stream_playbacks.clear();
  70. } break;
  71. case NOTIFICATION_PAUSED: {
  72. if (!can_process()) {
  73. // Node can't process so we start fading out to silence
  74. set_stream_paused(true);
  75. }
  76. } break;
  77. case NOTIFICATION_UNPAUSED: {
  78. set_stream_paused(false);
  79. } break;
  80. }
  81. }
  82. void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {
  83. stop();
  84. stream = p_stream;
  85. }
  86. Ref<AudioStream> AudioStreamPlayer::get_stream() const {
  87. return stream;
  88. }
  89. void AudioStreamPlayer::set_volume_db(float p_volume) {
  90. volume_db = p_volume;
  91. Vector<AudioFrame> volume_vector = _get_volume_vector();
  92. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  93. AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector);
  94. }
  95. }
  96. float AudioStreamPlayer::get_volume_db() const {
  97. return volume_db;
  98. }
  99. void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {
  100. ERR_FAIL_COND(!(p_pitch_scale > 0.0));
  101. pitch_scale = p_pitch_scale;
  102. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  103. AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);
  104. }
  105. }
  106. float AudioStreamPlayer::get_pitch_scale() const {
  107. return pitch_scale;
  108. }
  109. void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) {
  110. if (p_max_polyphony > 0) {
  111. max_polyphony = p_max_polyphony;
  112. }
  113. }
  114. int AudioStreamPlayer::get_max_polyphony() const {
  115. return max_polyphony;
  116. }
  117. void AudioStreamPlayer::play(float p_from_pos) {
  118. if (stream.is_null()) {
  119. return;
  120. }
  121. ERR_FAIL_COND_MSG(!is_inside_tree(), "Playback can only happen when a node is inside the scene tree");
  122. if (stream->is_monophonic() && is_playing()) {
  123. stop();
  124. }
  125. Ref<AudioStreamPlayback> stream_playback = stream->instantiate_playback();
  126. ERR_FAIL_COND_MSG(stream_playback.is_null(), "Failed to instantiate playback.");
  127. AudioServer::get_singleton()->start_playback_stream(stream_playback, bus, _get_volume_vector(), p_from_pos, pitch_scale);
  128. stream_playbacks.push_back(stream_playback);
  129. active.set();
  130. set_process_internal(true);
  131. while (stream_playbacks.size() > max_polyphony) {
  132. AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]);
  133. stream_playbacks.remove_at(0);
  134. }
  135. }
  136. void AudioStreamPlayer::seek(float p_seconds) {
  137. if (is_playing()) {
  138. stop();
  139. play(p_seconds);
  140. }
  141. }
  142. void AudioStreamPlayer::stop() {
  143. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  144. AudioServer::get_singleton()->stop_playback_stream(playback);
  145. }
  146. stream_playbacks.clear();
  147. active.clear();
  148. set_process_internal(false);
  149. }
  150. bool AudioStreamPlayer::is_playing() const {
  151. for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  152. if (AudioServer::get_singleton()->is_playback_active(playback)) {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. float AudioStreamPlayer::get_playback_position() {
  159. // Return the playback position of the most recently started playback stream.
  160. if (!stream_playbacks.is_empty()) {
  161. return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
  162. }
  163. return 0;
  164. }
  165. void AudioStreamPlayer::set_bus(const StringName &p_bus) {
  166. bus = p_bus;
  167. for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  168. AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector());
  169. }
  170. }
  171. StringName AudioStreamPlayer::get_bus() const {
  172. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  173. if (AudioServer::get_singleton()->get_bus_name(i) == String(bus)) {
  174. return bus;
  175. }
  176. }
  177. return SNAME("Master");
  178. }
  179. void AudioStreamPlayer::set_autoplay(bool p_enable) {
  180. autoplay = p_enable;
  181. }
  182. bool AudioStreamPlayer::is_autoplay_enabled() {
  183. return autoplay;
  184. }
  185. void AudioStreamPlayer::set_mix_target(MixTarget p_target) {
  186. mix_target = p_target;
  187. }
  188. AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const {
  189. return mix_target;
  190. }
  191. void AudioStreamPlayer::_set_playing(bool p_enable) {
  192. if (p_enable) {
  193. play();
  194. } else {
  195. stop();
  196. }
  197. }
  198. bool AudioStreamPlayer::_is_active() const {
  199. for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  200. if (AudioServer::get_singleton()->is_playback_active(playback)) {
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. void AudioStreamPlayer::set_stream_paused(bool p_pause) {
  207. // TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
  208. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  209. AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
  210. }
  211. }
  212. bool AudioStreamPlayer::get_stream_paused() const {
  213. // There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
  214. if (!stream_playbacks.is_empty()) {
  215. return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
  216. }
  217. return false;
  218. }
  219. Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() {
  220. Vector<AudioFrame> volume_vector;
  221. // We need at most four stereo pairs (for 7.1 systems).
  222. volume_vector.resize(4);
  223. // Initialize the volume vector to zero.
  224. for (AudioFrame &channel_volume_db : volume_vector) {
  225. channel_volume_db = AudioFrame(0, 0);
  226. }
  227. float volume_linear = Math::db_to_linear(volume_db);
  228. // Set the volume vector up according to the speaker mode and mix target.
  229. // TODO do we need to scale the volume down when we output to more channels?
  230. if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {
  231. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  232. } else {
  233. switch (mix_target) {
  234. case MIX_TARGET_STEREO: {
  235. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  236. } break;
  237. case MIX_TARGET_SURROUND: {
  238. // TODO Make sure this is right.
  239. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  240. volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
  241. volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);
  242. volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);
  243. } break;
  244. case MIX_TARGET_CENTER: {
  245. // TODO Make sure this is right.
  246. volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
  247. } break;
  248. }
  249. }
  250. return volume_vector;
  251. }
  252. void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const {
  253. if (p_property.name == "bus") {
  254. String options;
  255. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  256. if (i > 0) {
  257. options += ",";
  258. }
  259. String name = AudioServer::get_singleton()->get_bus_name(i);
  260. options += name;
  261. }
  262. p_property.hint_string = options;
  263. }
  264. }
  265. void AudioStreamPlayer::_bus_layout_changed() {
  266. notify_property_list_changed();
  267. }
  268. bool AudioStreamPlayer::has_stream_playback() {
  269. return !stream_playbacks.is_empty();
  270. }
  271. Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {
  272. ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");
  273. return stream_playbacks[stream_playbacks.size() - 1];
  274. }
  275. void AudioStreamPlayer::_bind_methods() {
  276. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);
  277. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);
  278. ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);
  279. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);
  280. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);
  281. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);
  282. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));
  283. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);
  284. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);
  285. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);
  286. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);
  287. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);
  288. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);
  289. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);
  290. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);
  291. ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);
  292. ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);
  293. ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer::_set_playing);
  294. ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer::_is_active);
  295. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);
  296. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);
  297. ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony);
  298. ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony);
  299. ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback);
  300. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);
  301. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  302. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
  303. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
  304. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
  305. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  306. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  307. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
  308. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
  309. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  310. ADD_SIGNAL(MethodInfo("finished"));
  311. BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);
  312. BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);
  313. BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);
  314. }
  315. AudioStreamPlayer::AudioStreamPlayer() {
  316. AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &AudioStreamPlayer::_bus_layout_changed));
  317. }
  318. AudioStreamPlayer::~AudioStreamPlayer() {
  319. }