Exemple #1
0
        public void BindDataBuffer(byte[] dataBuffer, ALFormat format, int size, int sampleRate, int sampleAlignment = 0)
        {
            if ((format == ALFormat.MonoMSAdpcm || format == ALFormat.StereoMSAdpcm) && !OpenALSoundController.Instance.SupportsAdpcm)
            {
                throw new InvalidOperationException("MS-ADPCM is not supported by this OpenAL driver");
            }
            if ((format == ALFormat.MonoIma4 || format == ALFormat.StereoIma4) && !OpenALSoundController.Instance.SupportsIma4)
            {
                throw new InvalidOperationException("IMA/ADPCM is not supported by this OpenAL driver");
            }

            openALFormat = format;
            dataSize     = size;
            int unpackedSize = 0;

            if (sampleAlignment > 0)
            {
                AL.Bufferi(openALDataBuffer, ALBufferi.UnpackBlockAlignmentSoft, sampleAlignment);
                ALHelper.CheckError("Failed to fill buffer.");
            }

            AL.BufferData(openALDataBuffer, openALFormat, dataBuffer, size, sampleRate);
            ALHelper.CheckError("Failed to fill buffer.");

            int bits, channels;

            Duration = -1;
            AL.GetBuffer(openALDataBuffer, ALGetBufferi.Bits, out bits);
            ALHelper.CheckError("Failed to get buffer bits");
            AL.GetBuffer(openALDataBuffer, ALGetBufferi.Channels, out channels);
            ALHelper.CheckError("Failed to get buffer channels");
            AL.GetBuffer(openALDataBuffer, ALGetBufferi.Size, out unpackedSize);
            ALHelper.CheckError("Failed to get buffer size");
            Duration = (float)(unpackedSize / ((bits / 8) * channels)) / (float)sampleRate;
        }
        public void BindDataBuffer(byte[] dataBuffer, ALFormat format, int size, int sampleRate, int alignment = 0)
        {
            openALFormat    = format;
            dataSize        = size;
            this.sampleRate = sampleRate;
            int unpackedSize = 0;

#if DESKTOPGL
            if (alignment > 0)
            {
                AL.Bufferi(openALDataBuffer, ALBufferi.UnpackBlockAlignmentSoft, alignment);
                ALHelper.CheckError("Failed to fill buffer.");
            }
#endif

            AL.BufferData(openALDataBuffer, openALFormat, dataBuffer, size, this.sampleRate);
            ALHelper.CheckError("Failed to fill buffer.");

            int bits, channels;

            AL.GetBuffer(openALDataBuffer, ALGetBufferi.Bits, out bits);
            ALError alError = AL.GetError();
            if (alError != ALError.NoError)
            {
                Console.WriteLine("Failed to get buffer bits: {0}, format={1}, size={2}, sampleRate={3}", AL.GetErrorString(alError), format, size, sampleRate);
                Duration = -1;
            }
            else
            {
                AL.GetBuffer(openALDataBuffer, ALGetBufferi.Channels, out channels);

                alError = AL.GetError();
                if (alError != ALError.NoError)
                {
                    Console.WriteLine("Failed to get buffer channels: {0}, format={1}, size={2}, sampleRate={3}", AL.GetErrorString(alError), format, size, sampleRate);
                    Duration = -1;
                }
                else
                {
                    AL.GetBuffer(openALDataBuffer, ALGetBufferi.Size, out unpackedSize);
                    alError = AL.GetError();
                    if (alError != ALError.NoError)
                    {
                        Console.WriteLine("Failed to get buffer size: {0}, format={1}, size={2}, sampleRate={3}", AL.GetErrorString(alError), format, size, sampleRate);
                        Duration = -1;
                    }
                    else
                    {
                        Duration = (float)(unpackedSize / ((bits / 8) * channels)) / (float)sampleRate;
                    }
                }
            }
            //Console.WriteLine("Duration: " + Duration + " / size: " + size + " bits: " + bits + " channels: " + channels + " rate: " + sampleRate);
        }
Exemple #3
0
        public void BufferData <T>(
            ReadOnlySpan <T> data, ALFormat format, int sampleRate, int sampleAlignment = 0)
            where T : unmanaged
        {
            AssertNotDisposed();

            var controller = ALController.Get();

            if (!controller.SupportsFloat32 && (format == ALFormat.MonoFloat32 || format == ALFormat.StereoFloat32))
            {
                throw new InvalidOperationException("Float data is not supported by this OpenAL driver.");
            }

            if (!controller.SupportsAdpcm && (format == ALFormat.MonoMSAdpcm || format == ALFormat.StereoMSAdpcm))
            {
                throw new InvalidOperationException("MS-ADPCM is not supported by this OpenAL driver.");
            }

            if (!controller.SupportsIma4 && (format == ALFormat.MonoIma4 || format == ALFormat.StereoIma4))
            {
                throw new InvalidOperationException("IMA/ADPCM is not supported by this OpenAL driver.");
            }

            if (BufferId != 0)
            {
                ClearBuffer();
            }

            BufferId = AL.GenBuffer();
            ALHelper.CheckError("Failed to generate OpenAL data buffer.");

            if (sampleAlignment > 0)
            {
                AL.Bufferi(BufferId, ALBufferi.UnpackBlockAlignmentSoft, sampleAlignment);
                ALHelper.CheckError("Failed to set buffer alignment.");
            }

            AL.BufferData(BufferId, format, MemoryMarshal.AsBytes(data), sampleRate);
            ALHelper.CheckError("Failed to fill buffer.");
        }