1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.jme3.audio;
34 
35 import com.jme3.math.FastMath;
36 
37 /**
38  * Audio environment, for reverb effects.
39  * @author Kirill
40  */
41 public class Environment {
42 
43     private float airAbsorbGainHf   = 0.99426f;
44     private float roomRolloffFactor = 0;
45 
46     private float decayTime         = 1.49f;
47     private float decayHFRatio      = 0.54f;
48 
49     private float density           = 1.0f;
50     private float diffusion         = 0.3f;
51 
52     private float gain              = 0.316f;
53     private float gainHf            = 0.022f;
54 
55     private float lateReverbDelay   = 0.088f;
56     private float lateReverbGain    = 0.768f;
57 
58     private float reflectDelay      = 0.162f;
59     private float reflectGain       = 0.052f;
60 
61     private boolean decayHfLimit    = true;
62 
63     public static final Environment Garage, Dungeon, Cavern, AcousticLab, Closet;
64 
65     static {
66         Garage = new Environment(1, 1, 1, 1, .9f, .5f, .751f, .0039f, .661f, .0137f);
67         Dungeon = new Environment(.75f, 1, 1, .75f, 1.6f, 1, 0.95f, 0.0026f, 0.93f, 0.0103f);
68         Cavern = new Environment(.5f, 1, 1, .5f, 2.25f, 1, .908f, .0103f, .93f, .041f);
69         AcousticLab = new Environment(.5f, 1, 1, 1, .28f, 1, .87f, .002f, .81f, .008f);
70         Closet = new Environment(1, 1, 1, 1, .15f, 1, .6f, .0025f, .5f, .0006f);
71     }
72 
eaxDbToAmp(float eaxDb)73     private static final float eaxDbToAmp(float eaxDb){
74         float dB = eaxDb / 2000f;
75         return FastMath.pow(10f, dB);
76     }
77 
Environment()78     public Environment(){
79     }
80 
Environment(Environment source)81     public Environment(Environment source) {
82         this.airAbsorbGainHf = source.airAbsorbGainHf;
83         this.roomRolloffFactor = source.roomRolloffFactor;
84         this.decayTime = source.decayTime;
85         this.decayHFRatio = source.decayHFRatio;
86         this.density = source.density;
87         this.diffusion = source.diffusion;
88         this.gain = source.gain;
89         this.gainHf = source.gainHf;
90         this.lateReverbDelay = source.lateReverbDelay;
91         this.lateReverbGain = source.lateReverbGain;
92         this.reflectDelay = source.reflectDelay;
93         this.reflectGain = source.reflectGain;
94         this.decayHfLimit = source.decayHfLimit;
95     }
96 
Environment(float density, float diffusion, float gain, float gainHf, float decayTime, float decayHf, float reflGain, float reflDelay, float lateGain, float lateDelay)97     public Environment(float density, float diffusion, float gain, float gainHf,
98                        float decayTime, float decayHf, float reflGain,
99                        float reflDelay, float lateGain, float lateDelay){
100         this.decayTime = decayTime;
101         this.decayHFRatio = decayHf;
102         this.density = density;
103         this.diffusion = diffusion;
104         this.gain = gain;
105         this.gainHf = gainHf;
106         this.lateReverbDelay = lateDelay;
107         this.lateReverbGain = lateGain;
108         this.reflectDelay = reflDelay;
109         this.reflectGain = reflGain;
110     }
111 
Environment(float[] e)112     public Environment(float[] e){
113         if (e.length != 28)
114             throw new IllegalArgumentException("Not an EAX preset");
115 
116         // skip env id
117         // e[0]
118         // skip room size
119         // e[1]
120 
121 //        density = 0;
122         diffusion = e[2];
123         gain = eaxDbToAmp(e[3]); // convert
124         gainHf = eaxDbToAmp(e[4]) / eaxDbToAmp(e[5]); // convert
125         decayTime = e[6];
126         decayHFRatio = e[7] / e[8];
127         reflectGain = eaxDbToAmp(e[9]); // convert
128         reflectDelay = e[10];
129 
130         // skip 3 pan values
131         // e[11] e[12] e[13]
132 
133         lateReverbGain = eaxDbToAmp(e[14]); // convert
134         lateReverbDelay = e[15];
135 
136         // skip 3 pan values
137         // e[16] e[17] e[18]
138 
139         // skip echo time, echo damping, mod time, mod damping
140         // e[19] e[20] e[21] e[22]
141 
142         airAbsorbGainHf = eaxDbToAmp(e[23]);
143 
144         // skip HF Reference and LF Reference
145         // e[24] e[25]
146 
147         roomRolloffFactor = e[26];
148 
149         // skip flags
150         // e[27]
151     }
152 
getAirAbsorbGainHf()153     public float getAirAbsorbGainHf() {
154         return airAbsorbGainHf;
155     }
156 
setAirAbsorbGainHf(float airAbsorbGainHf)157     public void setAirAbsorbGainHf(float airAbsorbGainHf) {
158         this.airAbsorbGainHf = airAbsorbGainHf;
159     }
160 
getDecayHFRatio()161     public float getDecayHFRatio() {
162         return decayHFRatio;
163     }
164 
setDecayHFRatio(float decayHFRatio)165     public void setDecayHFRatio(float decayHFRatio) {
166         this.decayHFRatio = decayHFRatio;
167     }
168 
isDecayHfLimit()169     public boolean isDecayHfLimit() {
170         return decayHfLimit;
171     }
172 
setDecayHfLimit(boolean decayHfLimit)173     public void setDecayHfLimit(boolean decayHfLimit) {
174         this.decayHfLimit = decayHfLimit;
175     }
176 
getDecayTime()177     public float getDecayTime() {
178         return decayTime;
179     }
180 
setDecayTime(float decayTime)181     public void setDecayTime(float decayTime) {
182         this.decayTime = decayTime;
183     }
184 
getDensity()185     public float getDensity() {
186         return density;
187     }
188 
setDensity(float density)189     public void setDensity(float density) {
190         this.density = density;
191     }
192 
getDiffusion()193     public float getDiffusion() {
194         return diffusion;
195     }
196 
setDiffusion(float diffusion)197     public void setDiffusion(float diffusion) {
198         this.diffusion = diffusion;
199     }
200 
getGain()201     public float getGain() {
202         return gain;
203     }
204 
setGain(float gain)205     public void setGain(float gain) {
206         this.gain = gain;
207     }
208 
getGainHf()209     public float getGainHf() {
210         return gainHf;
211     }
212 
setGainHf(float gainHf)213     public void setGainHf(float gainHf) {
214         this.gainHf = gainHf;
215     }
216 
getLateReverbDelay()217     public float getLateReverbDelay() {
218         return lateReverbDelay;
219     }
220 
setLateReverbDelay(float lateReverbDelay)221     public void setLateReverbDelay(float lateReverbDelay) {
222         this.lateReverbDelay = lateReverbDelay;
223     }
224 
getLateReverbGain()225     public float getLateReverbGain() {
226         return lateReverbGain;
227     }
228 
setLateReverbGain(float lateReverbGain)229     public void setLateReverbGain(float lateReverbGain) {
230         this.lateReverbGain = lateReverbGain;
231     }
232 
getReflectDelay()233     public float getReflectDelay() {
234         return reflectDelay;
235     }
236 
setReflectDelay(float reflectDelay)237     public void setReflectDelay(float reflectDelay) {
238         this.reflectDelay = reflectDelay;
239     }
240 
getReflectGain()241     public float getReflectGain() {
242         return reflectGain;
243     }
244 
setReflectGain(float reflectGain)245     public void setReflectGain(float reflectGain) {
246         this.reflectGain = reflectGain;
247     }
248 
getRoomRolloffFactor()249     public float getRoomRolloffFactor() {
250         return roomRolloffFactor;
251     }
252 
setRoomRolloffFactor(float roomRolloffFactor)253     public void setRoomRolloffFactor(float roomRolloffFactor) {
254         this.roomRolloffFactor = roomRolloffFactor;
255     }
256 }
257