コード例 #1
0
        public static byte[] Decode(IAudioDecoder input)
        {
            const int bufferSize = 10 * 1024;
            IntPtr    buffer     = Marshal.AllocHGlobal(bufferSize);

            byte[] decodedSound = new byte[1024 * 64];
            try {
                int blockSize  = input.GetBlockSize();
                int needToRead = bufferSize / blockSize;
                int totalRead  = 0;
                while (true)
                {
                    int actuallyRead = input.ReadBlocks(buffer, 0, needToRead);
                    if (actuallyRead == 0)
                    {
                        break;
                    }
                    totalRead += actuallyRead;
                    if (totalRead * blockSize > decodedSound.Length)
                    {
                        Array.Resize(ref decodedSound, totalRead * blockSize * 3 / 2);
                    }
                    Marshal.Copy(buffer, decodedSound, (totalRead - actuallyRead) * blockSize, actuallyRead * blockSize);
                }
                Array.Resize(ref decodedSound, totalRead * blockSize);
            } finally {
                Marshal.FreeHGlobal(buffer);
            }
            return(decodedSound);
        }
コード例 #2
0
        private bool FillBuffer(int buffer)
        {
            int totalRead  = 0;
            int needToRead = BufferSize / decoder.GetBlockSize();

            while (true)
            {
                int actuallyRead = decoder.ReadBlocks(decodedData, totalRead, needToRead - totalRead);
                totalRead += actuallyRead;
                if (totalRead == needToRead || !looping)
                {
                    break;
                }
                decoder.Rewind();
            }
            if (totalRead > 0)
            {
                ALFormat format   = (decoder.GetFormat() == AudioFormat.Stereo16) ? ALFormat.Stereo16 : ALFormat.Mono16;
                int      dataSize = totalRead * decoder.GetBlockSize();
                AL.BufferData(buffer, format, decodedData, dataSize, decoder.GetFrequency());
                return(true);
            }
            return(false);
        }