예제 #1
0
        /// <summary>
        /// Resamples data from multiple channels to single one.
        /// </summary>
        /// <param name="audio">audio with</param>
        public static short[] ConvertToMono(IAudioFormat audio)
        {
            if (audio == null)
            {
                throw new ArgumentNullException("Argument 'audio' is null.");
            }
            if (audio.Data == null)
            {
                throw new ArgumentNullException("Argument 'audio.Data' is null");
            }

            switch (audio.Channels)
            {
            case 1:
                return(audio.Data);

            case 2:
                short[] mono = new short[audio.NumOfDataSamples / 2];

                for (int i = 0; i < audio.NumOfDataSamples; i += 2)                         //4 bytes per loop are processed (2 left + 2 right samples)
                {
                    mono[i / 2] = Arithmetics.Average(audio.Data[i], audio.Data[i + 1]);
                }

                return(mono);

            default:
                throw new NotImplementedException($"Convert from {audio.Channels} channels to mono is not supported.");
            }
        }