internal uint DecodeBlocks(byte[] waveDataBuffer, uint startBlockIndex)
        {
            if (waveDataBuffer == null)
            {
                throw new ArgumentNullException(nameof(waveDataBuffer));
            }
            var waveBlockSize = GetMinWaveDataBufferSize();

            if (waveDataBuffer.Length < waveBlockSize)
            {
                throw new HcaException(ErrorMessages.GetBufferTooSmall(waveBlockSize, waveDataBuffer.Length), ActionResult.BufferTooSmall);
            }
            var hcaInfo           = HcaInfo;
            var numBlocksToDecode = (uint)(waveDataBuffer.Length / waveBlockSize);

            if (startBlockIndex + numBlocksToDecode >= hcaInfo.BlockCount)
            {
                numBlocksToDecode = hcaInfo.BlockCount - startBlockIndex;
            }
            if (numBlocksToDecode == 0)
            {
                return(0);
            }
            TransformWaveDataBlocks(SourceStream, waveDataBuffer, startBlockIndex, numBlocksToDecode, GetProperWaveWriter());
            return(numBlocksToDecode);
        }
        internal uint DecodeBlock(byte[] waveDataBuffer, uint blockIndex)
        {
            if (waveDataBuffer == null)
            {
                throw new ArgumentNullException(nameof(waveDataBuffer));
            }
            var waveBlockSize = GetMinWaveDataBufferSize();

            if (waveDataBuffer.Length < waveBlockSize)
            {
                throw new HcaException(ErrorMessages.GetBufferTooSmall(waveBlockSize, waveDataBuffer.Length), ActionResult.BufferTooSmall);
            }
            TransformWaveDataBlocks(SourceStream, waveDataBuffer, blockIndex, 1, GetProperWaveWriter());
            return(1);
        }
Esempio n. 3
0
        public int WriteWaveHeader(byte[] stream, AudioParams audioParams)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            var hcaInfo = HcaInfo;

            if (hcaInfo.LoopFlag && audioParams.InfiniteLoop)
            {
                // See remarks of AudioParams.InfiniteLoop.
                throw new HcaException(ErrorMessages.GetInvalidParameter(nameof(audioParams.InfiniteLoop)), ActionResult.InvalidParameter);
            }
            var minimumHeaderBufferSize = GetMinWaveHeaderBufferSize();

            if (stream.Length < minimumHeaderBufferSize)
            {
                throw new HcaException(ErrorMessages.GetBufferTooSmall(minimumHeaderBufferSize, stream.Length), ActionResult.BufferTooSmall);
            }
            var sampleBits = GetSampleBitsFromParams();
            var wavRiff    = WaveRiffSection.CreateDefault();
            var wavNote    = WaveNoteSection.CreateDefault();
            var wavData    = WaveDataSection.CreateDefault();

            wavRiff.FmtType          = (ushort)(_decodeParams.Mode != SamplingMode.R32 ? 1 : 3);
            wavRiff.FmtChannels      = (ushort)hcaInfo.ChannelCount;
            wavRiff.FmtBitCount      = (ushort)(sampleBits > 0 ? sampleBits : sizeof(float));
            wavRiff.FmtSamplingRate  = hcaInfo.SamplingRate;
            wavRiff.FmtSamplingSize  = (ushort)(wavRiff.FmtBitCount / 8 * wavRiff.FmtChannels);
            wavRiff.FmtSamplesPerSec = wavRiff.FmtSamplingRate * wavRiff.FmtSamplingSize;
            if (hcaInfo.Comment != null)
            {
                wavNote.NoteSize = 4 + hcaInfo.CommentLength + 1;
                if ((wavNote.NoteSize & 3) != 0)
                {
                    wavNote.NoteSize += 4 - (wavNote.NoteSize & 3);
                }
            }

            var totalBlockCount = hcaInfo.BlockCount;

            if (hcaInfo.LoopFlag)
            {
                totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart) * audioParams.SimulatedLoopCount;
            }
            wavData.DataSize = totalBlockCount * 0x80 * 8 * wavRiff.FmtSamplingSize;
            wavRiff.RiffSize = (uint)(0x1c + (hcaInfo.Comment != null ? wavNote.NoteSize : 0) + Marshal.SizeOf(wavData) + wavData.DataSize);

            var bytesWritten = stream.Write(wavRiff, 0);

            if (hcaInfo.Comment != null)
            {
                var address = bytesWritten;
                bytesWritten += stream.Write(wavNote, bytesWritten);
                stream.Write(hcaInfo.Comment, bytesWritten);
                bytesWritten  = address + 8 + (int)wavNote.NoteSize;
                bytesWritten += 8 + (int)wavNote.NoteSize;
            }
            bytesWritten += stream.Write(wavData, bytesWritten);
            return(bytesWritten);
        }