Exemple #1
0
        public OpenALAudioLayer(string name, OpenALAudioAdapter parent) : base(name)
        {
            if (_openALFormatId == 0)
            {
                _openALFormatId = _openALAudioFormat.BitsPerSample switch
                {
                    32 => Al.FORMAT_STEREO32F,
                    16 => _openALAudioFormat.Channels == 2 ? Al.FORMAT_STEREO16 : Al.FORMAT_MONO16,
                    8 => _openALAudioFormat.Channels == 2 ? Al.FORMAT_STEREO8 : Al.FORMAT_MONO8,
                    _ => _openALFormatId
                }
            }
            ;

            if (_frameRequestSize == 0)
            {
                _frameRequestSize  = _openALAudioFormat.GetFrameCount(BackendBufferExpectedAhead / 1000f);
                _frameRequestSize /= BUFFER_COUNT;
            }

            _parent = parent;
            Al.GenSource(out _source);

            _buffers    = new uint[BUFFER_COUNT];
            _bufferBusy = new bool[BUFFER_COUNT];
            for (var i = 0; i < _buffers.Length; i++)
            {
                Al.GenBuffer(out _buffers[i]);
            }

            _uploadBuffer = new byte[_frameRequestSize * _openALAudioFormat.FrameSize];
        }
Exemple #2
0
        public static OpenALAudioAdapter TryCreate(PlatformBase _)
        {
            var newCtx = new OpenALAudioAdapter();

            newCtx.AudioDevice = Alc.OpenDevice(null);
            if (newCtx.AudioDevice == IntPtr.Zero)
            {
                Engine.Log.Error("Couldn't find an OpenAL device.", MessageSource.OpenAL);
                return(null);
            }

            var attr = new int[0];

            newCtx.AudioContext = Alc.CreateContext(newCtx.AudioDevice, attr);
            if (newCtx.AudioContext == IntPtr.Zero)
            {
                Engine.Log.Error("Couldn't create OpenAL context.", MessageSource.OpenAL);
                return(null);
            }

            bool success = Alc.MakeContextCurrent(newCtx.AudioContext);

            if (!success)
            {
                Engine.Log.Error("Couldn't make OpenAL context current.", MessageSource.OpenAL);
                return(null);
            }

            return(newCtx);
        }