Пример #1
0
        private void ReleaseUnmanagedResources()
        {
            if (_disposed)
            {
                return;
            }

            ALC10.alcMakeContextCurrent(MainContext.Handle);
            for (var i = 0; i < _buffers.Count; i++)
            {
                var buf = _buffers[i];
                AL10.alDeleteBuffers(_buffers.Count, ref buf);
            }
            _buffers.Clear();

            ALC10.alcMakeContextCurrent(IntPtr.Zero);
            foreach (var ctx in _contexts)
            {
                ctx.Destroy();
            }

            ALC10.alcCloseDevice(_handle);

            _disposed = true;
        }
Пример #2
0
        public AudioSystem(Game game) : base(game)
        {
            _device = ALC10.alcOpenDevice("");
            alcCheckError();

            _context = ALC10.alcCreateContext(_device, null);
            alcCheckError();

            ALC10.alcMakeContextCurrent(_context);
            alcCheckError();

            _sources = new List <AudioSource>();
            _files   = new Dictionary <string, AudioBuffer>();

            switch (game.ContentManager.SageGame)
            {
            case SageGame.Ra3:
            case SageGame.Ra3Uprising:
            case SageGame.Cnc4:
                // TODO
                break;

            default:
                game.ContentManager.IniDataContext.LoadIniFile(@"Data\INI\AudioSettings.ini");
                game.ContentManager.IniDataContext.LoadIniFile(@"Data\INI\SoundEffects.ini");
                game.ContentManager.IniDataContext.LoadIniFile(@"Data\INI\MiscAudio.ini");
                break;
            }

            _settings = game.ContentManager.IniDataContext.AudioSettings;
        }
Пример #3
0
        private void InitOpenAL()
        {
            try
            {
                AL.Create();
            }
            catch (LWJGLException e)
            {
                throw new OpenALException(e);
            }

            string deviceName = null;
            string os         = System.GetProperty("os.name");

            if (os.StartsWith("Windows"))
            {
                deviceName = "DirectSound3D";
            }

            string defaultSpecifier = ALC10.AlcGetString(AL.GetDevice(), ALC10.ALC_DEFAULT_DEVICE_SPECIFIER);

            Com.Printf(os + " using " + ((deviceName == null) ? defaultSpecifier : deviceName) + '\\');
            if (ALC10.AlcGetError(AL.GetDevice()) != ALC10.ALC_NO_ERROR)
            {
                Com.DPrintf("Error with SoundDevice");
            }
        }
Пример #4
0
 public AudioDevice()
 {
     _device  = ALC10.alcOpenDevice(null); Check();
     _context = ALC10.alcCreateContext(_device, new int[0]); Check();
     ALC10.alcMakeContextCurrent(_context); Check();
     AL10.alGetError(); // Clear error code for subsequent callers
 }
Пример #5
0
 void Dispose(bool disposing)
 {
     if (device != IntPtr.Zero)
     {
         ALC10.alcCloseDevice(device);
         device = IntPtr.Zero;
     }
 }
Пример #6
0
 internal void DestroyContext(AlContext ctx)
 {
     if (!_contexts.Remove(ctx))
     {
         throw new InvalidOperationException("Device does not own given context.");
     }
     ALC10.alcMakeContextCurrent(IntPtr.Zero);
     ctx.Destroy();
 }
Пример #7
0
        static AlDevice()
        {
            var ptr = ALC10.alcGetString(IntPtr.Zero, ALC10.ALC_DEFAULT_DEVICE_SPECIFIER);

            if (ptr != IntPtr.Zero)
            {
                DefaultDeviceName = Marshal.PtrToStringAnsi(ptr);
            }
        }
Пример #8
0
 internal void Destroy()
 {
     // notify all sources first
     foreach (var source in _sources)
     {
         source.OnContextDestroyed();
     }
     ALC10.alcDestroyContext(Handle);
 }
Пример #9
0
        internal static void AlcCheckError(IntPtr device, string message = "")
        {
            var error = ALC10.alcGetError(device);

            if (error != ALC10.ALC_NO_ERROR)
            {
                throw new InvalidOperationException(message + $" ({error})");
            }
        }
Пример #10
0
        /// <summary>
        /// Create an <see cref="AlStaticSource"/> for this context.
        /// </summary>
        public AlStaticSource CreateStaticSource()
        {
            ALC10.alcMakeContextCurrent(Handle);
            AL10.alGenSources(1, out var name);
            AlHelper.AlAlwaysCheckError("Call to alGenSources failed.");
            var source = new AlStaticSource(name, this);

            _sources.Add(source);
            return(source);
        }
