Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new WaveFormat struct.
        /// </summary>
        /// <param name="rate">The SamplesPerSecond.</param>
        /// <param name="bits">The BitsPerSample.</param>
        /// <param name="channels">The Channels.</param>
        public WaveFormat(int rate, int bits, int channels)
        {
            Format        = WaveFormats.Pcm;
            Channels      = (short)channels;
            SamplesPerSec = rate;
            BitsPerSample = (short)bits;
            cbSize        = 0;

            BlockAlign     = (short)(channels * (bits / 8));
            AvgBytesPerSec = SamplesPerSec * BlockAlign;
        }
Ejemplo n.º 2
0
        public bool FindAudioInputFormat(uint inputNum, Guid subtype, WaveFormat readerWaveFormat)
        {
            bool success = false;
            IWMInputMediaProps writerInputProps = null;
            WM_MEDIA_TYPE      mediaType;
            uint bufferSize = (uint)(Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat)));
            uint formatCount;

            Logger.WriteLogMessage("Finding audio input formats for writer, input [" + inputNum + "].");

            _writer.GetInputFormatCount(inputNum, out formatCount);

            Logger.WriteLogMessage("Audio writer can consume " + formatCount + " possible audio input formats.");

            IntPtr buffer = Marshal.AllocCoTaskMem((int)bufferSize);

            try
            {
                for (uint j = 0; j < formatCount; j++)
                {
                    uint size = 0;

                    try
                    {
                        _writer.GetInputFormat(inputNum, j, out writerInputProps);

                        writerInputProps.GetMediaType(IntPtr.Zero, ref size);

                        if (size > bufferSize)
                        {
                            bufferSize = size;
                            Marshal.FreeCoTaskMem(buffer);
                            buffer = Marshal.AllocCoTaskMem((int)bufferSize);
                        }

                        writerInputProps.GetMediaType(buffer, ref size);

                        mediaType = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE));

                        if (mediaType.formattype == FormatTypes.WMFORMAT_WaveFormatEx)
                        {
                            Logger.WriteLogMessage("Found writer audio input format [" + j + "], format type [" + GetFormatTypeName(mediaType.formattype) + "], subtype [" + GetSubTypeName(mediaType.subtype) + "], sample size [" + mediaType.lSampleSize + "].");

                            WaveFormat  waveFormat = (WaveFormat)Marshal.PtrToStructure(mediaType.pbFormat, typeof(WaveFormat));
                            WaveFormats format     = (WaveFormats)waveFormat.wFormatTag;

                            Logger.WriteLogMessage("Found audio stream, format [" + format + "], sample rate [" + waveFormat.nSamplesPerSec + "], bits per sample [" + waveFormat.wBitsPerSample +
                                                   "], bytes/sec [" + waveFormat.nAvgBytesPerSec + "], channels [" + waveFormat.nChannels + "].");

                            if (waveFormat.nSamplesPerSec == readerWaveFormat.nSamplesPerSec &&
                                waveFormat.nChannels == readerWaveFormat.nChannels &&
                                waveFormat.wBitsPerSample == readerWaveFormat.wBitsPerSample &&
                                waveFormat.wFormatTag == readerWaveFormat.wFormatTag)
                            {
                                writerInputProps.SetMediaType(ref mediaType);

                                _writer.SetInputProps(inputNum, writerInputProps);
                                success = true;
                                break;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // error handle
                        throw;
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(writerInputProps);
                        writerInputProps = null;
                    }
                }
            }
            catch (Exception)
            {
                // error handle
                throw;
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
            }

            return(success);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new WaveFormat struct.
        /// </summary>
        /// <param name="rate">The SamplesPerSecond.</param>
        /// <param name="bits">The BitsPerSample.</param>
        /// <param name="channels">The Channels.</param>
        public WaveFormat(int rate, int bits, int channels)
        {
            Format = WaveFormats.Pcm;
            Channels = (short) channels;
            SamplesPerSec = rate;
            BitsPerSample = (short) bits;
            cbSize = 0;

            BlockAlign = (short) (channels*(bits/8));
            AvgBytesPerSec = SamplesPerSec*BlockAlign;
        }
Ejemplo n.º 4
0
        public void FindAudioOutputFormat(uint outputNum, ref WM_MEDIA_TYPE mediaType, ref Guid subtype, ref WaveFormat waveFormat)
        {
            IWMOutputMediaProps readerOutputProps = null;
            uint bufferSize = (uint)(Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat)));
            uint formatCount;

            Logger.WriteLogMessage("Finding audio output formats for reader, output [" + outputNum + "].");

            _reader.GetOutputFormatCount(outputNum, out formatCount);

            Logger.WriteLogMessage("Reader can produce " + formatCount + " possible audio output formats.");

            IntPtr buffer = Marshal.AllocCoTaskMem((int)bufferSize);

            try
            {
                for (uint j = 0; j < formatCount; j++)
                {
                    uint size = 0;

                    _reader.GetOutputFormat(outputNum, j, out readerOutputProps);

                    readerOutputProps.GetMediaType(IntPtr.Zero, ref size);

                    if (size > bufferSize)
                    {
                        bufferSize = size;
                        Marshal.FreeCoTaskMem(buffer);
                        buffer = Marshal.AllocCoTaskMem((int)bufferSize);
                    }

                    readerOutputProps.GetMediaType(buffer, ref size);

                    mediaType = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE));

                    if (mediaType.formattype == FormatTypes.WMFORMAT_WaveFormatEx)
                    {
                        Logger.WriteLogMessage("Walking output format [" + j + "], format type [" + GetFormatTypeName(mediaType.formattype) + "], subtype [" + GetSubTypeName(mediaType.subtype) + "], sample size [" + mediaType.lSampleSize + "].");

                        //
                        // NOTE: only look for PCM subtypes
                        //
                        if (mediaType.subtype == MediaSubTypes.WMMEDIASUBTYPE_PCM)
                        {
                            subtype = mediaType.subtype;

                            Logger.WriteLogMessage("- Found PCM sub type, grabbing WaveFormat.");

                            waveFormat = (WaveFormat)Marshal.PtrToStructure(mediaType.pbFormat, typeof(WaveFormat));
                            WaveFormats format = (WaveFormats)waveFormat.wFormatTag;

                            Logger.WriteLogMessage("- format [" + format + "], sample rate [" + waveFormat.nSamplesPerSec + "], bits per sample [" + waveFormat.wBitsPerSample +
                                                   "], bytes/sec [" + waveFormat.nAvgBytesPerSec + "], channels [" + waveFormat.nChannels + "].");

                            _reader.SetOutputProps(outputNum, readerOutputProps);
                            break;
                        }
                    }
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
            }

            Marshal.ReleaseComObject(readerOutputProps);
        }
Ejemplo n.º 5
0
 /// <summary>Creates a new empty in-memory wave file in specified audio format</summary>
 /// <param name="sampleRate">Desired sample rate</param>
 /// <param name="bitsPerSample">Desired bits-per-sample</param>
 /// <param name="channels">Desired data channels</param>
 /// <param name="audioFormat">Desired audio format</param>
 /// <remarks>Consumer will need to apply appropriate data compression for non-PCM data formats.</remarks>
 public WaveFile(SampleRate sampleRate, BitsPerSample bitsPerSample, DataChannels channels, WaveFormats audioFormat)
     : this((int)sampleRate, (short)bitsPerSample, (short)channels, (short)audioFormat)
 {
 }