- case SPATIAL_LOG:
- if(dist == 0)
- f = spatialmin + spatialdiff * (spatialfactor < 0); // avoid log(0), but do the right thing
- else
- f = spatialmin + spatialdiff * bound(0, (log(dist) - spatialoffset) * spatialfactor, 1);
- VectorScale(source_vec, f, source_vec);
- break;
- case SPATIAL_POW:
- f = spatialmin + spatialdiff * bound(0, (pow(dist, spatialpower) - spatialoffset) * spatialfactor, 1);
- VectorScale(source_vec, f, source_vec);
- break;
- case SPATIAL_THRESH:
- f = spatialmin + spatialdiff * (dist < spatialoffset);
- VectorScale(source_vec, f, source_vec);
- break;
- case SPATIAL_NONE:
- default:
- break;
+ Matrix4x4_Transform(&listener_basematrix, ch->origin, source_vec);
+ VectorNormalize(source_vec);
+
+ switch(spatialmethod)
+ {
+ case SPATIAL_LOG:
+ if(dist == 0)
+ f = spatialmin + spatialdiff * (spatialfactor < 0); // avoid log(0), but do the right thing
+ else
+ f = spatialmin + spatialdiff * bound(0, (log(dist) - spatialoffset) * spatialfactor, 1);
+ VectorScale(source_vec, f, source_vec);
+ break;
+ case SPATIAL_POW:
+ f = (pow(dist, spatialpower) - spatialoffset) * spatialfactor;
+ f = spatialmin + spatialdiff * bound(0, f, 1);
+ VectorScale(source_vec, f, source_vec);
+ break;
+ case SPATIAL_THRESH:
+ f = spatialmin + spatialdiff * (dist < spatialoffset);
+ VectorScale(source_vec, f, source_vec);
+ break;
+ case SPATIAL_NONE:
+ default:
+ break;
+ }
+
+ // the z axis needs to be removed and normalized because otherwise the volume would get lower as the sound source goes higher or lower then normal
+ source_vec[2] = 0;
+ VectorNormalize(source_vec);
+ angle_side = acos(source_vec[0]) / M_PI * 180; // angle between 0 and 180 degrees
+ angle_front = asin(source_vec[1]) / M_PI * 180; // angle between -90 and 90 degrees
+ if (angle_side > snd_spatialization_prologic_frontangle.value)
+ {
+ ch->prologic_invert = -1; // this will cause the right channel to do a 180 degrees phase shift (turning the sound wave upside down),
+ // but the best would be 90 degrees phase shift left and a -90 degrees phase shift right.
+ angle_factor = (angle_side - snd_spatialization_prologic_frontangle.value) / (360 - 2 * snd_spatialization_prologic_frontangle.value);
+ // angle_factor is between 0 and 1 and represents the angle range from the front left, to all the surround speakers (amount may vary,
+ // 1 in prologic I 2 in prologic II and 3 or 4 in prologic IIx) to the front right speaker.
+ if (angle_front > 0)
+ angle_factor = 1 - angle_factor;
+ }
+ else
+ angle_factor = angle_front / snd_spatialization_prologic_frontangle.value / 2.0 + 0.5;
+ //angle_factor is between 0 and 1 and represents the angle range from the front left to the center to the front right speaker