audio_stream_player.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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(!can_process());
  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 SceneStringNames::get_singleton()->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::_on_bus_layout_changed() {
  207. notify_property_list_changed();
  208. }
  209. void AudioStreamPlayer::_on_bus_renamed(int p_bus_index, const StringName &p_old_name, const StringName &p_new_name) {
  210. notify_property_list_changed();
  211. }
  212. void AudioStreamPlayer::set_stream_paused(bool p_pause) {
  213. // TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
  214. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  215. AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
  216. }
  217. }
  218. bool AudioStreamPlayer::get_stream_paused() const {
  219. // There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
  220. if (!stream_playbacks.is_empty()) {
  221. return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
  222. }
  223. return false;
  224. }
  225. Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() {
  226. Vector<AudioFrame> volume_vector;
  227. // We need at most four stereo pairs (for 7.1 systems).
  228. volume_vector.resize(4);
  229. // Initialize the volume vector to zero.
  230. for (AudioFrame &channel_volume_db : volume_vector) {
  231. channel_volume_db = AudioFrame(0, 0);
  232. }
  233. float volume_linear = Math::db_to_linear(volume_db);
  234. // Set the volume vector up according to the speaker mode and mix target.
  235. // TODO do we need to scale the volume down when we output to more channels?
  236. if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {
  237. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  238. } else {
  239. switch (mix_target) {
  240. case MIX_TARGET_STEREO: {
  241. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  242. } break;
  243. case MIX_TARGET_SURROUND: {
  244. // TODO Make sure this is right.
  245. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  246. volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
  247. volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);
  248. volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);
  249. } break;
  250. case MIX_TARGET_CENTER: {
  251. // TODO Make sure this is right.
  252. volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
  253. } break;
  254. }
  255. }
  256. return volume_vector;
  257. }
  258. void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const {
  259. if (p_property.name == "bus") {
  260. String options;
  261. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  262. if (i > 0) {
  263. options += ",";
  264. }
  265. String name = AudioServer::get_singleton()->get_bus_name(i);
  266. options += name;
  267. }
  268. p_property.hint_string = options;
  269. }
  270. }
  271. bool AudioStreamPlayer::has_stream_playback() {
  272. return !stream_playbacks.is_empty();
  273. }
  274. Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {
  275. ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");
  276. return stream_playbacks[stream_playbacks.size() - 1];
  277. }
  278. void AudioStreamPlayer::_bind_methods() {
  279. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);
  280. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);
  281. ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);
  282. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);
  283. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);
  284. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);
  285. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));
  286. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);
  287. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);
  288. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);
  289. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);
  290. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);
  291. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);
  292. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);
  293. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);
  294. ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);
  295. ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);
  296. ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer::_set_playing);
  297. ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer::_is_active);
  298. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);
  299. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);
  300. ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony);
  301. ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony);
  302. ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback);
  303. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);
  304. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  305. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
  306. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
  307. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
  308. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  309. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  310. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
  311. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
  312. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  313. ADD_SIGNAL(MethodInfo("finished"));
  314. BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);
  315. BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);
  316. BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);
  317. }
  318. AudioStreamPlayer::AudioStreamPlayer() {
  319. AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &AudioStreamPlayer::_on_bus_layout_changed));
  320. AudioServer::get_singleton()->connect("bus_renamed", callable_mp(this, &AudioStreamPlayer::_on_bus_renamed));
  321. }
  322. AudioStreamPlayer::~AudioStreamPlayer() {
  323. }