audio_stream_polyphonic.cpp 11 KB

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