Exemple #1
0
        /// <summary>
        /// Creates a MediaType based on a given WaveFormat. Don't forget to call Free() for the returend MediaType.
        /// </summary>
        /// <param name="waveFormat">WaveFormat to create a MediaType from.</param>
        /// <returns>Dmo MediaType</returns>
        public static MediaType FromWaveFormat(WaveFormat waveFormat)
        {
            if (waveFormat == null)
            {
                throw new ArgumentNullException("waveFormat");
            }

            MediaType mediaType = new MediaType();

            NativeMethods.MoInitMediaType(ref mediaType, Marshal.SizeOf(waveFormat));

            mediaType.MajorType        = MediaTypes.MediaTypeAudio;
            mediaType.SubType          = WaveFormatExtensible.SubTypeFromWaveFormat(waveFormat);
            mediaType.FixedSizeSamples = (mediaType.SubType == MediaTypes.MEDIATYPE_IeeeFloat || mediaType.SubType == MediaTypes.MEDIATYPE_Pcm) ? 1 : 0;
            mediaType.FormatType       = FORMAT_WaveFormatEx;

            IntPtr hWaveFormat = Marshal.AllocHGlobal(Marshal.SizeOf(waveFormat));

            Marshal.StructureToPtr(waveFormat, hWaveFormat, false);

            if (hWaveFormat == IntPtr.Zero)
            {
                throw new InvalidOperationException("hWaveFormat == IntPtr.Zero");
            }
            if (mediaType.CbFormat < Marshal.SizeOf(waveFormat))
            {
                throw new InvalidOperationException("No memory for Format reserved");
            }
            mediaType.PtrFormat = hWaveFormat;

            return(mediaType);
        }
Exemple #2
0
        internal WaveFormat BuildOutputWaveFormat(IAudioSource audioSource)
        {
            if (audioSource == null)
            {
                throw new ArgumentNullException("source");
            }

            return(new WaveFormatExtensible(
                       audioSource.WaveFormat.SampleRate,
                       audioSource.WaveFormat.BitsPerSample,
                       OutputChannelCount,
                       WaveFormatExtensible.SubTypeFromWaveFormat(audioSource.WaveFormat),
                       OutputMask));
        }
        private static WaveFormat GetOutputWaveFormat(IWaveSource source, int sampleRate, ChannelMatrix channelMatrix)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (channelMatrix == null)
            {
                throw new ArgumentNullException("channelMatrix");
            }

            return(new WaveFormatExtensible(
                       sampleRate,
                       source.WaveFormat.BitsPerSample,
                       channelMatrix.OutputChannelCount,
                       WaveFormatExtensible.SubTypeFromWaveFormat(source.WaveFormat),
                       channelMatrix.OutputMask));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DmoChannelResampler"/> class.
        /// </summary>
        /// <param name="source">Underlying source which has to get resampled.</param>
        /// <param name="channelMatrix"><see cref="ChannelMatrix" /> which defines how to map each channel.</param>
        /// <param name="outputFormat">Waveformat, which specifies the new format. Note, that by far not all formats are supported.</param>
        /// <exception cref="System.ArgumentNullException">
        /// source
        /// or
        /// channelMatrix
        /// or
        /// outputFormat
        /// </exception>
        /// <exception cref="System.ArgumentException">The number of channels of the source has to be equal to the number of input channels specified by the channelMatrix.</exception>
        public DmoChannelResampler(IWaveSource source, ChannelMatrix channelMatrix, WaveFormat outputFormat)
            : base(source, outputFormat)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (channelMatrix == null)
            {
                throw new ArgumentNullException("channelMatrix");
            }
            if (outputFormat == null)
            {
                throw new ArgumentNullException("outputFormat");
            }

            if (source.WaveFormat.Channels != channelMatrix.InputChannelCount)
            {
                throw new ArgumentException(
                          "The number of channels of the source has to be equal to the number of input channels specified by the channelMatrix.");
            }

            var inputFormat = new WaveFormatExtensible(
                source.WaveFormat.SampleRate,
                source.WaveFormat.BitsPerSample,
                source.WaveFormat.Channels,
                WaveFormatExtensible.SubTypeFromWaveFormat(source.WaveFormat),
                channelMatrix.InputMask);

            Outputformat = new WaveFormatExtensible(
                outputFormat.SampleRate,
                outputFormat.BitsPerSample,
                outputFormat.Channels,
                WaveFormatExtensible.SubTypeFromWaveFormat(outputFormat),
                channelMatrix.OutputMask);

            Initialize(inputFormat, Outputformat);
            _channelMatrix = channelMatrix;
            CommitChannelMatrixChanges();
        }
        public DmoChannelResampler(IWaveSource source, ChannelMatrix channelMatrix, int destSampleRate)
            : base(source, destSampleRate)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (channelMatrix == null)
            {
                throw new ArgumentNullException("channelMatrix");
            }

            if (source.WaveFormat.Channels != channelMatrix.InputChannelCount)
            {
                throw new ArgumentException("source.WaveFormat.Channels != channelMatrix.InputChannelCount");
            }

            WaveFormatExtensible inputformat = new WaveFormatExtensible(source.WaveFormat.SampleRate, source.WaveFormat.BitsPerSample,
                                                                        source.WaveFormat.Channels, WaveFormatExtensible.SubTypeFromWaveFormat(source.WaveFormat), _channelMatrix.InputMask);

            _outputformat = new WaveFormat(destSampleRate, source.WaveFormat.BitsPerSample, 6, source.WaveFormat.WaveFormatTag, source.WaveFormat.ExtraSize);
            WaveFormatExtensible outputformat = new WaveFormatExtensible(_outputformat.SampleRate, _outputformat.BitsPerSample,
                                                                         _outputformat.Channels, WaveFormatExtensible.SubTypeFromWaveFormat(_outputformat), _channelMatrix.OutputMask);

            Init(inputformat, outputformat);
            _resampler.ResamplerProps.SetUserChannelMtx(_channelMatrix.GetMatrix());
        }