audio_server_javascript.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*************************************************************************/
  2. /* audio_server_javascript.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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. #ifndef AUDIO_SERVER_JAVASCRIPT_H
  30. #define AUDIO_SERVER_JAVASCRIPT_H
  31. #include "servers/audio_server.h"
  32. class AudioServerJavascript : public AudioServer {
  33. GDCLASS(AudioServerJavascript,AudioServer);
  34. enum {
  35. INTERNAL_BUFFER_SIZE=4096,
  36. STREAM_SCALE_BITS=12
  37. };
  38. AudioMixer *get_mixer();
  39. void audio_mixer_chunk_callback(int p_frames);
  40. struct Sample {
  41. SampleFormat format;
  42. SampleLoopFormat loop_format;
  43. int loop_begin;
  44. int loop_end;
  45. int length;
  46. int index;
  47. int mix_rate;
  48. bool stereo;
  49. Vector<float> tmp_data;
  50. };
  51. mutable RID_Owner<Sample> sample_owner;
  52. int sample_base;
  53. struct Voice {
  54. int index;
  55. float volume;
  56. float pan;
  57. float pan_depth;
  58. float pan_height;
  59. float chorus;
  60. ReverbRoomType reverb_type;
  61. float reverb;
  62. int mix_rate;
  63. int sample_mix_rate;
  64. bool positional;
  65. bool active;
  66. };
  67. mutable RID_Owner<Voice> voice_owner;
  68. int voice_base;
  69. struct Stream {
  70. bool active;
  71. List<Stream*>::Element *E;
  72. AudioStream *audio_stream;
  73. EventStream *event_stream;
  74. float volume_scale;
  75. };
  76. List<Stream*> active_audio_streams;
  77. //List<Stream*> event_streams;
  78. float * internal_buffer;
  79. int internal_buffer_channels;
  80. int32_t * stream_buffer;
  81. mutable RID_Owner<Stream> stream_owner;
  82. float stream_volume;
  83. float stream_volume_scale;
  84. float event_voice_scale;
  85. float fx_volume_scale;
  86. void driver_process_chunk(int p_frames);
  87. int webaudio_mix_rate;
  88. static AudioServerJavascript *singleton;
  89. public:
  90. void mix_to_js(int p_frames);
  91. /* SAMPLE API */
  92. virtual RID sample_create(SampleFormat p_format, bool p_stereo, int p_length);
  93. virtual void sample_set_description(RID p_sample, const String& p_description);
  94. virtual String sample_get_description(RID p_sample) const;
  95. virtual SampleFormat sample_get_format(RID p_sample) const;
  96. virtual bool sample_is_stereo(RID p_sample) const;
  97. virtual int sample_get_length(RID p_sample) const;
  98. virtual const void* sample_get_data_ptr(RID p_sample) const;
  99. virtual void sample_set_data(RID p_sample, const PoolVector<uint8_t>& p_buffer);
  100. virtual PoolVector<uint8_t> sample_get_data(RID p_sample) const;
  101. virtual void sample_set_mix_rate(RID p_sample,int p_rate);
  102. virtual int sample_get_mix_rate(RID p_sample) const;
  103. virtual void sample_set_loop_format(RID p_sample,SampleLoopFormat p_format);
  104. virtual SampleLoopFormat sample_get_loop_format(RID p_sample) const;
  105. virtual void sample_set_loop_begin(RID p_sample,int p_pos);
  106. virtual int sample_get_loop_begin(RID p_sample) const;
  107. virtual void sample_set_loop_end(RID p_sample,int p_pos);
  108. virtual int sample_get_loop_end(RID p_sample) const;
  109. /* VOICE API */
  110. virtual RID voice_create();
  111. virtual void voice_play(RID p_voice, RID p_sample);
  112. virtual void voice_set_volume(RID p_voice, float p_volume);
  113. virtual void voice_set_pan(RID p_voice, float p_pan, float p_depth=0,float height=0); //pan and depth go from -1 to 1
  114. virtual void voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain=0);
  115. virtual void voice_set_chorus(RID p_voice, float p_chorus );
  116. virtual void voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb);
  117. virtual void voice_set_mix_rate(RID p_voice, int p_mix_rate);
  118. virtual void voice_set_positional(RID p_voice, bool p_positional);
  119. virtual float voice_get_volume(RID p_voice) const;
  120. virtual float voice_get_pan(RID p_voice) const; //pan and depth go from -1 to 1
  121. virtual float voice_get_pan_depth(RID p_voice) const; //pan and depth go from -1 to 1
  122. virtual float voice_get_pan_height(RID p_voice) const; //pan and depth go from -1 to 1
  123. virtual FilterType voice_get_filter_type(RID p_voice) const;
  124. virtual float voice_get_filter_cutoff(RID p_voice) const;
  125. virtual float voice_get_filter_resonance(RID p_voice) const;
  126. virtual float voice_get_chorus(RID p_voice) const;
  127. virtual ReverbRoomType voice_get_reverb_type(RID p_voice) const;
  128. virtual float voice_get_reverb(RID p_voice) const;
  129. virtual int voice_get_mix_rate(RID p_voice) const;
  130. virtual bool voice_is_positional(RID p_voice) const;
  131. virtual void voice_stop(RID p_voice);
  132. virtual bool voice_is_active(RID p_voice) const;
  133. /* STREAM API */
  134. virtual RID audio_stream_create(AudioStream *p_stream);
  135. virtual RID event_stream_create(EventStream *p_stream);
  136. virtual void stream_set_active(RID p_stream, bool p_active);
  137. virtual bool stream_is_active(RID p_stream) const;
  138. virtual void stream_set_volume_scale(RID p_stream, float p_scale);
  139. virtual float stream_set_volume_scale(RID p_stream) const;
  140. /* Audio Physics API */
  141. virtual void free(RID p_id);
  142. virtual void init();
  143. virtual void finish();
  144. virtual void update();
  145. /* MISC config */
  146. virtual void lock();
  147. virtual void unlock();
  148. virtual int get_default_channel_count() const;
  149. virtual int get_default_mix_rate() const;
  150. virtual void set_stream_global_volume_scale(float p_volume);
  151. virtual void set_fx_global_volume_scale(float p_volume);
  152. virtual void set_event_voice_global_volume_scale(float p_volume);
  153. virtual float get_stream_global_volume_scale() const;
  154. virtual float get_fx_global_volume_scale() const;
  155. virtual float get_event_voice_global_volume_scale() const;
  156. virtual uint32_t read_output_peak() const;
  157. static AudioServer *get_singleton();
  158. virtual double get_mix_time() const; //useful for video -> audio sync
  159. virtual double get_output_delay() const;
  160. AudioServerJavascript();
  161. };
  162. #endif // AUDIO_SERVER_JAVASCRIPT_H