audio_server_sw.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*************************************************************************/
  2. /* audio_server_sw.h */
  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. #ifndef AUDIO_SERVER_SW_H
  30. #define AUDIO_SERVER_SW_H
  31. #include "servers/audio_server.h"
  32. #include "servers/audio/audio_mixer_sw.h"
  33. #include "servers/audio/voice_rb_sw.h"
  34. #include "self_list.h"
  35. #include "os/thread_safe.h"
  36. #include "os/thread.h"
  37. class AudioServerSW : public AudioServer {
  38. OBJ_TYPE( AudioServerSW, AudioServer );
  39. _THREAD_SAFE_CLASS_
  40. enum {
  41. INTERNAL_BUFFER_SIZE=4096,
  42. STREAM_SCALE_BITS=12
  43. };
  44. SampleManagerSW *sample_manager;
  45. AudioMixerSW *mixer;
  46. virtual AudioMixer *get_mixer();
  47. virtual void audio_mixer_chunk_callback(int p_frames);
  48. struct Voice {
  49. float volume;
  50. volatile bool active;
  51. SelfList<Voice> active_item;
  52. AudioMixer::ChannelID channel;
  53. Voice () : active_item(this) { channel=AudioMixer::INVALID_CHANNEL; active=false;}
  54. };
  55. mutable RID_Owner<Voice> voice_owner;
  56. SelfList<Voice>::List active_list;
  57. struct Stream {
  58. bool active;
  59. List<Stream*>::Element *E;
  60. AudioStream *audio_stream;
  61. EventStream *event_stream;
  62. float volume_scale;
  63. };
  64. List<Stream*> active_audio_streams;
  65. //List<Stream*> event_streams;
  66. int32_t * internal_buffer;
  67. int internal_buffer_channels;
  68. int32_t * stream_buffer;
  69. mutable RID_Owner<Stream> stream_owner;
  70. float stream_volume;
  71. float stream_volume_scale;
  72. float fx_volume_scale;
  73. float event_voice_volume_scale;
  74. float peak_left,peak_right;
  75. uint32_t max_peak;
  76. double _output_delay;
  77. VoiceRBSW voice_rb;
  78. bool exit_update_thread;
  79. Thread *thread;
  80. static void _thread_func(void *self);
  81. void _update_streams(bool p_thread);
  82. void driver_process_chunk(int p_frames,int32_t *p_buffer);
  83. AudioMixerSW::InterpolationType mixer_interp;
  84. bool mixer_use_fx;
  85. uint64_t mixer_step_usecs;
  86. static void _mixer_callback(void *p_udata);
  87. friend class AudioDriverSW;
  88. void driver_process(int p_frames,int32_t *p_buffer);
  89. public:
  90. /* SAMPLE API */
  91. virtual RID sample_create(SampleFormat p_format, bool p_stereo, int p_length);
  92. virtual void sample_set_description(RID p_sample, const String& p_description);
  93. virtual String sample_get_description(RID p_sample, const String& p_description) const;
  94. virtual SampleFormat sample_get_format(RID p_sample) const;
  95. virtual bool sample_is_stereo(RID p_sample) const;
  96. virtual int sample_get_length(RID p_sample) const;
  97. const void* sample_get_data_ptr(RID p_sample) const;
  98. virtual void sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer);
  99. virtual const DVector<uint8_t> sample_get_data(RID p_sample) const;
  100. virtual void sample_set_mix_rate(RID p_sample,int p_rate);
  101. virtual int sample_get_mix_rate(RID p_sample) const;
  102. virtual void sample_set_loop_format(RID p_sample,SampleLoopFormat p_format);
  103. virtual SampleLoopFormat sample_get_loop_format(RID p_sample) const;
  104. virtual void sample_set_loop_begin(RID p_sample,int p_pos);
  105. virtual int sample_get_loop_begin(RID p_sample) const;
  106. virtual void sample_set_loop_end(RID p_sample,int p_pos);
  107. virtual int sample_get_loop_end(RID p_sample) const;
  108. /* VOICE API */
  109. virtual RID voice_create();
  110. virtual void voice_play(RID p_voice, RID p_sample);
  111. virtual void voice_set_volume(RID p_voice, float p_db);
  112. 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
  113. virtual void voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance,float p_gain=0);
  114. virtual void voice_set_chorus(RID p_voice, float p_chorus );
  115. virtual void voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb);
  116. virtual void voice_set_mix_rate(RID p_voice, int p_mix_rate);
  117. virtual void voice_set_positional(RID p_voice, bool p_positional);
  118. virtual float voice_get_volume(RID p_voice) const;
  119. virtual float voice_get_pan(RID p_voice) const; //pan and depth go from -1 to 1
  120. virtual float voice_get_pan_depth(RID p_voice) const; //pan and depth go from -1 to 1
  121. virtual float voice_get_pan_height(RID p_voice) const; //pan and depth go from -1 to 1
  122. virtual FilterType voice_get_filter_type(RID p_voice) const;
  123. virtual float voice_get_filter_cutoff(RID p_voice) const;
  124. virtual float voice_get_filter_resonance(RID p_voice) const;
  125. virtual float voice_get_chorus(RID p_voice) const;
  126. virtual ReverbRoomType voice_get_reverb_type(RID p_voice) const;
  127. virtual float voice_get_reverb(RID p_voice) const;
  128. virtual int voice_get_mix_rate(RID p_voice) const;
  129. virtual bool voice_is_positional(RID p_voice) const;
  130. virtual void voice_stop(RID p_voice);
  131. virtual bool voice_is_active(RID p_voice) const;
  132. /* STREAM API */
  133. virtual RID audio_stream_create(AudioStream *p_stream);
  134. virtual RID event_stream_create(EventStream *p_stream);
  135. virtual void stream_set_active(RID p_stream, bool p_active);
  136. virtual bool stream_is_active(RID p_stream) const;
  137. virtual void stream_set_volume_scale(RID p_stream, float p_scale);
  138. virtual float stream_set_volume_scale(RID p_stream) const;
  139. virtual void free(RID p_id);
  140. virtual void init();
  141. virtual void finish();
  142. virtual void update();
  143. virtual void lock();
  144. virtual void unlock();
  145. virtual int get_default_channel_count() const;
  146. virtual int get_default_mix_rate() const;
  147. void set_mixer_params(AudioMixerSW::InterpolationType p_interp, bool p_use_fx);
  148. virtual void set_stream_global_volume_scale(float p_volume);
  149. virtual void set_fx_global_volume_scale(float p_volume);
  150. virtual void set_event_voice_global_volume_scale(float p_volume);
  151. virtual float get_stream_global_volume_scale() const;
  152. virtual float get_fx_global_volume_scale() const;
  153. virtual float get_event_voice_global_volume_scale() const;
  154. virtual uint32_t read_output_peak() const;
  155. virtual double get_mix_time() const; //useful for video -> audio sync
  156. virtual double get_output_delay() const;
  157. AudioServerSW(SampleManagerSW *p_sample_manager);
  158. ~AudioServerSW();
  159. };
  160. class AudioDriverSW {
  161. static AudioDriverSW *singleton;
  162. uint64_t _last_mix_time;
  163. uint64_t _mix_amount;
  164. protected:
  165. void audio_server_process(int p_frames,int32_t *p_buffer,bool p_update_mix_time=true);
  166. void update_mix_time(int p_frames);
  167. public:
  168. double get_mix_time() const; //useful for video -> audio sync
  169. enum OutputFormat {
  170. OUTPUT_MONO,
  171. OUTPUT_STEREO,
  172. OUTPUT_QUAD,
  173. OUTPUT_5_1
  174. };
  175. static AudioDriverSW *get_singleton();
  176. void set_singleton();
  177. virtual const char* get_name() const=0;
  178. virtual Error init()=0;
  179. virtual void start()=0;
  180. virtual int get_mix_rate() const =0;
  181. virtual OutputFormat get_output_format() const=0;
  182. virtual void lock()=0;
  183. virtual void unlock()=0;
  184. virtual void finish()=0;
  185. AudioDriverSW();
  186. virtual ~AudioDriverSW() {};
  187. };
  188. class AudioDriverManagerSW {
  189. enum {
  190. MAX_DRIVERS=10
  191. };
  192. static AudioDriverSW *drivers[MAX_DRIVERS];
  193. static int driver_count;
  194. public:
  195. static void add_driver(AudioDriverSW *p_driver);
  196. static int get_driver_count();
  197. static AudioDriverSW *get_driver(int p_driver);
  198. };
  199. #endif // AUDIO_SERVER_SW_H