| 16 | } |
| 17 | |
| 18 | double ConeEffect::gain(FloatPoint3D sourcePosition, FloatPoint3D sourceOrientation, FloatPoint3D listenerPosition) |
| 19 | { |
| 20 | if (is_zero(sourceOrientation) || ((m_innerAngle == 360.0) && (m_outerAngle == 360.0))) |
| 21 | return 1.0; // no cone specified - unity gain |
| 22 | |
| 23 | // Normalized source-listener vector |
| 24 | FloatPoint3D sourceToListener = listenerPosition - sourcePosition; |
| 25 | sourceToListener = normalize(sourceToListener); |
| 26 | |
| 27 | FloatPoint3D normalizedSourceOrientation = sourceOrientation; |
| 28 | normalizedSourceOrientation = normalize(normalizedSourceOrientation); |
| 29 | |
| 30 | // Angle between the source orientation vector and the source-listener vector |
| 31 | double dotProduct = dot(sourceToListener, normalizedSourceOrientation); |
| 32 | double angle = 180.0 * acos(dotProduct) / static_cast<double>(LAB_PI); |
| 33 | double absAngle = fabs(angle); |
| 34 | |
| 35 | // Divide by 2.0 here since API is entire angle (not half-angle) |
| 36 | double absInnerAngle = fabs(m_innerAngle) / 2.0; |
| 37 | double absOuterAngle = fabs(m_outerAngle) / 2.0; |
| 38 | double gain = 1.0; |
| 39 | |
| 40 | if (absAngle <= absInnerAngle) |
| 41 | // No attenuation |
| 42 | gain = 1.0; |
| 43 | else if (absAngle >= absOuterAngle) |
| 44 | // Max attenuation |
| 45 | gain = m_outerGain; |
| 46 | else |
| 47 | { |
| 48 | // Between inner and outer cones |
| 49 | // inner -> outer, x goes from 0 -> 1 |
| 50 | double x = (absAngle - absInnerAngle) / (absOuterAngle - absInnerAngle); |
| 51 | gain = (1.0 - x) + m_outerGain * x; |
| 52 | } |
| 53 | |
| 54 | return gain; |
| 55 | } |
| 56 | |
| 57 | } // namespace lab |