Path: blob/master/modules/interactive_music/audio_stream_playlist.cpp
10277 views
/**************************************************************************/1/* audio_stream_playlist.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_stream_playlist.h"3132#include "core/math/math_funcs.h"3334Ref<AudioStreamPlayback> AudioStreamPlaylist::instantiate_playback() {35Ref<AudioStreamPlaybackPlaylist> playback_playlist;36playback_playlist.instantiate();37playback_playlist->playlist = Ref<AudioStreamPlaylist>(this);38playback_playlist->_update_playback_instances();39playbacks.insert(playback_playlist.operator->());40return playback_playlist;41}4243String AudioStreamPlaylist::get_stream_name() const {44return "Playlist";45}4647void AudioStreamPlaylist::set_list_stream(int p_stream_index, Ref<AudioStream> p_stream) {48ERR_FAIL_COND(p_stream == this);49ERR_FAIL_INDEX(p_stream_index, MAX_STREAMS);5051AudioServer::get_singleton()->lock();52audio_streams[p_stream_index] = p_stream;53for (AudioStreamPlaybackPlaylist *E : playbacks) {54E->_update_playback_instances();55}56AudioServer::get_singleton()->unlock();57}5859Ref<AudioStream> AudioStreamPlaylist::get_list_stream(int p_stream_index) const {60ERR_FAIL_INDEX_V(p_stream_index, MAX_STREAMS, Ref<AudioStream>());6162return audio_streams[p_stream_index];63}6465double AudioStreamPlaylist::get_bpm() const {66for (int i = 0; i < stream_count; i++) {67if (audio_streams[i].is_valid()) {68double bpm = audio_streams[i]->get_bpm();69if (bpm != 0.0) {70return bpm;71}72}73}74return 0.0;75}7677double AudioStreamPlaylist::get_length() const {78double total_length = 0.0;79for (int i = 0; i < stream_count; i++) {80if (audio_streams[i].is_valid()) {81double bpm = audio_streams[i]->get_bpm();82int beat_count = audio_streams[i]->get_beat_count();83if (bpm > 0.0 && beat_count > 0) {84total_length += beat_count * 60.0 / bpm;85} else {86total_length += audio_streams[i]->get_length();87}88}89}90return total_length;91}9293void AudioStreamPlaylist::set_stream_count(int p_count) {94ERR_FAIL_COND(p_count < 0 || p_count > MAX_STREAMS);95AudioServer::get_singleton()->lock();96stream_count = p_count;97AudioServer::get_singleton()->unlock();98notify_property_list_changed();99}100101int AudioStreamPlaylist::get_stream_count() const {102return stream_count;103}104105void AudioStreamPlaylist::set_fade_time(float p_time) {106fade_time = p_time;107}108109float AudioStreamPlaylist::get_fade_time() const {110return fade_time;111}112113void AudioStreamPlaylist::set_shuffle(bool p_shuffle) {114shuffle = p_shuffle;115}116117bool AudioStreamPlaylist::get_shuffle() const {118return shuffle;119}120121void AudioStreamPlaylist::set_loop(bool p_loop) {122loop = p_loop;123}124125bool AudioStreamPlaylist::has_loop() const {126return loop;127}128129void AudioStreamPlaylist::_validate_property(PropertyInfo &r_property) const {130String prop = r_property.name;131if (prop != "stream_count" && prop.begins_with("stream_")) {132int stream = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();133if (stream >= stream_count) {134r_property.usage = PROPERTY_USAGE_INTERNAL;135}136}137}138139void AudioStreamPlaylist::_bind_methods() {140ClassDB::bind_method(D_METHOD("set_stream_count", "stream_count"), &AudioStreamPlaylist::set_stream_count);141ClassDB::bind_method(D_METHOD("get_stream_count"), &AudioStreamPlaylist::get_stream_count);142143ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamPlaylist::get_bpm);144145ClassDB::bind_method(D_METHOD("set_list_stream", "stream_index", "audio_stream"), &AudioStreamPlaylist::set_list_stream);146ClassDB::bind_method(D_METHOD("get_list_stream", "stream_index"), &AudioStreamPlaylist::get_list_stream);147148ClassDB::bind_method(D_METHOD("set_shuffle", "shuffle"), &AudioStreamPlaylist::set_shuffle);149ClassDB::bind_method(D_METHOD("get_shuffle"), &AudioStreamPlaylist::get_shuffle);150151ClassDB::bind_method(D_METHOD("set_fade_time", "dec"), &AudioStreamPlaylist::set_fade_time);152ClassDB::bind_method(D_METHOD("get_fade_time"), &AudioStreamPlaylist::get_fade_time);153154ClassDB::bind_method(D_METHOD("set_loop", "loop"), &AudioStreamPlaylist::set_loop);155ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamPlaylist::has_loop);156157ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shuffle"), "set_shuffle", "get_shuffle");158ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");159ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fade_time", PROPERTY_HINT_RANGE, "0,1,0.01,suffix:s"), "set_fade_time", "get_fade_time");160161ADD_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(TTRC("Add Stream"))), "set_stream_count", "get_stream_count");162163for (int i = 0; i < MAX_STREAMS; i++) {164ADD_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);165}166167BIND_CONSTANT(MAX_STREAMS);168}169170//////////////////////171//////////////////////172173AudioStreamPlaybackPlaylist::~AudioStreamPlaybackPlaylist() {174if (playlist.is_valid()) {175playlist->playbacks.erase(this);176}177}178179void AudioStreamPlaybackPlaylist::stop() {180active = false;181for (int i = 0; i < playlist->stream_count; i++) {182if (playback[i].is_valid()) {183playback[i]->stop();184}185}186}187188void AudioStreamPlaybackPlaylist::_update_order() {189for (int i = 0; i < playlist->stream_count; i++) {190play_order[i] = i;191}192193if (playlist->shuffle) {194for (int i = 0; i < playlist->stream_count; i++) {195int swap_with = Math::rand() % uint32_t(playlist->stream_count);196SWAP(play_order[i], play_order[swap_with]);197}198}199}200201void AudioStreamPlaybackPlaylist::start(double p_from_pos) {202if (active) {203stop();204}205206p_from_pos = MAX(0, p_from_pos);207208float pl_length = playlist->get_length();209if (p_from_pos >= pl_length) {210if (!playlist->loop) {211return; // No loop, end.212}213p_from_pos = Math::fmod((float)p_from_pos, (float)pl_length);214}215216_update_order();217218play_index = -1;219220double play_ofs = p_from_pos;221for (int i = 0; i < playlist->stream_count; i++) {222int idx = play_order[i];223if (playlist->audio_streams[idx].is_valid()) {224double bpm = playlist->audio_streams[idx]->get_bpm();225int beat_count = playlist->audio_streams[idx]->get_beat_count();226double length;227if (bpm > 0.0 && beat_count > 0) {228length = beat_count * 60.0 / bpm;229} else {230length = playlist->audio_streams[idx]->get_length();231}232if (play_ofs < length) {233play_index = i;234stream_todo = length - play_ofs;235break;236} else {237play_ofs -= length;238}239}240}241242if (play_index == -1) {243return;244}245246playback[play_order[play_index]]->start(play_ofs);247fade_index = -1;248loop_count = 0;249250active = true;251}252253void AudioStreamPlaybackPlaylist::seek(double p_time) {254stop();255start(p_time);256}257258int AudioStreamPlaybackPlaylist::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {259if (!active) {260return 0;261}262263double time_dec = (1.0 / AudioServer::get_singleton()->get_mix_rate());264double fade_dec = (1.0 / playlist->fade_time) / AudioServer::get_singleton()->get_mix_rate();265266int todo = p_frames;267268while (todo) {269int to_mix = MIN(todo, MIX_BUFFER_SIZE);270271playback[play_order[play_index]]->mix(mix_buffer, 1.0, to_mix);272if (fade_index != -1) {273playback[fade_index]->mix(fade_buffer, 1.0, to_mix);274}275276offset += time_dec * to_mix;277278for (int i = 0; i < to_mix; i++) {279*p_buffer = mix_buffer[i];280stream_todo -= time_dec;281if (stream_todo < 0) {282//find next stream.283int prev = play_order[play_index];284285for (int j = 0; j < playlist->stream_count; j++) {286play_index++;287if (play_index >= playlist->stream_count) {288// No loop, exit.289if (!playlist->loop) {290for (int k = i; k < todo - i; k++) {291p_buffer[k] = AudioFrame(0, 0);292}293todo = to_mix;294active = false;295break;296}297298_update_order();299play_index = 0;300loop_count++;301offset = time_dec * (to_mix - i);302}303if (playback[play_order[play_index]].is_valid()) {304break;305}306}307308if (!active) {309break;310}311312if (playback[play_order[play_index]].is_null()) {313todo = to_mix; // Weird error.314active = false;315break;316}317318bool restart = true;319if (prev == play_order[play_index]) {320// Went back to the same one, continue loop (if it loops) or restart if it does not.321if (playlist->audio_streams[prev]->has_loop()) {322restart = false;323}324fade_index = -1;325} else {326// Move current mixed data to fade buffer.327for (int j = i; j < to_mix; j++) {328fade_buffer[j] = mix_buffer[j];329}330331fade_index = prev;332fade_volume = 1.0;333}334335int idx = play_order[play_index];336337if (restart) {338playback[idx]->start(0); // No loop, just cold-restart.339playback[idx]->mix(mix_buffer + i, 1.0, to_mix - i); // Fill rest of mix buffer340}341342// Update fade todo.343double bpm = playlist->audio_streams[idx]->get_bpm();344int beat_count = playlist->audio_streams[idx]->get_beat_count();345346if (bpm > 0.0 && beat_count > 0) {347stream_todo = beat_count * 60.0 / bpm;348} else {349stream_todo = playlist->audio_streams[idx]->get_length();350}351}352353if (fade_index != -1) {354*p_buffer += fade_buffer[i] * fade_volume;355fade_volume -= fade_dec;356if (fade_volume <= 0.0) {357playback[fade_index]->stop();358fade_index = -1;359}360}361362p_buffer++;363}364365todo -= to_mix;366}367368return p_frames;369}370371void AudioStreamPlaybackPlaylist::tag_used_streams() {372if (active) {373playlist->audio_streams[play_order[play_index]]->tag_used(playback[play_order[play_index]]->get_playback_position());374}375376playlist->tag_used(0);377}378379int AudioStreamPlaybackPlaylist::get_loop_count() const {380return loop_count;381}382383double AudioStreamPlaybackPlaylist::get_playback_position() const {384return offset;385}386387bool AudioStreamPlaybackPlaylist::is_playing() const {388return active;389}390391void AudioStreamPlaybackPlaylist::_update_playback_instances() {392stop();393394for (int i = 0; i < playlist->stream_count; i++) {395if (playlist->audio_streams[i].is_valid()) {396playback[i] = playlist->audio_streams[i]->instantiate_playback();397} else {398playback[i].unref();399}400}401}402403404