Ejemplo n.º 1
0
        public IALBuffer GenBuffer(
            byte[] data,
            uint sampleRate,
            uint channels,
            uint loopStart,
            uint loopEnd,
            bool isADPCM,
            uint formatParameter
            )
        {
            uint result;

            // Generate the buffer now, in case we need to perform alBuffer ops.
            AL10.alGenBuffers(1, out result);
#if VERBOSE_AL_DEBUGGING
            CheckALError();
#endif

            int format;
            int length = data.Length;
            if (isADPCM)
            {
                format = (channels == 2) ?
                         ALEXT.AL_FORMAT_STEREO_MSADPCM_SOFT :
                         ALEXT.AL_FORMAT_MONO_MSADPCM_SOFT;
                AL10.alBufferi(
                    result,
                    ALEXT.AL_UNPACK_BLOCK_ALIGNMENT_SOFT,
                    (int)formatParameter
                    );
            }
            else
            {
                if (formatParameter == 1)
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO16:
                             AL10.AL_FORMAT_MONO16;

                    /* We have to perform extra data validation on
                     * PCM16 data, as the MS SoundEffect builder will
                     * leave extra bytes at the end which will confuse
                     * alBufferData and throw an AL_INVALID_VALUE.
                     * -flibit
                     */
                    length &= 0x7FFFFFFE;
                }
                else
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO8:
                             AL10.AL_FORMAT_MONO8;
                }
            }

            // Load it!
            AL10.alBufferData(
                result,
                format,
                data,
                length,
                (int)sampleRate
                );
#if VERBOSE_AL_DEBUGGING
            CheckALError();
#endif

            // Calculate the duration now, after we've unpacked the buffer
            int bufLen, bits;
            AL10.alGetBufferi(
                result,
                AL10.AL_SIZE,
                out bufLen
                );
            AL10.alGetBufferi(
                result,
                AL10.AL_BITS,
                out bits
                );
            if (bufLen == 0 || bits == 0)
            {
                throw new InvalidOperationException(
                          "OpenAL buffer allocation failed!"
                          );
            }
            TimeSpan resultDur = TimeSpan.FromSeconds(
                bufLen /
                (bits / 8) /
                channels /
                ((double)sampleRate)
                );

            // Set the loop points, if applicable
            if (loopStart > 0 || loopEnd > 0)
            {
                AL10.alBufferiv(
                    result,
                    ALEXT.AL_LOOP_POINTS_SOFT,
                    new int[]
                {
                    (int)loopStart,
                    (int)loopEnd
                }
                    );
            }
#if VERBOSE_AL_DEBUGGING
            CheckALError();
#endif

            // Finally.
            return(new OpenALBuffer(result, resultDur, (int)channels, (int)sampleRate));
        }
Ejemplo n.º 2
0
        public IALBuffer GenBuffer(
            byte[] data,
            uint sampleRate,
            uint channels,
            uint loopStart,
            uint loopEnd,
            bool isADPCM,
            uint formatParameter
            )
        {
            uint result;

            // Generate the buffer now, in case we need to perform alBuffer ops.
            AL10.alGenBuffers((IntPtr)1, out result);

            int format;

            if (isADPCM)
            {
                format = (channels == 2) ?
                         ALEXT.AL_FORMAT_STEREO_MSADPCM_SOFT :
                         ALEXT.AL_FORMAT_MONO_MSADPCM_SOFT;
                AL10.alBufferi(
                    result,
                    ALEXT.AL_UNPACK_BLOCK_ALIGNMENT_SOFT,
                    (int)formatParameter
                    );
            }
            else
            {
                if (formatParameter == 1)
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO16:
                             AL10.AL_FORMAT_MONO16;
                }
                else
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO8:
                             AL10.AL_FORMAT_MONO8;
                }
            }

            // Load it!
            AL10.alBufferData(
                result,
                format,
                data,
                (IntPtr)data.Length,
                (IntPtr)sampleRate
                );

            // Calculate the duration now, after we've unpacked the buffer
            int bufLen, bits;

            AL10.alGetBufferi(
                result,
                AL10.AL_SIZE,
                out bufLen
                );
            AL10.alGetBufferi(
                result,
                AL10.AL_BITS,
                out bits
                );
            TimeSpan resultDur = TimeSpan.FromSeconds(
                bufLen /
                (bits / 8) /
                channels /
                ((double)sampleRate)
                );

            // Set the loop points, if applicable
            if (loopStart > 0 || loopEnd > 0)
            {
                AL10.alBufferiv(
                    result,
                    ALEXT.AL_LOOP_POINTS_SOFT,
                    new int[]
                {
                    (int)loopStart,
                    (int)loopEnd
                }
                    );
            }

            // Finally.
            return(new OpenALBuffer(result, resultDur));
        }
