unsafe private static short mix(short a, short b)
{
// From: http://atastypixel.com/blog/how-to-mix-audio-samples-properly-on-ios/
if (a < 0 && b < 0)
// If both samples are negative, mixed signal must have
// an amplitude between the lesser of A and B, and the
// minimum permissible negative amplitude
return (short)(((int)a + (int)b) - (((int)a * (int)b) / Int16.MinValue));
else if (a > 0 && b > 0)
// If both samples are positive, mixed signal must
// have an amplitude between the greater of A and B,
// and the maximum permissible positive amplitude
return (short)(((int)a + (int)b) - (((int)a * (int)b) / Int16.MaxValue));
else
// If samples are on opposite sides of the 0-crossing,
// mixed signal should reflect that samples cancel each
// other out somewhat
return (short)(a + b);
}