Exemplo n.º 1
0
        private void SetupAudio(SoundFormat audioFormat, AcmEncoder audioEncoder)
        {
            IntPtr pwfx = audioFormat.ToPtr();

            try {
                Avi32Interop.AVISTREAMINFO asi = new Avi32Interop.AVISTREAMINFO();
                asi.fccType               = Avi32Interop.streamtypeAUDIO;
                asi.dwScale               = audioFormat.BlockAlign;
                asi.dwRate                = audioFormat.AverageBytesPerSecond;
                asi.dwStart               = 0;
                asi.dwLength              = -1;
                asi.dwInitialFrames       = 0;
                asi.dwSuggestedBufferSize = 0;
                asi.dwQuality             = -1;
                asi.dwSampleSize          = audioFormat.BlockAlign;
                int hr = Avi32Interop.AVIFileCreateStream(this.pAviFile, out this.pAudioStream, ref asi);
                if (hr != 0)
                {
                    throw new AviException("AVIStreamSetFormat", hr);
                }
                hr = Avi32Interop.AVIStreamSetFormat(this.pAudioStream, 0, pwfx, audioFormat.ToalSize);
                if (hr != 0)
                {
                    throw new AviException("AVIStreamSetFormat", hr);
                }
                if (audioEncoder != null)
                {
                    audioEncoder.Open();
                }
                this.audioFormat  = audioFormat;
                this.audioEncoder = audioEncoder;
            }
            finally {
                Marshal.FreeHGlobal(pwfx);
            }
        }
Exemplo n.º 2
0
        public static AcmConvertionMap GetConvertionMap(SoundFormat[] inputFormats, SoundFormatTag tagFilter)
        {
            // First, we enumerate convertion formats
            AcmConvertionMap initConvertionMap = new AcmConvertionMap();
            int maxFormatSize = GetMaxFormatSize();

            // Enumerate acm drivers
            foreach (int driverId in GetDriverIds())
            {
                // Open driver
                IntPtr phDriver;
                int    mmr = AcmInterop.acmDriverOpen(out phDriver, driverId, 0);
                if (mmr != 0)
                {
                    continue;
                }
                // For each input format, we do enumeration
                foreach (SoundFormat inputFormat in inputFormats)
                {
                    // Fill format details struct
                    AcmInterop.ACMFORMATDETAILS fmtDetails = new AcmInterop.ACMFORMATDETAILS();
                    IntPtr pwfxFormat = inputFormat.ToPtr(maxFormatSize);
                    fmtDetails.cbStruct = Marshal.SizeOf(fmtDetails);
                    fmtDetails.pwfx     = pwfxFormat;
                    fmtDetails.cbwfx    = maxFormatSize;

                    // Enumerate convertion formats
                    callbackFormats = new List <SoundFormat>();
                    IntPtr pwfxInput = inputFormat.ToPtr();
                    mmr = AcmInterop.acmFormatEnum(phDriver, ref fmtDetails, FormatEnumCallback, IntPtr.Zero,
                                                   AcmInterop.ACM_FORMATENUMF_CONVERT);
                    Marshal.FreeHGlobal(pwfxInput);

                    // Add formats to the map (if succeed)
                    if (mmr == 0)
                    {
                        initConvertionMap.Add(inputFormat, callbackFormats);
                    }
                    callbackFormats = null;
                }

                // Close driver
                mmr = AcmInterop.acmDriverClose(phDriver, 0);
            }

            // Now we query ACM to make sure each convertion is supported
            AcmConvertionMap finalConvertionMap = new AcmConvertionMap();

            SoundFormat[] inputs = initConvertionMap.GetInputs();
            foreach (SoundFormat inputFormat in inputs)
            {
                IntPtr pwfxSrc = inputFormat.ToPtr();
                foreach (SoundFormat outputFormat in initConvertionMap.GetOutputs(inputFormat))
                {
                    // Filter tags
                    if (tagFilter != SoundFormatTag.UNKNOWN && outputFormat.Tag != tagFilter)
                    {
                        continue;
                    }
                    IntPtr phs;
                    IntPtr pwfxDst = outputFormat.ToPtr();
                    // Open acm stream using the query flag
                    int mmr = AcmInterop.acmStreamOpen(out phs, IntPtr.Zero, pwfxSrc, pwfxDst, IntPtr.Zero, IntPtr.Zero,
                                                       UIntPtr.Zero, AcmInterop.ACM_STREAMOPENF_QUERY);
                    Marshal.FreeHGlobal(pwfxDst);

                    // Add format to the final map if succeed
                    if (mmr == 0)
                    {
                        finalConvertionMap.Add(inputFormat, outputFormat);
                    }
                }
                Marshal.FreeHGlobal(pwfxSrc);
            }
            return(finalConvertionMap);
        }