/// <summary> /// 获取音频文件信息 /// </summary> /// <returns>音频文件信息</returns> public BASS_CHANNELINFO_INTERNAL GetWavInfo() { if (this.HandleValid()) { return(WavHelper.ChannelGetInfo(this._handle)); } else { throw new WavException("未加载音频数据"); } }
/// <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); } }
/// <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); } }