Esempio n. 1
0
        public AudioSource()
        {
            // Open default device
            device = Alc.alcOpenDevice(null);
            if (device == IntPtr.Zero)
            {
                throw new Exception("Unable to open OpenAL device");
            }

            // Create context
            context = Alc.alcCreateContext(device, IntPtr.Zero);
            if (context == IntPtr.Zero)
            {
                throw new Exception("Unable to create OpenAL context");
            }
            int rv = Alc.alcMakeContextCurrent(context);

            // Create buffers
            for (int i = 0; i < 8; ++i)
            {
                int b;
                Al.alGenBuffers(1, out b);
                buffers.Enqueue(b);
            }

            // Create source
            Al.alGenSources(1, out source);
        }
Esempio n. 2
0
        public OpenAlSoundEngine()
        {
            //var str = Alc.alcGetString(IntPtr.Zero, Alc.ALC_DEFAULT_DEVICE_SPECIFIER);
            var dev = Alc.alcOpenDevice(null);

            if (dev == IntPtr.Zero)
            {
                throw new InvalidOperationException("Can't create OpenAL device");
            }
            var ctx = Alc.alcCreateContext(dev, IntPtr.Zero);

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

            for (var i = 0; i < POOL_SIZE; i++)
            {
                var source = 0;
                Al.alGenSources(1, out source);
                if (0 != Al.alGetError())
                {
                    Log.Write("debug", "Failed generating OpenAL source {0}", i);
                    return;
                }

                sourcePool.Add(source, new PoolSlot()
                {
                    isActive = false
                });
            }
        }
