audio_stream_polyphonic.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**************************************************************************/
  2. /* audio_stream_polyphonic.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_polyphonic.h"
  31. #include "audio_stream_polyphonic.compat.inc"
  32. #include "scene/main/scene_tree.h"
  33. #include "servers/audio_server.h"
  34. constexpr uint64_t ID_MASK = 0xFFFFFFFF;
  35. constexpr uint64_t INDEX_SHIFT = 32;
  36. Ref<AudioStreamPlayback> AudioStreamPolyphonic::instantiate_playback() {
  37. Ref<AudioStreamPlaybackPolyphonic> playback;
  38. playback.instantiate();
  39. playback->streams.resize(polyphony);
  40. return playback;
  41. }
  42. String AudioStreamPolyphonic::get_stream_name() const {
  43. return "AudioStreamPolyphonic";
  44. }
  45. bool AudioStreamPolyphonic::is_monophonic() const {
  46. return true; // This avoids stream players to instantiate more than one of these.
  47. }
  48. void AudioStreamPolyphonic::set_polyphony(int p_voices) {
  49. ERR_FAIL_COND(p_voices < 0 || p_voices > 128);
  50. polyphony = p_voices;
  51. }
  52. int AudioStreamPolyphonic::get_polyphony() const {
  53. return polyphony;
  54. }
  55. void AudioStreamPolyphonic::_bind_methods() {
  56. ClassDB::bind_method(D_METHOD("set_polyphony", "voices"), &AudioStreamPolyphonic::set_polyphony);
  57. ClassDB::bind_method(D_METHOD("get_polyphony"), &AudioStreamPolyphonic::get_polyphony);
  58. ADD_PROPERTY(PropertyInfo(Variant::INT, "polyphony", PROPERTY_HINT_RANGE, "1,128,1"), "set_polyphony", "get_polyphony");
  59. }
  60. AudioStreamPolyphonic::AudioStreamPolyphonic() {
  61. }
  62. ////////////////////////
  63. void AudioStreamPlaybackPolyphonic::start(double p_from_pos) {
  64. if (active) {
  65. stop();
  66. }
  67. active = true;
  68. }
  69. void AudioStreamPlaybackPolyphonic::stop() {
  70. if (!active) {
  71. return;
  72. }
  73. bool locked = false;
  74. for (Stream &s : streams) {
  75. if (s.active.is_set()) {
  76. // Need locking because something may still be mixing.
  77. locked = true;
  78. AudioServer::get_singleton()->lock();
  79. }
  80. s.active.clear();
  81. s.finish_request.clear();
  82. s.stream_playback.unref();
  83. s.stream.unref();
  84. }
  85. if (locked) {
  86. AudioServer::get_singleton()->unlock();
  87. }
  88. active = false;
  89. }
  90. bool AudioStreamPlaybackPolyphonic::is_playing() const {
  91. return active;
  92. }
  93. int AudioStreamPlaybackPolyphonic::get_loop_count() const {
  94. return 0;
  95. }
  96. double AudioStreamPlaybackPolyphonic::get_playback_position() const {
  97. return 0;
  98. }
  99. void AudioStreamPlaybackPolyphonic::seek(double p_time) {
  100. // Ignored.
  101. }
  102. void AudioStreamPlaybackPolyphonic::tag_used_streams() {
  103. for (Stream &s : streams) {
  104. if (s.active.is_set()) {
  105. s.stream_playback->tag_used_streams();
  106. }
  107. }
  108. }
  109. int AudioStreamPlaybackPolyphonic::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  110. if (!active) {
  111. return 0;
  112. }
  113. // Pre-clear buffer.
  114. for (int i = 0; i < p_frames; i++) {
  115. p_buffer[i] = AudioFrame(0, 0);
  116. }
  117. for (Stream &s : streams) {
  118. if (!s.active.is_set()) {
  119. continue;
  120. }
  121. if (s.stream_playback->get_is_sample()) {
  122. if (s.finish_request.is_set()) {
  123. s.active.clear();
  124. AudioServer::get_singleton()->stop_sample_playback(s.stream_playback->get_sample_playback());
  125. }
  126. continue;
  127. }
  128. float volume_db = s.volume_db; // Copy because it can be overridden at any time.
  129. float next_volume = Math::db_to_linear(volume_db);
  130. s.prev_volume_db = volume_db;
  131. if (s.finish_request.is_set()) {
  132. if (s.pending_play.is_set()) {
  133. // Did not get the chance to play, was finalized too soon.
  134. s.active.clear();
  135. continue;
  136. }
  137. next_volume = 0;
  138. }
  139. if (s.pending_play.is_set()) {
  140. s.stream_playback->start(s.play_offset);
  141. s.pending_play.clear();
  142. }
  143. float prev_volume = Math::db_to_linear(s.prev_volume_db);
  144. float volume_inc = (next_volume - prev_volume) / float(p_frames);
  145. int todo = p_frames;
  146. int offset = 0;
  147. float volume = prev_volume;
  148. bool stream_done = false;
  149. while (todo) {
  150. int to_mix = MIN(todo, int(INTERNAL_BUFFER_LEN));
  151. int mixed = s.stream_playback->mix(internal_buffer, s.pitch_scale, to_mix);
  152. for (int i = 0; i < to_mix; i++) {
  153. p_buffer[offset + i] += internal_buffer[i] * volume;
  154. volume += volume_inc;
  155. }
  156. if (mixed < to_mix) {
  157. // Stream is done.
  158. s.active.clear();
  159. stream_done = true;
  160. break;
  161. }
  162. todo -= to_mix;
  163. offset += to_mix;
  164. }
  165. if (stream_done) {
  166. continue;
  167. }
  168. if (s.finish_request.is_set()) {
  169. s.active.clear();
  170. }
  171. }
  172. return p_frames;
  173. }
  174. AudioStreamPlaybackPolyphonic::ID AudioStreamPlaybackPolyphonic::play_stream(const Ref<AudioStream> &p_stream, float p_from_offset, float p_volume_db, float p_pitch_scale, AudioServer::PlaybackType p_playback_type, const StringName &p_bus) {
  175. ERR_FAIL_COND_V(p_stream.is_null(), INVALID_ID);
  176. AudioServer::PlaybackType playback_type = p_playback_type == AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT
  177. ? AudioServer::get_singleton()->get_default_playback_type()
  178. : p_playback_type;
  179. for (uint32_t i = 0; i < streams.size(); i++) {
  180. if (!streams[i].active.is_set()) {
  181. // Can use this stream, as it's not active.
  182. streams[i].stream = p_stream;
  183. streams[i].stream_playback = streams[i].stream->instantiate_playback();
  184. streams[i].play_offset = p_from_offset;
  185. streams[i].volume_db = p_volume_db;
  186. streams[i].prev_volume_db = p_volume_db;
  187. streams[i].pitch_scale = p_pitch_scale;
  188. streams[i].id = id_counter++;
  189. streams[i].finish_request.clear();
  190. streams[i].pending_play.set();
  191. streams[i].active.set();
  192. // Sample playback.
  193. if (playback_type == AudioServer::PlaybackType::PLAYBACK_TYPE_SAMPLE && p_stream->can_be_sampled()) {
  194. streams[i].stream_playback->set_is_sample(true);
  195. if (!AudioServer::get_singleton()->is_stream_registered_as_sample(p_stream)) {
  196. AudioServer::get_singleton()->register_stream_as_sample(p_stream);
  197. }
  198. float linear_volume = Math::db_to_linear(p_volume_db);
  199. Ref<AudioSamplePlayback> sp;
  200. sp.instantiate();
  201. sp->stream = streams[i].stream;
  202. sp->offset = p_from_offset;
  203. sp->volume_vector.resize(4);
  204. sp->volume_vector.write[0] = AudioFrame(linear_volume, linear_volume);
  205. sp->volume_vector.write[1] = AudioFrame(linear_volume, /* LFE= */ 1.0f);
  206. sp->volume_vector.write[2] = AudioFrame(linear_volume, linear_volume);
  207. sp->volume_vector.write[3] = AudioFrame(linear_volume, linear_volume);
  208. sp->bus = p_bus;
  209. if (streams[i].stream_playback->get_sample_playback().is_valid()) {
  210. AudioServer::get_singleton()->stop_playback_stream(sp);
  211. }
  212. streams[i].stream_playback->set_sample_playback(sp);
  213. AudioServer::get_singleton()->start_sample_playback(sp);
  214. }
  215. return (ID(i) << INDEX_SHIFT) | ID(streams[i].id);
  216. }
  217. }
  218. return INVALID_ID;
  219. }
  220. AudioStreamPlaybackPolyphonic::Stream *AudioStreamPlaybackPolyphonic::_find_stream(int64_t p_id) {
  221. uint32_t index = static_cast<uint64_t>(p_id) >> INDEX_SHIFT;
  222. if (index >= streams.size()) {
  223. return nullptr;
  224. }
  225. if (!streams[index].active.is_set()) {
  226. return nullptr; // Not active, no longer exists.
  227. }
  228. int64_t id = static_cast<uint64_t>(p_id) & ID_MASK;
  229. if (streams[index].id != id) {
  230. return nullptr;
  231. }
  232. return &streams[index];
  233. }
  234. void AudioStreamPlaybackPolyphonic::set_stream_volume(ID p_stream_id, float p_volume_db) {
  235. Stream *s = _find_stream(p_stream_id);
  236. if (!s) {
  237. return;
  238. }
  239. s->volume_db = p_volume_db;
  240. }
  241. void AudioStreamPlaybackPolyphonic::set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale) {
  242. Stream *s = _find_stream(p_stream_id);
  243. if (!s) {
  244. return;
  245. }
  246. s->pitch_scale = p_pitch_scale;
  247. }
  248. bool AudioStreamPlaybackPolyphonic::is_stream_playing(ID p_stream_id) const {
  249. return const_cast<AudioStreamPlaybackPolyphonic *>(this)->_find_stream(p_stream_id) != nullptr;
  250. }
  251. void AudioStreamPlaybackPolyphonic::stop_stream(ID p_stream_id) {
  252. Stream *s = _find_stream(p_stream_id);
  253. if (!s) {
  254. return;
  255. }
  256. s->finish_request.set();
  257. }
  258. void AudioStreamPlaybackPolyphonic::set_is_sample(bool p_is_sample) {
  259. _is_sample = p_is_sample;
  260. }
  261. bool AudioStreamPlaybackPolyphonic::get_is_sample() const {
  262. return _is_sample;
  263. }
  264. Ref<AudioSamplePlayback> AudioStreamPlaybackPolyphonic::get_sample_playback() const {
  265. return sample_playback;
  266. }
  267. void AudioStreamPlaybackPolyphonic::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
  268. sample_playback = p_playback;
  269. if (sample_playback.is_valid()) {
  270. sample_playback->stream_playback = Ref<AudioStreamPlayback>(this);
  271. }
  272. }
  273. void AudioStreamPlaybackPolyphonic::_bind_methods() {
  274. ClassDB::bind_method(D_METHOD("play_stream", "stream", "from_offset", "volume_db", "pitch_scale", "playback_type", "bus"), &AudioStreamPlaybackPolyphonic::play_stream, DEFVAL(0), DEFVAL(0), DEFVAL(1.0), DEFVAL(0), DEFVAL(SceneStringName(Master)));
  275. ClassDB::bind_method(D_METHOD("set_stream_volume", "stream", "volume_db"), &AudioStreamPlaybackPolyphonic::set_stream_volume);
  276. ClassDB::bind_method(D_METHOD("set_stream_pitch_scale", "stream", "pitch_scale"), &AudioStreamPlaybackPolyphonic::set_stream_pitch_scale);
  277. ClassDB::bind_method(D_METHOD("is_stream_playing", "stream"), &AudioStreamPlaybackPolyphonic::is_stream_playing);
  278. ClassDB::bind_method(D_METHOD("stop_stream", "stream"), &AudioStreamPlaybackPolyphonic::stop_stream);
  279. BIND_CONSTANT(INVALID_ID);
  280. }
  281. AudioStreamPlaybackPolyphonic::AudioStreamPlaybackPolyphonic() {
  282. }