コード例 #1
0
ファイル: WmaWriter.cs プロジェクト: 4dvn/yeti
        /// <summary>
        /// Create the writer indicating Metadata information
        /// </summary>
        /// <param name="output"><see cref="System.IO.Stream"/> Where resulting WMA string will be written</param>
        /// <param name="format">PCM format of input data received in <see cref="WmaWriter.Write"/> method</param>
        /// <param name="profile">IWMProfile that describe the resulting compressed stream</param>
        /// <param name="metadataAttributes">Array of <see cref="yeti.wma.structs.WM_Attr"/> structures describing the metadata information that will be in the result stream</param>
        public WmaWriter(Stream output, WaveFormat format, IWMProfile profile, IEnumerable <WM_Attr> metadataAttributes)
            : base(output, format)
        {
            m_Writer = WM.CreateWriter();
            var wa = (IWMWriterAdvanced)m_Writer;

            wa.AddSink((IWMWriterSink)this);
            m_Writer.SetProfile(profile);
            uint inputs;

            m_Writer.GetInputCount(out inputs);
            if (inputs == 1)
            {
                IWMInputMediaProps inpProps;
                Guid type;
                m_Writer.GetInputProps(0, out inpProps);
                inpProps.GetType(out type);
                if (type == MediaTypes.WMMEDIATYPE_Audio)
                {
                    WM_MEDIA_TYPE mt;
                    mt.majortype            = MediaTypes.WMMEDIATYPE_Audio;
                    mt.subtype              = MediaTypes.WMMEDIASUBTYPE_PCM;
                    mt.bFixedSizeSamples    = true;
                    mt.bTemporalCompression = false;
                    mt.lSampleSize          = (uint)m_InputDataFormat.nBlockAlign;
                    mt.formattype           = MediaTypes.WMFORMAT_WaveFormatEx;
                    mt.pUnk     = IntPtr.Zero;
                    mt.cbFormat = (uint)Marshal.SizeOf(m_InputDataFormat);

                    GCHandle h = GCHandle.Alloc(m_InputDataFormat, GCHandleType.Pinned);
                    try
                    {
                        mt.pbFormat = h.AddrOfPinnedObject();
                        inpProps.SetMediaType(ref mt);
                    }
                    finally
                    {
                        h.Free();
                    }
                    m_Writer.SetInputProps(0, inpProps);
                    if (metadataAttributes != null)
                    {
                        var info = new WMHeaderInfo((IWMHeaderInfo)m_Writer);
                        foreach (WM_Attr attr in metadataAttributes)
                        {
                            info.SetAttribute(attr);
                        }
                        info = null;
                    }
                    m_Writer.BeginWriting();
                    m_Profile = profile;
                }
                else
                {
                    throw new ArgumentException("Invalid profile", "profile");
                }
            }
            else
            {
                throw new ArgumentException("Invalid profile", "profile");
            }
        }