Пример #11
0
 /// <summary>
 /// Create a number of OpenAL buffers for this device.
 /// </summary>
 /// <param name="buffers">Array to fill with buffer names.</param>
 /// <param name="n">Number of buffers to generate.</param>
 public void CreateBuffers(uint[] buffers, int n)
 {
     CheckDisposed();
     ALC10.alcMakeContextCurrent(MainContext.Handle);
     AL10.alGenBuffers(n, buffers);
     AlHelper.AlAlwaysCheckError("Failed to generate buffers.");
     for (var i = 0; i < n; i++)
     {
         _buffers.Add(buffers[i]);
     }
 }
Пример #12
0
        internal void alcCheckError()
        {
            int error;

            error = ALC10.alcGetError(_device);

            if (error != ALC10.ALC_NO_ERROR)
            {
                throw new InvalidOperationException("ALC error!");
            }
        }
Пример #13
0
        protected override void Dispose(bool disposeManagedResources)
        {
            base.Dispose(disposeManagedResources);
            _sources.Clear();
            _files.Clear();


            ALC10.alcMakeContextCurrent(IntPtr.Zero);
            ALC10.alcDestroyContext(_context);
            ALC10.alcCloseDevice(_device);
        }
Пример #14
0
        private bool CheckALCError()
        {
            int err = ALC10.alcGetError(alDevice);

            if (err == ALC10.ALC_NO_ERROR)
            {
                return(false);
            }

            FNALoggerEXT.LogError("OpenAL Device Error: " + err.ToString("X4"));
            return(true);
        }
Пример #15
0
        private bool CheckALCError()
        {
            int err = ALC10.alcGetError(alDevice);

            if (err == ALC10.ALC_NO_ERROR)
            {
                return(false);
            }

            System.Console.WriteLine("OpenAL Device Error: {0:X}", err);
            return(true);
        }
Пример #16
0
        public OpenALDevice()
        {
            string envDevice = Environment.GetEnvironmentVariable("FNA_AUDIO_DEVICE_NAME");

            if (String.IsNullOrEmpty(envDevice))
            {
                /* Be sure ALC won't explode if the variable doesn't exist.
                 * But, fail if the device name is wrong. The user needs to know
                 * if their environment variable was incorrect.
                 * -flibit
                 */
                envDevice = String.Empty;
            }
            alDevice = ALC10.alcOpenDevice(envDevice);
            if (CheckALCError() || alDevice == IntPtr.Zero)
            {
                throw new InvalidOperationException("Could not open audio device!");
            }

            int[] attribute = new int[0];
            alContext = ALC10.alcCreateContext(alDevice, attribute);
            if (CheckALCError() || alContext == IntPtr.Zero)
            {
                Dispose();
                throw new InvalidOperationException("Could not create OpenAL context");
            }

            ALC10.alcMakeContextCurrent(alContext);
            if (CheckALCError())
            {
                Dispose();
                throw new InvalidOperationException("Could not make OpenAL context current");
            }

            float[] ori = new float[]
            {
                0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f
            };
            AL10.alListenerfv(AL10.AL_ORIENTATION, ori);
            AL10.alListener3f(AL10.AL_POSITION, 0.0f, 0.0f, 0.0f);
            AL10.alListener3f(AL10.AL_VELOCITY, 0.0f, 0.0f, 0.0f);
            AL10.alListenerf(AL10.AL_GAIN, 1.0f);

            EFX.alGenFilters(1, out INTERNAL_alFilter);

            // FIXME: Remove for FNA 16.11! -flibit
            if (!AL10.alIsExtensionPresent("AL_SOFT_gain_clamp_ex"))
            {
                FNALoggerEXT.LogWarn("AL_SOFT_gain_clamp_ex not found!");
                FNALoggerEXT.LogWarn("Update your OpenAL Soft library!");
            }
        }
Пример #17
0
        /// <summary>
        /// Delete a buffer.
        /// </summary>
        /// <param name="name">Name of the buffer.</param>
        /// <exception cref="InvalidOperationException">If the buffer is not owned by this device.</exception>
        public void DeleteBuffer(uint name)
        {
            CheckDisposed();

            if (!_buffers.Remove(name))
            {
                throw new InvalidOperationException("Device does not own given buffer.");
            }

            ALC10.alcMakeContextCurrent(MainContext.Handle);
            AL10.alDeleteBuffers(1, ref name);
            AlHelper.AlCheckError("alDeleteBuffers call failed.");
        }
