audio_stream_playlist.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**************************************************************************/
  2. /* audio_stream_playlist.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_playlist.h"
  31. #include "core/math/math_funcs.h"
  32. Ref<AudioStreamPlayback> AudioStreamPlaylist::instantiate_playback() {
  33. Ref<AudioStreamPlaybackPlaylist> playback_playlist;
  34. playback_playlist.instantiate();
  35. playback_playlist->playlist = Ref<AudioStreamPlaylist>(this);
  36. playback_playlist->_update_playback_instances();
  37. playbacks.insert(playback_playlist.operator->());
  38. return playback_playlist;
  39. }
  40. String AudioStreamPlaylist::get_stream_name() const {
  41. return "Playlist";
  42. }
  43. void AudioStreamPlaylist::set_list_stream(int p_stream_index, Ref<AudioStream> p_stream) {
  44. ERR_FAIL_COND(p_stream == this);
  45. ERR_FAIL_INDEX(p_stream_index, MAX_STREAMS);
  46. AudioServer::get_singleton()->lock();
  47. audio_streams[p_stream_index] = p_stream;
  48. for (AudioStreamPlaybackPlaylist *E : playbacks) {
  49. E->_update_playback_instances();
  50. }
  51. AudioServer::get_singleton()->unlock();
  52. }
  53. Ref<AudioStream> AudioStreamPlaylist::get_list_stream(int p_stream_index) const {
  54. ERR_FAIL_INDEX_V(p_stream_index, MAX_STREAMS, Ref<AudioStream>());
  55. return audio_streams[p_stream_index];
  56. }
  57. double AudioStreamPlaylist::get_bpm() const {
  58. for (int i = 0; i < stream_count; i++) {
  59. if (audio_streams[i].is_valid()) {
  60. double bpm = audio_streams[i]->get_bpm();
  61. if (bpm != 0.0) {
  62. return bpm;
  63. }
  64. }
  65. }
  66. return 0.0;
  67. }
  68. double AudioStreamPlaylist::get_length() const {
  69. double total_length = 0.0;
  70. for (int i = 0; i < stream_count; i++) {
  71. if (audio_streams[i].is_valid()) {
  72. double bpm = audio_streams[i]->get_bpm();
  73. int beat_count = audio_streams[i]->get_beat_count();
  74. if (bpm > 0.0 && beat_count > 0) {
  75. total_length += beat_count * 60.0 / bpm;
  76. } else {
  77. total_length += audio_streams[i]->get_length();
  78. }
  79. }
  80. }
  81. return total_length;
  82. }
  83. void AudioStreamPlaylist::set_stream_count(int p_count) {
  84. ERR_FAIL_COND(p_count < 0 || p_count > MAX_STREAMS);
  85. AudioServer::get_singleton()->lock();
  86. stream_count = p_count;
  87. AudioServer::get_singleton()->unlock();
  88. notify_property_list_changed();
  89. }
  90. int AudioStreamPlaylist::get_stream_count() const {
  91. return stream_count;
  92. }
  93. void AudioStreamPlaylist::set_fade_time(float p_time) {
  94. fade_time = p_time;
  95. }
  96. float AudioStreamPlaylist::get_fade_time() const {
  97. return fade_time;
  98. }
  99. void AudioStreamPlaylist::set_shuffle(bool p_shuffle) {
  100. shuffle = p_shuffle;
  101. }
  102. bool AudioStreamPlaylist::get_shuffle() const {
  103. return shuffle;
  104. }
  105. void AudioStreamPlaylist::set_loop(bool p_loop) {
  106. loop = p_loop;
  107. }
  108. bool AudioStreamPlaylist::has_loop() const {
  109. return loop;
  110. }
  111. void AudioStreamPlaylist::_validate_property(PropertyInfo &r_property) const {
  112. String prop = r_property.name;
  113. if (prop != "stream_count" && prop.begins_with("stream_")) {
  114. int stream = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
  115. if (stream >= stream_count) {
  116. r_property.usage = PROPERTY_USAGE_INTERNAL;
  117. }
  118. }
  119. }
  120. void AudioStreamPlaylist::_bind_methods() {
  121. ClassDB::bind_method(D_METHOD("set_stream_count", "stream_count"), &AudioStreamPlaylist::set_stream_count);
  122. ClassDB::bind_method(D_METHOD("get_stream_count"), &AudioStreamPlaylist::get_stream_count);
  123. ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamPlaylist::get_bpm);
  124. ClassDB::bind_method(D_METHOD("set_list_stream", "stream_index", "audio_stream"), &AudioStreamPlaylist::set_list_stream);
  125. ClassDB::bind_method(D_METHOD("get_list_stream", "stream_index"), &AudioStreamPlaylist::get_list_stream);
  126. ClassDB::bind_method(D_METHOD("set_shuffle", "shuffle"), &AudioStreamPlaylist::set_shuffle);
  127. ClassDB::bind_method(D_METHOD("get_shuffle"), &AudioStreamPlaylist::get_shuffle);
  128. ClassDB::bind_method(D_METHOD("set_fade_time", "dec"), &AudioStreamPlaylist::set_fade_time);
  129. ClassDB::bind_method(D_METHOD("get_fade_time"), &AudioStreamPlaylist::get_fade_time);
  130. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &AudioStreamPlaylist::set_loop);
  131. ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamPlaylist::has_loop);
  132. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shuffle"), "set_shuffle", "get_shuffle");
  133. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  134. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fade_time", PROPERTY_HINT_RANGE, "0,1,0.01,suffix:s"), "set_fade_time", "get_fade_time");
  135. 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");
  136. for (int i = 0; i < MAX_STREAMS; i++) {
  137. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "stream_" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "AudioStream", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_list_stream", "get_list_stream", i);
  138. }
  139. BIND_CONSTANT(MAX_STREAMS);
  140. }
  141. //////////////////////
  142. //////////////////////
  143. AudioStreamPlaybackPlaylist::~AudioStreamPlaybackPlaylist() {
  144. if (playlist.is_valid()) {
  145. playlist->playbacks.erase(this);
  146. }
  147. }
  148. void AudioStreamPlaybackPlaylist::stop() {
  149. active = false;
  150. for (int i = 0; i < playlist->stream_count; i++) {
  151. if (playback[i].is_valid()) {
  152. playback[i]->stop();
  153. }
  154. }
  155. }
  156. void AudioStreamPlaybackPlaylist::_update_order() {
  157. for (int i = 0; i < playlist->stream_count; i++) {
  158. play_order[i] = i;
  159. }
  160. if (playlist->shuffle) {
  161. for (int i = 0; i < playlist->stream_count; i++) {
  162. int swap_with = Math::rand() % uint32_t(playlist->stream_count);
  163. SWAP(play_order[i], play_order[swap_with]);
  164. }
  165. }
  166. }
  167. void AudioStreamPlaybackPlaylist::start(double p_from_pos) {
  168. if (active) {
  169. stop();
  170. }
  171. p_from_pos = MAX(0, p_from_pos);
  172. float pl_length = playlist->get_length();
  173. if (p_from_pos >= pl_length) {
  174. if (!playlist->loop) {
  175. return; // No loop, end.
  176. }
  177. p_from_pos = Math::fmod((float)p_from_pos, (float)pl_length);
  178. }
  179. _update_order();
  180. play_index = -1;
  181. double play_ofs = p_from_pos;
  182. for (int i = 0; i < playlist->stream_count; i++) {
  183. int idx = play_order[i];
  184. if (playlist->audio_streams[idx].is_valid()) {
  185. double bpm = playlist->audio_streams[idx]->get_bpm();
  186. int beat_count = playlist->audio_streams[idx]->get_beat_count();
  187. double length;
  188. if (bpm > 0.0 && beat_count > 0) {
  189. length = beat_count * 60.0 / bpm;
  190. } else {
  191. length = playlist->audio_streams[idx]->get_length();
  192. }
  193. if (play_ofs < length) {
  194. play_index = i;
  195. stream_todo = length - play_ofs;
  196. break;
  197. } else {
  198. play_ofs -= length;
  199. }
  200. }
  201. }
  202. if (play_index == -1) {
  203. return;
  204. }
  205. playback[play_order[play_index]]->start(play_ofs);
  206. fade_index = -1;
  207. loop_count = 0;
  208. active = true;
  209. }
  210. void AudioStreamPlaybackPlaylist::seek(double p_time) {
  211. stop();
  212. start(p_time);
  213. }
  214. int AudioStreamPlaybackPlaylist::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  215. if (!active) {
  216. return 0;
  217. }
  218. double time_dec = (1.0 / AudioServer::get_singleton()->get_mix_rate());
  219. double fade_dec = (1.0 / playlist->fade_time) / AudioServer::get_singleton()->get_mix_rate();
  220. int todo = p_frames;
  221. while (todo) {
  222. int to_mix = MIN(todo, MIX_BUFFER_SIZE);
  223. playback[play_order[play_index]]->mix(mix_buffer, 1.0, to_mix);
  224. if (fade_index != -1) {
  225. playback[fade_index]->mix(fade_buffer, 1.0, to_mix);
  226. }
  227. offset += time_dec * to_mix;
  228. for (int i = 0; i < to_mix; i++) {
  229. *p_buffer = mix_buffer[i];
  230. stream_todo -= time_dec;
  231. if (stream_todo < 0) {
  232. //find next stream.
  233. int prev = play_order[play_index];
  234. for (int j = 0; j < playlist->stream_count; j++) {
  235. play_index++;
  236. if (play_index >= playlist->stream_count) {
  237. // No loop, exit.
  238. if (!playlist->loop) {
  239. for (int k = i; k < todo - i; k++) {
  240. p_buffer[k] = AudioFrame(0, 0);
  241. }
  242. todo = to_mix;
  243. active = false;
  244. break;
  245. }
  246. _update_order();
  247. play_index = 0;
  248. loop_count++;
  249. offset = time_dec * (to_mix - i);
  250. }
  251. if (playback[play_order[play_index]].is_valid()) {
  252. break;
  253. }
  254. }
  255. if (!active) {
  256. break;
  257. }
  258. if (playback[play_order[play_index]].is_null()) {
  259. todo = to_mix; // Weird error.
  260. active = false;
  261. break;
  262. }
  263. bool restart = true;
  264. if (prev == play_order[play_index]) {
  265. // Went back to the same one, continue loop (if it loops) or restart if it does not.
  266. if (playlist->audio_streams[prev]->has_loop()) {
  267. restart = false;
  268. }
  269. fade_index = -1;
  270. } else {
  271. // Move current mixed data to fade buffer.
  272. for (int j = i; j < to_mix; j++) {
  273. fade_buffer[j] = mix_buffer[j];
  274. }
  275. fade_index = prev;
  276. fade_volume = 1.0;
  277. }
  278. int idx = play_order[play_index];
  279. if (restart) {
  280. playback[idx]->start(0); // No loop, just cold-restart.
  281. playback[idx]->mix(mix_buffer + i, 1.0, to_mix - i); // Fill rest of mix buffer
  282. }
  283. // Update fade todo.
  284. double bpm = playlist->audio_streams[idx]->get_bpm();
  285. int beat_count = playlist->audio_streams[idx]->get_beat_count();
  286. if (bpm > 0.0 && beat_count > 0) {
  287. stream_todo = beat_count * 60.0 / bpm;
  288. } else {
  289. stream_todo = playlist->audio_streams[idx]->get_length();
  290. }
  291. }
  292. if (fade_index != -1) {
  293. *p_buffer += fade_buffer[i] * fade_volume;
  294. fade_volume -= fade_dec;
  295. if (fade_volume <= 0.0) {
  296. playback[fade_index]->stop();
  297. fade_index = -1;
  298. }
  299. }
  300. p_buffer++;
  301. }
  302. todo -= to_mix;
  303. }
  304. return p_frames;
  305. }
  306. void AudioStreamPlaybackPlaylist::tag_used_streams() {
  307. if (active) {
  308. playlist->audio_streams[play_order[play_index]]->tag_used(playback[play_order[play_index]]->get_playback_position());
  309. }
  310. playlist->tag_used(0);
  311. }
  312. int AudioStreamPlaybackPlaylist::get_loop_count() const {
  313. return loop_count;
  314. }
  315. double AudioStreamPlaybackPlaylist::get_playback_position() const {
  316. return offset;
  317. }
  318. bool AudioStreamPlaybackPlaylist::is_playing() const {
  319. return active;
  320. }
  321. void AudioStreamPlaybackPlaylist::_update_playback_instances() {
  322. stop();
  323. for (int i = 0; i < playlist->stream_count; i++) {
  324. if (playlist->audio_streams[i].is_valid()) {
  325. playback[i] = playlist->audio_streams[i]->instantiate_playback();
  326. } else {
  327. playback[i].unref();
  328. }
  329. }
  330. }