コード例 #1
0
        /// <summary>
        /// 加载音频文件
        /// </summary>
        /// <param name="filePath">音频路径</param>
        public void LoadWav(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath), "文件路径不能为空或null");
            }

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

            this._fileName = filePath;
            this.FreeBASS();
            base._handle            = WavHelper.StreamCreateFile(filePath);
            this._playEndSyncHandle = WavHelper.ChannelSetSync(this._handle, BASSSync.BASS_SYNC_END, -1, this._playEndNotifyCallback, IntPtr.Zero);
            //WavHelper.ChannelSetSync(this._handle, BASSSync.BASS_SYNC_MIXTIME, -1, this._playPositionChangedNotifyCallback, IntPtr.Zero);
            this.SetPlayerAttribute();
        }
コード例 #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);
            }
        }