sample_player.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*************************************************************************/
  2. /* sample_player.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_player.h"
  31. #include "servers/audio_server.h"
  32. bool SamplePlayer::_set(const StringName &p_name, const Variant &p_value) {
  33. String name = p_name;
  34. if (name == "play/play") {
  35. if (library.is_valid()) {
  36. String what = p_value;
  37. if (what == "")
  38. stop_all();
  39. else
  40. play(what);
  41. played_back = what;
  42. }
  43. } else if (name == "config/samples")
  44. set_sample_library(p_value);
  45. else if (name == "config/polyphony")
  46. set_polyphony(p_value);
  47. else if (name.begins_with("default/")) {
  48. String what = name.right(8);
  49. if (what == "volume_db")
  50. set_default_volume_db(p_value);
  51. else if (what == "pitch_scale")
  52. set_default_pitch_scale(p_value);
  53. else if (what == "pan")
  54. _default.pan = p_value;
  55. else if (what == "depth")
  56. _default.depth = p_value;
  57. else if (what == "height")
  58. _default.height = p_value;
  59. else if (what == "filter/type")
  60. _default.filter_type = FilterType(p_value.operator int());
  61. else if (what == "filter/cutoff")
  62. _default.filter_cutoff = p_value;
  63. else if (what == "filter/resonance")
  64. _default.filter_resonance = p_value;
  65. else if (what == "filter/gain")
  66. _default.filter_gain = p_value;
  67. else if (what == "reverb_room")
  68. _default.reverb_room = ReverbRoomType(p_value.operator int());
  69. else if (what == "reverb_send")
  70. _default.reverb_send = p_value;
  71. else if (what == "chorus_send")
  72. _default.chorus_send = p_value;
  73. else
  74. return false;
  75. } else
  76. return false;
  77. return true;
  78. }
  79. bool SamplePlayer::_get(const StringName &p_name, Variant &r_ret) const {
  80. String name = p_name;
  81. if (name == "play/play") {
  82. r_ret = played_back;
  83. } else if (name == "config/polyphony") {
  84. r_ret = get_polyphony();
  85. } else if (name == "config/samples") {
  86. r_ret = get_sample_library();
  87. } else if (name.begins_with("default/")) {
  88. String what = name.right(8);
  89. if (what == "volume_db")
  90. r_ret = get_default_volume_db();
  91. else if (what == "pitch_scale")
  92. r_ret = get_default_pitch_scale();
  93. else if (what == "pan")
  94. r_ret = _default.pan;
  95. else if (what == "depth")
  96. r_ret = _default.depth;
  97. else if (what == "height")
  98. r_ret = _default.height;
  99. else if (what == "filter/type")
  100. r_ret = _default.filter_type;
  101. else if (what == "filter/cutoff")
  102. r_ret = _default.filter_cutoff;
  103. else if (what == "filter/resonance")
  104. r_ret = _default.filter_resonance;
  105. else if (what == "filter/gain")
  106. r_ret = _default.filter_gain;
  107. else if (what == "reverb_room")
  108. r_ret = _default.reverb_room;
  109. else if (what == "reverb_send")
  110. r_ret = _default.reverb_send;
  111. else if (what == "chorus_send")
  112. r_ret = _default.chorus_send;
  113. else
  114. return false;
  115. } else
  116. return false;
  117. return true;
  118. }
  119. void SamplePlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  120. String en = "";
  121. if (library.is_valid()) {
  122. List<StringName> samples;
  123. Ref<SampleLibrary> ncl = library;
  124. ncl->get_sample_list(&samples);
  125. for (List<StringName>::Element *E = samples.front(); E; E = E->next()) {
  126. en += ",";
  127. en += E->get();
  128. }
  129. }
  130. p_list->push_back(PropertyInfo(Variant::STRING, "play/play", PROPERTY_HINT_ENUM, en, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ANIMATE_AS_TRIGGER));
  131. p_list->push_back(PropertyInfo(Variant::INT, "config/polyphony", PROPERTY_HINT_RANGE, "1,256,1"));
  132. p_list->push_back(PropertyInfo(Variant::OBJECT, "config/samples", PROPERTY_HINT_RESOURCE_TYPE, "SampleLibrary"));
  133. p_list->push_back(PropertyInfo(Variant::REAL, "default/volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"));
  134. p_list->push_back(PropertyInfo(Variant::REAL, "default/pitch_scale", PROPERTY_HINT_RANGE, "0.01,48,0.01"));
  135. p_list->push_back(PropertyInfo(Variant::REAL, "default/pan", PROPERTY_HINT_RANGE, "-1,1,0.01"));
  136. p_list->push_back(PropertyInfo(Variant::REAL, "default/depth", PROPERTY_HINT_RANGE, "-1,1,0.01"));
  137. p_list->push_back(PropertyInfo(Variant::REAL, "default/height", PROPERTY_HINT_RANGE, "-1,1,0.01"));
  138. p_list->push_back(PropertyInfo(Variant::INT, "default/filter/type", PROPERTY_HINT_ENUM, "Disabled,Lowpass,Bandpass,Highpass,Notch,Peak,BandLimit,LowShelf,HighShelf"));
  139. p_list->push_back(PropertyInfo(Variant::REAL, "default/filter/cutoff", PROPERTY_HINT_RANGE, "20,16384.0,0.01"));
  140. p_list->push_back(PropertyInfo(Variant::REAL, "default/filter/resonance", PROPERTY_HINT_RANGE, "0,4,0.01"));
  141. p_list->push_back(PropertyInfo(Variant::REAL, "default/filter/gain", PROPERTY_HINT_RANGE, "0,2,0.01"));
  142. p_list->push_back(PropertyInfo(Variant::INT, "default/reverb_room", PROPERTY_HINT_ENUM, "Small,Medium,Large,Hall"));
  143. p_list->push_back(PropertyInfo(Variant::REAL, "default/reverb_send", PROPERTY_HINT_RANGE, "0,1,0.01"));
  144. p_list->push_back(PropertyInfo(Variant::REAL, "default/chorus_send", PROPERTY_HINT_RANGE, "0,1,0.01"));
  145. }
  146. SamplePlayer::Voice::Voice() {
  147. voice = AudioServer::get_singleton()->voice_create();
  148. clear();
  149. }
  150. void SamplePlayer::Voice::clear() {
  151. check = 0;
  152. mix_rate = 44100;
  153. volume = 1;
  154. pan = 0;
  155. pan_depth = 0;
  156. pan_height = 0;
  157. filter_type = FILTER_NONE;
  158. filter_cutoff = 0;
  159. filter_resonance = 0;
  160. chorus_send = 0;
  161. reverb_room = REVERB_HALL;
  162. reverb_send = 0;
  163. active = false;
  164. priority = 0;
  165. }
  166. SamplePlayer::Voice::~Voice() {
  167. AudioServer::get_singleton()->free(voice);
  168. }
  169. void SamplePlayer::set_polyphony(int p_voice_count) {
  170. ERR_FAIL_COND(p_voice_count < 1 || p_voice_count > 0xFFFE);
  171. voices.resize(p_voice_count);
  172. }
  173. int SamplePlayer::get_polyphony() const {
  174. return voices.size();
  175. }
  176. SamplePlayer::VoiceID SamplePlayer::play(const String &p_name, bool unique) {
  177. if (library.is_null())
  178. return INVALID_VOICE_ID;
  179. ERR_FAIL_COND_V(!library->has_sample(p_name), INVALID_VOICE_ID);
  180. Ref<Sample> sample = library->get_sample(p_name);
  181. float vol_change = library->sample_get_volume_db(p_name);
  182. float pitch_change = library->sample_get_pitch_scale(p_name);
  183. int priority = library->sample_get_priority(p_name);
  184. last_check++;
  185. const int num_voices = voices.size();
  186. bool free_found = false;
  187. int lowest_priority_voice = -1;
  188. int lowest_priority = 0x7FFFFFFF;
  189. for (int i = 0; i < num_voices; i++) {
  190. const int candidate = (last_id + 1 + i) % num_voices;
  191. const Voice &v = voices[candidate];
  192. if (!(v.active && AudioServer::get_singleton()->voice_is_active(v.voice))) {
  193. free_found = true;
  194. last_id = candidate;
  195. break;
  196. }
  197. if (v.priority <= lowest_priority) {
  198. lowest_priority = v.priority;
  199. lowest_priority_voice = candidate;
  200. }
  201. }
  202. if (!free_found) {
  203. if (lowest_priority > priority) {
  204. return INVALID_VOICE_ID;
  205. } else {
  206. last_id = lowest_priority_voice;
  207. }
  208. }
  209. Voice &v = voices[last_id];
  210. v.clear();
  211. v.priority = priority;
  212. v.mix_rate = sample->get_mix_rate() * (_default.pitch_scale * pitch_change);
  213. v.sample_mix_rate = sample->get_mix_rate();
  214. v.check = last_check;
  215. v.volume = Math::db2linear(_default.volume_db + vol_change);
  216. v.pan = _default.pan;
  217. v.pan_depth = _default.depth;
  218. v.pan_height = _default.height;
  219. v.filter_type = _default.filter_type;
  220. v.filter_cutoff = _default.filter_cutoff;
  221. v.filter_resonance = _default.filter_resonance;
  222. v.filter_gain = _default.filter_gain;
  223. v.chorus_send = _default.chorus_send;
  224. v.reverb_room = _default.reverb_room;
  225. v.reverb_send = _default.reverb_send;
  226. AudioServer::get_singleton()->voice_play(v.voice, sample->get_rid());
  227. AudioServer::get_singleton()->voice_set_mix_rate(v.voice, v.mix_rate);
  228. AudioServer::get_singleton()->voice_set_volume(v.voice, v.volume);
  229. AudioServer::get_singleton()->voice_set_pan(v.voice, v.pan, v.pan_depth, v.pan_height);
  230. AudioServer::get_singleton()->voice_set_filter(v.voice, (AudioServer::FilterType)v.filter_type, v.filter_cutoff, v.filter_resonance, v.filter_gain);
  231. AudioServer::get_singleton()->voice_set_chorus(v.voice, v.chorus_send);
  232. AudioServer::get_singleton()->voice_set_reverb(v.voice, (AudioServer::ReverbRoomType)v.reverb_room, v.reverb_send);
  233. v.active = true;
  234. if (unique) {
  235. for (int i = 0; i < voices.size(); i++) {
  236. if (!voices[i].active || uint32_t(i) == last_id)
  237. continue;
  238. AudioServer::get_singleton()->voice_stop(voices[i].voice);
  239. voices[i].clear();
  240. }
  241. }
  242. return last_id | (last_check << 16);
  243. }
  244. void SamplePlayer::stop_all() {
  245. for (int i = 0; i < voices.size(); i++) {
  246. if (!voices[i].active)
  247. continue;
  248. AudioServer::get_singleton()->voice_stop(voices[i].voice);
  249. voices[i].clear();
  250. }
  251. }
  252. #define _GET_VOICE \
  253. uint32_t voice = p_voice & 0xFFFF; \
  254. ERR_FAIL_COND(voice >= (uint32_t)voices.size()); \
  255. Voice &v = voices[voice]; \
  256. if (v.check != uint32_t(p_voice >> 16)) \
  257. return; \
  258. ERR_FAIL_COND(!v.active);
  259. void SamplePlayer::stop(VoiceID p_voice) {
  260. _GET_VOICE
  261. AudioServer::get_singleton()->voice_stop(v.voice);
  262. v.active = false;
  263. }
  264. void SamplePlayer::set_mix_rate(VoiceID p_voice, int p_mix_rate) {
  265. _GET_VOICE
  266. v.mix_rate = p_mix_rate;
  267. AudioServer::get_singleton()->voice_set_mix_rate(v.voice, v.mix_rate);
  268. }
  269. void SamplePlayer::set_pitch_scale(VoiceID p_voice, float p_pitch_scale) {
  270. _GET_VOICE
  271. v.mix_rate = v.sample_mix_rate * p_pitch_scale;
  272. AudioServer::get_singleton()->voice_set_mix_rate(v.voice, v.mix_rate);
  273. }
  274. void SamplePlayer::set_volume(VoiceID p_voice, float p_volume) {
  275. _GET_VOICE
  276. v.volume = p_volume;
  277. AudioServer::get_singleton()->voice_set_volume(v.voice, v.volume);
  278. }
  279. void SamplePlayer::set_volume_db(VoiceID p_voice, float p_db) {
  280. //@TODO handle 0 volume as -80db or something
  281. _GET_VOICE
  282. v.volume = Math::db2linear(p_db);
  283. AudioServer::get_singleton()->voice_set_volume(v.voice, v.volume);
  284. }
  285. void SamplePlayer::set_pan(VoiceID p_voice, float p_pan, float p_pan_depth, float p_pan_height) {
  286. _GET_VOICE
  287. v.pan = p_pan;
  288. v.pan_depth = p_pan_depth;
  289. v.pan_height = p_pan_height;
  290. AudioServer::get_singleton()->voice_set_pan(v.voice, v.pan, v.pan_depth, v.pan_height);
  291. }
  292. void SamplePlayer::set_filter(VoiceID p_voice, FilterType p_filter, float p_cutoff, float p_resonance, float p_gain) {
  293. _GET_VOICE
  294. v.filter_type = p_filter;
  295. v.filter_cutoff = p_cutoff;
  296. v.filter_resonance = p_resonance;
  297. v.filter_gain = p_gain;
  298. AudioServer::get_singleton()->voice_set_filter(v.voice, (AudioServer::FilterType)p_filter, p_cutoff, p_resonance);
  299. }
  300. void SamplePlayer::set_chorus(VoiceID p_voice, float p_send) {
  301. _GET_VOICE
  302. v.chorus_send = p_send;
  303. AudioServer::get_singleton()->voice_set_chorus(v.voice, p_send);
  304. }
  305. void SamplePlayer::set_reverb(VoiceID p_voice, ReverbRoomType p_room, float p_send) {
  306. _GET_VOICE
  307. v.reverb_room = p_room;
  308. v.reverb_send = p_send;
  309. AudioServer::get_singleton()->voice_set_reverb(v.voice, (AudioServer::ReverbRoomType)p_room, p_send);
  310. }
  311. #define _GET_VOICE_V(m_ret) \
  312. uint32_t voice = p_voice & 0xFFFF; \
  313. ERR_FAIL_COND_V(voice >= (uint32_t)voices.size(), m_ret); \
  314. const Voice &v = voices[voice]; \
  315. if (v.check != (p_voice >> 16)) \
  316. return m_ret; \
  317. ERR_FAIL_COND_V(!v.active, m_ret);
  318. int SamplePlayer::get_mix_rate(VoiceID p_voice) const {
  319. _GET_VOICE_V(0);
  320. return v.mix_rate;
  321. }
  322. float SamplePlayer::get_pitch_scale(VoiceID p_voice) const {
  323. _GET_VOICE_V(0);
  324. return v.sample_mix_rate / (float)v.mix_rate;
  325. }
  326. float SamplePlayer::get_volume(VoiceID p_voice) const {
  327. _GET_VOICE_V(0);
  328. return v.volume;
  329. }
  330. float SamplePlayer::get_volume_db(VoiceID p_voice) const {
  331. _GET_VOICE_V(0);
  332. return Math::linear2db(v.volume);
  333. }
  334. float SamplePlayer::get_pan(VoiceID p_voice) const {
  335. _GET_VOICE_V(0);
  336. return v.pan;
  337. }
  338. float SamplePlayer::get_pan_depth(VoiceID p_voice) const {
  339. _GET_VOICE_V(0);
  340. return v.pan_depth;
  341. }
  342. float SamplePlayer::get_pan_height(VoiceID p_voice) const {
  343. _GET_VOICE_V(0);
  344. return v.pan_height;
  345. }
  346. SamplePlayer::FilterType SamplePlayer::get_filter_type(VoiceID p_voice) const {
  347. _GET_VOICE_V(FILTER_NONE);
  348. return v.filter_type;
  349. }
  350. float SamplePlayer::get_filter_cutoff(VoiceID p_voice) const {
  351. _GET_VOICE_V(0);
  352. return v.filter_cutoff;
  353. }
  354. float SamplePlayer::get_filter_resonance(VoiceID p_voice) const {
  355. _GET_VOICE_V(0);
  356. return v.filter_resonance;
  357. }
  358. float SamplePlayer::get_filter_gain(VoiceID p_voice) const {
  359. _GET_VOICE_V(0);
  360. return v.filter_gain;
  361. }
  362. float SamplePlayer::get_chorus(VoiceID p_voice) const {
  363. _GET_VOICE_V(0);
  364. return v.chorus_send;
  365. }
  366. SamplePlayer::ReverbRoomType SamplePlayer::get_reverb_room(VoiceID p_voice) const {
  367. _GET_VOICE_V(REVERB_SMALL);
  368. return v.reverb_room;
  369. }
  370. float SamplePlayer::get_reverb(VoiceID p_voice) const {
  371. _GET_VOICE_V(0);
  372. return v.reverb_send;
  373. }
  374. bool SamplePlayer::is_voice_active(VoiceID p_voice) const {
  375. _GET_VOICE_V(false);
  376. return v.active && AudioServer::get_singleton()->voice_is_active(v.voice);
  377. }
  378. bool SamplePlayer::is_active() const {
  379. for (int i = 0; i < voices.size(); i++) {
  380. if (voices[i].active && AudioServer::get_singleton()->voice_is_active(voices[i].voice))
  381. return true;
  382. }
  383. return false;
  384. }
  385. void SamplePlayer::set_sample_library(const Ref<SampleLibrary> &p_library) {
  386. library = p_library;
  387. _change_notify();
  388. }
  389. Ref<SampleLibrary> SamplePlayer::get_sample_library() const {
  390. return library;
  391. }
  392. void SamplePlayer::set_default_pitch_scale(float p_pitch_scale) {
  393. _default.pitch_scale = p_pitch_scale;
  394. }
  395. void SamplePlayer::set_default_volume(float p_volume) {
  396. _default.volume_db = Math::linear2db(p_volume);
  397. }
  398. void SamplePlayer::set_default_volume_db(float p_db) {
  399. _default.volume_db = p_db;
  400. }
  401. void SamplePlayer::set_default_pan(float p_pan, float p_pan_depth, float p_pan_height) {
  402. _default.pan = p_pan;
  403. _default.depth = p_pan_depth;
  404. _default.height = p_pan_height;
  405. }
  406. void SamplePlayer::set_default_filter(FilterType p_filter, float p_cutoff, float p_resonance, float p_gain) {
  407. _default.filter_type = p_filter;
  408. _default.filter_cutoff = p_cutoff;
  409. _default.filter_resonance = p_resonance;
  410. _default.filter_gain = p_gain;
  411. }
  412. void SamplePlayer::set_default_chorus(float p_send) {
  413. _default.chorus_send = p_send;
  414. }
  415. void SamplePlayer::set_default_reverb(ReverbRoomType p_room, float p_send) {
  416. _default.reverb_room = p_room;
  417. _default.reverb_send = p_send;
  418. }
  419. float SamplePlayer::get_default_volume() const {
  420. return Math::db2linear(_default.volume_db);
  421. }
  422. float SamplePlayer::get_default_volume_db() const {
  423. return _default.volume_db;
  424. }
  425. float SamplePlayer::get_default_pitch_scale() const {
  426. return _default.pitch_scale;
  427. }
  428. float SamplePlayer::get_default_pan() const {
  429. return _default.pan;
  430. }
  431. float SamplePlayer::get_default_pan_depth() const {
  432. return _default.depth;
  433. }
  434. float SamplePlayer::get_default_pan_height() const {
  435. return _default.height;
  436. }
  437. SamplePlayer::FilterType SamplePlayer::get_default_filter_type() const {
  438. return _default.filter_type;
  439. }
  440. float SamplePlayer::get_default_filter_cutoff() const {
  441. return _default.filter_cutoff;
  442. }
  443. float SamplePlayer::get_default_filter_resonance() const {
  444. return _default.filter_resonance;
  445. }
  446. float SamplePlayer::get_default_filter_gain() const {
  447. return _default.filter_gain;
  448. }
  449. float SamplePlayer::get_default_chorus() const {
  450. return _default.chorus_send;
  451. }
  452. SamplePlayer::ReverbRoomType SamplePlayer::get_default_reverb_room() const {
  453. return _default.reverb_room;
  454. }
  455. float SamplePlayer::get_default_reverb() const {
  456. return _default.reverb_send;
  457. }
  458. String SamplePlayer::get_configuration_warning() const {
  459. if (library.is_null()) {
  460. return TTR("A SampleLibrary resource must be created or set in the 'samples' property in order for SamplePlayer to play sound.");
  461. }
  462. return String();
  463. }
  464. void SamplePlayer::_bind_methods() {
  465. ObjectTypeDB::bind_method(_MD("set_sample_library", "library:SampleLibrary"), &SamplePlayer::set_sample_library);
  466. ObjectTypeDB::bind_method(_MD("get_sample_library:SampleLibrary"), &SamplePlayer::get_sample_library);
  467. ObjectTypeDB::bind_method(_MD("set_polyphony", "max_voices"), &SamplePlayer::set_polyphony);
  468. ObjectTypeDB::bind_method(_MD("get_polyphony"), &SamplePlayer::get_polyphony);
  469. ObjectTypeDB::bind_method(_MD("play", "name", "unique"), &SamplePlayer::play, DEFVAL(false));
  470. ObjectTypeDB::bind_method(_MD("stop", "voice"), &SamplePlayer::stop);
  471. ObjectTypeDB::bind_method(_MD("stop_all"), &SamplePlayer::stop_all);
  472. ObjectTypeDB::bind_method(_MD("set_mix_rate", "voice", "hz"), &SamplePlayer::set_mix_rate);
  473. ObjectTypeDB::bind_method(_MD("set_pitch_scale", "voice", "ratio"), &SamplePlayer::set_pitch_scale);
  474. ObjectTypeDB::bind_method(_MD("set_volume", "voice", "volume"), &SamplePlayer::set_volume);
  475. ObjectTypeDB::bind_method(_MD("set_volume_db", "voice", "db"), &SamplePlayer::set_volume_db);
  476. ObjectTypeDB::bind_method(_MD("set_pan", "voice", "pan", "depth", "height"), &SamplePlayer::set_pan, DEFVAL(0), DEFVAL(0));
  477. ObjectTypeDB::bind_method(_MD("set_filter", "voice", "type", "cutoff_hz", "resonance", "gain"), &SamplePlayer::set_filter, DEFVAL(0));
  478. ObjectTypeDB::bind_method(_MD("set_chorus", "voice", "send"), &SamplePlayer::set_chorus);
  479. ObjectTypeDB::bind_method(_MD("set_reverb", "voice", "room_type", "send"), &SamplePlayer::set_reverb);
  480. ObjectTypeDB::bind_method(_MD("get_mix_rate", "voice"), &SamplePlayer::get_mix_rate);
  481. ObjectTypeDB::bind_method(_MD("get_pitch_scale", "voice"), &SamplePlayer::get_pitch_scale);
  482. ObjectTypeDB::bind_method(_MD("get_volume", "voice"), &SamplePlayer::get_volume);
  483. ObjectTypeDB::bind_method(_MD("get_volume_db", "voice"), &SamplePlayer::get_volume_db);
  484. ObjectTypeDB::bind_method(_MD("get_pan", "voice"), &SamplePlayer::get_pan);
  485. ObjectTypeDB::bind_method(_MD("get_pan_depth", "voice"), &SamplePlayer::get_pan_depth);
  486. ObjectTypeDB::bind_method(_MD("get_pan_height", "voice"), &SamplePlayer::get_pan_height);
  487. ObjectTypeDB::bind_method(_MD("get_filter_type", "voice"), &SamplePlayer::get_filter_type);
  488. ObjectTypeDB::bind_method(_MD("get_filter_cutoff", "voice"), &SamplePlayer::get_filter_cutoff);
  489. ObjectTypeDB::bind_method(_MD("get_filter_resonance", "voice"), &SamplePlayer::get_filter_resonance);
  490. ObjectTypeDB::bind_method(_MD("get_filter_gain", "voice"), &SamplePlayer::get_filter_gain);
  491. ObjectTypeDB::bind_method(_MD("get_chorus", "voice"), &SamplePlayer::get_chorus);
  492. ObjectTypeDB::bind_method(_MD("get_reverb_room", "voice"), &SamplePlayer::get_reverb_room);
  493. ObjectTypeDB::bind_method(_MD("get_reverb", "voice"), &SamplePlayer::get_reverb);
  494. ObjectTypeDB::bind_method(_MD("set_default_pitch_scale", "ratio"), &SamplePlayer::set_default_pitch_scale);
  495. ObjectTypeDB::bind_method(_MD("set_default_volume", "volume"), &SamplePlayer::set_default_volume);
  496. ObjectTypeDB::bind_method(_MD("set_default_volume_db", "db"), &SamplePlayer::set_default_volume_db);
  497. ObjectTypeDB::bind_method(_MD("set_default_pan", "pan", "depth", "height"), &SamplePlayer::set_default_pan, DEFVAL(0), DEFVAL(0));
  498. ObjectTypeDB::bind_method(_MD("set_default_filter", "type", "cutoff_hz", "resonance", "gain"), &SamplePlayer::set_default_filter, DEFVAL(0));
  499. ObjectTypeDB::bind_method(_MD("set_default_chorus", "send"), &SamplePlayer::set_default_chorus);
  500. ObjectTypeDB::bind_method(_MD("set_default_reverb", "room_type", "send"), &SamplePlayer::set_default_reverb);
  501. ObjectTypeDB::bind_method(_MD("get_default_pitch_scale"), &SamplePlayer::get_default_pitch_scale);
  502. ObjectTypeDB::bind_method(_MD("get_default_volume"), &SamplePlayer::get_default_volume);
  503. ObjectTypeDB::bind_method(_MD("get_default_volume_db"), &SamplePlayer::get_default_volume_db);
  504. ObjectTypeDB::bind_method(_MD("get_default_pan"), &SamplePlayer::get_default_pan);
  505. ObjectTypeDB::bind_method(_MD("get_default_pan_depth"), &SamplePlayer::get_default_pan_depth);
  506. ObjectTypeDB::bind_method(_MD("get_default_pan_height"), &SamplePlayer::get_default_pan_height);
  507. ObjectTypeDB::bind_method(_MD("get_default_filter_type"), &SamplePlayer::get_default_filter_type);
  508. ObjectTypeDB::bind_method(_MD("get_default_filter_cutoff"), &SamplePlayer::get_default_filter_cutoff);
  509. ObjectTypeDB::bind_method(_MD("get_default_filter_resonance"), &SamplePlayer::get_default_filter_resonance);
  510. ObjectTypeDB::bind_method(_MD("get_default_filter_gain"), &SamplePlayer::get_default_filter_gain);
  511. ObjectTypeDB::bind_method(_MD("get_default_chorus"), &SamplePlayer::get_default_chorus);
  512. ObjectTypeDB::bind_method(_MD("get_default_reverb_room"), &SamplePlayer::get_default_reverb_room);
  513. ObjectTypeDB::bind_method(_MD("get_default_reverb"), &SamplePlayer::get_default_reverb);
  514. ObjectTypeDB::bind_method(_MD("is_active"), &SamplePlayer::is_active);
  515. ObjectTypeDB::bind_method(_MD("is_voice_active", "voice"), &SamplePlayer::is_voice_active);
  516. BIND_CONSTANT(FILTER_NONE);
  517. BIND_CONSTANT(FILTER_LOWPASS);
  518. BIND_CONSTANT(FILTER_BANDPASS);
  519. BIND_CONSTANT(FILTER_HIPASS);
  520. BIND_CONSTANT(FILTER_NOTCH);
  521. BIND_CONSTANT(FILTER_PEAK);
  522. BIND_CONSTANT(FILTER_BANDLIMIT); ///< cutoff is LP resonace is HP
  523. BIND_CONSTANT(FILTER_LOW_SHELF);
  524. BIND_CONSTANT(FILTER_HIGH_SHELF);
  525. BIND_CONSTANT(REVERB_SMALL);
  526. BIND_CONSTANT(REVERB_MEDIUM);
  527. BIND_CONSTANT(REVERB_LARGE);
  528. BIND_CONSTANT(REVERB_HALL);
  529. BIND_CONSTANT(INVALID_VOICE_ID);
  530. }
  531. SamplePlayer::SamplePlayer() {
  532. voices.resize(1);
  533. _default.pitch_scale = 1;
  534. _default.volume_db = 0;
  535. _default.pan = 0;
  536. _default.depth = 0;
  537. _default.height = 0;
  538. _default.filter_type = FILTER_NONE;
  539. _default.filter_cutoff = 5000;
  540. _default.filter_resonance = 1;
  541. _default.filter_gain = 1;
  542. _default.chorus_send = 0;
  543. _default.reverb_room = REVERB_LARGE;
  544. _default.reverb_send = 0;
  545. last_id = 0;
  546. last_check = 0;
  547. }
  548. SamplePlayer::~SamplePlayer() {
  549. }