2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* sound_player_2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:11:45 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:45:00 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "sound_player_2d.h"
|
|
|
|
|
|
|
|
#include "servers/audio_server.h"
|
|
|
|
|
|
|
|
#include "scene/resources/surface_tool.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "servers/spatial_sound_2d_server.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void SoundPlayer2D::_notification(int p_what) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
//find the sound space
|
|
|
|
|
|
|
|
source_rid = SpatialSound2DServer::get_singleton()->source_create(get_world_2d()->get_sound_space());
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < PARAM_MAX; i++)
|
|
|
|
set_param(Param(i), params[i]);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
SpatialSound2DServer::get_singleton()->source_set_transform(source_rid, get_global_transform());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
SpatialSound2DServer::get_singleton()->source_set_transform(source_rid, get_global_transform());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (source_rid.is_valid())
|
|
|
|
SpatialSound2DServer::get_singleton()->free(source_rid);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SoundPlayer2D::set_param(Param p_param, float p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_param, PARAM_MAX);
|
|
|
|
params[p_param] = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (source_rid.is_valid())
|
2017-03-19 00:36:26 +01:00
|
|
|
SpatialSound2DServer::get_singleton()->source_set_param(source_rid, (SpatialSound2DServer::SourceParam)p_param, p_value);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float SoundPlayer2D::get_param(Param p_param) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
return params[p_param];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundPlayer2D::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_param", "param", "value"), &SoundPlayer2D::set_param);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_param", "param"), &SoundPlayer2D::get_param);
|
|
|
|
|
|
|
|
BIND_CONSTANT(PARAM_VOLUME_DB);
|
|
|
|
BIND_CONSTANT(PARAM_PITCH_SCALE);
|
|
|
|
BIND_CONSTANT(PARAM_ATTENUATION_MIN_DISTANCE);
|
|
|
|
BIND_CONSTANT(PARAM_ATTENUATION_MAX_DISTANCE);
|
|
|
|
BIND_CONSTANT(PARAM_ATTENUATION_DISTANCE_EXP);
|
|
|
|
BIND_CONSTANT(PARAM_MAX);
|
|
|
|
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_VOLUME_DB);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/pitch_scale", PROPERTY_HINT_RANGE, "0.001,32,0.001"), _SCS("set_param"), _SCS("get_param"), PARAM_PITCH_SCALE);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/attenuation/min_distance", PROPERTY_HINT_EXP_RANGE, "16,16384,1"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_MIN_DISTANCE);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/attenuation/max_distance", PROPERTY_HINT_EXP_RANGE, "16,16384,1"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_MAX_DISTANCE);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/attenuation/distance_exp", PROPERTY_HINT_EXP_EASING, "attenuation"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_DISTANCE_EXP);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SoundPlayer2D::SoundPlayer2D() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
params[PARAM_VOLUME_DB] = 0.0;
|
|
|
|
params[PARAM_PITCH_SCALE] = 1.0;
|
|
|
|
params[PARAM_ATTENUATION_MIN_DISTANCE] = 1;
|
|
|
|
params[PARAM_ATTENUATION_MAX_DISTANCE] = 2048;
|
|
|
|
params[PARAM_ATTENUATION_DISTANCE_EXP] = 1.0; //linear (and not really good)
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SoundPlayer2D::~SoundPlayer2D() {
|
|
|
|
}
|