audio_stream_player_internal.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**************************************************************************/
  2. /* audio_stream_player_internal.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_internal.h"
  31. #include "scene/main/node.h"
  32. #include "servers/audio/audio_stream.h"
  33. void AudioStreamPlayerInternal::_set_process(bool p_enabled) {
  34. if (physical) {
  35. node->set_physics_process_internal(p_enabled);
  36. } else {
  37. node->set_process_internal(p_enabled);
  38. }
  39. }
  40. void AudioStreamPlayerInternal::_update_stream_parameters() {
  41. if (stream.is_null()) {
  42. return;
  43. }
  44. List<AudioStream::Parameter> parameters;
  45. stream->get_parameter_list(&parameters);
  46. for (const AudioStream::Parameter &K : parameters) {
  47. const PropertyInfo &pi = K.property;
  48. StringName key = PARAM_PREFIX + pi.name;
  49. if (!playback_parameters.has(key)) {
  50. ParameterData pd;
  51. pd.path = pi.name;
  52. pd.value = K.default_value;
  53. playback_parameters.insert(key, pd);
  54. }
  55. }
  56. }
  57. void AudioStreamPlayerInternal::process() {
  58. Vector<Ref<AudioStreamPlayback>> playbacks_to_remove;
  59. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  60. if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) {
  61. playbacks_to_remove.push_back(playback);
  62. }
  63. }
  64. // Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.
  65. for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) {
  66. stream_playbacks.erase(playback);
  67. }
  68. if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) {
  69. // This node is no longer actively playing audio.
  70. active.clear();
  71. _set_process(false);
  72. }
  73. if (!playbacks_to_remove.is_empty()) {
  74. node->emit_signal(SceneStringName(finished));
  75. }
  76. }
  77. void AudioStreamPlayerInternal::ensure_playback_limit() {
  78. while (stream_playbacks.size() > max_polyphony) {
  79. AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]);
  80. stream_playbacks.remove_at(0);
  81. }
  82. }
  83. void AudioStreamPlayerInternal::notification(int p_what) {
  84. switch (p_what) {
  85. case Node::NOTIFICATION_ENTER_TREE: {
  86. if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
  87. play_callable.call(0.0);
  88. }
  89. set_stream_paused(!node->can_process());
  90. } break;
  91. case Node::NOTIFICATION_EXIT_TREE: {
  92. set_stream_paused(true);
  93. } break;
  94. case Node::NOTIFICATION_INTERNAL_PROCESS: {
  95. process();
  96. } break;
  97. case Node::NOTIFICATION_PREDELETE: {
  98. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  99. AudioServer::get_singleton()->stop_playback_stream(playback);
  100. }
  101. stream_playbacks.clear();
  102. } break;
  103. case Node::NOTIFICATION_SUSPENDED:
  104. case Node::NOTIFICATION_PAUSED: {
  105. if (!node->can_process()) {
  106. // Node can't process so we start fading out to silence
  107. set_stream_paused(true);
  108. }
  109. } break;
  110. case Node::NOTIFICATION_UNSUSPENDED: {
  111. if (node->get_tree()->is_paused()) {
  112. break;
  113. }
  114. [[fallthrough]];
  115. }
  116. case Node::NOTIFICATION_UNPAUSED: {
  117. set_stream_paused(false);
  118. } break;
  119. }
  120. }
  121. Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {
  122. Ref<AudioStreamPlayback> stream_playback;
  123. if (stream.is_null()) {
  124. return stream_playback;
  125. }
  126. ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree");
  127. if (stream->is_monophonic() && is_playing()) {
  128. stop_callable.call();
  129. }
  130. stream_playback = stream->instantiate_playback();
  131. ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback.");
  132. for (const KeyValue<StringName, ParameterData> &K : playback_parameters) {
  133. stream_playback->set_parameter(K.value.path, K.value.value);
  134. }
  135. // Sample handling.
  136. if (_is_sample()) {
  137. if (stream->can_be_sampled()) {
  138. stream_playback->set_is_sample(true);
  139. if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_null()) {
  140. if (!AudioServer::get_singleton()->is_stream_registered_as_sample(stream)) {
  141. AudioServer::get_singleton()->register_stream_as_sample(stream);
  142. }
  143. Ref<AudioSamplePlayback> sample_playback;
  144. sample_playback.instantiate();
  145. sample_playback->stream = stream;
  146. sample_playback->pitch_scale = pitch_scale;
  147. stream_playback->set_sample_playback(sample_playback);
  148. }
  149. } else if (!stream->is_meta_stream()) {
  150. WARN_PRINT(vformat(R"(%s is trying to play a sample from a stream that cannot be sampled.)", node->get_path()));
  151. }
  152. }
  153. stream_playbacks.push_back(stream_playback);
  154. active.set();
  155. _set_process(true);
  156. return stream_playback;
  157. }
  158. void AudioStreamPlayerInternal::set_stream_paused(bool p_pause) {
  159. // TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
  160. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  161. AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
  162. if (_is_sample() && playback->get_sample_playback().is_valid()) {
  163. AudioServer::get_singleton()->set_sample_playback_pause(playback->get_sample_playback(), p_pause);
  164. }
  165. }
  166. }
  167. bool AudioStreamPlayerInternal::get_stream_paused() const {
  168. // There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
  169. if (!stream_playbacks.is_empty()) {
  170. return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
  171. }
  172. return false;
  173. }
  174. void AudioStreamPlayerInternal::validate_property(PropertyInfo &p_property) const {
  175. if (p_property.name == "bus") {
  176. String options;
  177. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  178. if (i > 0) {
  179. options += ",";
  180. }
  181. String name = AudioServer::get_singleton()->get_bus_name(i);
  182. options += name;
  183. }
  184. p_property.hint_string = options;
  185. }
  186. }
  187. bool AudioStreamPlayerInternal::set(const StringName &p_name, const Variant &p_value) {
  188. ParameterData *pd = playback_parameters.getptr(p_name);
  189. if (!pd) {
  190. return false;
  191. }
  192. pd->value = p_value;
  193. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  194. playback->set_parameter(pd->path, pd->value);
  195. }
  196. return true;
  197. }
  198. bool AudioStreamPlayerInternal::get(const StringName &p_name, Variant &r_ret) const {
  199. const ParameterData *pd = playback_parameters.getptr(p_name);
  200. if (!pd) {
  201. return false;
  202. }
  203. r_ret = pd->value;
  204. return true;
  205. }
  206. void AudioStreamPlayerInternal::get_property_list(List<PropertyInfo> *p_list) const {
  207. if (stream.is_null()) {
  208. return;
  209. }
  210. List<AudioStream::Parameter> parameters;
  211. stream->get_parameter_list(&parameters);
  212. for (const AudioStream::Parameter &K : parameters) {
  213. PropertyInfo pi = K.property;
  214. pi.name = PARAM_PREFIX + pi.name;
  215. const ParameterData *pd = playback_parameters.getptr(pi.name);
  216. if (pd && pd->value == K.default_value) {
  217. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  218. }
  219. p_list->push_back(pi);
  220. }
  221. }
  222. void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {
  223. if (stream.is_valid()) {
  224. stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
  225. }
  226. stop_callable.call();
  227. stream = p_stream;
  228. _update_stream_parameters();
  229. if (stream.is_valid()) {
  230. stream->connect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
  231. }
  232. node->notify_property_list_changed();
  233. }
  234. void AudioStreamPlayerInternal::seek(float p_seconds) {
  235. if (is_playing()) {
  236. stop_callable.call();
  237. play_callable.call(p_seconds);
  238. }
  239. }
  240. void AudioStreamPlayerInternal::stop_basic() {
  241. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  242. AudioServer::get_singleton()->stop_playback_stream(playback);
  243. }
  244. stream_playbacks.clear();
  245. active.clear();
  246. _set_process(false);
  247. }
  248. bool AudioStreamPlayerInternal::is_playing() const {
  249. for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  250. if (AudioServer::get_singleton()->is_playback_active(playback)) {
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. float AudioStreamPlayerInternal::get_playback_position() {
  257. // Return the playback position of the most recently started playback stream.
  258. if (!stream_playbacks.is_empty()) {
  259. return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
  260. }
  261. return 0;
  262. }
  263. void AudioStreamPlayerInternal::set_playing(bool p_enable) {
  264. if (p_enable) {
  265. play_callable.call(0.0);
  266. } else {
  267. stop_callable.call();
  268. }
  269. }
  270. bool AudioStreamPlayerInternal::is_active() const {
  271. return active.is_set();
  272. }
  273. void AudioStreamPlayerInternal::set_pitch_scale(float p_pitch_scale) {
  274. ERR_FAIL_COND(p_pitch_scale <= 0.0);
  275. pitch_scale = p_pitch_scale;
  276. for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
  277. AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);
  278. }
  279. }
  280. void AudioStreamPlayerInternal::set_max_polyphony(int p_max_polyphony) {
  281. if (p_max_polyphony > 0) {
  282. max_polyphony = p_max_polyphony;
  283. }
  284. }
  285. bool AudioStreamPlayerInternal::has_stream_playback() {
  286. return !stream_playbacks.is_empty();
  287. }
  288. Ref<AudioStreamPlayback> AudioStreamPlayerInternal::get_stream_playback() {
  289. ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback().");
  290. return stream_playbacks[stream_playbacks.size() - 1];
  291. }
  292. void AudioStreamPlayerInternal::set_playback_type(AudioServer::PlaybackType p_playback_type) {
  293. playback_type = p_playback_type;
  294. }
  295. AudioServer::PlaybackType AudioStreamPlayerInternal::get_playback_type() const {
  296. return playback_type;
  297. }
  298. StringName AudioStreamPlayerInternal::get_bus() const {
  299. const String bus_name = bus;
  300. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  301. if (AudioServer::get_singleton()->get_bus_name(i) == bus_name) {
  302. return bus;
  303. }
  304. }
  305. return SceneStringName(Master);
  306. }
  307. AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) {
  308. node = p_node;
  309. play_callable = p_play_callable;
  310. stop_callable = p_stop_callable;
  311. physical = p_physical;
  312. bus = SceneStringName(Master);
  313. AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp((Object *)node, &Object::notify_property_list_changed));
  314. AudioServer::get_singleton()->connect("bus_renamed", callable_mp((Object *)node, &Object::notify_property_list_changed).unbind(3));
  315. }