Exemplo n.º 1
0
        private static int CalSampleBit(BASS_CHANNELINFO_INTERNAL wavInfo, double durationSeconds, int dataLength, bool dataType)
        {
            int sampleBit = wavInfo.origres;

            if (sampleBit == 0)
            {
                //1秒内播放的数据 sd=data.Length / durationSeconds=fs*chans*bits
                sampleBit = (int)(dataLength / durationSeconds) / wavInfo.freq / wavInfo.chans * 8;
                if (dataType)
                {
                    sampleBit = sampleBit * 2;
                }

                if (sampleBit <= 8)
                {
                    sampleBit = 8;
                }
                else if (sampleBit <= 16)
                {
                    sampleBit = 16;
                }
                else if (sampleBit <= 24)
                {
                    sampleBit = 24;
                }
                else
                {
                    sampleBit = 32;
                }
            }

            return(sampleBit);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieves information on a channel
        /// </summary>
        /// <param name="handle">The channel handle... a HCHANNEL, HMUSIC, HSTREAM, or HRECORD</param>
        /// <returns>Pointer to structure to receive the channel information</returns>
        public static BASS_CHANNELINFO_INTERNAL ChannelGetInfo(int handle)
        {
            BASS_CHANNELINFO_INTERNAL info = new BASS_CHANNELINFO_INTERNAL();
            bool result = NativeMethods.BASS_ChannelGetInfo(handle, ref info);

            if (!result)
            {
                throw new WavException(BassErrorCode.GetErrorInfo());
            }

            return(info);
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Retrieves information on a channel
        /// </summary>
        /// <param name="handle">The channel handle... a HCHANNEL, HMUSIC, HSTREAM, or HRECORD</param>
        /// <returns>If successful, TRUE is returned, else FALSE is returned. Use BASS_ErrorGetCode to get the error code</returns>
        public static BASS_CHANNELINFO_INTERNAL BASS_ChannelGetInfo(int handle)
        {
            BASS_CHANNELINFO_INTERNAL info = new BASS_CHANNELINFO_INTERNAL();
            bool result = false;

            if (Environment.Is64BitProcess)
            {
                result = BassX64.BASS_ChannelGetInfo(handle, ref info);
            }
            else
            {
                result = BassX86.BASS_ChannelGetInfo(handle, ref info);
            }

            if (result)
            {
                return(info);
            }
            else
            {
                throw new ApplicationException("BASS_ChannelGetInfo Fail," + BassErrorCode.GetErrorInfo());
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
 public static extern bool BASS_ChannelGetInfo(int handle, [In, Out] ref BASS_CHANNELINFO_INTERNAL info);
Exemplo n.º 7
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="wavInfo">音频数据信息</param>
 /// <param name="durationSeconds">线性数据播放持续时长,单位/秒</param>
 /// <param name="data">线性数据</param>
 internal PcmDataInfo(BASS_CHANNELINFO_INTERNAL wavInfo, double durationSeconds, short[] data)
     : this(wavInfo.chans, wavInfo.freq, CalSampleBit(wavInfo, durationSeconds, data.Length, true), durationSeconds, data)
 {
 }