// @Brief : Get normalized data with Float. Between -1 and 1 public override float[] GetData() { // Since it is acquired every 2 bytes from pWaveData, it halves the length of pResult float[] pResult = new float[pLength / 2]; int sByteIndex = 0; for (int sIndex = 0; sIndex < pResult.Length; sIndex++) { // Get 2 byte short pShort = KrByteReader.ByteToShort(pWaveData, ref sByteIndex); // Convert byte to float -1 to 1 // NOTE : 1. Convert the value to a float // : 2. Divide the value by 32768 to complete the conversion pResult[sIndex] = (float)pShort / (short.MaxValue + 1); } return(pResult); }
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: // PROTECTED FUNCTION //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: // @Brief : Convert bytes // @Param : pBytes => byte data protected override void ConvertBytes(byte[] pBytes) { KrDebug.Log("Read wav data", typeof(KrWav)); int sIndex = 0; // Get RIFF header. 4byte string pRIFFHeader = KrByteReader.ByteToString(pBytes, 4, ref sIndex); if (!pRIFFHeader.Equals("RIFF")) { KrDebug.Assert(false, "Format is not REFF. string = " + pRIFFHeader, typeof(KrWav)); } // Get Subsequent file sizes. (File size - 8). 4byte int sFileSize = KrByteReader.ByteToInt(pBytes, ref sIndex); // Get WAVE header. 4byte string pWaveHeader = KrByteReader.ByteToString(pBytes, 4, ref sIndex); if (!pWaveHeader.Equals("WAVE")) { KrDebug.Assert(false, "Format is not WAVE string = " + pWaveHeader, typeof(KrWav)); } while (sIndex < sFileSize) { // Get tag string pTag = KrByteReader.ByteToString(pBytes, 4, ref sIndex); int sDataLength = KrByteReader.ByteToInt(pBytes, ref sIndex); if (pTag.Equals("fmt ")) { m_pFmt = new KrFmtChunk(); m_pFmt.sFormatId = KrByteReader.ByteToShort(pBytes, ref sIndex); m_pFmt.sChannelNum = KrByteReader.ByteToShort(pBytes, ref sIndex); m_pFmt.sSampleRate = KrByteReader.ByteToInt(pBytes, ref sIndex); m_pFmt.sDataSpeed = KrByteReader.ByteToInt(pBytes, ref sIndex); m_pFmt.sBlockSize = KrByteReader.ByteToShort(pBytes, ref sIndex); m_pFmt.sBitPerSample = KrByteReader.ByteToShort(pBytes, ref sIndex); // If sDataLength is greater than 16, there is an extension if (sDataLength > 16) { m_pFmt.sExtensionSize = KrByteReader.ByteToShort(pBytes, ref sIndex); m_pFmt.pExtension = KrByteReader.ExtractByte(pBytes, m_pFmt.sExtensionSize, ref sIndex); } KrDebug.Log(m_pFmt.ToString(), typeof(KrWav)); } else if (pTag.Equals("data")) { // Data output varies depending on the number of bits per sample (bit/sample) if (m_pFmt.sBitPerSample == 16) { m_pData = new KrDataChunk16(); } else { m_pData = new KrDataChunk(); } m_pData.pLength = sDataLength; m_pData.pWaveData = KrByteReader.ExtractByte(pBytes, sDataLength, ref sIndex); KrDebug.Log(m_pData.ToString(), typeof(KrWav)); } else { // Add data length sIndex += sDataLength; } } KrDebug.Log("Read complete", typeof(KrWav)); }