2
0

audio_server_javascript.h 7.5 KB

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