Esempio n. 3
0
        // --- initialization and deinitialization ---

        /// <summary>Initializes audio. A call to Deinitialize must be made when terminating the program.</summary>
        /// <returns>Whether initializing audio was successful.</returns>
        internal static bool Initialize()
        {
            Deinitialize();
            switch (Interface.CurrentOptions.SoundRange)
            {
            case Interface.SoundRange.Low:
                OuterRadiusFactorMinimum      = 2.0;
                OuterRadiusFactorMaximum      = 8.0;
                OuterRadiusFactorMaximumSpeed = 1.0;
                break;

            case Interface.SoundRange.Medium:
                OuterRadiusFactorMinimum      = 4.0;
                OuterRadiusFactorMaximum      = 16.0;
                OuterRadiusFactorMaximumSpeed = 2.0;
                break;

            case Interface.SoundRange.High:
                OuterRadiusFactorMinimum      = 6.0;
                OuterRadiusFactorMaximum      = 24.0;
                OuterRadiusFactorMaximumSpeed = 3.0;
                break;
            }
            OuterRadiusFactor      = Math.Sqrt(OuterRadiusFactorMinimum * OuterRadiusFactorMaximum);
            OuterRadiusFactorSpeed = 0.0;
            OpenAlDevice           = Alc.alcOpenDevice(null);
            if (OpenAlDevice != IntPtr.Zero)
            {
                OpenAlContext = Alc.alcCreateContext(OpenAlDevice, IntPtr.Zero);
                if (OpenAlContext != IntPtr.Zero)
                {
                    Alc.alcMakeContextCurrent(OpenAlContext);
                    try {
                        Al.alSpeedOfSound(343.0f);
                    } catch {
                        MessageBox.Show("OpenAL 1.1 is required. You seem to have OpenAL 1.0.", "openBVE", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    Al.alDistanceModel(Al.AL_NONE);
                    return(true);
                }
                else
                {
                    Alc.alcCloseDevice(OpenAlDevice);
                    OpenAlDevice = IntPtr.Zero;
                    MessageBox.Show("The OpenAL context could not be created.", "openBVE", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return(false);
                }
            }
            else
            {
                OpenAlContext = IntPtr.Zero;
                MessageBox.Show("The OpenAL sound device could not be opened.", "openBVE", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return(false);
            }
        }
Esempio n. 4
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");
            }

            var dev = Alc.alcOpenDevice(Game.Settings.Sound.Device);

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

            var ctx = Alc.alcCreateContext(dev, IntPtr.Zero);

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

            for (var i = 0; i < POOL_SIZE; i++)
            {
                var source = 0;
                Al.alGenSources(1, out source);
                if (0 != Al.alGetError())
                {
                    Log.Write("sound", "Failed generating OpenAL source {0}", i);
                    return;
                }

                sourcePool.Add(source, new PoolSlot()
                {
                    isActive = false
                });
            }
        }
Esempio n. 5
0
 // deinitialize
 internal static void Deinitialize()
 {
     if (OpenAlContext != IntPtr.Zero)
     {
         SoundManager.StopAllSounds(true);
         SoundManager.UnuseAllSoundsBuffers();
         Alc.alcMakeContextCurrent(IntPtr.Zero);
         Alc.alcDestroyContext(OpenAlContext);
         OpenAlContext = IntPtr.Zero;
     }
     if (OpenAlDevice != IntPtr.Zero)
     {
         Alc.alcCloseDevice(OpenAlDevice);
         OpenAlDevice = IntPtr.Zero;
     }
 }
Esempio n. 6
0
 /// <summary>Deinitializes audio.</summary>
 internal static void Deinitialize()
 {
     StopAllSounds();
     UnloadAllBuffers();
     if (OpenAlContext != IntPtr.Zero)
     {
         Alc.alcMakeContextCurrent(IntPtr.Zero);
         Alc.alcDestroyContext(OpenAlContext);
         OpenAlContext = IntPtr.Zero;
     }
     if (OpenAlDevice != IntPtr.Zero)
     {
         Alc.alcCloseDevice(OpenAlDevice);
         OpenAlDevice = IntPtr.Zero;
     }
 }
Esempio n. 7
0
        // initialize
        internal static void Initialize()
        {
            // openal
            OpenAlDevice = Alc.alcOpenDevice(null);
            if (OpenAlDevice != IntPtr.Zero)
            {
                OpenAlContext = Alc.alcCreateContext(OpenAlDevice, IntPtr.Zero);
                if (OpenAlContext != IntPtr.Zero)
                {
                    Alc.alcMakeContextCurrent(OpenAlContext);
                    Al.alSpeedOfSound(343.0f);
                    Al.alDistanceModel(Al.AL_NONE);
                }
                else
                {
                    Alc.alcCloseDevice(OpenAlDevice);
                    OpenAlDevice = IntPtr.Zero;
                    System.Windows.Forms.MessageBox.Show("The sound device could be opened, but the sound context could not be created.", "openBVE", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Hand);
                }
            }
            else
            {
                OpenAlContext = IntPtr.Zero;
                System.Windows.Forms.MessageBox.Show("The sound device could not be opened.", "openBVE", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Hand);
            }
            // outer radius
            switch (Interface.CurrentOptions.SoundRange)
            {
            case Interface.SoundRange.Low:
                OuterRadiusFactorMinimum = 2.0;
                OuterRadiusFactorMaximum = 8.0;
                break;

            case Interface.SoundRange.Medium:
                OuterRadiusFactorMinimum = 4.0;
                OuterRadiusFactorMaximum = 16.0;
                break;

            case Interface.SoundRange.High:
                OuterRadiusFactorMinimum = 8.0;
                OuterRadiusFactorMaximum = 32.0;
                break;
            }
            OuterRadiusFactor = OuterRadiusFactorMaximum;
        }
Esempio n. 8
0
        protected override void ShutdownLibrary()
        {
            if (thread != null)
            {
                needAbortThread = true;
                Thread.Sleep(50);
                thread.Abort();
            }

            if (realChannels != null)
            {
                foreach (OpenALRealChannel realChannel in realChannels)
                {
                    if (realChannel.alSource != 0)
                    {
                        Al.alDeleteSources(1, ref realChannel.alSource);
                        realChannel.alSource = 0;
                    }
                }
            }

            try
            {
                Alc.alcMakeContextCurrent(IntPtr.Zero);
                Alc.alcDestroyContext(alContext);
                Alc.alcCloseDevice(alDevice);
            }
            catch { }

            if (realChannels != null)
            {
                realChannels.Clear();
                realChannels = null;
            }

            if (criticalSection != null)
            {
                criticalSection.Dispose();
                criticalSection = null;
            }

            instance = null;
        }
Esempio n. 9
0
        protected override bool InitLibrary(IntPtr mainWindowHandle, int maxReal2DChannels, int maxReal3DChannels)
        {
            //NativeLibraryManager.PreLoadLibrary( "libogg" );
            //NativeLibraryManager.PreLoadLibrary( "libvorbis" );
            //NativeLibraryManager.PreLoadLibrary( "libvorbisfile" );

            //preload dlls
            {
                var fileNames = new List <string>();
                if (SystemSettings.CurrentPlatform == SystemSettings.Platform.Windows)
                {
                    fileNames.Add("OpenAL32.dll");
                }
                else if (SystemSettings.CurrentPlatform == SystemSettings.Platform.UWP)
                {
                    fileNames.Add("SDL2.dll");
                    fileNames.Add("OpenAL32.dll");
                }
                else if (SystemSettings.CurrentPlatform == SystemSettings.Platform.MacOS)
                {
                    fileNames.Add("OpenAL32.dylib");
                }
                else if (SystemSettings.CurrentPlatform == SystemSettings.Platform.Android)
                {
                    //fileNames.Add( "libOpenAL.so" );
                }
                else
                {
                    Log.Fatal("OpenALSoundWorld: InitLibrary: Unknown platform.");
                    return(false);
                }

                foreach (var fileName in fileNames)
                {
                    var path = Path.Combine(VirtualFileSystem.Directories.PlatformSpecific, fileName);
                    if (File.Exists(path))
                    {
                        NativeLibraryManager.PreLoadLibrary(fileName);
                    }
                }
            }

            criticalSection = CriticalSection.Create();

            //if( PlatformInfo.Platform == PlatformInfo.Platforms.Android )
            //{
            //   Alc.alcSetJNIEnvironmentAndJavaVM(
            //      EngineApp.Instance._CallCustomPlatformSpecificMethod( "GetJNIEnvironment", IntPtr.Zero ),
            //      EngineApp.Instance._CallCustomPlatformSpecificMethod( "GetJavaVM", IntPtr.Zero ) );
            //}

            //string[] devices = Alc.alcGetStringv( IntPtr.Zero, Alc.ALC_DEVICE_SPECIFIER );

            try
            {
                alDevice = Alc.alcOpenDevice(null);
            }
            catch (DllNotFoundException)
            {
                Log.InvisibleInfo("OpenALSoundSystem: OpenAL not found.");
                return(false);
            }
            catch (Exception e)
            {
                Log.InvisibleInfo("OpenALSoundSystem: Open device failed. " + e.Message);
                return(false);
            }
            if (alDevice == IntPtr.Zero)
            {
                Log.InvisibleInfo("OpenALSoundSystem: No sound driver.");
                return(false);
            }

            alContext = Alc.alcCreateContext(alDevice, IntPtr.Zero);
            if (alContext == IntPtr.Zero)
            {
                Log.Error("OpenALSoundSystem: Create context failed.");
                return(false);
            }

            try
            {
                Alc.alcMakeContextCurrent(alContext);
            }
            catch (Exception e)
            {
                Log.InvisibleInfo("OpenALSoundSystem: alcMakeContextCurrent failed. " + e.Message);
                return(false);
            }

            if (CheckError())
            {
                return(false);
            }

            //get captureDeviceName
            try
            {
                captureDeviceName = Alc.alcGetString(alDevice, Alc.ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
            }
            catch { }

            //Channels
            realChannels = new List <OpenALRealChannel>();
            for (int n = 0; n < maxReal2DChannels; n++)
            {
                OpenALRealChannel realChannel = new OpenALRealChannel();
                AddRealChannel(realChannel, false);
                realChannels.Add(realChannel);
            }
            for (int n = 0; n < maxReal3DChannels; n++)
            {
                OpenALRealChannel realChannel = new OpenALRealChannel();
                AddRealChannel(realChannel, true);
                realChannels.Add(realChannel);
            }

            fileStreamRealChannels = new List <OpenALRealChannel>();

            thread = new Thread(new ThreadStart(ThreadFunction));
            try
            {
                if (SystemSettings.CurrentPlatform != SystemSettings.Platform.UWP)
                {
                    thread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                }
            }
            catch { }
            thread.IsBackground = true;
            thread.Start();

            hWnd = mainWindowHandle;

            Al.alDistanceModel(Al.AL_NONE);

            return(true);
        }
Esempio n. 10
0
 public void RegisterThread()
 {
     int rv = Alc.alcMakeContextCurrent(context);
 }