2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* event_player.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//set_idle_process(false); //don't annoy
|
2014-11-06 00:20:42 +00:00
|
|
|
if (playback.is_valid() && autoplay && !get_tree()->is_editor_hint())
|
2014-02-10 01:10:30 +00:00
|
|
|
play();
|
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
stop(); //wathever it may be doing, stop
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventPlayer::set_stream(const Ref<EventStream> &p_stream) {
|
|
|
|
|
|
|
|
stop();
|
2017-03-18 23:36:26 +00:00
|
|
|
stream = p_stream;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (stream.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
playback = stream->instance_playback();
|
2016-02-20 23:59:02 +00:00
|
|
|
else
|
|
|
|
playback.unref();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (playback.is_valid()) {
|
|
|
|
|
|
|
|
playback->set_loop(loops);
|
|
|
|
playback->set_paused(paused);
|
|
|
|
playback->set_volume(volume);
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < (MIN(MAX_CHANNELS, stream->get_channel_count())); i++)
|
|
|
|
playback->set_channel_volume(i, channel_volume[i]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<EventStream> EventPlayer::get_stream() const {
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventPlayer::play() {
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
ERR_FAIL_COND(!is_inside_tree());
|
2014-09-17 23:03:10 +00:00
|
|
|
if (playback.is_null()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2014-09-17 23:03:10 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
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() {
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
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) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
loops = p_enable;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (playback.is_null())
|
|
|
|
return;
|
|
|
|
playback->set_loop(loops);
|
|
|
|
}
|
|
|
|
bool EventPlayer::has_loop() const {
|
|
|
|
|
|
|
|
return loops;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventPlayer::set_volume(float p_volume) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
volume = p_volume;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (playback.is_valid())
|
|
|
|
playback->set_volume(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
float EventPlayer::get_volume() const {
|
|
|
|
|
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventPlayer::set_volume_db(float p_db) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_db < -79)
|
2014-02-10 01:10:30 +00:00
|
|
|
set_volume(0);
|
|
|
|
else
|
|
|
|
set_volume(Math::db2linear(p_db));
|
|
|
|
}
|
|
|
|
|
|
|
|
float EventPlayer::get_volume_db() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (volume == 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
return -80;
|
|
|
|
else
|
|
|
|
return Math::linear2db(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventPlayer::set_pitch_scale(float p_pitch_scale) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
pitch_scale = p_pitch_scale;
|
2014-02-10 01:10:30 +00:00
|
|
|
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) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
tempo_scale = p_tempo_scale;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (playback.is_valid())
|
|
|
|
playback->set_tempo_scale(tempo_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
float EventPlayer::get_tempo_scale() const {
|
|
|
|
|
|
|
|
return tempo_scale;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String EventPlayer::get_stream_name() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (stream.is_null())
|
|
|
|
return "<No Stream>";
|
|
|
|
return stream->get_name();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int EventPlayer::get_loop_count() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (playback.is_null())
|
|
|
|
return 0;
|
|
|
|
return playback->get_loop_count();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float EventPlayer::get_pos() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
autoplay = p_enable;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EventPlayer::has_autoplay() const {
|
|
|
|
|
|
|
|
return autoplay;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventPlayer::set_paused(bool p_paused) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
paused = p_paused;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (playback.is_valid())
|
|
|
|
playback->set_paused(p_paused);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventPlayer::is_paused() const {
|
|
|
|
|
|
|
|
return paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventPlayer::_set_play(bool p_play) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_play = p_play;
|
2014-11-06 00:20:42 +00:00
|
|
|
if (is_inside_tree()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (_play)
|
2014-02-10 01:10:30 +00:00
|
|
|
play();
|
|
|
|
else
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EventPlayer::_get_play() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return _play;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void EventPlayer::set_channel_volume(int p_channel, float p_volume) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_channel, MAX_CHANNELS);
|
|
|
|
channel_volume[p_channel] = p_volume;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (playback.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
playback->set_channel_volume(p_channel, p_volume);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float EventPlayer::get_channel_volume(int p_channel) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_channel, MAX_CHANNELS, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
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() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_stream", "stream:EventStream"), &EventPlayer::set_stream);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_stream:EventStream"), &EventPlayer::get_stream);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("play"), &EventPlayer::play);
|
|
|
|
ObjectTypeDB::bind_method(_MD("stop"), &EventPlayer::stop);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("is_playing"), &EventPlayer::is_playing);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_paused", "paused"), &EventPlayer::set_paused);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_paused"), &EventPlayer::is_paused);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_loop", "enabled"), &EventPlayer::set_loop);
|
|
|
|
ObjectTypeDB::bind_method(_MD("has_loop"), &EventPlayer::has_loop);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_volume", "volume"), &EventPlayer::set_volume);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_volume"), &EventPlayer::get_volume);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_pitch_scale", "pitch_scale"), &EventPlayer::set_pitch_scale);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_pitch_scale"), &EventPlayer::get_pitch_scale);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_tempo_scale", "tempo_scale"), &EventPlayer::set_tempo_scale);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_tempo_scale"), &EventPlayer::get_tempo_scale);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_volume_db", "db"), &EventPlayer::set_volume_db);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_volume_db"), &EventPlayer::get_volume_db);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_stream_name"), &EventPlayer::get_stream_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_loop_count"), &EventPlayer::get_loop_count);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_pos"), &EventPlayer::get_pos);
|
|
|
|
ObjectTypeDB::bind_method(_MD("seek_pos", "time"), &EventPlayer::seek_pos);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_length"), &EventPlayer::get_length);
|
2015-12-28 01:13:05 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_autoplay", "enabled"), &EventPlayer::set_autoplay);
|
|
|
|
ObjectTypeDB::bind_method(_MD("has_autoplay"), &EventPlayer::has_autoplay);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_play", "play"), &EventPlayer::_set_play);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_play"), &EventPlayer::_get_play);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EventPlayer::EventPlayer() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EventPlayer::~EventPlayer() {
|
|
|
|
}
|