123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- /*************************************************************************/
- /* event_player.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "event_player.h"
- void EventPlayer::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE: {
- //set_idle_process(false); //don't annoy
- if (playback.is_valid() && autoplay && !get_tree()->is_editor_hint())
- play();
- } break;
- case NOTIFICATION_EXIT_TREE: {
- stop(); //wathever it may be doing, stop
- } break;
- }
- }
- void EventPlayer::set_stream(const Ref<EventStream> &p_stream) {
- stop();
- stream = p_stream;
- if (stream.is_valid())
- playback = stream->instance_playback();
- else
- playback.unref();
- if (playback.is_valid()) {
- playback->set_loop(loops);
- playback->set_paused(paused);
- playback->set_volume(volume);
- for (int i = 0; i < (MIN(MAX_CHANNELS, stream->get_channel_count())); i++)
- playback->set_channel_volume(i, channel_volume[i]);
- }
- }
- Ref<EventStream> EventPlayer::get_stream() const {
- return stream;
- }
- void EventPlayer::play() {
- ERR_FAIL_COND(!is_inside_tree());
- if (playback.is_null()) {
- return;
- }
- if (playback->is_playing()) {
- AudioServer::get_singleton()->lock();
- stop();
- AudioServer::get_singleton()->unlock();
- }
- AudioServer::get_singleton()->lock();
- playback->play();
- AudioServer::get_singleton()->unlock();
- }
- void EventPlayer::stop() {
- if (!is_inside_tree())
- return;
- if (playback.is_null())
- return;
- AudioServer::get_singleton()->lock();
- playback->stop();
- AudioServer::get_singleton()->unlock();
- }
- bool EventPlayer::is_playing() const {
- if (playback.is_null())
- return false;
- return playback->is_playing();
- }
- void EventPlayer::set_loop(bool p_enable) {
- loops = p_enable;
- if (playback.is_null())
- return;
- playback->set_loop(loops);
- }
- bool EventPlayer::has_loop() const {
- return loops;
- }
- void EventPlayer::set_volume(float p_volume) {
- volume = p_volume;
- if (playback.is_valid())
- playback->set_volume(volume);
- }
- float EventPlayer::get_volume() const {
- return volume;
- }
- void EventPlayer::set_volume_db(float p_db) {
- if (p_db < -79)
- set_volume(0);
- else
- set_volume(Math::db2linear(p_db));
- }
- float EventPlayer::get_volume_db() const {
- if (volume == 0)
- return -80;
- else
- return Math::linear2db(volume);
- }
- void EventPlayer::set_pitch_scale(float p_pitch_scale) {
- pitch_scale = p_pitch_scale;
- if (playback.is_valid())
- playback->set_pitch_scale(pitch_scale);
- }
- float EventPlayer::get_pitch_scale() const {
- return pitch_scale;
- }
- void EventPlayer::set_tempo_scale(float p_tempo_scale) {
- tempo_scale = p_tempo_scale;
- if (playback.is_valid())
- playback->set_tempo_scale(tempo_scale);
- }
- float EventPlayer::get_tempo_scale() const {
- return tempo_scale;
- }
- String EventPlayer::get_stream_name() const {
- if (stream.is_null())
- return "<No Stream>";
- return stream->get_name();
- }
- int EventPlayer::get_loop_count() const {
- if (playback.is_null())
- return 0;
- return playback->get_loop_count();
- }
- float EventPlayer::get_pos() const {
- if (playback.is_null())
- return 0;
- return playback->get_pos();
- }
- float EventPlayer::get_length() const {
- if (stream.is_null())
- return 0;
- return stream->get_length();
- }
- void EventPlayer::seek_pos(float p_time) {
- if (playback.is_null())
- return;
- return playback->seek_pos(p_time);
- }
- void EventPlayer::set_autoplay(bool p_enable) {
- autoplay = p_enable;
- }
- bool EventPlayer::has_autoplay() const {
- return autoplay;
- }
- void EventPlayer::set_paused(bool p_paused) {
- paused = p_paused;
- if (playback.is_valid())
- playback->set_paused(p_paused);
- }
- bool EventPlayer::is_paused() const {
- return paused;
- }
- void EventPlayer::_set_play(bool p_play) {
- _play = p_play;
- if (is_inside_tree()) {
- if (_play)
- play();
- else
- stop();
- }
- }
- bool EventPlayer::_get_play() const {
- return _play;
- }
- void EventPlayer::set_channel_volume(int p_channel, float p_volume) {
- ERR_FAIL_INDEX(p_channel, MAX_CHANNELS);
- channel_volume[p_channel] = p_volume;
- if (playback.is_valid())
- playback->set_channel_volume(p_channel, p_volume);
- }
- float EventPlayer::get_channel_volume(int p_channel) const {
- ERR_FAIL_INDEX_V(p_channel, MAX_CHANNELS, 0);
- return channel_volume[p_channel];
- }
- float EventPlayer::get_channel_last_note_time(int p_channel) const {
- if (playback.is_valid())
- return playback->get_last_note_time(p_channel);
- return 0;
- }
- void EventPlayer::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("set_stream", "stream:EventStream"), &EventPlayer::set_stream);
- ObjectTypeDB::bind_method(_MD("get_stream:EventStream"), &EventPlayer::get_stream);
- ObjectTypeDB::bind_method(_MD("play"), &EventPlayer::play);
- ObjectTypeDB::bind_method(_MD("stop"), &EventPlayer::stop);
- ObjectTypeDB::bind_method(_MD("is_playing"), &EventPlayer::is_playing);
- ObjectTypeDB::bind_method(_MD("set_paused", "paused"), &EventPlayer::set_paused);
- ObjectTypeDB::bind_method(_MD("is_paused"), &EventPlayer::is_paused);
- ObjectTypeDB::bind_method(_MD("set_loop", "enabled"), &EventPlayer::set_loop);
- ObjectTypeDB::bind_method(_MD("has_loop"), &EventPlayer::has_loop);
- ObjectTypeDB::bind_method(_MD("set_volume", "volume"), &EventPlayer::set_volume);
- ObjectTypeDB::bind_method(_MD("get_volume"), &EventPlayer::get_volume);
- ObjectTypeDB::bind_method(_MD("set_pitch_scale", "pitch_scale"), &EventPlayer::set_pitch_scale);
- ObjectTypeDB::bind_method(_MD("get_pitch_scale"), &EventPlayer::get_pitch_scale);
- ObjectTypeDB::bind_method(_MD("set_tempo_scale", "tempo_scale"), &EventPlayer::set_tempo_scale);
- ObjectTypeDB::bind_method(_MD("get_tempo_scale"), &EventPlayer::get_tempo_scale);
- ObjectTypeDB::bind_method(_MD("set_volume_db", "db"), &EventPlayer::set_volume_db);
- ObjectTypeDB::bind_method(_MD("get_volume_db"), &EventPlayer::get_volume_db);
- ObjectTypeDB::bind_method(_MD("get_stream_name"), &EventPlayer::get_stream_name);
- ObjectTypeDB::bind_method(_MD("get_loop_count"), &EventPlayer::get_loop_count);
- ObjectTypeDB::bind_method(_MD("get_pos"), &EventPlayer::get_pos);
- ObjectTypeDB::bind_method(_MD("seek_pos", "time"), &EventPlayer::seek_pos);
- ObjectTypeDB::bind_method(_MD("get_length"), &EventPlayer::get_length);
- ObjectTypeDB::bind_method(_MD("set_autoplay", "enabled"), &EventPlayer::set_autoplay);
- ObjectTypeDB::bind_method(_MD("has_autoplay"), &EventPlayer::has_autoplay);
- ObjectTypeDB::bind_method(_MD("set_channel_volume", "channel", "channel_volume"), &EventPlayer::set_channel_volume);
- ObjectTypeDB::bind_method(_MD("get_channel_volume", "channel"), &EventPlayer::get_channel_volume);
- ObjectTypeDB::bind_method(_MD("get_channel_last_note_time", "channel"), &EventPlayer::get_channel_last_note_time);
- ObjectTypeDB::bind_method(_MD("_set_play", "play"), &EventPlayer::_set_play);
- ObjectTypeDB::bind_method(_MD("_get_play"), &EventPlayer::_get_play);
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream/stream", PROPERTY_HINT_RESOURCE_TYPE, "EventStream"), _SCS("set_stream"), _SCS("get_stream"));
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream/play"), _SCS("_set_play"), _SCS("_get_play"));
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream/loop"), _SCS("set_loop"), _SCS("has_loop"));
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "stream/volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), _SCS("set_volume_db"), _SCS("get_volume_db"));
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "stream/pitch_scale", PROPERTY_HINT_RANGE, "0.001,16,0.001"), _SCS("set_pitch_scale"), _SCS("get_pitch_scale"));
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "stream/tempo_scale", PROPERTY_HINT_RANGE, "0.001,16,0.001"), _SCS("set_tempo_scale"), _SCS("get_tempo_scale"));
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream/autoplay"), _SCS("set_autoplay"), _SCS("has_autoplay"));
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream/paused"), _SCS("set_paused"), _SCS("is_paused"));
- }
- EventPlayer::EventPlayer() {
- volume = 1;
- loops = false;
- paused = false;
- autoplay = false;
- _play = false;
- pitch_scale = 1.0;
- tempo_scale = 1.0;
- for (int i = 0; i < MAX_CHANNELS; i++)
- channel_volume[i] = 1.0;
- }
- EventPlayer::~EventPlayer() {
- }
|