Beispiel #1
0
        public LameEncConfig GetConfig(ref string summary)
        {
            LameEncConfig cfg = new LameEncConfig(this.WaveFormat, (uint)this.Bitrate);

            cfg.format.nMode           = (MpegMode)this.ChannelMode;
            cfg.format.dwReSampleRate  = (uint)this.ResampleFrequency;
            cfg.format.bStrictIso      = 1; // Audacity does the same
            cfg.format.nPreset         = LAME_QUALITY_PRESET.LQP_NOPRESET;
            cfg.format.bWriteVBRHeader = cfg.format.bEnableVBR = 1;
            cfg.format.dwBitrate       = cfg.format.dwMaxBitrate = 0;

            switch (BitrateMode)
            {
            case BitrateMode.CBR:
                cfg.format.bWriteVBRHeader = cfg.format.bEnableVBR = 0;
                cfg.format.dwBitrate       = (uint)this.Bitrate;
                cfg.format.nVbrMethod      = VBRMETHOD.VBR_METHOD_NONE;
                summary = Translator.Translate("TXT_CBR_SUMMARY", this.ChannelMode, this.Bitrate, this.ResampleFrequency);
                break;

            case BitrateMode.ABR:
                // ABR == VBR bitrate-based
                // Caution: LAME DLL calculates avg bitrate as (dwVbrAbr_bps + 500) / 1000
                cfg.format.dwVbrAbr_bps = (uint)this.Bitrate * 1000 - 500;
                cfg.format.nVBRQuality  = 4; // Audacity does the same
                cfg.format.nVbrMethod   = VBRMETHOD.VBR_METHOD_ABR;
                summary = Translator.Translate("TXT_ABR_SUMMARY", this.ChannelMode, this.Bitrate, this.ResampleFrequency);
                break;

            case BitrateMode.VBR:
                // VBR quality-based
                int q = 9 - this.VBRQuality;
                cfg.format.nVBRQuality = (int)Math.Max(0, Math.Min(9, q));
                cfg.format.nVbrMethod  = VBRMETHOD.VBR_METHOD_NEW;
                summary = Translator.Translate("TXT_VBR_SUMMARY", this.ChannelMode, cfg.format.nVBRQuality, this.ResampleFrequency);
                break;

            case BitrateMode.Preset:
                // Preset mode is VBR or CBR/320 in case that VBRPreset == INSANE
                cfg.format.nPreset = (LAME_QUALITY_PRESET)this.VBRPreset;
                if (this.VBRPreset != Preset.Insane)
                {
                    summary = Translator.Translate("TXT_PRESET_SUMMARY", this.ChannelMode, this.VBRPreset, this.ResampleFrequency);
                }
                else
                {
                    summary = Translator.Translate("TXT_PRESET_INSANE", this.ChannelMode, this.VBRPreset, this.ResampleFrequency);
                }
                break;
            }

            return(cfg);
        }
Beispiel #2
0
        public void EncodeBuffer(byte[] buff, string destFile, bool generateTags, ID3FileInfoSlim ifiSlim)
        {
            string summary = "";

            LameEncConfig cfg = this.Options.GetConfig(ref summary);

            if (ifiSlim != null && ifiSlim.Frequency.HasValue)
            {
                cfg.format.dwSampleRate = (uint)ifiSlim.Frequency;
            }

            uint LameResult = Lame_encDll.Init(cfg, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream);

            if (LameResult != Lame_encDll.ERR_SUCCESSFUL)
            {
                throw new ApplicationException(string.Format("Lame_encDll.Init failed with the error code {0}", LameResult));
            }

            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None))
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    uint EncodedSize = 0;
                    m_OutBuffer = new byte[m_OutBufferSize];

                    if (MustCancel())
                    {
                        return;
                    }

                    try
                    {
                        int buffPos = 0;
                        while (buffPos < buff.Length)
                        {
                            if (MustCancel())
                            {
                                return;
                            }

                            m_OutBuffer = new byte[m_OutBufferSize];

                            uint bytesToCopy = Math.Min(2 * m_InputSamples, (uint)buff.Length - (uint)buffPos);

                            if ((LameResult = Lame_encDll.Encode(m_hLameStream, buff, buffPos, (uint)bytesToCopy, m_OutBuffer, ref EncodedSize)) == Lame_encDll.ERR_SUCCESSFUL)
                            {
                                if (EncodedSize > 0)
                                {
                                    bw.Write(m_OutBuffer, 0, (int)EncodedSize);
                                }
                            }
                            else
                            {
                                throw new ApplicationException(string.Format("Lame_encDll.Encode failed with the error code {0}", LameResult));
                            }

                            buffPos += (int)bytesToCopy;
                        }
                    }
                    finally
                    {
                        EncodedSize = 0;
                        if (Lame_encDll.Release(m_hLameStream, m_OutBuffer, ref EncodedSize) == Lame_encDll.ERR_SUCCESSFUL)
                        {
                            if (EncodedSize > 0)
                            {
                                bw.Write(m_OutBuffer, 0, (int)EncodedSize);
                            }
                        }
                        Lame_encDll.Close(m_hLameStream);
                    }
                }

            if (!MustCancel() && cfg.format.bWriteVBRHeader != 0)
            {
                uint err = Lame_encDll.WriteVBRHeader(destFile);
            }

            if (!MustCancel() && generateTags && ifiSlim != null)
            {
                ID3FileInfo ifi = new ID3FileInfo(destFile, false);
                ifi.Album  = StringUtils.Capitalize(ifiSlim.Album, WordCasing.CapitalizeWords);
                ifi.Artist = StringUtils.Capitalize(ifiSlim.Artist, WordCasing.CapitalizeWords);
                ifi.Genre  = StringUtils.Capitalize(ifiSlim.Genre, WordCasing.CapitalizeWords);
                ifi.Title  = StringUtils.Capitalize(ifiSlim.Title, WordCasing.CapitalizeWords);
                ifi.Track  = ifiSlim.Track.GetValueOrDefault();
                ifi.Year   = ifiSlim.Year.GetValueOrDefault();
                ifi.Save();
            }
        }
 public static extern uint Init(LameEncConfig pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);