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