示例#1
0
 protected virtual void FreeBASS()
 {
     try
     {
         if (this.HandleValid())
         {
             WavHelper.StreamFree(this._handle);
             this._handle = WavConstant.NONE_HANDLE;
         }
     }
     catch (Exception ex)
     {
         WavLoger.OnRaiseLog(this, ex);
     }
 }
示例#2
0
        /// <summary>
        /// 获取解码后的线性short数据
        /// </summary>
        /// <returns>解码后的线性short数据</returns>
        public static PcmDataInfo GetPcmDataShort(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("源文件不存在", filePath);
            }

            int handle = WavHelper.StreamCreateFile(filePath, 0, 0, BASSFlag.BASS_UNICODE | BASSFlag.BASS_STREAM_DECODE);

            try
            {
                long   byteLength      = WavHelper.ChannelGetLength(handle, BASSMode.BASS_POS_BYTE);
                double durationSeconds = WavHelper.ChannelBytes2Seconds(handle, byteLength);

                int     shortCount = (int)(byteLength);
                short[] pcmData    = new short[shortCount];
                int     count      = WavHelper.ChannelGetData(handle, pcmData, shortCount);

                //注:结果数据长度之所以除以2,是因为short=2byte,bass获取数据时必须使用字节长度,否则数据会少一半;
                //但是使用字节长度获取数据后,后一半数据又全是0,需要截取掉

                count = count / 2;
                short[] pcmDataSrc = pcmData;
                pcmData = new short[count];
                Array.Copy(pcmDataSrc, pcmData, count);

                BASS_CHANNELINFO_INTERNAL wavInfo = WavHelper.ChannelGetInfo(handle);
                return(new PcmDataInfo(wavInfo, durationSeconds, pcmData));
            }
            finally
            {
                WavHelper.StreamFree(handle);
            }
        }
示例#3
0
        /// <summary>
        /// 获取解码后的线性byte数据
        /// </summary>
        /// <returns>解码后的线性byte数据</returns>
        public static PcmDataInfo GetPcmDataByte(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("源文件不存在", filePath);
            }

            int handle = WavHelper.StreamCreateFile(filePath, 0, 0, BASSFlag.BASS_UNICODE | BASSFlag.BASS_STREAM_DECODE);

            try
            {
                long   byteLength      = WavHelper.ChannelGetLength(handle, BASSMode.BASS_POS_BYTE);
                double durationSeconds = WavHelper.ChannelBytes2Seconds(handle, byteLength);

                byte[] pcmData = new byte[byteLength];
                int    count   = WavHelper.ChannelGetData(handle, pcmData, (int)byteLength);
                if (count != byteLength)
                {
                    WavLoger.OnRaiseLog(filePath, $"获取到文件长度:{byteLength},读取到数据长度:{count},二者不一致,根据实际读取进行拷贝");
                    byte[] pcmDataSrc = pcmData;
                    pcmData = new byte[count];
                    Array.Copy(pcmDataSrc, pcmData, pcmData.Length);
                }

                BASS_CHANNELINFO_INTERNAL wavInfo = WavHelper.ChannelGetInfo(handle);
                return(new PcmDataInfo(wavInfo, durationSeconds, pcmData));
            }
            finally
            {
                WavHelper.StreamFree(handle);
            }
        }