audio_server.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*************************************************************************/
  2. /* audio_server.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 "audio_server.h"
  31. #include "globals.h"
  32. void AudioMixer::audio_mixer_chunk_call(int p_frames) {
  33. AudioServer::get_singleton()->audio_mixer_chunk_callback(p_frames);
  34. }
  35. AudioMixer *AudioServer::EventStream::get_mixer() const {
  36. return AudioServer::get_singleton()->get_mixer();
  37. }
  38. AudioServer *AudioServer::singleton = NULL;
  39. AudioServer *AudioServer::get_singleton() {
  40. return singleton;
  41. }
  42. void AudioServer::sample_set_signed_data(RID p_sample, const DVector<float> &p_buffer) {
  43. SampleFormat format = sample_get_format(p_sample);
  44. ERR_EXPLAIN("IMA ADPCM is not supported.");
  45. ERR_FAIL_COND(format == SAMPLE_FORMAT_IMA_ADPCM);
  46. int len = p_buffer.size();
  47. ERR_FAIL_COND(len == 0);
  48. DVector<uint8_t> data;
  49. DVector<uint8_t>::Write w;
  50. DVector<float>::Read r = p_buffer.read();
  51. switch (format) {
  52. case SAMPLE_FORMAT_PCM8: {
  53. data.resize(len);
  54. w = data.write();
  55. int8_t *samples8 = (int8_t *)w.ptr();
  56. for (int i = 0; i < len; i++) {
  57. float sample = Math::floor(r[i] * (1 << 8));
  58. if (sample < -128)
  59. sample = -128;
  60. else if (sample > 127)
  61. sample = 127;
  62. samples8[i] = sample;
  63. }
  64. } break;
  65. case SAMPLE_FORMAT_PCM16: {
  66. data.resize(len * 2);
  67. w = data.write();
  68. int16_t *samples16 = (int16_t *)w.ptr();
  69. for (int i = 0; i < len; i++) {
  70. float sample = Math::floor(r[i] * (1 << 16));
  71. if (sample < -32768)
  72. sample = -32768;
  73. else if (sample > 32767)
  74. sample = 32767;
  75. samples16[i] = sample;
  76. }
  77. } break;
  78. }
  79. w = DVector<uint8_t>::Write();
  80. sample_set_data(p_sample, data);
  81. }
  82. void AudioServer::_bind_methods() {
  83. ObjectTypeDB::bind_method(_MD("sample_create", "format", "stereo", "length"), &AudioServer::sample_create);
  84. ObjectTypeDB::bind_method(_MD("sample_set_description", "sample", "description"), &AudioServer::sample_set_description);
  85. ObjectTypeDB::bind_method(_MD("sample_get_description", "sample"), &AudioServer::sample_get_description);
  86. ObjectTypeDB::bind_method(_MD("sample_get_format", "sample"), &AudioServer::sample_get_format);
  87. ObjectTypeDB::bind_method(_MD("sample_is_stereo", "sample"), &AudioServer::sample_is_stereo);
  88. ObjectTypeDB::bind_method(_MD("sample_get_length", "sample"), &AudioServer::sample_get_length);
  89. ObjectTypeDB::bind_method(_MD("sample_set_signed_data", "sample", "data"), &AudioServer::sample_set_signed_data);
  90. ObjectTypeDB::bind_method(_MD("sample_set_data", "sample", "data"), &AudioServer::sample_set_data);
  91. ObjectTypeDB::bind_method(_MD("sample_get_data", "sample"), &AudioServer::sample_get_data);
  92. ObjectTypeDB::bind_method(_MD("sample_set_mix_rate", "sample", "mix_rate"), &AudioServer::sample_set_mix_rate);
  93. ObjectTypeDB::bind_method(_MD("sample_get_mix_rate", "sample"), &AudioServer::sample_get_mix_rate);
  94. ObjectTypeDB::bind_method(_MD("sample_set_loop_format", "sample", "loop_format"), &AudioServer::sample_set_loop_format);
  95. ObjectTypeDB::bind_method(_MD("sample_get_loop_format", "sample"), &AudioServer::sample_get_loop_format);
  96. ObjectTypeDB::bind_method(_MD("sample_set_loop_begin", "sample", "pos"), &AudioServer::sample_set_loop_begin);
  97. ObjectTypeDB::bind_method(_MD("sample_get_loop_begin", "sample"), &AudioServer::sample_get_loop_begin);
  98. ObjectTypeDB::bind_method(_MD("sample_set_loop_end", "sample", "pos"), &AudioServer::sample_set_loop_end);
  99. ObjectTypeDB::bind_method(_MD("sample_get_loop_end", "sample"), &AudioServer::sample_get_loop_end);
  100. ObjectTypeDB::bind_method(_MD("voice_create"), &AudioServer::voice_create);
  101. ObjectTypeDB::bind_method(_MD("voice_play", "voice", "sample"), &AudioServer::voice_play);
  102. ObjectTypeDB::bind_method(_MD("voice_set_volume", "voice", "volume"), &AudioServer::voice_set_volume);
  103. ObjectTypeDB::bind_method(_MD("voice_set_pan", "voice", "pan", "depth", "height"), &AudioServer::voice_set_pan, DEFVAL(0), DEFVAL(0));
  104. ObjectTypeDB::bind_method(_MD("voice_set_filter", "voice", "type", "cutoff", "resonance", "gain"), &AudioServer::voice_set_filter, DEFVAL(0));
  105. ObjectTypeDB::bind_method(_MD("voice_set_chorus", "voice", "chorus"), &AudioServer::voice_set_chorus);
  106. ObjectTypeDB::bind_method(_MD("voice_set_reverb", "voice", "room", "reverb"), &AudioServer::voice_set_reverb);
  107. ObjectTypeDB::bind_method(_MD("voice_set_mix_rate", "voice", "rate"), &AudioServer::voice_set_mix_rate);
  108. ObjectTypeDB::bind_method(_MD("voice_set_positional", "voice", "enabled"), &AudioServer::voice_set_positional);
  109. ObjectTypeDB::bind_method(_MD("voice_get_volume", "voice"), &AudioServer::voice_get_volume);
  110. ObjectTypeDB::bind_method(_MD("voice_get_pan", "voice"), &AudioServer::voice_get_pan);
  111. ObjectTypeDB::bind_method(_MD("voice_get_pan_height", "voice"), &AudioServer::voice_get_pan_height);
  112. ObjectTypeDB::bind_method(_MD("voice_get_pan_depth", "voice"), &AudioServer::voice_get_pan_depth);
  113. ObjectTypeDB::bind_method(_MD("voice_get_filter_type", "voice"), &AudioServer::voice_get_filter_type);
  114. ObjectTypeDB::bind_method(_MD("voice_get_filter_cutoff", "voice"), &AudioServer::voice_get_filter_cutoff);
  115. ObjectTypeDB::bind_method(_MD("voice_get_filter_resonance", "voice"), &AudioServer::voice_get_filter_resonance);
  116. ObjectTypeDB::bind_method(_MD("voice_get_chorus", "voice"), &AudioServer::voice_get_chorus);
  117. ObjectTypeDB::bind_method(_MD("voice_get_reverb_type", "voice"), &AudioServer::voice_get_reverb_type);
  118. ObjectTypeDB::bind_method(_MD("voice_get_reverb", "voice"), &AudioServer::voice_get_reverb);
  119. ObjectTypeDB::bind_method(_MD("voice_get_mix_rate", "voice"), &AudioServer::voice_get_mix_rate);
  120. ObjectTypeDB::bind_method(_MD("voice_is_positional", "voice"), &AudioServer::voice_is_positional);
  121. ObjectTypeDB::bind_method(_MD("voice_stop", "voice"), &AudioServer::voice_stop);
  122. ObjectTypeDB::bind_method(_MD("free_rid", "rid"), &AudioServer::free);
  123. ObjectTypeDB::bind_method(_MD("set_stream_global_volume_scale", "scale"), &AudioServer::set_stream_global_volume_scale);
  124. ObjectTypeDB::bind_method(_MD("get_stream_global_volume_scale"), &AudioServer::get_stream_global_volume_scale);
  125. ObjectTypeDB::bind_method(_MD("set_fx_global_volume_scale", "scale"), &AudioServer::set_fx_global_volume_scale);
  126. ObjectTypeDB::bind_method(_MD("get_fx_global_volume_scale"), &AudioServer::get_fx_global_volume_scale);
  127. ObjectTypeDB::bind_method(_MD("set_event_voice_global_volume_scale", "scale"), &AudioServer::set_event_voice_global_volume_scale);
  128. ObjectTypeDB::bind_method(_MD("get_event_voice_global_volume_scale"), &AudioServer::get_event_voice_global_volume_scale);
  129. BIND_CONSTANT(SAMPLE_FORMAT_PCM8);
  130. BIND_CONSTANT(SAMPLE_FORMAT_PCM16);
  131. BIND_CONSTANT(SAMPLE_FORMAT_IMA_ADPCM);
  132. BIND_CONSTANT(SAMPLE_LOOP_NONE);
  133. BIND_CONSTANT(SAMPLE_LOOP_FORWARD);
  134. BIND_CONSTANT(SAMPLE_LOOP_PING_PONG);
  135. BIND_CONSTANT(FILTER_NONE);
  136. BIND_CONSTANT(FILTER_LOWPASS);
  137. BIND_CONSTANT(FILTER_BANDPASS);
  138. BIND_CONSTANT(FILTER_HIPASS);
  139. BIND_CONSTANT(FILTER_NOTCH);
  140. BIND_CONSTANT(FILTER_BANDLIMIT); ///< cutoff is LP resonace is HP
  141. BIND_CONSTANT(REVERB_SMALL);
  142. BIND_CONSTANT(REVERB_MEDIUM);
  143. BIND_CONSTANT(REVERB_LARGE);
  144. BIND_CONSTANT(REVERB_HALL);
  145. GLOBAL_DEF("audio/stream_buffering_ms", 500);
  146. GLOBAL_DEF("audio/video_delay_compensation_ms", 300);
  147. }
  148. AudioServer::AudioServer() {
  149. singleton = this;
  150. }
  151. AudioServer::~AudioServer() {
  152. }