Пример #1
0
        private unsafe PinMameController(IInputContext input)
        {
            _pinMame = PinMame.PinMame.Instance();

            _pinMame.OnGameStarted        += OnGameStarted;
            _pinMame.OnDisplayAvailable   += OnDisplayAvailable;
            _pinMame.OnDisplayUpdated     += OnDisplayUpdated;
            _pinMame.OnAudioAvailable     += OnAudioAvailable;
            _pinMame.OnAudioUpdated       += OnAudioUpdated;
            _pinMame.OnConsoleDataUpdated += OnConsoleDataUpdated;
            _pinMame.OnGameEnded          += OnGameEnded;
            _pinMame.IsKeyPressed         += IsKeyPressed;

            _pinMame.SetHandleKeyboard(true);

            _dmdController = DmdController.Instance();

            _input = input;

            foreach (var keyboard in _input.Keyboards)
            {
                keyboard.KeyDown += (arg1, arg2, arg3) =>
                {
                    if (_keycodeMap.TryGetValue(arg2, out var keycode))
                    {
                        Logger.Trace($"KeyDown() {keycode} ({(int)keycode})");

                        _keypress[(int)keycode] = 1;
                    }
                };

                keyboard.KeyUp += (arg1, arg2, arg3) =>
                {
                    if (_keycodeMap.TryGetValue(arg2, out var keycode))
                    {
                        Logger.Trace($"KeyUp() {keycode} ({(int)keycode})");

                        _keypress[(int)keycode] = 0;
                    }
                };
            }

            _al = AL.GetApi(true);

            ALContext alContext = ALContext.GetApi(true);
            var       device    = alContext.OpenDevice("");

            if (device != null)
            {
                alContext.MakeContextCurrent(
                    alContext.CreateContext(device, null));

                _audioSource  = _al.GenSource();
                _audioBuffers = _al.GenBuffers(_maxAudioBuffers);
            }
            else
            {
                Logger.Error("PinMameController(): Could not create device");
            }
        }
Пример #2
0
        public OpenAlAudioBackend()
        {
            _al = AL.GetApi(true);

            // So.
            // Current version of Silk.NET is smashed and doesn't let us use soft_oal
            // for the ALContext (but does for the OpenAL API itself).
            //
            // This'll be fixed in two weeks.
            //
            // Oh and they protected the native constructor. Ew. That'll also
            // be fixed in two weeks.
            //
            // For now... YOUR IDE WILL SCREAM AT YOU IN TERROR BECAUSE I'M USING
            // SUPER SPOOKY SCARY EVIL REFLECTION STUFF but...
            _alc = (ALContext)typeof(ALContext)
                   .GetConstructor(
                BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance,
                null,
                new[]
            {
                typeof(INativeContext)
            },
                null
                )?.Invoke(new[] { _al.Context });

            unsafe
            {
                var device = _alc.OpenDevice(string.Empty);
                var error  = _alc.GetError(device);
                if (error != ContextError.NoError)
                {
                    throw new InvalidOperationException("OpenAL threw an error: " + error);
                }


                var ctx = _alc.CreateContext(device, null);
                error = _alc.GetError(device);
                if (error != ContextError.NoError)
                {
                    throw new InvalidOperationException("OpenAL threw an error: " + error);
                }


                _alc.MakeContextCurrent(ctx);
                error = _alc.GetError(device);
                if (error != ContextError.NoError)
                {
                    throw new InvalidOperationException("OpenAL threw an error: " + error);
                }
            }

            ThrowOnError();
        }
Пример #3
0
        internal static void Init()
        {
            // Jos initialisointia on jo kutsuttu.
            if (initialized)
            {
                return;
            }

            initialized = true;

            // Don't initialize OpenAL in headless mode.
            if (CommandLineOptions.Headless ?? Game.Instance?.Headless ?? false)
            {
                return;
            }

            ALContext alc = null;

            try
            {
                // Yritetään ladata OpenAL Soft.
                // Jos softia ei ole saatavilla (Esim. M1 tms. ARM suorittimella varustettu kone),
                // Koitetaan sitten käyttää laitteen omaa OpenAL kirjastoa.
                // TODO: Kumpi on oikeasti parempi? Pitäisikö logiikan mennä toisinpäin?
                alc = ALContext.GetApi(true);
                al  = AL.GetApi(true);
            }
            catch
            {
                alc = ALContext.GetApi();
                al  = AL.GetApi();
            }

            var device = alc.OpenDevice("");

            if (device == null)
            {
                al = null;
                throw new AudioDeviceException("Unable to initialize OpenAL device.");
            }

            var context = alc.CreateContext(device, null);

            alc.MakeContextCurrent(context);
            al.DistanceModel(DistanceModel.InverseDistance);
            al.GetError();

            al.SetListenerProperty(ListenerVector3.Position, 0, 0, 1);
        }
Пример #4
0
        public AudioOutput(int channels = 1, int sampleRate = 44100)
        {
            if (channels < 1 || channels > 2)
            {
                throw new ArgumentOutOfRangeException(nameof(channels));
            }

            if (sampleRate < 2000 || sampleRate > 200000)
            {
                throw new ArgumentOutOfRangeException(nameof(sampleRate));
            }

            try
            {
                al        = AL.GetApi(true);
                alContext = ALContext.GetApi(true);
            }
            catch
            {
                al        = AL.GetApi(false);
                alContext = ALContext.GetApi(false);
            }
            device = alContext.OpenDevice("");

            Available = device != null;

            if (Available)
            {
                context = alContext.CreateContext(device, null);
                alContext.MakeContextCurrent(context);
                if (al.GetError() != AudioError.NoError)
                {
                    Available = false;
                    if (context != null)
                    {
                        alContext.DestroyContext(context);
                    }
                    alContext.CloseDevice(device);
                    al.Dispose();
                    alContext.Dispose();
                    disposed = true;
                    return;
                }
                source = al.GenSource();
                al.SetSourceProperty(source, SourceBoolean.Looping, true);
                al.SetSourceProperty(source, SourceFloat.Gain, 1.0f);
            }
        }
