audio_server.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*************************************************************************/
  2. /* audio_server.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_H
  30. #define AUDIO_SERVER_H
  31. #include "variant.h"
  32. #include "object.h"
  33. class AudioMixer {
  34. protected:
  35. void audio_mixer_chunk_call(int p_frames);
  36. public:
  37. enum {
  38. INVALID_CHANNEL=0xFFFFFFFF
  39. };
  40. typedef uint32_t ChannelID;
  41. /* CHANNEL API */
  42. enum FilterType {
  43. FILTER_NONE,
  44. FILTER_LOWPASS,
  45. FILTER_BANDPASS,
  46. FILTER_HIPASS,
  47. FILTER_NOTCH,
  48. FILTER_PEAK,
  49. FILTER_BANDLIMIT, ///< cutoff is LP resonace is HP
  50. FILTER_LOW_SHELF,
  51. FILTER_HIGH_SHELF
  52. };
  53. enum ReverbRoomType {
  54. REVERB_SMALL,
  55. REVERB_MEDIUM,
  56. REVERB_LARGE,
  57. REVERB_HALL
  58. };
  59. virtual ChannelID channel_alloc(RID p_sample)=0;
  60. virtual void channel_set_volume(ChannelID p_channel, float p_gain)=0;
  61. virtual void channel_set_pan(ChannelID p_channel, float p_pan, float p_depth=0,float height=0)=0; //pan and depth go from -1 to 1
  62. virtual void channel_set_filter(ChannelID p_channel, FilterType p_type, float p_cutoff, float p_resonance, float p_gain=1.0)=0;
  63. virtual void channel_set_chorus(ChannelID p_channel, float p_chorus )=0;
  64. virtual void channel_set_reverb(ChannelID p_channel, ReverbRoomType p_room_type, float p_reverb)=0;
  65. virtual void channel_set_mix_rate(ChannelID p_channel, int p_mix_rate)=0;
  66. virtual void channel_set_positional(ChannelID p_channel, bool p_positional)=0;
  67. virtual float channel_get_volume(ChannelID p_channel) const=0;
  68. virtual float channel_get_pan(ChannelID p_channel) const=0; //pan and depth go from -1 to 1
  69. virtual float channel_get_pan_depth(ChannelID p_channel) const=0; //pan and depth go from -1 to 1
  70. virtual float channel_get_pan_height(ChannelID p_channel) const=0; //pan and depth go from -1 to 1
  71. virtual FilterType channel_get_filter_type(ChannelID p_channel) const=0;
  72. virtual float channel_get_filter_cutoff(ChannelID p_channel) const=0;
  73. virtual float channel_get_filter_resonance(ChannelID p_channel) const=0;
  74. virtual float channel_get_filter_gain(ChannelID p_channel) const=0;
  75. virtual float channel_get_chorus(ChannelID p_channel) const=0;
  76. virtual ReverbRoomType channel_get_reverb_type(ChannelID p_channel) const=0;
  77. virtual float channel_get_reverb(ChannelID p_channel) const=0;
  78. virtual int channel_get_mix_rate(ChannelID p_channel) const=0;
  79. virtual bool channel_is_positional(ChannelID p_channel) const=0;
  80. virtual bool channel_is_valid(ChannelID p_channel) const=0;
  81. virtual void channel_free(ChannelID p_channel)=0;
  82. virtual void set_mixer_volume(float p_volume)=0;
  83. virtual ~AudioMixer() {}
  84. };
  85. class AudioServer : public Object {
  86. OBJ_TYPE( AudioServer, Object );
  87. static AudioServer *singleton;
  88. protected:
  89. friend class AudioStream;
  90. friend class EventStream;
  91. friend class AudioMixer;
  92. virtual AudioMixer *get_mixer()=0;
  93. virtual void audio_mixer_chunk_callback(int p_frames)=0;
  94. static void _bind_methods();
  95. public:
  96. class EventStream {
  97. protected:
  98. AudioMixer *get_mixer() const;
  99. public:
  100. virtual void update(uint64_t p_usec)=0;
  101. virtual ~EventStream() {}
  102. };
  103. class AudioStream {
  104. public:
  105. virtual int get_channel_count() const=0;
  106. virtual void set_mix_rate(int p_rate)=0; //notify the stream of the mix rate
  107. virtual bool mix(int32_t *p_buffer,int p_frames)=0;
  108. virtual void update()=0;
  109. virtual bool can_update_mt() const { return true; }
  110. virtual ~AudioStream() {}
  111. };
  112. enum SampleFormat {
  113. SAMPLE_FORMAT_PCM8,
  114. SAMPLE_FORMAT_PCM16,
  115. SAMPLE_FORMAT_IMA_ADPCM
  116. };
  117. enum SampleLoopFormat {
  118. SAMPLE_LOOP_NONE,
  119. SAMPLE_LOOP_FORWARD,
  120. SAMPLE_LOOP_PING_PONG // not supported in every platform
  121. };
  122. /* SAMPLE API */
  123. virtual RID sample_create(SampleFormat p_format, bool p_stereo, int p_length)=0;
  124. virtual void sample_set_description(RID p_sample, const String& p_description)=0;
  125. virtual String sample_get_description(RID p_sample, const String& p_description) const=0;
  126. virtual SampleFormat sample_get_format(RID p_sample) const=0;
  127. virtual bool sample_is_stereo(RID p_sample) const=0;
  128. virtual int sample_get_length(RID p_sample) const=0;
  129. virtual const void* sample_get_data_ptr(RID p_sample) const=0;
  130. virtual void sample_set_signed_data(RID p_sample, const DVector<float>& p_buffer);
  131. virtual void sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer)=0;
  132. virtual const DVector<uint8_t> sample_get_data(RID p_sample) const=0;
  133. virtual void sample_set_mix_rate(RID p_sample,int p_rate)=0;
  134. virtual int sample_get_mix_rate(RID p_sample) const=0;
  135. virtual void sample_set_loop_format(RID p_sample,SampleLoopFormat p_format)=0;
  136. virtual SampleLoopFormat sample_get_loop_format(RID p_sample) const=0;
  137. virtual void sample_set_loop_begin(RID p_sample,int p_pos)=0;
  138. virtual int sample_get_loop_begin(RID p_sample) const=0;
  139. virtual void sample_set_loop_end(RID p_sample,int p_pos)=0;
  140. virtual int sample_get_loop_end(RID p_sample) const=0;
  141. /* VOICE API */
  142. enum FilterType {
  143. FILTER_NONE,
  144. FILTER_LOWPASS,
  145. FILTER_BANDPASS,
  146. FILTER_HIPASS,
  147. FILTER_NOTCH,
  148. FILTER_PEAK,
  149. FILTER_BANDLIMIT, ///< cutoff is LP resonace is HP
  150. FILTER_LOW_SHELF,
  151. FILTER_HIGH_SHELF
  152. };
  153. enum ReverbRoomType {
  154. REVERB_SMALL,
  155. REVERB_MEDIUM,
  156. REVERB_LARGE,
  157. REVERB_HALL
  158. };
  159. virtual RID voice_create()=0;
  160. virtual void voice_play(RID p_voice, RID p_sample)=0;
  161. virtual void voice_set_volume(RID p_voice, float p_gain)=0;
  162. virtual void voice_set_pan(RID p_voice, float p_pan, float p_depth=0,float height=0)=0; //pan and depth go from -1 to 1
  163. virtual void voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain=0)=0;
  164. virtual void voice_set_chorus(RID p_voice, float p_chorus )=0;
  165. virtual void voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb)=0;
  166. virtual void voice_set_mix_rate(RID p_voice, int p_mix_rate)=0;
  167. virtual void voice_set_positional(RID p_voice, bool p_positional)=0;
  168. virtual float voice_get_volume(RID p_voice) const=0;
  169. virtual float voice_get_pan(RID p_voice) const=0; //pan and depth go from -1 to 1
  170. virtual float voice_get_pan_depth(RID p_voice) const=0; //pan and depth go from -1 to 1
  171. virtual float voice_get_pan_height(RID p_voice) const=0; //pan and depth go from -1 to 1
  172. virtual FilterType voice_get_filter_type(RID p_voice) const=0;
  173. virtual float voice_get_filter_cutoff(RID p_voice) const=0;
  174. virtual float voice_get_filter_resonance(RID p_voice) const=0;
  175. virtual float voice_get_chorus(RID p_voice) const=0;
  176. virtual ReverbRoomType voice_get_reverb_type(RID p_voice) const=0;
  177. virtual float voice_get_reverb(RID p_voice) const=0;
  178. virtual int voice_get_mix_rate(RID p_voice) const=0;
  179. virtual bool voice_is_positional(RID p_voice) const=0;
  180. virtual void voice_stop(RID p_voice)=0;
  181. virtual bool voice_is_active(RID p_voice) const=0;
  182. /* STREAM API */
  183. virtual RID audio_stream_create(AudioStream *p_stream)=0;
  184. virtual RID event_stream_create(EventStream *p_stream)=0;
  185. virtual void stream_set_active(RID p_stream, bool p_active)=0;
  186. virtual bool stream_is_active(RID p_stream) const=0;
  187. virtual void stream_set_volume_scale(RID p_stream, float p_scale)=0;
  188. virtual float stream_set_volume_scale(RID p_stream) const=0;
  189. /* Audio Physics API */
  190. virtual void free(RID p_id)=0;
  191. virtual void init()=0;
  192. virtual void finish()=0;
  193. virtual void update()=0;
  194. /* MISC config */
  195. virtual void lock()=0;
  196. virtual void unlock()=0;
  197. virtual int get_default_channel_count() const=0;
  198. virtual int get_default_mix_rate() const=0;
  199. virtual void set_stream_global_volume_scale(float p_volume)=0;
  200. virtual void set_fx_global_volume_scale(float p_volume)=0;
  201. virtual void set_event_voice_global_volume_scale(float p_volume)=0;
  202. virtual float get_stream_global_volume_scale() const=0;
  203. virtual float get_fx_global_volume_scale() const=0;
  204. virtual float get_event_voice_global_volume_scale() const=0;
  205. virtual uint32_t read_output_peak() const=0;
  206. static AudioServer *get_singleton();
  207. virtual double get_mix_time() const=0; //useful for video -> audio sync
  208. virtual double get_output_delay() const=0;
  209. AudioServer();
  210. virtual ~AudioServer();
  211. };
  212. VARIANT_ENUM_CAST( AudioServer::SampleFormat );
  213. VARIANT_ENUM_CAST( AudioServer::SampleLoopFormat );
  214. VARIANT_ENUM_CAST( AudioServer::FilterType );
  215. VARIANT_ENUM_CAST( AudioServer::ReverbRoomType );
  216. typedef AudioServer AS;
  217. #endif // AUDIO_SERVER_H