コード例 #1
0
        public static SoundFormat[] GetFormats(string deviceId, bool queryEncoderFormats)
        {
            // Get device by id
            if (string.IsNullOrEmpty(deviceId))
            {
                throw new ArgumentNullException("deviceId");
            }
            if (!queryEncoderFormats)
            {
                return(sharedWrapper.GetDeviceFormats(deviceId));
            }
            // Get device formats
            SoundFormat[] deviceFormats = sharedWrapper.GetDeviceFormats(deviceId);
            // Get list of input formats that has not been quieried yet
            List <SoundFormat> newInputFormats = new List <SoundFormat>();

            foreach (SoundFormat deviceFormat in deviceFormats)
            {
                // Any convertion exists?
                if (!convertionMap.Contains(deviceFormat) && deviceFormat.Tag == preferredFormat.Tag)
                {
                    newInputFormats.Add(deviceFormat);
                }
            }
            // Is there any new format to query
            if (newInputFormats.Count > 0)
            {
                var encoderConvertionMap = AcmEncoder.GetConvertionMap(newInputFormats.ToArray(), preferredFormat.Tag);
                // Add new map to the current map
                convertionMap.Add(encoderConvertionMap);
                // Add no convertion -input format as output format- to the map
                foreach (SoundFormat newInputFormat in newInputFormats)
                {
                    convertionMap.Add(newInputFormat, newInputFormat);
                }
            }
            // Get all of the output formats matching device formats as input
            return(convertionMap.GetOutputs(deviceFormats));
        }
コード例 #2
0
        private SoundFormat GetInputFormat(out bool encoderNeeded)
        {
            int nMaxAvgBytesPerSec = 0;

            // Get device formats
            SoundFormat[]      deviceFormats    = this.wrapper.GetDeviceFormats(deviceId);
            List <SoundFormat> deviceFormatList = new List <SoundFormat>(deviceFormats);
            SoundFormat        inputFormat      = null;

            if (this.format == null)
            {
                // If format is not specified, find the format with maximum average bytes per second
                foreach (SoundFormat deviceFormat in deviceFormatList)
                {
                    if (inputFormat == null || nMaxAvgBytesPerSec < deviceFormat.AverageBytesPerSecond)
                    {
                        inputFormat        = deviceFormat;
                        nMaxAvgBytesPerSec = deviceFormat.AverageBytesPerSecond;
                    }
                }
                if (inputFormat == null)
                {
                    // This happens only if device has not formats
                    throw new InvalidOperationException("Cannot find an appropriate input format.");
                }
                encoderNeeded = false;
                return(inputFormat);
            }

            // Check if device supports the format
            if (deviceFormatList.Contains(this.format))
            {
                encoderNeeded = false;
                return(this.format);
            }

            // Get available input formats for convertion
            SoundFormat[] availableInputs = convertionMap.GetInputs(this.format);
            if (availableInputs.Length == 0)
            {
                // Get convertion map again
                // We currenty use PCM format for output.
                convertionMap.Add(AcmEncoder.GetConvertionMap(deviceFormatList.ToArray(), preferredFormat.Tag));
                // Get available input formats for convertion
                availableInputs = convertionMap.GetInputs(this.format);
                if (availableInputs.Length == 0)
                {
                    throw new InvalidOperationException("Cannot find an appropriate input format.");
                }
            }

            // Find the input format that device supports and has
            // maximum average bytes per second
            foreach (SoundFormat input in availableInputs)
            {
                if (deviceFormatList.Contains(input))
                {
                    if (nMaxAvgBytesPerSec < input.AverageBytesPerSecond &&
                        (inputFormat == null ||
                         input.AverageBytesPerSecond == (input.BitsPerSample / 8) * input.Channels * input.SamplesPerSecond))
                    {
                        inputFormat        = input;
                        nMaxAvgBytesPerSec = (int)input.AverageBytesPerSecond;
                    }
                }
            }
            if (inputFormat == null)
            {
                throw new InvalidOperationException("Cannot find an appropriate input format.");
            }
            encoderNeeded = true;
            return(inputFormat);
        }