audio_stream_player.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "audio_stream_player.compat.inc"
  32. #include "scene/audio/audio_stream_player_internal.h"
  33. #include "servers/audio/audio_stream.h"
  34. void AudioStreamPlayer::_notification(int p_what) {
  35. internal->notification(p_what);
  36. }
  37. void AudioStreamPlayer::set_stream(Ref<AudioStream> p_stream) {
  38. internal->set_stream(p_stream);
  39. }
  40. bool AudioStreamPlayer::_set(const StringName &p_name, const Variant &p_value) {
  41. return internal->set(p_name, p_value);
  42. }
  43. bool AudioStreamPlayer::_get(const StringName &p_name, Variant &r_ret) const {
  44. return internal->get(p_name, r_ret);
  45. }
  46. void AudioStreamPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  47. internal->get_property_list(p_list);
  48. }
  49. Ref<AudioStream> AudioStreamPlayer::get_stream() const {
  50. return internal->stream;
  51. }
  52. void AudioStreamPlayer::set_volume_db(float p_volume) {
  53. ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN.");
  54. internal->volume_db = p_volume;
  55. Vector<AudioFrame> volume_vector = _get_volume_vector();
  56. for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  57. AudioServer::get_singleton()->set_playback_all_bus_volumes_linear(playback, volume_vector);
  58. }
  59. }
  60. float AudioStreamPlayer::get_volume_db() const {
  61. return internal->volume_db;
  62. }
  63. void AudioStreamPlayer::set_pitch_scale(float p_pitch_scale) {
  64. internal->set_pitch_scale(p_pitch_scale);
  65. }
  66. float AudioStreamPlayer::get_pitch_scale() const {
  67. return internal->pitch_scale;
  68. }
  69. void AudioStreamPlayer::set_max_polyphony(int p_max_polyphony) {
  70. internal->set_max_polyphony(p_max_polyphony);
  71. }
  72. int AudioStreamPlayer::get_max_polyphony() const {
  73. return internal->max_polyphony;
  74. }
  75. void AudioStreamPlayer::play(float p_from_pos) {
  76. Ref<AudioStreamPlayback> stream_playback = internal->play_basic();
  77. if (stream_playback.is_null()) {
  78. return;
  79. }
  80. AudioServer::get_singleton()->start_playback_stream(stream_playback, internal->bus, _get_volume_vector(), p_from_pos, internal->pitch_scale);
  81. internal->ensure_playback_limit();
  82. // Sample handling.
  83. if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_valid()) {
  84. Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback();
  85. sample_playback->offset = p_from_pos;
  86. sample_playback->volume_vector = _get_volume_vector();
  87. sample_playback->bus = get_bus();
  88. AudioServer::get_singleton()->start_sample_playback(sample_playback);
  89. }
  90. }
  91. void AudioStreamPlayer::seek(float p_seconds) {
  92. internal->seek(p_seconds);
  93. }
  94. void AudioStreamPlayer::stop() {
  95. internal->stop_basic();
  96. }
  97. bool AudioStreamPlayer::is_playing() const {
  98. return internal->is_playing();
  99. }
  100. float AudioStreamPlayer::get_playback_position() {
  101. return internal->get_playback_position();
  102. }
  103. void AudioStreamPlayer::set_bus(const StringName &p_bus) {
  104. internal->bus = p_bus;
  105. for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
  106. AudioServer::get_singleton()->set_playback_bus_exclusive(playback, p_bus, _get_volume_vector());
  107. }
  108. }
  109. StringName AudioStreamPlayer::get_bus() const {
  110. return internal->get_bus();
  111. }
  112. void AudioStreamPlayer::set_autoplay(bool p_enable) {
  113. internal->autoplay = p_enable;
  114. }
  115. bool AudioStreamPlayer::is_autoplay_enabled() const {
  116. return internal->autoplay;
  117. }
  118. void AudioStreamPlayer::set_mix_target(MixTarget p_target) {
  119. mix_target = p_target;
  120. }
  121. AudioStreamPlayer::MixTarget AudioStreamPlayer::get_mix_target() const {
  122. return mix_target;
  123. }
  124. void AudioStreamPlayer::_set_playing(bool p_enable) {
  125. internal->set_playing(p_enable);
  126. }
  127. void AudioStreamPlayer::set_stream_paused(bool p_pause) {
  128. internal->set_stream_paused(p_pause);
  129. }
  130. bool AudioStreamPlayer::get_stream_paused() const {
  131. return internal->get_stream_paused();
  132. }
  133. Vector<AudioFrame> AudioStreamPlayer::_get_volume_vector() {
  134. Vector<AudioFrame> volume_vector;
  135. // We need at most four stereo pairs (for 7.1 systems).
  136. volume_vector.resize(4);
  137. // Initialize the volume vector to zero.
  138. for (AudioFrame &channel_volume_db : volume_vector) {
  139. channel_volume_db = AudioFrame(0, 0);
  140. }
  141. float volume_linear = Math::db_to_linear(internal->volume_db);
  142. // Set the volume vector up according to the speaker mode and mix target.
  143. // TODO do we need to scale the volume down when we output to more channels?
  144. if (AudioServer::get_singleton()->get_speaker_mode() == AudioServer::SPEAKER_MODE_STEREO) {
  145. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  146. } else {
  147. switch (mix_target) {
  148. case MIX_TARGET_STEREO: {
  149. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  150. } break;
  151. case MIX_TARGET_SURROUND: {
  152. // TODO Make sure this is right.
  153. volume_vector.write[0] = AudioFrame(volume_linear, volume_linear);
  154. volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
  155. volume_vector.write[2] = AudioFrame(volume_linear, volume_linear);
  156. volume_vector.write[3] = AudioFrame(volume_linear, volume_linear);
  157. } break;
  158. case MIX_TARGET_CENTER: {
  159. // TODO Make sure this is right.
  160. volume_vector.write[1] = AudioFrame(volume_linear, /* LFE= */ 1.0f);
  161. } break;
  162. }
  163. }
  164. return volume_vector;
  165. }
  166. void AudioStreamPlayer::_validate_property(PropertyInfo &p_property) const {
  167. internal->validate_property(p_property);
  168. }
  169. bool AudioStreamPlayer::has_stream_playback() {
  170. return internal->has_stream_playback();
  171. }
  172. Ref<AudioStreamPlayback> AudioStreamPlayer::get_stream_playback() {
  173. return internal->get_stream_playback();
  174. }
  175. AudioServer::PlaybackType AudioStreamPlayer::get_playback_type() const {
  176. return internal->get_playback_type();
  177. }
  178. void AudioStreamPlayer::set_playback_type(AudioServer::PlaybackType p_playback_type) {
  179. internal->set_playback_type(p_playback_type);
  180. }
  181. void AudioStreamPlayer::_bind_methods() {
  182. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer::set_stream);
  183. ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer::get_stream);
  184. ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer::set_volume_db);
  185. ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer::get_volume_db);
  186. ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer::set_pitch_scale);
  187. ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer::get_pitch_scale);
  188. ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer::play, DEFVAL(0.0));
  189. ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer::seek);
  190. ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer::stop);
  191. ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer::is_playing);
  192. ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer::get_playback_position);
  193. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer::set_bus);
  194. ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer::get_bus);
  195. ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer::set_autoplay);
  196. ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer::is_autoplay_enabled);
  197. ClassDB::bind_method(D_METHOD("set_mix_target", "mix_target"), &AudioStreamPlayer::set_mix_target);
  198. ClassDB::bind_method(D_METHOD("get_mix_target"), &AudioStreamPlayer::get_mix_target);
  199. ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer::_set_playing);
  200. ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer::set_stream_paused);
  201. ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer::get_stream_paused);
  202. ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer::set_max_polyphony);
  203. ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer::get_max_polyphony);
  204. ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer::has_stream_playback);
  205. ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer::get_stream_playback);
  206. ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer::set_playback_type);
  207. ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer::get_playback_type);
  208. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
  209. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
  210. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
  211. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing");
  212. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
  213. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
  214. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_target", PROPERTY_HINT_ENUM, "Stereo,Surround,Center"), "set_mix_target", "get_mix_target");
  215. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
  216. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  217. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type");
  218. ADD_SIGNAL(MethodInfo("finished"));
  219. BIND_ENUM_CONSTANT(MIX_TARGET_STEREO);
  220. BIND_ENUM_CONSTANT(MIX_TARGET_SURROUND);
  221. BIND_ENUM_CONSTANT(MIX_TARGET_CENTER);
  222. }
  223. AudioStreamPlayer::AudioStreamPlayer() {
  224. internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), callable_mp(this, &AudioStreamPlayer::stop), false));
  225. }
  226. AudioStreamPlayer::~AudioStreamPlayer() {
  227. memdelete(internal);
  228. }