2018-01-05 00:50:27 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream_player_2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-01-05 00:50:27 +01: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. */
|
|
|
|
/*************************************************************************/
|
2017-06-19 02:07:32 +02:00
|
|
|
|
|
|
|
#include "audio_stream_player_2d.h"
|
2017-08-19 01:02:56 +02:00
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
#include "scene/2d/area_2d.h"
|
2021-09-16 21:28:20 +02:00
|
|
|
#include "scene/2d/audio_listener_2d.h"
|
2020-03-04 02:51:12 +01:00
|
|
|
#include "scene/main/window.h"
|
2017-08-19 01:02:56 +02:00
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
void AudioStreamPlayer2D::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2021-08-27 19:28:23 +02:00
|
|
|
AudioServer::get_singleton()->add_listener_changed_callback(_listener_changed_cb, this);
|
2017-08-19 01:02:56 +02:00
|
|
|
if (autoplay && !Engine::get_singleton()->is_editor_hint()) {
|
2017-06-19 02:07:32 +02:00
|
|
|
play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2021-08-27 19:28:23 +02:00
|
|
|
stop();
|
|
|
|
AudioServer::get_singleton()->remove_listener_changed_callback(_listener_changed_cb, this);
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 20:29:10 +02:00
|
|
|
if (p_what == NOTIFICATION_PAUSED) {
|
|
|
|
if (!can_process()) {
|
|
|
|
// Node can't process so we start fading out to silence
|
|
|
|
set_stream_paused(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_UNPAUSED) {
|
|
|
|
set_stream_paused(false);
|
|
|
|
}
|
|
|
|
|
2017-09-30 16:19:07 +02:00
|
|
|
if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
|
2017-06-19 02:07:32 +02:00
|
|
|
//update anything related to position first, if possible of course
|
2021-08-28 06:51:03 +02:00
|
|
|
if (setplay.get() > 0 || (active.is_set() && last_mix_count != AudioServer::get_singleton()->get_mix_count())) {
|
|
|
|
_update_panning();
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-28 06:51:03 +02:00
|
|
|
if (setplay.get() >= 0 && stream.is_valid()) {
|
|
|
|
active.set();
|
|
|
|
Ref<AudioStreamPlayback> new_playback = stream->instance_playback();
|
|
|
|
ERR_FAIL_COND_MSG(new_playback.is_null(), "Failed to instantiate playback.");
|
2021-10-10 18:20:56 +02:00
|
|
|
AudioServer::get_singleton()->start_playback_stream(new_playback, _get_actual_bus(), volume_vector, setplay.get(), pitch_scale);
|
2021-08-28 06:51:03 +02:00
|
|
|
stream_playbacks.push_back(new_playback);
|
|
|
|
setplay.set(-1);
|
2021-08-27 19:28:23 +02:00
|
|
|
}
|
2021-08-28 06:51:03 +02:00
|
|
|
|
|
|
|
if (!stream_playbacks.is_empty() && active.is_set()) {
|
|
|
|
// Stop playing if no longer active.
|
|
|
|
Vector<Ref<AudioStreamPlayback>> playbacks_to_remove;
|
|
|
|
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
|
|
|
|
if (playback.is_valid() && !AudioServer::get_singleton()->is_playback_active(playback) && !AudioServer::get_singleton()->is_playback_paused(playback)) {
|
|
|
|
playbacks_to_remove.push_back(playback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now go through and remove playbacks that have finished. Removing elements from a Vector in a range based for is asking for trouble.
|
|
|
|
for (Ref<AudioStreamPlayback> &playback : playbacks_to_remove) {
|
|
|
|
stream_playbacks.erase(playback);
|
|
|
|
}
|
|
|
|
if (!playbacks_to_remove.is_empty() && stream_playbacks.is_empty()) {
|
|
|
|
// This node is no longer actively playing audio.
|
|
|
|
active.clear();
|
|
|
|
set_physics_process_internal(false);
|
2021-08-27 19:28:23 +02:00
|
|
|
}
|
2021-10-29 04:45:29 +02:00
|
|
|
if (!playbacks_to_remove.is_empty()) {
|
|
|
|
emit_signal(SNAME("finished"));
|
|
|
|
}
|
2021-08-27 19:28:23 +02:00
|
|
|
}
|
|
|
|
|
2021-08-28 06:51:03 +02:00
|
|
|
while (stream_playbacks.size() > max_polyphony) {
|
|
|
|
AudioServer::get_singleton()->stop_playback_stream(stream_playbacks[0]);
|
2021-07-04 00:17:03 +02:00
|
|
|
stream_playbacks.remove_at(0);
|
2021-08-27 19:28:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
StringName AudioStreamPlayer2D::_get_actual_bus() {
|
|
|
|
Vector2 global_pos = get_global_position();
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
//check if any area is diverting sound into a bus
|
|
|
|
Ref<World2D> world_2d = get_world_2d();
|
|
|
|
ERR_FAIL_COND_V(world_2d.is_null(), SNAME("Master"));
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
PhysicsDirectSpaceState2D *space_state = PhysicsServer2D::get_singleton()->space_get_direct_state(world_2d->get_space());
|
|
|
|
PhysicsDirectSpaceState2D::ShapeResult sr[MAX_INTERSECT_AREAS];
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-11-02 02:00:58 +01:00
|
|
|
PhysicsDirectSpaceState2D::PointParameters point_params;
|
|
|
|
point_params.position = global_pos;
|
|
|
|
point_params.collision_mask = area_mask;
|
|
|
|
point_params.collide_with_bodies = false;
|
|
|
|
point_params.collide_with_areas = true;
|
|
|
|
|
|
|
|
int areas = space_state->intersect_point(point_params, sr, MAX_INTERSECT_AREAS);
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
for (int i = 0; i < areas; i++) {
|
|
|
|
Area2D *area2d = Object::cast_to<Area2D>(sr[i].collider);
|
|
|
|
if (!area2d) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
if (!area2d->is_overriding_audio_bus()) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
return area2d->get_audio_bus_name();
|
|
|
|
}
|
|
|
|
return default_bus;
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
void AudioStreamPlayer2D::_update_panning() {
|
2021-08-28 06:51:03 +02:00
|
|
|
if (!active.is_set() || stream.is_null()) {
|
2021-08-27 19:28:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
Ref<World2D> world_2d = get_world_2d();
|
|
|
|
ERR_FAIL_COND(world_2d.is_null());
|
2021-06-15 22:33:24 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
Vector2 global_pos = get_global_position();
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
Set<Viewport *> viewports = world_2d->get_viewports();
|
|
|
|
viewports.insert(get_viewport()); // TODO: This is a mediocre workaround for #50958. Remove when that bug is fixed!
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
volume_vector.resize(4);
|
|
|
|
volume_vector.write[0] = AudioFrame(0, 0);
|
|
|
|
volume_vector.write[1] = AudioFrame(0, 0);
|
|
|
|
volume_vector.write[2] = AudioFrame(0, 0);
|
|
|
|
volume_vector.write[3] = AudioFrame(0, 0);
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
for (Viewport *vp : viewports) {
|
|
|
|
if (!vp->is_audio_listener_2d()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//compute matrix to convert to screen
|
|
|
|
Vector2 screen_size = vp->get_visible_rect().size;
|
2020-12-31 21:34:09 +01:00
|
|
|
Vector2 listener_in_global;
|
|
|
|
Vector2 relative_to_listener;
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
//screen in global is used for attenuation
|
2021-09-16 21:28:20 +02:00
|
|
|
AudioListener2D *listener = vp->get_audio_listener_2d();
|
2020-12-31 21:34:09 +01:00
|
|
|
if (listener) {
|
|
|
|
listener_in_global = listener->get_global_position();
|
|
|
|
relative_to_listener = global_pos - listener_in_global;
|
|
|
|
} else {
|
|
|
|
Transform2D to_listener = vp->get_global_canvas_transform() * vp->get_canvas_transform();
|
|
|
|
listener_in_global = to_listener.affine_inverse().xform(screen_size * 0.5);
|
|
|
|
relative_to_listener = to_listener.xform(global_pos) - screen_size * 0.5;
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2020-12-31 21:34:09 +01:00
|
|
|
float dist = global_pos.distance_to(listener_in_global); // Distance to listener, or screen if none.
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
if (dist > max_distance) {
|
|
|
|
continue; //can't hear this sound in this viewport
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
float multiplier = Math::pow(1.0f - dist / max_distance, attenuation);
|
|
|
|
multiplier *= Math::db2linear(volume_db); //also apply player volume!
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2020-12-31 21:34:09 +01:00
|
|
|
float pan = CLAMP((relative_to_listener.x + screen_size.x * 0.5) / screen_size.x, 0.0, 1.0);
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
float l = 1.0 - pan;
|
|
|
|
float r = pan;
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-27 19:28:23 +02:00
|
|
|
volume_vector.write[0] = AudioFrame(l, r) * multiplier;
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
2021-08-27 19:28:23 +02:00
|
|
|
|
2021-08-28 06:51:03 +02:00
|
|
|
for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
|
|
|
|
AudioServer::get_singleton()->set_playback_bus_exclusive(playback, _get_actual_bus(), volume_vector);
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-28 06:51:03 +02:00
|
|
|
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
|
|
|
|
AudioServer::get_singleton()->set_playback_pitch_scale(playback, pitch_scale);
|
2018-06-19 13:56:05 +02:00
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
|
2021-08-28 06:51:03 +02:00
|
|
|
last_mix_count = AudioServer::get_singleton()->get_mix_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_stream(Ref<AudioStream> p_stream) {
|
|
|
|
stop();
|
|
|
|
stream = p_stream;
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AudioStream> AudioStreamPlayer2D::get_stream() const {
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_volume_db(float p_volume) {
|
|
|
|
volume_db = p_volume;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
float AudioStreamPlayer2D::get_volume_db() const {
|
|
|
|
return volume_db;
|
|
|
|
}
|
|
|
|
|
2018-01-01 22:23:16 +01:00
|
|
|
void AudioStreamPlayer2D::set_pitch_scale(float p_pitch_scale) {
|
2018-08-20 10:25:48 +02:00
|
|
|
ERR_FAIL_COND(p_pitch_scale <= 0.0);
|
2018-01-01 22:23:16 +01:00
|
|
|
pitch_scale = p_pitch_scale;
|
2021-08-28 06:51:03 +02:00
|
|
|
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
|
|
|
|
AudioServer::get_singleton()->set_playback_pitch_scale(playback, p_pitch_scale);
|
2021-08-27 19:28:23 +02:00
|
|
|
}
|
2018-01-01 22:23:16 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2018-01-01 22:23:16 +01:00
|
|
|
float AudioStreamPlayer2D::get_pitch_scale() const {
|
|
|
|
return pitch_scale;
|
|
|
|
}
|
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
void AudioStreamPlayer2D::play(float p_from_pos) {
|
2021-08-28 06:51:03 +02:00
|
|
|
if (stream.is_null()) {
|
|
|
|
return;
|
2018-07-11 03:57:50 +02:00
|
|
|
}
|
2021-08-28 06:51:03 +02:00
|
|
|
ERR_FAIL_COND_MSG(!is_inside_tree(), "Playback can only happen when a node is inside the scene tree");
|
|
|
|
if (stream->is_monophonic() && is_playing()) {
|
|
|
|
stop();
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
2021-08-28 06:51:03 +02:00
|
|
|
|
|
|
|
setplay.set(p_from_pos);
|
|
|
|
active.set();
|
|
|
|
set_physics_process_internal(true);
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::seek(float p_seconds) {
|
2021-08-28 06:51:03 +02:00
|
|
|
if (is_playing()) {
|
|
|
|
stop();
|
2021-08-27 19:28:23 +02:00
|
|
|
play(p_seconds);
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::stop() {
|
2021-08-28 06:51:03 +02:00
|
|
|
setplay.set(-1);
|
|
|
|
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
|
|
|
|
AudioServer::get_singleton()->stop_playback_stream(playback);
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
2021-08-28 06:51:03 +02:00
|
|
|
stream_playbacks.clear();
|
|
|
|
active.clear();
|
|
|
|
set_physics_process_internal(false);
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioStreamPlayer2D::is_playing() const {
|
2021-08-28 06:51:03 +02:00
|
|
|
for (const Ref<AudioStreamPlayback> &playback : stream_playbacks) {
|
|
|
|
if (AudioServer::get_singleton()->is_playback_active(playback)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-21 05:31:36 +02:00
|
|
|
float AudioStreamPlayer2D::get_playback_position() {
|
2021-08-28 06:51:03 +02:00
|
|
|
// Return the playback position of the most recently started playback stream.
|
|
|
|
if (!stream_playbacks.is_empty()) {
|
|
|
|
return AudioServer::get_singleton()->get_playback_position(stream_playbacks[stream_playbacks.size() - 1]);
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_bus(const StringName &p_bus) {
|
2021-08-27 19:28:23 +02:00
|
|
|
default_bus = p_bus; // This will be pushed to the audio server during the next physics timestep, which is fast enough.
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
StringName AudioStreamPlayer2D::get_bus() const {
|
|
|
|
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
|
2021-08-27 19:28:23 +02:00
|
|
|
if (AudioServer::get_singleton()->get_bus_name(i) == default_bus) {
|
|
|
|
return default_bus;
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-27 19:28:23 +02:00
|
|
|
return SNAME("Master");
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_autoplay(bool p_enable) {
|
|
|
|
autoplay = p_enable;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
bool AudioStreamPlayer2D::is_autoplay_enabled() {
|
|
|
|
return autoplay;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::_set_playing(bool p_enable) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_enable) {
|
2017-06-19 02:07:32 +02:00
|
|
|
play();
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-06-19 02:07:32 +02:00
|
|
|
stop();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
bool AudioStreamPlayer2D::_is_active() const {
|
2021-08-28 06:51:03 +02:00
|
|
|
return active.is_set();
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::_validate_property(PropertyInfo &property) const {
|
|
|
|
if (property.name == "bus") {
|
|
|
|
String options;
|
|
|
|
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2017-06-19 02:07:32 +02:00
|
|
|
options += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-06-19 02:07:32 +02:00
|
|
|
String name = AudioServer::get_singleton()->get_bus_name(i);
|
|
|
|
options += name;
|
|
|
|
}
|
|
|
|
|
|
|
|
property.hint_string = options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::_bus_layout_changed() {
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed();
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_max_distance(float p_pixels) {
|
|
|
|
ERR_FAIL_COND(p_pixels <= 0.0);
|
|
|
|
max_distance = p_pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlayer2D::get_max_distance() const {
|
|
|
|
return max_distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_attenuation(float p_curve) {
|
|
|
|
attenuation = p_curve;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
float AudioStreamPlayer2D::get_attenuation() const {
|
|
|
|
return attenuation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_area_mask(uint32_t p_mask) {
|
|
|
|
area_mask = p_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AudioStreamPlayer2D::get_area_mask() const {
|
|
|
|
return area_mask;
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:29:10 +02:00
|
|
|
void AudioStreamPlayer2D::set_stream_paused(bool p_pause) {
|
2021-08-28 06:51:03 +02:00
|
|
|
// TODO this does not have perfect recall, fix that maybe? If there are zero playbacks registered with the AudioServer, this bool isn't persisted.
|
|
|
|
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
|
|
|
|
AudioServer::get_singleton()->set_playback_paused(playback, p_pause);
|
2018-05-27 20:29:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioStreamPlayer2D::get_stream_paused() const {
|
2021-08-28 06:51:03 +02:00
|
|
|
// There's currently no way to pause some playback streams but not others. Check the first and don't bother looking at the rest.
|
|
|
|
if (!stream_playbacks.is_empty()) {
|
|
|
|
return AudioServer::get_singleton()->is_playback_paused(stream_playbacks[0]);
|
2021-08-27 19:28:23 +02:00
|
|
|
}
|
|
|
|
return false;
|
2018-05-27 20:29:10 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:57:03 +02:00
|
|
|
Ref<AudioStreamPlayback> AudioStreamPlayer2D::get_stream_playback() {
|
2021-08-28 06:51:03 +02:00
|
|
|
if (!stream_playbacks.is_empty()) {
|
|
|
|
return stream_playbacks[stream_playbacks.size() - 1];
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayer2D::set_max_polyphony(int p_max_polyphony) {
|
|
|
|
if (p_max_polyphony > 0) {
|
|
|
|
max_polyphony = p_max_polyphony;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioStreamPlayer2D::get_max_polyphony() const {
|
|
|
|
return max_polyphony;
|
2019-04-10 17:57:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
void AudioStreamPlayer2D::_bind_methods() {
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer2D::set_stream);
|
2017-06-19 02:07:32 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer2D::get_stream);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer2D::set_volume_db);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer2D::get_volume_db);
|
|
|
|
|
2018-01-01 22:23:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer2D::set_pitch_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer2D::get_pitch_scale);
|
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer2D::play, DEFVAL(0.0));
|
|
|
|
ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer2D::seek);
|
2017-06-19 02:07:32 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer2D::stop);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer2D::is_playing);
|
2017-09-21 05:31:36 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer2D::get_playback_position);
|
2017-06-19 02:07:32 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer2D::set_bus);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer2D::get_bus);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer2D::set_autoplay);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer2D::is_autoplay_enabled);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_playing", "enable"), &AudioStreamPlayer2D::_set_playing);
|
|
|
|
ClassDB::bind_method(D_METHOD("_is_active"), &AudioStreamPlayer2D::_is_active);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_max_distance", "pixels"), &AudioStreamPlayer2D::set_max_distance);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_distance"), &AudioStreamPlayer2D::get_max_distance);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_attenuation", "curve"), &AudioStreamPlayer2D::set_attenuation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_attenuation"), &AudioStreamPlayer2D::get_attenuation);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_area_mask", "mask"), &AudioStreamPlayer2D::set_area_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_area_mask"), &AudioStreamPlayer2D::get_area_mask);
|
|
|
|
|
2018-05-27 20:29:10 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer2D::set_stream_paused);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer2D::get_stream_paused);
|
|
|
|
|
2021-08-28 06:51:03 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer2D::set_max_polyphony);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer2D::get_max_polyphony);
|
|
|
|
|
2019-04-10 17:57:03 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer2D::get_stream_playback);
|
|
|
|
|
2017-06-19 02:07:32 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24"), "set_volume_db", "get_volume_db");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
|
2017-09-13 13:40:41 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
|
2017-06-19 02:07:32 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
|
2018-05-27 20:29:10 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
|
Fix editor suffixes and degrees conversion
* Functions to convert to/from degrees are all gone. Conversion is done by the editor.
* Use PROPERTY_HINT_ANGLE instead of PROPERTY_HINT_RANGE to edit radian angles in degrees.
* Added possibility to add suffixes to range properties, use "min,max[,step][,suffix:<something>]" example "0,100,1,suffix:m"
* In general, can add suffixes for EditorSpinSlider
Not covered by this PR, will have to be addressed by future ones:
* Ability to switch radians/degrees in the inspector for angle properties (if actually wanted).
* Animations previously made will most likely break, need to add a way to make old ones compatible.
* Only added a "px" suffix to 2D position and a "m" one to 3D position, someone needs to go through the rest of the engine and add all remaining suffixes.
* Likely also need to track down usage of EditorSpinSlider outside properties to add suffixes to it too.
2021-06-29 21:42:12 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "1,4096,1,or_greater,exp"), "set_max_distance", "get_max_distance");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attenuation", PROPERTY_HINT_EXP_EASING, "attenuation"), "set_attenuation", "get_attenuation");
|
2021-08-28 06:51:03 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
|
2020-02-20 22:58:05 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
|
2017-06-19 02:07:32 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "area_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_area_mask", "get_area_mask");
|
2017-08-25 16:58:21 +02:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("finished"));
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamPlayer2D::AudioStreamPlayer2D() {
|
2020-02-21 18:28:45 +01:00
|
|
|
AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &AudioStreamPlayer2D::_bus_layout_changed));
|
2017-06-19 02:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamPlayer2D::~AudioStreamPlayer2D() {
|
|
|
|
}
|