1 /*************************************************************************/
2 /*  sound_player_2d.cpp                                                  */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #include "sound_player_2d.h"
31 
32 #include "servers/audio_server.h"
33 
34 #include "scene/resources/surface_tool.h"
35 #include "servers/spatial_sound_2d_server.h"
36 
_notification(int p_what)37 void SoundPlayer2D::_notification(int p_what) {
38 
39 	switch (p_what) {
40 
41 		case NOTIFICATION_ENTER_TREE: {
42 			//find the sound space
43 
44 			source_rid = SpatialSound2DServer::get_singleton()->source_create(get_world_2d()->get_sound_space());
45 
46 			for (int i = 0; i < PARAM_MAX; i++)
47 				set_param(Param(i), params[i]);
48 
49 			SpatialSound2DServer::get_singleton()->source_set_transform(source_rid, get_global_transform());
50 
51 		} break;
52 		case NOTIFICATION_TRANSFORM_CHANGED: {
53 
54 			SpatialSound2DServer::get_singleton()->source_set_transform(source_rid, get_global_transform());
55 
56 		} break;
57 		case NOTIFICATION_EXIT_TREE: {
58 
59 			if (source_rid.is_valid())
60 				SpatialSound2DServer::get_singleton()->free(source_rid);
61 
62 		} break;
63 	}
64 }
65 
set_param(Param p_param,float p_value)66 void SoundPlayer2D::set_param(Param p_param, float p_value) {
67 
68 	ERR_FAIL_INDEX(p_param, PARAM_MAX);
69 	params[p_param] = p_value;
70 	if (source_rid.is_valid())
71 		SpatialSound2DServer::get_singleton()->source_set_param(source_rid, (SpatialSound2DServer::SourceParam)p_param, p_value);
72 }
73 
get_param(Param p_param) const74 float SoundPlayer2D::get_param(Param p_param) const {
75 
76 	ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
77 	return params[p_param];
78 }
79 
_bind_methods()80 void SoundPlayer2D::_bind_methods() {
81 
82 	ObjectTypeDB::bind_method(_MD("set_param", "param", "value"), &SoundPlayer2D::set_param);
83 	ObjectTypeDB::bind_method(_MD("get_param", "param"), &SoundPlayer2D::get_param);
84 
85 	BIND_CONSTANT(PARAM_VOLUME_DB);
86 	BIND_CONSTANT(PARAM_PITCH_SCALE);
87 	BIND_CONSTANT(PARAM_ATTENUATION_MIN_DISTANCE);
88 	BIND_CONSTANT(PARAM_ATTENUATION_MAX_DISTANCE);
89 	BIND_CONSTANT(PARAM_ATTENUATION_DISTANCE_EXP);
90 	BIND_CONSTANT(PARAM_MAX);
91 
92 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_VOLUME_DB);
93 	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);
94 	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);
95 	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);
96 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/attenuation/distance_exp", PROPERTY_HINT_EXP_EASING, "attenuation"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_DISTANCE_EXP);
97 }
98 
SoundPlayer2D()99 SoundPlayer2D::SoundPlayer2D() {
100 
101 	params[PARAM_VOLUME_DB] = 0.0;
102 	params[PARAM_PITCH_SCALE] = 1.0;
103 	params[PARAM_ATTENUATION_MIN_DISTANCE] = 1;
104 	params[PARAM_ATTENUATION_MAX_DISTANCE] = 2048;
105 	params[PARAM_ATTENUATION_DISTANCE_EXP] = 1.0; //linear (and not really good)
106 }
107 
~SoundPlayer2D()108 SoundPlayer2D::~SoundPlayer2D() {
109 }
110