Ejemplo n.º 1
0
        unsafe protected override bool InitLibrary(IntPtr mainWindowHandle,
                                                   int maxReal2DChannels, int maxReal3DChannels)
        {
            NativeLibraryManager.PreLoadLibrary("libogg");
            NativeLibraryManager.PreLoadLibrary("libvorbis");
            NativeLibraryManager.PreLoadLibrary("libvorbisfile");
            NativeLibraryManager.PreLoadLibrary("DirectSoundNativeWrapper");

            {
                DSoundStructureSizes sizes = new DSoundStructureSizes();
                sizes.Init();

                DSoundStructureSizes originalSizes;
                DSound.GetStructureSizes(out originalSizes);

                FieldInfo[] fields = sizes.GetType().GetFields(
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                foreach (FieldInfo field in fields)
                {
                    int originalSize = (int)field.GetValue(originalSizes);
                    int size         = (int)field.GetValue(sizes);

                    if (originalSize != size)
                    {
                        Log.Fatal("DirectXSoundSystem: Invalid unmanaged bridge. " +
                                  "Invalid \"{0}\". Native size = \"{1}\". Managed size = \"{2}\".", field.Name,
                                  originalSize, size);
                        return(false);
                    }
                }
            }

            instance = this;

            criticalSection = CriticalSection.Create();

            DSound.CoInitialize(null);

            int hr;

            //create IDirectSound using the primary sound device
            void */*IDirectSound8*/ directSoundTemp;

            hr = DSound.DirectSoundCreate8(null, out directSoundTemp, null);
            if (Wrapper.FAILED(hr))
            {
                if (hr == DSound.Get_DSERR_NODRIVER())
                {
                    Log.InvisibleInfo("DirectXSoundSystem: No sound driver.");
                    return(false);
                }

                Error("DirectSoundCreate8", hr);
                return(false);
            }
            directSound = (IDirectSound8 *)directSoundTemp;

            //set DirectSound cooperative level
            hWnd = mainWindowHandle;
            hr   = IDirectSound8.SetCooperativeLevel(directSound, hWnd, DSound.DSSCL_PRIORITY);
            if (Wrapper.FAILED(hr))
            {
                Error("SetCooperativeLevel", hr);
                return(false);
            }

            //set primary buffer format
            {
                hr = SetPrimaryBufferFormat(2, 44100, 16, false);
                if (Wrapper.FAILED(hr))
                {
                    hr = SetPrimaryBufferFormat(2, 22050, 16, true);
                }
                if (Wrapper.FAILED(hr))
                {
                    return(false);
                }
            }

            //get listener
            {
                void */*IDirectSoundBuffer*/ primaryBuffer = null;

                // Obtain primary buffer, asking it for 3D control
                DSBUFFERDESC bufferDesc = new DSBUFFERDESC();
                //ZeroMemory( &bufferDesc, sizeof( DSBUFFERDESC ) );
                bufferDesc.dwSize  = (uint)sizeof(DSBUFFERDESC);
                bufferDesc.dwFlags = DSound.DSBCAPS_CTRL3D | DSound.DSBCAPS_PRIMARYBUFFER;

                hr = IDirectSound8.CreateSoundBuffer(directSound, ref bufferDesc,
                                                     out primaryBuffer, null);
                if (Wrapper.FAILED(hr))
                {
                    Error("CreateSoundBuffer", hr);
                    return(false);
                }

                void */*IDirectSound3DListener*/ listenerTemp = null;

                GUID guid = DSound.IID_IDirectSound3DListener;
                if (Wrapper.FAILED(hr = IDirectSoundBuffer.QueryInterface(primaryBuffer,
                                                                          ref guid, &listenerTemp)))
                {
                    IDirectSoundBuffer.Release(primaryBuffer);
                    Error("QueryInterface", hr);
                    return(false);
                }
                listener = (IDirectSound3DListener *)listenerTemp;

                IDirectSoundBuffer.Release(primaryBuffer);
            }

            //update general parameters
            {
                DS3DLISTENER parameters = new DS3DLISTENER();
                parameters.dwSize = (uint)sizeof(DS3DLISTENER);
                IDirectSound3DListener.GetAllParameters(listener, ref parameters);
                parameters.flDistanceFactor = 1;
                parameters.flRolloffFactor  = 0;
                parameters.flDopplerFactor  = DopplerScale;
                hr = IDirectSound3DListener.SetAllParameters(listener, ref parameters, DSound.DS3D_IMMEDIATE);
                if (Wrapper.FAILED(hr))
                {
                    Warning("IDirectSound3DListener.SetAllParameters", hr);
                }
            }

            GenerateRecordDriverList();

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

            fileStreamRealChannels = new List <DirectSoundRealChannel>();

            thread = new Thread(new ThreadStart(ThreadFunction));
            thread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            thread.IsBackground   = true;
            thread.Start();

            return(true);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        //

        public OggFile()
        {
            criticalSection = CriticalSection.Create();
        }