sample_library.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*************************************************************************/
  2. /* sample_library.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "sample_library.h"
  31. bool SampleLibrary::_set(const StringName &p_name, const Variant &p_value) {
  32. if (String(p_name).begins_with("samples/")) {
  33. String name = String(p_name).get_slicec('/', 1);
  34. if (p_value.get_type() == Variant::NIL)
  35. sample_map.erase(name);
  36. else {
  37. SampleData sd;
  38. if (p_value.get_type() == Variant::OBJECT)
  39. sd.sample = p_value;
  40. else if (p_value.get_type() == Variant::DICTIONARY) {
  41. Dictionary d = p_value;
  42. ERR_FAIL_COND_V(!d.has("sample"), false);
  43. ERR_FAIL_COND_V(!d.has("pitch"), false);
  44. ERR_FAIL_COND_V(!d.has("db"), false);
  45. sd.sample = d["sample"];
  46. sd.pitch_scale = d["pitch"];
  47. sd.db = d["db"];
  48. sd.priority = d.has("priority") ? d["priority"] : Variant(0); // For libraries before priority was introduced
  49. }
  50. sample_map[name] = sd;
  51. }
  52. return true;
  53. }
  54. return false;
  55. }
  56. bool SampleLibrary::_get(const StringName &p_name, Variant &r_ret) const {
  57. if (String(p_name).begins_with("samples/")) {
  58. String name = String(p_name).get_slicec('/', 1);
  59. if (sample_map.has(name)) {
  60. Dictionary d;
  61. d["sample"] = sample_map[name].sample;
  62. d["pitch"] = sample_map[name].pitch_scale;
  63. d["db"] = sample_map[name].db;
  64. d["priority"] = sample_map[name].priority;
  65. r_ret = d;
  66. } else {
  67. return false;
  68. }
  69. return true;
  70. }
  71. return false;
  72. }
  73. void SampleLibrary::add_sample(const StringName &p_name, const Ref<Sample> &p_sample) {
  74. ERR_FAIL_COND(p_sample.is_null());
  75. SampleData sd;
  76. sd.sample = p_sample;
  77. sample_map[p_name] = sd;
  78. }
  79. Ref<Sample> SampleLibrary::get_sample(const StringName &p_name) const {
  80. ERR_FAIL_COND_V(!sample_map.has(p_name), Ref<Sample>());
  81. return sample_map[p_name].sample;
  82. }
  83. void SampleLibrary::remove_sample(const StringName &p_name) {
  84. sample_map.erase(p_name);
  85. }
  86. void SampleLibrary::get_sample_list(List<StringName> *p_samples) const {
  87. for (const Map<StringName, SampleData>::Element *E = sample_map.front(); E; E = E->next()) {
  88. p_samples->push_back(E->key());
  89. }
  90. }
  91. bool SampleLibrary::has_sample(const StringName &p_name) const {
  92. return sample_map.has(p_name);
  93. }
  94. void SampleLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  95. List<PropertyInfo> tpl;
  96. for (Map<StringName, SampleData>::Element *E = sample_map.front(); E; E = E->next()) {
  97. tpl.push_back(PropertyInfo(Variant::DICTIONARY, "samples/" + E->key(), PROPERTY_HINT_RESOURCE_TYPE, "Sample", PROPERTY_USAGE_NOEDITOR));
  98. }
  99. tpl.sort();
  100. //sort so order is kept
  101. for (List<PropertyInfo>::Element *E = tpl.front(); E; E = E->next()) {
  102. p_list->push_back(E->get());
  103. }
  104. }
  105. StringName SampleLibrary::get_sample_idx(int p_idx) const {
  106. int idx = 0;
  107. for (Map<StringName, SampleData>::Element *E = sample_map.front(); E; E = E->next()) {
  108. if (p_idx == idx)
  109. return E->key();
  110. idx++;
  111. }
  112. return "";
  113. }
  114. void SampleLibrary::sample_set_volume_db(const StringName &p_name, float p_db) {
  115. ERR_FAIL_COND(!sample_map.has(p_name));
  116. sample_map[p_name].db = p_db;
  117. }
  118. float SampleLibrary::sample_get_volume_db(const StringName &p_name) const {
  119. ERR_FAIL_COND_V(!sample_map.has(p_name), 0);
  120. return sample_map[p_name].db;
  121. }
  122. void SampleLibrary::sample_set_pitch_scale(const StringName &p_name, float p_pitch) {
  123. ERR_FAIL_COND(!sample_map.has(p_name));
  124. sample_map[p_name].pitch_scale = p_pitch;
  125. }
  126. float SampleLibrary::sample_get_pitch_scale(const StringName &p_name) const {
  127. ERR_FAIL_COND_V(!sample_map.has(p_name), 0);
  128. return sample_map[p_name].pitch_scale;
  129. }
  130. void SampleLibrary::sample_set_priority(const StringName &p_name, int p_priority) {
  131. ERR_FAIL_COND(!sample_map.has(p_name));
  132. sample_map[p_name].priority = p_priority;
  133. }
  134. int SampleLibrary::sample_get_priority(const StringName &p_name) const {
  135. ERR_FAIL_COND_V(!sample_map.has(p_name), 0);
  136. return sample_map[p_name].priority;
  137. }
  138. Array SampleLibrary::_get_sample_list() const {
  139. List<StringName> snames;
  140. get_sample_list(&snames);
  141. snames.sort_custom<StringName::AlphCompare>();
  142. Array ret;
  143. for (List<StringName>::Element *E = snames.front(); E; E = E->next()) {
  144. ret.push_back(E->get());
  145. }
  146. return ret;
  147. }
  148. void SampleLibrary::_bind_methods() {
  149. ObjectTypeDB::bind_method(_MD("add_sample", "name", "sample:Sample"), &SampleLibrary::add_sample);
  150. ObjectTypeDB::bind_method(_MD("get_sample:Sample", "name"), &SampleLibrary::get_sample);
  151. ObjectTypeDB::bind_method(_MD("has_sample", "name"), &SampleLibrary::has_sample);
  152. ObjectTypeDB::bind_method(_MD("remove_sample", "name"), &SampleLibrary::remove_sample);
  153. ObjectTypeDB::bind_method(_MD("get_sample_list"), &SampleLibrary::_get_sample_list);
  154. ObjectTypeDB::bind_method(_MD("sample_set_volume_db", "name", "db"), &SampleLibrary::sample_set_volume_db);
  155. ObjectTypeDB::bind_method(_MD("sample_get_volume_db", "name"), &SampleLibrary::sample_get_volume_db);
  156. ObjectTypeDB::bind_method(_MD("sample_set_pitch_scale", "name", "pitch"), &SampleLibrary::sample_set_pitch_scale);
  157. ObjectTypeDB::bind_method(_MD("sample_get_pitch_scale", "name"), &SampleLibrary::sample_get_pitch_scale);
  158. ObjectTypeDB::bind_method(_MD("sample_set_priority", "name", "priority"), &SampleLibrary::sample_set_priority);
  159. ObjectTypeDB::bind_method(_MD("sample_get_priority", "name"), &SampleLibrary::sample_get_priority);
  160. }
  161. SampleLibrary::SampleLibrary() {
  162. }