audio_server.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2015 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. int len = p_buffer.size();
  43. ERR_FAIL_COND( len == 0 );
  44. DVector<uint8_t> data;
  45. data.resize(len*2);
  46. DVector<uint8_t>::Write w=data.write();
  47. int16_t *samples = (int16_t*)w.ptr();
  48. DVector<float>::Read r = p_buffer.read();
  49. for(int i=0;i<len;i++) {
  50. float sample = r[i];
  51. sample = Math::floor( sample * (1<<16) );
  52. if (sample<-32768)
  53. sample=-32768;
  54. else if (sample>32767)
  55. sample=32767;
  56. samples[i]=sample;
  57. }
  58. w = DVector<uint8_t>::Write();
  59. sample_set_data(p_sample,data);
  60. }
  61. void AudioServer::_bind_methods() {
  62. ObjectTypeDB::bind_method(_MD("sample_create","format","stereo","length"), &AudioServer::sample_create );
  63. ObjectTypeDB::bind_method(_MD("sample_set_description","sample","description"), &AudioServer::sample_set_description );
  64. ObjectTypeDB::bind_method(_MD("sample_get_description","sample"), &AudioServer::sample_get_description );
  65. ObjectTypeDB::bind_method(_MD("sample_get_format","sample"), &AudioServer::sample_get_format );
  66. ObjectTypeDB::bind_method(_MD("sample_is_stereo","sample"), &AudioServer::sample_is_stereo );
  67. ObjectTypeDB::bind_method(_MD("sample_get_length","sample"), &AudioServer::sample_get_length );
  68. ObjectTypeDB::bind_method(_MD("sample_set_signed_data","sample","data"), &AudioServer::sample_set_signed_data );
  69. ObjectTypeDB::bind_method(_MD("sample_set_data","sample"), &AudioServer::sample_set_data );
  70. ObjectTypeDB::bind_method(_MD("sample_get_data","sample"), &AudioServer::sample_get_data );
  71. ObjectTypeDB::bind_method(_MD("sample_set_mix_rate","sample","mix_rate"), &AudioServer::sample_set_mix_rate );
  72. ObjectTypeDB::bind_method(_MD("sample_get_mix_rate","sample"), &AudioServer::sample_get_mix_rate );
  73. ObjectTypeDB::bind_method(_MD("sample_set_loop_format","sample","loop_format"), &AudioServer::sample_set_loop_format );
  74. ObjectTypeDB::bind_method(_MD("sample_get_loop_format","sample"), &AudioServer::sample_get_loop_format );
  75. ObjectTypeDB::bind_method(_MD("sample_set_loop_begin","sample","pos"), &AudioServer::sample_set_loop_begin );
  76. ObjectTypeDB::bind_method(_MD("sample_get_loop_begin","sample"), &AudioServer::sample_get_loop_begin );
  77. ObjectTypeDB::bind_method(_MD("sample_set_loop_end","sample","pos"), &AudioServer::sample_set_loop_end );
  78. ObjectTypeDB::bind_method(_MD("sample_get_loop_end","sample"), &AudioServer::sample_get_loop_end );
  79. ObjectTypeDB::bind_method(_MD("voice_create"), &AudioServer::voice_create );
  80. ObjectTypeDB::bind_method(_MD("voice_play","voice","sample"), &AudioServer::voice_play );
  81. ObjectTypeDB::bind_method(_MD("voice_set_volume","voice","volume"), &AudioServer::voice_set_volume );
  82. ObjectTypeDB::bind_method(_MD("voice_set_pan","voice","pan","depth","height"), &AudioServer::voice_set_pan,DEFVAL(0),DEFVAL(0) );
  83. ObjectTypeDB::bind_method(_MD("voice_set_filter","voice","type","cutoff","resonance","gain"), &AudioServer::voice_set_filter,DEFVAL(0) );
  84. ObjectTypeDB::bind_method(_MD("voice_set_chorus","voice","chorus"), &AudioServer::voice_set_chorus );
  85. ObjectTypeDB::bind_method(_MD("voice_set_reverb","voice","room","reverb"), &AudioServer::voice_set_reverb );
  86. ObjectTypeDB::bind_method(_MD("voice_set_mix_rate","voice","rate"), &AudioServer::voice_set_mix_rate );
  87. ObjectTypeDB::bind_method(_MD("voice_set_positional","voice","enabled"), &AudioServer::voice_set_positional );
  88. ObjectTypeDB::bind_method(_MD("voice_get_volume","voice"), &AudioServer::voice_get_volume );
  89. ObjectTypeDB::bind_method(_MD("voice_get_pan","voice"), &AudioServer::voice_get_pan );
  90. ObjectTypeDB::bind_method(_MD("voice_get_pan_height","voice"), &AudioServer::voice_get_pan_height );
  91. ObjectTypeDB::bind_method(_MD("voice_get_pan_depth","voice"), &AudioServer::voice_get_pan_depth );
  92. ObjectTypeDB::bind_method(_MD("voice_get_filter_type","voice"), &AudioServer::voice_get_filter_type );
  93. ObjectTypeDB::bind_method(_MD("voice_get_filter_cutoff","voice"), &AudioServer::voice_get_filter_cutoff );
  94. ObjectTypeDB::bind_method(_MD("voice_get_filter_resonance","voice"), &AudioServer::voice_get_filter_resonance );
  95. ObjectTypeDB::bind_method(_MD("voice_get_chorus","voice"), &AudioServer::voice_get_chorus );
  96. ObjectTypeDB::bind_method(_MD("voice_get_reverb_type","voice"), &AudioServer::voice_get_reverb_type );
  97. ObjectTypeDB::bind_method(_MD("voice_get_reverb","voice"), &AudioServer::voice_get_reverb );
  98. ObjectTypeDB::bind_method(_MD("voice_get_mix_rate","voice"), &AudioServer::voice_get_mix_rate );
  99. ObjectTypeDB::bind_method(_MD("voice_is_positional","voice"), &AudioServer::voice_is_positional );
  100. ObjectTypeDB::bind_method(_MD("voice_stop","voice"), &AudioServer::voice_stop );
  101. ObjectTypeDB::bind_method(_MD("free","rid"), &AudioServer::free );
  102. ObjectTypeDB::bind_method(_MD("set_stream_global_volume_scale","scale"), &AudioServer::set_stream_global_volume_scale );
  103. ObjectTypeDB::bind_method(_MD("get_stream_global_volume_scale"), &AudioServer::get_stream_global_volume_scale );
  104. ObjectTypeDB::bind_method(_MD("set_fx_global_volume_scale","scale"), &AudioServer::set_fx_global_volume_scale );
  105. ObjectTypeDB::bind_method(_MD("get_fx_global_volume_scale"), &AudioServer::get_fx_global_volume_scale );
  106. ObjectTypeDB::bind_method(_MD("set_event_voice_global_volume_scale","scale"), &AudioServer::set_event_voice_global_volume_scale );
  107. ObjectTypeDB::bind_method(_MD("get_event_voice_global_volume_scale"), &AudioServer::get_event_voice_global_volume_scale );
  108. BIND_CONSTANT( SAMPLE_FORMAT_PCM8 );
  109. BIND_CONSTANT( SAMPLE_FORMAT_PCM16 );
  110. BIND_CONSTANT( SAMPLE_FORMAT_IMA_ADPCM );
  111. BIND_CONSTANT( SAMPLE_LOOP_NONE );
  112. BIND_CONSTANT( SAMPLE_LOOP_FORWARD );
  113. BIND_CONSTANT( SAMPLE_LOOP_PING_PONG );
  114. BIND_CONSTANT( FILTER_NONE );
  115. BIND_CONSTANT( FILTER_LOWPASS );
  116. BIND_CONSTANT( FILTER_BANDPASS );
  117. BIND_CONSTANT( FILTER_HIPASS );
  118. BIND_CONSTANT( FILTER_NOTCH );
  119. BIND_CONSTANT( FILTER_BANDLIMIT ); ///< cutoff is LP resonace is HP
  120. BIND_CONSTANT( REVERB_SMALL );
  121. BIND_CONSTANT( REVERB_MEDIUM );
  122. BIND_CONSTANT( REVERB_LARGE );
  123. BIND_CONSTANT( REVERB_HALL );
  124. GLOBAL_DEF("audio/stream_buffering_ms",500);
  125. }
  126. AudioServer::AudioServer() {
  127. singleton=this;
  128. }
  129. AudioServer::~AudioServer() {
  130. }