コード例 #1
0
ファイル: Plugin.cs プロジェクト: fedarovich/xplane-dotnet
        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);
        }