Пример #5
0
        private unsafe float InitSound(float elapsedSinceLastCall, float elapsedTimeSinceLastFlightLoop, int counter)
        {
            CheckError();

            // We have to save the old context and restore it later, so that we don't interfere with X-Plane
            // and other plugins.

            var oldContext = _alc.GetCurrentContext();

            // Try to create our own default device and context.  If we fail, we're dead, we won't play any sound.
            _device = _alc.OpenDevice(null);
            if (_device == null)
            {
                XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device.");
                return(0);
            }

            _context = _alc.CreateContext(_device, null);
            if (_context == null)
            {
                if (oldContext != null)
                {
                    _alc.MakeContextCurrent(oldContext);
                }

                _alc.CloseDevice(_device);
                _device = null;
                XPlane.Trace.WriteLine("[OpenAL Sample] Could not create a context.");
                return(0);
            }

            // Make our context current, so that OpenAL commands affect our, um, stuff.
            _alc.MakeContextCurrent(_context);

            var path = Path.Combine(Path.GetDirectoryName(PluginInfo.ThisPlugin.FilePath), "sound.wav");

            // Generate 1 source and load a buffer of audio.
            _soundSource = _al.GenSource();

            CheckError();

            _soundBuffer = WavHelper.LoadWav(path, _al);
            XPlane.Trace.WriteLine($"[OpenAL Sample] Loaded {_soundBuffer} from {path}.");

            // Basic initialization code to play a sound: specify the buffer the source is playing, as well as some
            // sound parameters. This doesn't play the sound - it's just one-time initialization.
            _al.SetSourceProperty(_soundSource, SourceInteger.Buffer, _soundBuffer);
            _al.SetSourceProperty(_soundSource, SourceFloat.Pitch, 1f);
            _al.SetSourceProperty(_soundSource, SourceFloat.Gain, 1f);
            _al.SetSourceProperty(_soundSource, SourceBoolean.Looping, false);
            _al.SetSourceProperty(_soundSource, SourceVector3.Position, Vector3.Zero);
            _al.SetSourceProperty(_soundSource, SourceVector3.Velocity, Vector3.Zero);
            CheckError();

            // Finally: put back the old context _if_ we had one.  If old_ctx was null, X-Plane isn't using OpenAL.
            if (oldContext != null)
            {
                _alc.MakeContextCurrent(oldContext);
            }

            return(0);
        }
Пример #6
0
        private unsafe float InitSound(float elapsedSinceLastCall, float elapsedTimeSinceLastFlightLoop, int counter)
        {
            CheckError();

            var oldContext = _alc.GetCurrentContext();

            if (oldContext == null)
            {
                XPlane.Trace.WriteLine("[OpenAL Sample] I found no OpenAL, I will be the first to init.");
                _device = _alc.OpenDevice(null);
                if (_device == null)
                {
                    XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device.");
                    return(0);
                }

                _context = _alc.CreateContext(_device, null);
                if (_context == null)
                {
                    _alc.CloseDevice(_device);
                    _device = null;
                    XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device.");
                    return(0);
                }

                _alc.MakeContextCurrent(_context);
                XPlane.Trace.WriteLine("[OpenAL Sample] Created the Open AL context.");

                var hardware = _alc.GetContextProperty(_device, GetContextString.DeviceSpecifier);
                var extensions = _alc.GetContextProperty(_device, GetContextString.Extensions);
                int major, minor;
                _alc.GetContextProperty(_device, GetContextInteger.MajorVersion, 1, &major);
                _alc.GetContextProperty(_device, GetContextInteger.MinorVersion, 1, &minor);
                XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL version   : {major}.{minor}");
                XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL hardware  : {hardware}");
                XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL extensions: {extensions}");
                CheckError();
            }
            else
            {
                XPlane.Trace.WriteLine($"[OpenAL Sample] I found someone else's context: {(ulong)oldContext:X8}");
            }

            var path = Path.Combine(Path.GetDirectoryName(PluginInfo.ThisPlugin.FilePath), "sound.wav");

            // Generate 1 source and load a buffer of audio.
            _soundSource = _al.GenSource();

            CheckError();

            _soundBuffer = WavHelper.LoadWav(path, _al);
            XPlane.Trace.WriteLine($"[OpenAL Sample] Loaded {_soundBuffer} from {path}.");

            // Basic initialization code to play a sound: specify the buffer the source is playing, as well as some
            // sound parameters. This doesn't play the sound - it's just one-time initialization.
            _al.SetSourceProperty(_soundSource, SourceInteger.Buffer, _soundBuffer);
            _al.SetSourceProperty(_soundSource, SourceFloat.Pitch, 1f);
            _al.SetSourceProperty(_soundSource, SourceFloat.Gain, 1f);
            _al.SetSourceProperty(_soundSource, SourceBoolean.Looping, false);
            _al.SetSourceProperty(_soundSource, SourceVector3.Position, Vector3.Zero);
            _al.SetSourceProperty(_soundSource, SourceVector3.Velocity, Vector3.Zero);
            CheckError();

            return(0);
        }