Пример #18
0
 public bool CaptureHasSamples(IntPtr handle)
 {
     int[] samples = new int[1] {
         0
     };
     ALC10.alcGetIntegerv(
         handle,
         ALC11.ALC_CAPTURE_SAMPLES,
         1,
         samples
         );
     return(samples[0] > 0);
 }
Пример #19
0
        private bool CheckALCError(string message)
        {
            bool retVal = false;
            int  err    = ALC10.alcGetError(alDevice);

            if (err != ALC10.ALC_NO_ERROR)
            {
                System.Console.WriteLine("OpenAL Error: " + err.ToString());
                retVal = true;
            }

            return(retVal);
        }
Пример #20
0
        public void Dispose()
        {
            if (_context != IntPtr.Zero)
            {
                ALC10.alcMakeContextCurrent(IntPtr.Zero); Check();
                ALC10.alcDestroyContext(_context); Check();
            }

            if (_device != IntPtr.Zero)
            {
                ALC10.alcCloseDevice(_device);
                Check();
            }
        }
Пример #21
0
        public AudioSystem(Game game) : base(game)
        {
            _device = ALC10.alcOpenDevice("");
            alcCheckError();

            _context = ALC10.alcCreateContext(_device, null);
            alcCheckError();

            ALC10.alcMakeContextCurrent(_context);
            alcCheckError();

            _sources = new List <AudioSource>();
            _files   = new Dictionary <string, AudioBuffer>();
        }
Пример #22
0
        /// <summary>
        /// Create an OpenAL buffer for this device.
        /// </summary>
        public uint CreateBuffer()
        {
            CheckDisposed();
            ALC10.alcMakeContextCurrent(MainContext.Handle);
            AL10.alGenBuffers(1, out var name);
            if (name == 0)
            {
                AlHelper.AlCheckError("alGenBuffer call failed.");
                throw new Exception("Failed to create buffer.");
            }

            _buffers.Add(name);
            return(name);
        }
Пример #23
0
 public void Dispose()
 {
     ALC10.alcMakeContextCurrent(IntPtr.Zero);
     if (alContext != IntPtr.Zero)
     {
         ALC10.alcDestroyContext(alContext);
         alContext = IntPtr.Zero;
     }
     if (alDevice != IntPtr.Zero)
     {
         ALC10.alcCloseDevice(alDevice);
         alDevice = IntPtr.Zero;
     }
 }
Пример #24
0
        /// <summary>
        /// Get the available devices.
        /// </summary>
        public static IEnumerable <string> GetDevices()
        {
            var deviceList = ALC11.alcIsExtensionPresent(IntPtr.Zero, "ALC_ENUMERATE_ALL_EXT") ?
                             ALC10.alcGetString(IntPtr.Zero, ALC11.ALC_ALL_DEVICES_SPECIFIER) :
                             ALC10.alcGetString(IntPtr.Zero, ALC10.ALC_DEVICE_SPECIFIER);
            var curString = Marshal.PtrToStringAnsi(deviceList);

            while (!string.IsNullOrEmpty(curString))
            {
                yield return(curString);

                deviceList += curString.Length + 1;
                curString   = Marshal.PtrToStringAnsi(deviceList);
            }
        }
Пример #25
0
        /// <summary>
        /// Create a context for this device.
        /// </summary>
        public AlContext CreateContext()
        {
            CheckDisposed();
            var ctxHandle = ALC10.alcCreateContext(_handle, new int[0]);

            if (ctxHandle == IntPtr.Zero)
            {
                AlHelper.AlcCheckError(_handle, "Failed to create context.");
                throw new Exception("Failed to create context.");
            }
            var ctx = new AlContext(this, ctxHandle);

            _contexts.Add(ctx);
            return(ctx);
        }
Пример #26
0
        void Dispose(bool disposing)
        {
            if (context != IntPtr.Zero)
            {
                ALC10.alcMakeContextCurrent(IntPtr.Zero);
                ALC10.alcDestroyContext(context);
                context = IntPtr.Zero;
            }

            if (device != IntPtr.Zero)
            {
                ALC10.alcCloseDevice(device);
                device = IntPtr.Zero;
            }
        }
