sample_library.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* sample_library.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "sample_library.h"
  30. bool SampleLibrary::_set(const StringName& p_name, const Variant& p_value) {
  31. if (String(p_name).begins_with("samples/")) {
  32. String name=String(p_name).get_slice("/",1);
  33. if (p_value.get_type()==Variant::NIL)
  34. sample_map.erase(name);
  35. else {
  36. SampleData sd;
  37. if (p_value.get_type()==Variant::OBJECT)
  38. sd.sample=p_value;
  39. else if (p_value.get_type()==Variant::DICTIONARY) {
  40. Dictionary d = p_value;
  41. ERR_FAIL_COND_V(!d.has("sample"),false);
  42. ERR_FAIL_COND_V(!d.has("pitch"),false);
  43. ERR_FAIL_COND_V(!d.has("db"),false);
  44. sd.sample=d["sample"];
  45. sd.pitch_scale=d["pitch"];
  46. sd.db=d["db"];
  47. }
  48. sample_map[name]=sd;
  49. }
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool SampleLibrary::_get(const StringName& p_name,Variant &r_ret) const {
  55. if (String(p_name).begins_with("samples/")) {
  56. String name=String(p_name).get_slice("/",1);
  57. if(sample_map.has(name)) {
  58. Dictionary d;
  59. d["sample"]=sample_map[name].sample;
  60. d["pitch"]=sample_map[name].pitch_scale;
  61. d["db"]=sample_map[name].db;
  62. r_ret=d;
  63. } else {
  64. return false;
  65. }
  66. return true;
  67. }
  68. return false;
  69. }
  70. void SampleLibrary::add_sample(const StringName& p_name, const Ref<Sample>& p_sample) {
  71. ERR_FAIL_COND(p_sample.is_null());
  72. SampleData sd;
  73. sd.sample=p_sample;
  74. sample_map[p_name]=sd;
  75. }
  76. Ref<Sample> SampleLibrary::get_sample(const StringName& p_name) const {
  77. ERR_FAIL_COND_V(!sample_map.has(p_name),Ref<Sample>());
  78. return sample_map[p_name].sample;
  79. }
  80. void SampleLibrary::remove_sample(const StringName& p_name) {
  81. sample_map.erase(p_name);
  82. }
  83. void SampleLibrary::get_sample_list(List<StringName> *p_samples) {
  84. for(Map<StringName,SampleData >::Element *E=sample_map.front();E;E=E->next()) {
  85. p_samples->push_back(E->key());
  86. }
  87. }
  88. bool SampleLibrary::has_sample(const StringName& p_name) const {
  89. return sample_map.has(p_name);
  90. }
  91. void SampleLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  92. for(Map<StringName,SampleData>::Element *E=sample_map.front();E;E=E->next()) {
  93. p_list->push_back( PropertyInfo( Variant::DICTIONARY, "samples/"+E->key(),PROPERTY_HINT_RESOURCE_TYPE,"Sample",PROPERTY_USAGE_NOEDITOR ) );
  94. }
  95. }
  96. StringName SampleLibrary::get_sample_idx(int p_idx) const {
  97. int idx=0;
  98. for (Map<StringName, SampleData >::Element *E=sample_map.front();E;E=E->next()) {
  99. if (p_idx==idx)
  100. return E->key();
  101. idx++;
  102. }
  103. return "";
  104. }
  105. void SampleLibrary::sample_set_volume_db(const StringName& p_name, float p_db) {
  106. ERR_FAIL_COND( !sample_map.has(p_name) );
  107. sample_map[p_name].db=p_db;
  108. }
  109. float SampleLibrary::sample_get_volume_db(const StringName& p_name) const{
  110. ERR_FAIL_COND_V( !sample_map.has(p_name),0 );
  111. return sample_map[p_name].db;
  112. }
  113. void SampleLibrary::sample_set_pitch_scale(const StringName& p_name, float p_pitch){
  114. ERR_FAIL_COND( !sample_map.has(p_name) );
  115. sample_map[p_name].pitch_scale=p_pitch;
  116. }
  117. float SampleLibrary::sample_get_pitch_scale(const StringName& p_name) const{
  118. ERR_FAIL_COND_V( !sample_map.has(p_name),0 );
  119. return sample_map[p_name].pitch_scale;
  120. }
  121. void SampleLibrary::_bind_methods() {
  122. ObjectTypeDB::bind_method(_MD("add_sample","name","sample:Sample"),&SampleLibrary::add_sample );
  123. ObjectTypeDB::bind_method(_MD("get_sample:Sample","name"),&SampleLibrary::get_sample );
  124. ObjectTypeDB::bind_method(_MD("has_sample","name"),&SampleLibrary::has_sample );
  125. ObjectTypeDB::bind_method(_MD("remove_sample","name"),&SampleLibrary::remove_sample );
  126. ObjectTypeDB::bind_method(_MD("sample_set_volume_db","name","db"),&SampleLibrary::sample_set_volume_db );
  127. ObjectTypeDB::bind_method(_MD("sample_get_volume_db","name"),&SampleLibrary::sample_get_volume_db );
  128. ObjectTypeDB::bind_method(_MD("sample_set_pitch_scale","name","pitch"),&SampleLibrary::sample_set_pitch_scale );
  129. ObjectTypeDB::bind_method(_MD("sample_get_pitch_scale","name"),&SampleLibrary::sample_get_pitch_scale );
  130. }
  131. SampleLibrary::SampleLibrary()
  132. {
  133. }