audio_stream_polyphonic.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "scene/main/scene_tree.h"
  32. Ref<AudioStreamPlayback> AudioStreamPolyphonic::instantiate_playback() {
  33. Ref<AudioStreamPlaybackPolyphonic> playback;
  34. playback.instantiate();
  35. playback->streams.resize(polyphony);
  36. return playback;
  37. }
  38. String AudioStreamPolyphonic::get_stream_name() const {
  39. return "AudioStreamPolyphonic";
  40. }
  41. bool AudioStreamPolyphonic::is_monophonic() const {
  42. return true; // This avoids stream players to instantiate more than one of these.
  43. }
  44. void AudioStreamPolyphonic::set_polyphony(int p_voices) {
  45. ERR_FAIL_COND(p_voices < 0 || p_voices > 128);
  46. polyphony = p_voices;
  47. }
  48. int AudioStreamPolyphonic::get_polyphony() const {
  49. return polyphony;
  50. }
  51. void AudioStreamPolyphonic::_bind_methods() {
  52. ClassDB::bind_method(D_METHOD("set_polyphony", "voices"), &AudioStreamPolyphonic::set_polyphony);
  53. ClassDB::bind_method(D_METHOD("get_polyphony"), &AudioStreamPolyphonic::get_polyphony);
  54. ADD_PROPERTY(PropertyInfo(Variant::INT, "polyphony", PROPERTY_HINT_RANGE, "1,128,1"), "set_polyphony", "get_polyphony");
  55. }
  56. AudioStreamPolyphonic::AudioStreamPolyphonic() {
  57. }
  58. ////////////////////////
  59. void AudioStreamPlaybackPolyphonic::start(double p_from_pos) {
  60. if (active) {
  61. stop();
  62. }
  63. active = true;
  64. }
  65. void AudioStreamPlaybackPolyphonic::stop() {
  66. if (!active) {
  67. return;
  68. }
  69. bool locked = false;
  70. for (Stream &s : streams) {
  71. if (s.active.is_set()) {
  72. // Need locking because something may still be mixing.
  73. locked = true;
  74. AudioServer::get_singleton()->lock();
  75. }
  76. s.active.clear();
  77. s.finish_request.clear();
  78. s.stream_playback.unref();
  79. s.stream.unref();
  80. }
  81. if (locked) {
  82. AudioServer::get_singleton()->unlock();
  83. }
  84. active = false;
  85. }
  86. bool AudioStreamPlaybackPolyphonic::is_playing() const {
  87. return active;
  88. }
  89. int AudioStreamPlaybackPolyphonic::get_loop_count() const {
  90. return 0;
  91. }
  92. double AudioStreamPlaybackPolyphonic::get_playback_position() const {
  93. return 0;
  94. }
  95. void AudioStreamPlaybackPolyphonic::seek(double p_time) {
  96. // Ignored.
  97. }
  98. void AudioStreamPlaybackPolyphonic::tag_used_streams() {
  99. for (Stream &s : streams) {
  100. if (s.active.is_set()) {
  101. s.stream_playback->tag_used_streams();
  102. }
  103. }
  104. }
  105. int AudioStreamPlaybackPolyphonic::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  106. if (!active) {
  107. return 0;
  108. }
  109. // Pre-clear buffer.
  110. for (int i = 0; i < p_frames; i++) {
  111. p_buffer[i] = AudioFrame(0, 0);
  112. }
  113. for (Stream &s : streams) {
  114. if (!s.active.is_set()) {
  115. continue;
  116. }
  117. float volume_db = s.volume_db; // Copy because it can be overridden at any time.
  118. float next_volume = Math::db_to_linear(volume_db);
  119. s.prev_volume_db = volume_db;
  120. if (s.finish_request.is_set()) {
  121. if (s.pending_play.is_set()) {
  122. // Did not get the chance to play, was finalized too soon.
  123. s.active.clear();
  124. continue;
  125. }
  126. next_volume = 0;
  127. }
  128. if (s.pending_play.is_set()) {
  129. s.stream_playback->start(s.play_offset);
  130. s.pending_play.clear();
  131. }
  132. float prev_volume = Math::db_to_linear(s.prev_volume_db);
  133. float volume_inc = (next_volume - prev_volume) / float(p_frames);
  134. int todo = p_frames;
  135. int offset = 0;
  136. float volume = prev_volume;
  137. bool stream_done = false;
  138. while (todo) {
  139. int to_mix = MIN(todo, int(INTERNAL_BUFFER_LEN));
  140. int mixed = s.stream_playback->mix(internal_buffer, s.pitch_scale, to_mix);
  141. for (int i = 0; i < to_mix; i++) {
  142. p_buffer[offset + i] += internal_buffer[i] * volume;
  143. volume += volume_inc;
  144. }
  145. if (mixed < to_mix) {
  146. // Stream is done.
  147. s.active.clear();
  148. stream_done = true;
  149. break;
  150. }
  151. todo -= to_mix;
  152. offset += to_mix;
  153. }
  154. if (stream_done) {
  155. continue;
  156. }
  157. if (s.finish_request.is_set()) {
  158. s.active.clear();
  159. }
  160. }
  161. return p_frames;
  162. }
  163. AudioStreamPlaybackPolyphonic::ID AudioStreamPlaybackPolyphonic::play_stream(const Ref<AudioStream> &p_stream, float p_from_offset, float p_volume_db, float p_pitch_scale) {
  164. ERR_FAIL_COND_V(p_stream.is_null(), INVALID_ID);
  165. for (uint32_t i = 0; i < streams.size(); i++) {
  166. if (!streams[i].active.is_set()) {
  167. // Can use this stream, as it's not active.
  168. streams[i].stream = p_stream;
  169. streams[i].stream_playback = streams[i].stream->instantiate_playback();
  170. streams[i].play_offset = p_from_offset;
  171. streams[i].volume_db = p_volume_db;
  172. streams[i].prev_volume_db = p_volume_db;
  173. streams[i].pitch_scale = p_pitch_scale;
  174. streams[i].id = id_counter++;
  175. streams[i].finish_request.clear();
  176. streams[i].pending_play.set();
  177. streams[i].active.set();
  178. return (ID(i) << INDEX_SHIFT) | ID(streams[i].id);
  179. }
  180. }
  181. return INVALID_ID;
  182. }
  183. AudioStreamPlaybackPolyphonic::Stream *AudioStreamPlaybackPolyphonic::_find_stream(int64_t p_id) {
  184. uint32_t index = p_id >> INDEX_SHIFT;
  185. if (index >= streams.size()) {
  186. return nullptr;
  187. }
  188. if (!streams[index].active.is_set()) {
  189. return nullptr; // Not active, no longer exists.
  190. }
  191. int64_t id = p_id & ID_MASK;
  192. if (streams[index].id != id) {
  193. return nullptr;
  194. }
  195. return &streams[index];
  196. }
  197. void AudioStreamPlaybackPolyphonic::set_stream_volume(ID p_stream_id, float p_volume_db) {
  198. Stream *s = _find_stream(p_stream_id);
  199. if (!s) {
  200. return;
  201. }
  202. s->volume_db = p_volume_db;
  203. }
  204. void AudioStreamPlaybackPolyphonic::set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale) {
  205. Stream *s = _find_stream(p_stream_id);
  206. if (!s) {
  207. return;
  208. }
  209. s->pitch_scale = p_pitch_scale;
  210. }
  211. bool AudioStreamPlaybackPolyphonic::is_stream_playing(ID p_stream_id) const {
  212. return const_cast<AudioStreamPlaybackPolyphonic *>(this)->_find_stream(p_stream_id) != nullptr;
  213. }
  214. void AudioStreamPlaybackPolyphonic::stop_stream(ID p_stream_id) {
  215. Stream *s = _find_stream(p_stream_id);
  216. if (!s) {
  217. return;
  218. }
  219. s->finish_request.set();
  220. }
  221. void AudioStreamPlaybackPolyphonic::_bind_methods() {
  222. ClassDB::bind_method(D_METHOD("play_stream", "stream", "from_offset", "volume_db", "pitch_scale"), &AudioStreamPlaybackPolyphonic::play_stream, DEFVAL(0), DEFVAL(0), DEFVAL(1.0));
  223. ClassDB::bind_method(D_METHOD("set_stream_volume", "stream", "volume_db"), &AudioStreamPlaybackPolyphonic::set_stream_volume);
  224. ClassDB::bind_method(D_METHOD("set_stream_pitch_scale", "stream", "pitch_scale"), &AudioStreamPlaybackPolyphonic::set_stream_pitch_scale);
  225. ClassDB::bind_method(D_METHOD("is_stream_playing", "stream"), &AudioStreamPlaybackPolyphonic::is_stream_playing);
  226. ClassDB::bind_method(D_METHOD("stop_stream", "stream"), &AudioStreamPlaybackPolyphonic::stop_stream);
  227. BIND_CONSTANT(INVALID_ID);
  228. }
  229. AudioStreamPlaybackPolyphonic::AudioStreamPlaybackPolyphonic() {
  230. }