audio_server.h 10 KB

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