audio_stream_synchronized.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**************************************************************************/
  2. /* audio_stream_synchronized.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_synchronized.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/string/print_string.h"
  33. AudioStreamSynchronized::AudioStreamSynchronized() {
  34. }
  35. Ref<AudioStreamPlayback> AudioStreamSynchronized::instantiate_playback() {
  36. Ref<AudioStreamPlaybackSynchronized> playback_playlist;
  37. playback_playlist.instantiate();
  38. playback_playlist->stream = Ref<AudioStreamSynchronized>(this);
  39. playback_playlist->_update_playback_instances();
  40. playbacks.insert(playback_playlist.operator->());
  41. return playback_playlist;
  42. }
  43. String AudioStreamSynchronized::get_stream_name() const {
  44. return "Synchronized";
  45. }
  46. void AudioStreamSynchronized::set_sync_stream(int p_stream_index, Ref<AudioStream> p_stream) {
  47. ERR_FAIL_COND(p_stream == this);
  48. ERR_FAIL_INDEX(p_stream_index, MAX_STREAMS);
  49. AudioServer::get_singleton()->lock();
  50. audio_streams[p_stream_index] = p_stream;
  51. for (AudioStreamPlaybackSynchronized *E : playbacks) {
  52. E->_update_playback_instances();
  53. }
  54. AudioServer::get_singleton()->unlock();
  55. }
  56. Ref<AudioStream> AudioStreamSynchronized::get_sync_stream(int p_stream_index) const {
  57. ERR_FAIL_INDEX_V(p_stream_index, MAX_STREAMS, Ref<AudioStream>());
  58. return audio_streams[p_stream_index];
  59. }
  60. void AudioStreamSynchronized::set_sync_stream_volume(int p_stream_index, float p_db) {
  61. ERR_FAIL_INDEX(p_stream_index, MAX_STREAMS);
  62. audio_stream_volume_db[p_stream_index] = p_db;
  63. }
  64. float AudioStreamSynchronized::get_sync_stream_volume(int p_stream_index) const {
  65. ERR_FAIL_INDEX_V(p_stream_index, MAX_STREAMS, 0);
  66. return audio_stream_volume_db[p_stream_index];
  67. }
  68. double AudioStreamSynchronized::get_bpm() const {
  69. for (int i = 0; i < stream_count; i++) {
  70. if (audio_streams[i].is_valid()) {
  71. double bpm = audio_streams[i]->get_bpm();
  72. if (bpm != 0.0) {
  73. return bpm;
  74. }
  75. }
  76. }
  77. return 0.0;
  78. }
  79. int AudioStreamSynchronized::get_beat_count() const {
  80. int max_beats = 0;
  81. for (int i = 0; i < stream_count; i++) {
  82. if (audio_streams[i].is_valid()) {
  83. max_beats = MAX(max_beats, audio_streams[i]->get_beat_count());
  84. }
  85. }
  86. return max_beats;
  87. }
  88. bool AudioStreamSynchronized::has_loop() const {
  89. for (int i = 0; i < stream_count; i++) {
  90. if (audio_streams[i].is_valid()) {
  91. if (audio_streams[i]->has_loop()) {
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. double AudioStreamSynchronized::get_length() const {
  99. double max_length = 0.0;
  100. for (int i = 0; i < stream_count; i++) {
  101. if (audio_streams[i].is_valid()) {
  102. max_length = MAX(max_length, audio_streams[i]->get_length());
  103. }
  104. }
  105. return max_length;
  106. }
  107. void AudioStreamSynchronized::set_stream_count(int p_count) {
  108. ERR_FAIL_COND(p_count < 0 || p_count > MAX_STREAMS);
  109. AudioServer::get_singleton()->lock();
  110. stream_count = p_count;
  111. AudioServer::get_singleton()->unlock();
  112. notify_property_list_changed();
  113. }
  114. int AudioStreamSynchronized::get_stream_count() const {
  115. return stream_count;
  116. }
  117. void AudioStreamSynchronized::_validate_property(PropertyInfo &property) const {
  118. String prop = property.name;
  119. if (prop != "stream_count" && prop.begins_with("stream_")) {
  120. int stream = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
  121. if (stream >= stream_count) {
  122. property.usage = PROPERTY_USAGE_INTERNAL;
  123. }
  124. }
  125. }
  126. void AudioStreamSynchronized::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_stream_count", "stream_count"), &AudioStreamSynchronized::set_stream_count);
  128. ClassDB::bind_method(D_METHOD("get_stream_count"), &AudioStreamSynchronized::get_stream_count);
  129. ClassDB::bind_method(D_METHOD("set_sync_stream", "stream_index", "audio_stream"), &AudioStreamSynchronized::set_sync_stream);
  130. ClassDB::bind_method(D_METHOD("get_sync_stream", "stream_index"), &AudioStreamSynchronized::get_sync_stream);
  131. ClassDB::bind_method(D_METHOD("set_sync_stream_volume", "stream_index", "volume_db"), &AudioStreamSynchronized::set_sync_stream_volume);
  132. ClassDB::bind_method(D_METHOD("get_sync_stream_volume", "stream_index"), &AudioStreamSynchronized::get_sync_stream_volume);
  133. ADD_PROPERTY(PropertyInfo(Variant::INT, "stream_count", PROPERTY_HINT_RANGE, "0," + itos(MAX_STREAMS), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Streams,stream_,unfoldable,page_size=999,add_button_text=" + String(RTR("Add Stream"))), "set_stream_count", "get_stream_count");
  134. for (int i = 0; i < MAX_STREAMS; i++) {
  135. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "stream_" + itos(i) + "/stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_sync_stream", "get_sync_stream", i);
  136. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "stream_" + itos(i) + "/volume", PROPERTY_HINT_RANGE, "-60,12,0.01,suffix:db", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_sync_stream_volume", "get_sync_stream_volume", i);
  137. }
  138. BIND_CONSTANT(MAX_STREAMS);
  139. }
  140. //////////////////////
  141. //////////////////////
  142. AudioStreamPlaybackSynchronized::AudioStreamPlaybackSynchronized() {
  143. }
  144. AudioStreamPlaybackSynchronized::~AudioStreamPlaybackSynchronized() {
  145. if (stream.is_valid()) {
  146. stream->playbacks.erase(this);
  147. }
  148. }
  149. void AudioStreamPlaybackSynchronized::stop() {
  150. active = false;
  151. for (int i = 0; i < stream->stream_count; i++) {
  152. if (playback[i].is_valid()) {
  153. playback[i]->stop();
  154. }
  155. }
  156. }
  157. void AudioStreamPlaybackSynchronized::start(double p_from_pos) {
  158. if (active) {
  159. stop();
  160. }
  161. for (int i = 0; i < stream->stream_count; i++) {
  162. if (playback[i].is_valid()) {
  163. playback[i]->start(p_from_pos);
  164. active = true;
  165. }
  166. }
  167. }
  168. void AudioStreamPlaybackSynchronized::seek(double p_time) {
  169. for (int i = 0; i < stream->stream_count; i++) {
  170. if (playback[i].is_valid()) {
  171. playback[i]->seek(p_time);
  172. }
  173. }
  174. }
  175. int AudioStreamPlaybackSynchronized::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  176. if (!active) {
  177. return 0;
  178. }
  179. int todo = p_frames;
  180. bool any_active = false;
  181. while (todo) {
  182. int to_mix = MIN(todo, MIX_BUFFER_SIZE);
  183. bool first = true;
  184. for (int i = 0; i < stream->stream_count; i++) {
  185. if (playback[i].is_valid() && playback[i]->is_playing()) {
  186. float volume = Math::db_to_linear(stream->audio_stream_volume_db[i]);
  187. if (first) {
  188. playback[i]->mix(p_buffer, p_rate_scale, to_mix);
  189. for (int j = 0; j < to_mix; j++) {
  190. p_buffer[j] *= volume;
  191. }
  192. first = false;
  193. any_active = true;
  194. } else {
  195. playback[i]->mix(mix_buffer, p_rate_scale, to_mix);
  196. for (int j = 0; j < to_mix; j++) {
  197. p_buffer[j] += mix_buffer[j] * volume;
  198. }
  199. }
  200. }
  201. }
  202. if (first) {
  203. // Nothing mixed, put zeroes.
  204. for (int j = 0; j < to_mix; j++) {
  205. p_buffer[j] = AudioFrame(0, 0);
  206. }
  207. }
  208. p_buffer += to_mix;
  209. todo -= to_mix;
  210. }
  211. if (!any_active) {
  212. active = false;
  213. }
  214. return p_frames;
  215. }
  216. void AudioStreamPlaybackSynchronized::tag_used_streams() {
  217. if (active) {
  218. for (int i = 0; i < stream->stream_count; i++) {
  219. if (playback[i].is_valid() && playback[i]->is_playing()) {
  220. stream->audio_streams[i]->tag_used(playback[i]->get_playback_position());
  221. }
  222. }
  223. stream->tag_used(0);
  224. }
  225. }
  226. int AudioStreamPlaybackSynchronized::get_loop_count() const {
  227. int min_loops = 0;
  228. bool min_loops_found = false;
  229. for (int i = 0; i < stream->stream_count; i++) {
  230. if (playback[i].is_valid() && playback[i]->is_playing()) {
  231. int loops = playback[i]->get_loop_count();
  232. if (!min_loops_found || loops < min_loops) {
  233. min_loops = loops;
  234. min_loops_found = true;
  235. }
  236. }
  237. }
  238. return min_loops;
  239. }
  240. double AudioStreamPlaybackSynchronized::get_playback_position() const {
  241. float max_pos = 0;
  242. bool pos_found = false;
  243. for (int i = 0; i < stream->stream_count; i++) {
  244. if (playback[i].is_valid() && playback[i]->is_playing()) {
  245. float pos = playback[i]->get_playback_position();
  246. if (!pos_found || pos > max_pos) {
  247. max_pos = pos;
  248. pos_found = true;
  249. }
  250. }
  251. }
  252. return max_pos;
  253. }
  254. bool AudioStreamPlaybackSynchronized::is_playing() const {
  255. return active;
  256. }
  257. void AudioStreamPlaybackSynchronized::_update_playback_instances() {
  258. stop();
  259. for (int i = 0; i < stream->stream_count; i++) {
  260. if (stream->audio_streams[i].is_valid()) {
  261. playback[i] = stream->audio_streams[i]->instantiate_playback();
  262. } else {
  263. playback[i].unref();
  264. }
  265. }
  266. }