Ejemplo n.º 3
0
        private void INTERNAL_bufferData(
            byte[] data,
            uint sampleRate,
            uint channels,
            uint loopStart,
            uint loopEnd,
            bool isADPCM,
            uint formatParameter
            )
        {
            if (OpenALDevice.Instance == null)
            {
                throw new NoAudioHardwareException();
            }

            // Generate the buffer now, in case we need to perform alBuffer ops.
            AL10.alGenBuffers((IntPtr)1, out INTERNAL_buffer);

            int format;

            if (isADPCM)
            {
                format = (channels == 2) ?
                         ALEXT.AL_FORMAT_STEREO_MSADPCM_SOFT :
                         ALEXT.AL_FORMAT_MONO_MSADPCM_SOFT;
                AL10.alBufferi(
                    INTERNAL_buffer,
                    ALEXT.AL_UNPACK_BLOCK_ALIGNMENT_SOFT,
                    (int)formatParameter
                    );
            }
            else
            {
                if (formatParameter == 1)
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO16:
                             AL10.AL_FORMAT_MONO16;
                }
                else
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO8:
                             AL10.AL_FORMAT_MONO8;
                }
            }

            // Load it!
            AL10.alBufferData(
                INTERNAL_buffer,
                format,
                data,
                (IntPtr)data.Length,
                (IntPtr)sampleRate
                );

            // Calculate the duration now, after we've unpacked the buffer
            int bufLen, bits;

            AL10.alGetBufferi(
                INTERNAL_buffer,
                AL10.AL_SIZE,
                out bufLen
                );
            AL10.alGetBufferi(
                INTERNAL_buffer,
                AL10.AL_BITS,
                out bits
                );
            Duration = TimeSpan.FromSeconds(
                bufLen /
                (bits / 8) /
                channels /
                ((double)sampleRate)
                );

            // Set the loop points, if applicable
            if (loopStart > 0 || loopEnd > 0)
            {
                AL10.alBufferiv(
                    INTERNAL_buffer,
                    ALEXT.AL_LOOP_POINTS_SOFT,
                    new int[]
                {
                    (int)loopStart,
                    (int)loopEnd
                }
                    );
            }
        }
Ejemplo n.º 4
0
        private void INTERNAL_bufferData(
            byte[] data,
            uint sampleRate,
            uint channels,
            uint loopStart,
            uint loopEnd,
            bool isADPCM,
            uint formatParameter
            )
        {
            if (OpenALDevice.Instance == null)
            {
                throw new NoAudioHardwareException();
            }

            // Generate the buffer now, in case we need to perform alBuffer ops.
            AL10.alGenBuffers((IntPtr)1, out INTERNAL_buffer);

            int format;

            if (isADPCM)
            {
                Platform.AssertSupported("ADPCM");

                format = (channels == 2) ?
                         ALEXT.AL_FORMAT_STEREO_MSADPCM_SOFT :
                         ALEXT.AL_FORMAT_MONO_MSADPCM_SOFT;
                AL10.alBufferi(
                    INTERNAL_buffer,
                    ALEXT.AL_UNPACK_BLOCK_ALIGNMENT_SOFT,
                    (int)formatParameter
                    );
            }
            else
            {
                if (formatParameter == 1)
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO16:
                             AL10.AL_FORMAT_MONO16;
                }
                else
                {
                    format = (channels == 2) ?
                             AL10.AL_FORMAT_STEREO8:
                             AL10.AL_FORMAT_MONO8;
                }
            }

            // Load it!
            AL10.alBufferData(
                INTERNAL_buffer,
                format,
                data,
                (IntPtr)data.Length,
                (IntPtr)sampleRate
                );

            // Calculate the duration now, after we've unpacked the buffer
            int bufLen, bits;

            AL10.alGetBufferi(
                INTERNAL_buffer,
                AL10.AL_SIZE,
                out bufLen
                );
            AL10.alGetBufferi(
                INTERNAL_buffer,
                AL10.AL_BITS,
                out bits
                );
            Duration = TimeSpan.FromSeconds(
                bufLen /
                (bits / 8) /
                channels /
                ((double)sampleRate)
                );

            // Compute the default loop end point (end of the buffer), because
            //  some content builders automatically set a loop endpoint here instead of at 0
            int defaultEndPoint = (bufLen / (bits / 8)) / (int)channels;

            var hasCustomStartPoint = (loopStart > 0);
            var hasCustomEndPoint   = (
                (loopEnd > loopStart) &&
                (loopEnd < defaultEndPoint)
                );

            if (hasCustomStartPoint || hasCustomEndPoint)
            {
                // Set the loop points, if applicable
                Platform.AssertSupported("CustomLoopPoints");

                AL10.alBufferiv(
                    INTERNAL_buffer,
                    ALEXT.AL_LOOP_POINTS_SOFT,
                    new int[]
                {
                    (int)loopStart,
                    (int)loopEnd
                }
                    );
            }
        }