audio_server_javascript.h 5.3 KB

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