audio_stream_synchronized.cpp 10 KB

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