OpenBve.Sounds.GetMonoMix C# (CSharp) Method

GetMonoMix() private static method

Mixes all channels into a single channel.
Raised when the bits per sample are not supported.
private static GetMonoMix ( Sound sound ) : byte[]
sound Sound The sound.
return byte[]
		private static byte[] GetMonoMix(Sound sound) {
			/*
			 * Convert integer samples to floating-point samples.
			 */
			float[][] samples;
			if (sound.Bytes.Length == 1 || sound.Bytes[0].Length == 0) {
				return sound.Bytes[0];
			}
		    switch (sound.BitsPerSample)
		    {
		        case 8:
		            samples = new float[sound.Bytes.Length][];
		            for (int i = 0; i < sound.Bytes.Length; i++) {
		                samples[i] = new float[sound.Bytes[i].Length];
		                for (int j = 0; j < sound.Bytes[i].Length; j++) {
		                    byte value = sound.Bytes[i][j];
		                    samples[i][j] = ((float)value - 128.0f) / (value < 128 ? 128.0f : 127.0f);
		                }
		            }
		            break;
		        case 16:
		            samples = new float[sound.Bytes.Length][];
		            for (int i = 0; i < sound.Bytes.Length; i++) {
		                samples[i] = new float[sound.Bytes[i].Length >> 1];
		                for (int j = 0; j < sound.Bytes[i].Length; j += 2) {
		                    short value = (short)(ushort)((int)sound.Bytes[i][j] | ((int)sound.Bytes[i][j + 1] << 8));
		                    samples[i][j >> 1] = (float)value / (value < 0 ? 32768.0f : 32767.0f);
		                }
		            }
		            break;
		        default:
		            throw new NotSupportedException();
		    }
		    /*
			 * Mix floating-point samples to mono.
			 * */
			float[] mix = GetNormalizedMonoMix(samples);
			/*
			 * Convert floating-point samples to integer samples.
			 */
			byte[] result;
			switch (sound.BitsPerSample)
			{
			    case 8:
			        result = new byte[mix.Length];
			        for (int i = 0; i < mix.Length; i++) {
			            result[i] = (byte)((mix[i] < 0.0f ? 128.0f : 127.0f) * mix[i] + 128.0f);
			        }
			        break;
			    case 16:
			        result = new byte[2 * mix.Length];
			        for (int i = 0; i < mix.Length; i++) {
			            int value = (int)(ushort)(short)((mix[i] < 0.0f ? 32768.0f : 32767.0f) * mix[i]);
			            result[2 * i + 0] = (byte)value;
			            result[2 * i + 1] = (byte)(value >> 8);
			        }
			        break;
			    default:
			        throw new NotSupportedException();
			}
			return result;
		}