audio_server.cpp 9.1 KB

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