Пример #27
0
        /// <summary>
        /// Delete a collection of buffers.
        /// </summary>
        /// <param name="buffers">Array to delete buffers from, starting at index 0.</param>
        /// <param name="n">Number of buffers to delete.</param>
        /// <remarks>
        /// Buffers that are not owned by this device are ignored.
        /// </remarks>
        /// <exception cref="IndexOutOfRangeException">
        /// If <paramref name="n"/> is larger than or equal to <code>buffers.Length</code>.
        /// </exception>
        public void DeleteBuffers(uint[] buffers, int n)
        {
            CheckDisposed();
            ALC10.alcMakeContextCurrent(MainContext.Handle);

            for (var i = 0; i < n; i++)
            {
                var buffer = buffers[i];
                if (_buffers.Remove(buffer))
                {
                    AL10.alDeleteBuffers(1, ref buffer);
                }
            }
            AlHelper.AlCheckError("alDeleteBuffers call failed.");
        }
Пример #28
0
        public OpenALDevice()
        {
            string envDevice = Environment.GetEnvironmentVariable("FNA_AUDIO_DEVICE_NAME");

            if (String.IsNullOrEmpty(envDevice))
            {
                /* Be sure ALC won't explode if the variable doesn't exist.
                 * But, fail if the device name is wrong. The user needs to know
                 * if their environment variable was incorrect.
                 * -flibit
                 */
                envDevice = String.Empty;
            }
            alDevice = ALC10.alcOpenDevice(envDevice);
            if (CheckALCError() || alDevice == IntPtr.Zero)
            {
                throw new InvalidOperationException("Could not open audio device!");
            }

            int[] attribute = new int[0];
            alContext = ALC10.alcCreateContext(alDevice, attribute);
            if (CheckALCError() || alContext == IntPtr.Zero)
            {
                Dispose();
                throw new InvalidOperationException("Could not create OpenAL context");
            }

            ALC10.alcMakeContextCurrent(alContext);
            if (CheckALCError())
            {
                Dispose();
                throw new InvalidOperationException("Could not make OpenAL context current");
            }

            float[] ori = new float[]
            {
                0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f
            };
            AL10.alListenerfv(AL10.AL_ORIENTATION, ori);
            AL10.alListener3f(AL10.AL_POSITION, 0.0f, 0.0f, 0.0f);
            AL10.alListener3f(AL10.AL_VELOCITY, 0.0f, 0.0f, 0.0f);
            AL10.alListenerf(AL10.AL_GAIN, 1.0f);

            // We do NOT use automatic attenuation! XNA does not do this!
            AL10.alDistanceModel(AL10.AL_NONE);

            EFX.alGenFilters(1, out INTERNAL_alFilter);
        }
Пример #29
0
        public OpenAlSoundEngine()
        {
            Console.WriteLine("Using OpenAL sound engine");

            if (Game.Settings.Sound.Device != null)
            {
                Console.WriteLine("Using device `{0}`", Game.Settings.Sound.Device);
            }
            else
            {
                Console.WriteLine("Using default device");
            }

            device = ALC10.alcOpenDevice(Game.Settings.Sound.Device);
            if (device == IntPtr.Zero)
            {
                Console.WriteLine("Failed to open device. Falling back to default");
                device = ALC10.alcOpenDevice(null);
                if (device == IntPtr.Zero)
                {
                    throw new InvalidOperationException("Can't create OpenAL device");
                }
            }

            var ctx = ALC10.alcCreateContext(device, null);

            if (ctx == IntPtr.Zero)
            {
                throw new InvalidOperationException("Can't create OpenAL context");
            }
            ALC10.alcMakeContextCurrent(ctx);

            for (var i = 0; i < PoolSize; i++)
            {
                var source = 0U;
                AL10.alGenSources(new IntPtr(1), out source);
                if (AL10.alGetError() != AL10.AL_NO_ERROR)
                {
                    Log.Write("sound", "Failed generating OpenAL source {0}", i);
                    return;
                }

                sourcePool.Add(source, new PoolSlot()
                {
                    IsActive = false
                });
            }
        }
Пример #30
0
        public void Dispose()
        {
            EFX.alDeleteFilters(1, ref INTERNAL_alFilter);

            ALC10.alcMakeContextCurrent(IntPtr.Zero);
            if (alContext != IntPtr.Zero)
            {
                ALC10.alcDestroyContext(alContext);
                alContext = IntPtr.Zero;
            }
            if (alDevice != IntPtr.Zero)
            {
                ALC10.alcCloseDevice(alDevice);
                alDevice = IntPtr.Zero;
            }
        }