1 /*************************************************************************/
2 /*  audio_mixer_sw.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_MIXER_SW_H
31 #define AUDIO_MIXER_SW_H
32 
33 #include "servers/audio/audio_filter_sw.h"
34 #include "servers/audio/reverb_sw.h"
35 #include "servers/audio/sample_manager_sw.h"
36 #include "servers/audio_server.h"
37 
38 class AudioMixerSW : public AudioMixer {
39 public:
40 	enum InterpolationType {
41 
42 		INTERPOLATION_RAW,
43 		INTERPOLATION_LINEAR,
44 		INTERPOLATION_CUBIC
45 	};
46 
47 	enum MixChannels {
48 
49 		MIX_STEREO = 2,
50 		MIX_QUAD = 4
51 	};
52 
53 	typedef void (*MixStepCallback)(void *);
54 
55 private:
56 	SampleManagerSW *sample_manager;
57 
58 	enum {
59 
60 		MAX_CHANNELS = 64,
61 		// fixed point defs
62 
63 		MIX_FRAC_BITS = 13,
64 		MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
65 		MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
66 		MIX_VOL_FRAC_BITS = 12,
67 		MIX_VOLRAMP_FRAC_BITS = 16,
68 		MIX_VOLRAMP_FRAC_LEN = (1 << MIX_VOLRAMP_FRAC_BITS),
69 		MIX_VOLRAMP_FRAC_MASK = MIX_VOLRAMP_FRAC_LEN - 1,
70 		MIX_FILTER_FRAC_BITS = 16,
71 		MIX_FILTER_RAMP_FRAC_BITS = 8,
72 		MIX_VOL_MOVE_TO_24 = 4
73 	};
74 
75 	struct Channel {
76 
77 		RID sample;
78 		struct Mix {
79 			int64_t offset;
80 			int32_t increment;
81 
82 			int32_t vol[4];
83 			int32_t reverb_vol[4];
84 			int32_t chorus_vol[4];
85 
86 			int32_t old_vol[4];
87 			int32_t old_reverb_vol[4];
88 			int32_t old_chorus_vol[4];
89 
90 			struct Filter { //history (stereo)
91 				float ha[2], hb[2];
92 			} filter_l, filter_r;
93 
94 			struct IMA_ADPCM_State {
95 
96 				int16_t step_index;
97 				int32_t predictor;
98 				/* values at loop point */
99 				int16_t loop_step_index;
100 				int32_t loop_predictor;
101 				int32_t last_nibble;
102 				int32_t loop_pos;
103 				int32_t window_ofs;
104 				const uint8_t *ptr;
105 			} ima_adpcm[2];
106 
107 		} mix;
108 
109 		float vol;
110 		float pan;
111 		float depth;
112 		float height;
113 
114 		float chorus_send;
115 		ReverbRoomType reverb_room;
116 		float reverb_send;
117 		int speed;
118 		int check;
119 		bool positional;
120 
121 		bool had_prev_reverb;
122 		bool had_prev_chorus;
123 		bool had_prev_vol;
124 
125 		struct Filter {
126 
127 			bool dirty;
128 
129 			FilterType type;
130 			float cutoff;
131 			float resonance;
132 			float gain;
133 
134 			struct Coefs {
135 
136 				float a1, a2, b0, b1, b2; // fixed point coefficients
137 			} coefs, old_coefs;
138 
139 		} filter;
140 
141 		bool first_mix;
142 		bool active;
ChannelChannel143 		Channel() {
144 			active = false;
145 			check = -1;
146 			first_mix = false;
147 			filter.dirty = true;
148 			filter.type = FILTER_NONE;
149 			filter.cutoff = 8000;
150 			filter.resonance = 0;
151 			filter.gain = 0;
152 		}
153 	};
154 
155 	Channel channels[MAX_CHANNELS];
156 
157 	uint32_t mix_rate;
158 	bool fx_enabled;
159 	InterpolationType interpolation_type;
160 
161 	int mix_chunk_bits;
162 	int mix_chunk_size;
163 	int mix_chunk_mask;
164 
165 	int32_t *mix_buffer;
166 	int32_t *zero_buffer; // fx feed when no input was mixed
167 
168 	struct ResamplerState {
169 
170 		uint32_t amount;
171 		int32_t increment;
172 
173 		int32_t pos;
174 
175 		int32_t vol[4];
176 		int32_t reverb_vol[4];
177 		int32_t chorus_vol[4];
178 
179 		int32_t vol_inc[4];
180 		int32_t reverb_vol_inc[4];
181 		int32_t chorus_vol_inc[4];
182 
183 		Channel::Mix::Filter *filter_l;
184 		Channel::Mix::Filter *filter_r;
185 		Channel::Filter::Coefs coefs;
186 		Channel::Filter::Coefs coefs_inc;
187 
188 		Channel::Mix::IMA_ADPCM_State *ima_adpcm;
189 
190 		int32_t *reverb_buffer;
191 	};
192 
193 	template <class Depth, bool is_stereo, bool use_filter, bool is_ima_adpcm, bool use_fx, InterpolationType type, MixChannels>
194 	_FORCE_INLINE_ void do_resample(const Depth *p_src, int32_t *p_dst, ResamplerState *p_state);
195 
196 	MixChannels mix_channels;
197 
198 	void mix_channel(Channel &p_channel);
199 	int mix_chunk_left;
200 	void mix_chunk();
201 
202 	float channel_nrg;
203 	int channel_id_count;
204 	bool inside_mix;
205 	MixStepCallback step_callback;
206 	void *step_udata;
207 	_FORCE_INLINE_ int _get_channel(ChannelID p_channel) const;
208 
209 	int max_reverbs;
210 	struct ReverbState {
211 
212 		bool used_in_chunk;
213 		bool enabled;
214 		ReverbSW *reverb;
215 		int frames_idle;
216 		int32_t *buffer; //reverb is sent here
ReverbStateReverbState217 		ReverbState() {
218 			enabled = false;
219 			frames_idle = 0;
220 			used_in_chunk = false;
221 		}
222 	};
223 
224 	ReverbState *reverb_state;
225 
226 public:
227 	virtual ChannelID channel_alloc(RID p_sample);
228 
229 	virtual void channel_set_volume(ChannelID p_channel, float p_gain);
230 	virtual void channel_set_pan(ChannelID p_channel, float p_pan, float p_depth = 0, float height = 0); //pan and depth go from -1 to 1
231 	virtual void channel_set_filter(ChannelID p_channel, FilterType p_type, float p_cutoff, float p_resonance, float p_gain = 1.0);
232 	virtual void channel_set_chorus(ChannelID p_channel, float p_chorus);
233 	virtual void channel_set_reverb(ChannelID p_channel, ReverbRoomType p_room_type, float p_reverb);
234 	virtual void channel_set_mix_rate(ChannelID p_channel, int p_mix_rate);
235 	virtual void channel_set_positional(ChannelID p_channel, bool p_positional);
236 
237 	virtual float channel_get_volume(ChannelID p_channel) const;
238 	virtual float channel_get_pan(ChannelID p_channel) const; //pan and depth go from -1 to 1
239 	virtual float channel_get_pan_depth(ChannelID p_channel) const; //pan and depth go from -1 to 1
240 	virtual float channel_get_pan_height(ChannelID p_channel) const; //pan and depth go from -1 to 1
241 	virtual FilterType channel_get_filter_type(ChannelID p_channel) const;
242 	virtual float channel_get_filter_cutoff(ChannelID p_channel) const;
243 	virtual float channel_get_filter_resonance(ChannelID p_channel) const;
244 	virtual float channel_get_filter_gain(ChannelID p_channel) const;
245 
246 	virtual float channel_get_chorus(ChannelID p_channel) const;
247 	virtual ReverbRoomType channel_get_reverb_type(ChannelID p_channel) const;
248 	virtual float channel_get_reverb(ChannelID p_channel) const;
249 
250 	virtual int channel_get_mix_rate(ChannelID p_channel) const;
251 	virtual bool channel_is_positional(ChannelID p_channel) const;
252 
253 	virtual bool channel_is_valid(ChannelID p_channel) const;
254 
255 	virtual void channel_free(ChannelID p_channel);
256 
257 	int mix(int32_t *p_buffer, int p_frames); //return amount of mixsteps
258 	uint64_t get_step_usecs() const;
259 
260 	virtual void set_mixer_volume(float p_volume);
261 
262 	AudioMixerSW(SampleManagerSW *p_sample_manager, int p_desired_latency_ms, int p_mix_rate, MixChannels p_mix_channels, bool p_use_fx = true, InterpolationType p_interp = INTERPOLATION_LINEAR, MixStepCallback p_step_callback = NULL, void *p_callback_udata = NULL);
263 	~AudioMixerSW();
264 };
265 
266 #endif // AUDIO_MIXER_SW_H
267