Example #1
0
        /// <summary>
        /// Initialize the FMOD sound system.
        /// </summary>
        private void InitFMOD()
        {
            try
            {
                FMODExec(FMOD.Factory.System_Create(ref system));
                uint version = 0;
                FMODExec(system.getVersion(ref version));

                if (version < FMOD.VERSION.number)
                {
                    throw new MediaException("You are using an old version of FMOD " +
                                             version.ToString("X") +
                                             ".  This program requires " +
                                             FMOD.VERSION.number.ToString("X") + ".");
                }

                // Assume no special hardware capabilities except 5.1 surround sound.
                FMOD.CAPS        caps        = FMOD.CAPS.NONE;
                FMOD.SPEAKERMODE speakermode = FMOD.SPEAKERMODE._5POINT1;

                // Try to detect soud system used
                if (System.Environment.OSVersion.Platform == PlatformID.Unix || System.Environment.OSVersion.Platform == PlatformID.MacOSX)
                {
                    bool audioOK = false;
                    var  res     = system.setOutput(FMOD.OUTPUTTYPE.COREAUDIO);
                    if (res == RESULT.OK)
                    {
                        audioOK = true;
                    }

                    if (!audioOK)
                    {
                        res = system.setOutput(FMOD.OUTPUTTYPE.PULSEAUDIO);
                        if (res == RESULT.OK)
                        {
                            audioOK = true;
                        }
                    }

                    if (!audioOK)
                    {
                        res = system.setOutput(FMOD.OUTPUTTYPE.ALSA);
                        if (res == RESULT.OK)
                        {
                            audioOK = true;
                        }
                    }

                    if (!audioOK)
                    {
                        res = system.setOutput(FMOD.OUTPUTTYPE.OSS);
                        if (res == RESULT.OK)
                        {
                            audioOK = true;
                        }
                    }

                    if (!audioOK)
                    {
                        res = system.setOutput(FMOD.OUTPUTTYPE.AUTODETECT);
                        if (res == RESULT.OK)
                        {
                            audioOK = true;
                        }
                    }
                }

                FMOD.OUTPUTTYPE outputType = OUTPUTTYPE.UNKNOWN;
                FMODExec(system.getOutput(ref outputType));

                // Fancy param checking on Linux can cause init to fail
                try
                {
                    // Get the capabilities of the driver.
                    int outputRate = 0;
                    FMODExec(system.getDriverCaps(0, ref caps,
                                                  ref outputRate,
                                                  ref speakermode));
                    // Set FMOD speaker mode to what the driver supports.
                    FMODExec(system.setSpeakerMode(speakermode));
                }
                catch {}

                // The user has the 'Acceleration' slider set to off, which
                // is really bad for latency.  At 48khz, the latency between
                // issuing an fmod command and hearing it will now be about 213ms.
                if ((caps & FMOD.CAPS.HARDWARE_EMULATED) == FMOD.CAPS.HARDWARE_EMULATED)
                {
                    FMODExec(system.setDSPBufferSize(1024, 10));
                }

                try
                {
                    StringBuilder name = new StringBuilder(128);
                    // Get driver information so we can check for a wierd one.
                    FMOD.GUID guid = new FMOD.GUID();
                    FMODExec(system.getDriverInfo(0, name, 128, ref guid));

                    // Sigmatel sound devices crackle for some reason if the format is pcm 16bit.
                    // pcm floating point output seems to solve it.
                    if (name.ToString().IndexOf("SigmaTel") != -1)
                    {
                        FMODExec(system.setSoftwareFormat(
                                     48000,
                                     FMOD.SOUND_FORMAT.PCMFLOAT,
                                     0, 0,
                                     FMOD.DSP_RESAMPLER.LINEAR)
                                 );
                    }
                }
                catch {}

                // Try to initialize with all those settings, and Max 32 channels.
                FMOD.RESULT result = system.init(32, FMOD.INITFLAGS.NORMAL, (IntPtr)null);
                if (result == FMOD.RESULT.ERR_OUTPUT_CREATEBUFFER)
                {
                    // Can not handle surround sound - back to Stereo.
                    FMODExec(system.setSpeakerMode(FMOD.SPEAKERMODE.STEREO));

                    // And init again.
                    FMODExec(system.init(
                                 32,
                                 FMOD.INITFLAGS.NORMAL,
                                 (IntPtr)null)
                             );
                }
                else if (result != FMOD.RESULT.OK)
                {
                    throw(new Exception(result.ToString()));
                }

                // Set real-world effect scales.
                FMODExec(system.set3DSettings(
                             1.0f, // Doppler scale
                             1.0f, // Distance scale is meters
                             1.0f) // Rolloff factor
                         );

                soundSystemAvailable = true;
                Logger.Log("Initialized FMOD Ex: " + outputType.ToString(), Helpers.LogLevel.Debug);
            }
            catch (Exception ex)
            {
                Logger.Log("Failed to initialize the sound system: " + ex.ToString(), Helpers.LogLevel.Warning);
            }
        }
Example #2
0
        /// <summary>
        /// Initialize the FMOD sound system.
        /// </summary>
        private void InitFMOD()
        {
            try
            {
                FMODExec(FMOD.Factory.System_Create(out system));
                uint version = 0;
                FMODExec(system.getVersion(out version));

                if (version < FMOD.VERSION.number)
                {
                    throw new MediaException("You are using an old version of FMOD " +
                                             version.ToString("X") +
                                             ".  This program requires " +
                                             FMOD.VERSION.number.ToString("X") + ".");
                }

                // Try to detect soud system used
                if (System.Environment.OSVersion.Platform == PlatformID.Unix || System.Environment.OSVersion.Platform == PlatformID.MacOSX)
                {
                    bool audioOK = false;
                    var  res     = system.setOutput(FMOD.OUTPUTTYPE.COREAUDIO);
                    if (res == RESULT.OK)
                    {
                        audioOK = true;
                    }

                    if (!audioOK)
                    {
                        res = system.setOutput(FMOD.OUTPUTTYPE.PULSEAUDIO);
                        if (res == RESULT.OK)
                        {
                            audioOK = true;
                        }
                    }

                    if (!audioOK)
                    {
                        res = system.setOutput(FMOD.OUTPUTTYPE.ALSA);
                        if (res == RESULT.OK)
                        {
                            audioOK = true;
                        }
                    }

                    if (!audioOK)
                    {
                        res = system.setOutput(FMOD.OUTPUTTYPE.AUTODETECT);
                        if (res == RESULT.OK)
                        {
                            audioOK = true;
                        }
                    }
                }

                FMOD.OUTPUTTYPE outputType = OUTPUTTYPE.UNKNOWN;
                FMODExec(system.getOutput(out outputType));

// *TODO: Investigate if this all is still needed under FMODStudio
#if false
                // The user has the 'Acceleration' slider set to off, which
                // is really bad for latency.  At 48khz, the latency between
                // issuing an fmod command and hearing it will now be about 213ms.
                if ((caps & FMOD.CAPS.HARDWARE_EMULATED) == FMOD.CAPS.HARDWARE_EMULATED)
                {
                    FMODExec(system.setDSPBufferSize(1024, 10));
                }

                try
                {
                    StringBuilder name = new StringBuilder(128);
                    // Get driver information so we can check for a wierd one.
                    Guid guid = new Guid();
                    FMODExec(system.getDriverInfo(0, name, 128, out guid));

                    // Sigmatel sound devices crackle for some reason if the format is pcm 16bit.
                    // pcm floating point output seems to solve it.
                    if (name.ToString().IndexOf("SigmaTel") != -1)
                    {
                        FMODExec(system.setSoftwareFormat(
                                     48000,
                                     FMOD.SOUND_FORMAT.PCMFLOAT,
                                     0, 0,
                                     FMOD.DSP_RESAMPLER.LINEAR)
                                 );
                    }
                }
                catch {}
#endif
                // FMODExec(system.setDSPBufferSize(1024, 10));

                // Try to initialize with all those settings, and Max 32 channels.
                FMOD.RESULT result = system.init(32, FMOD.INITFLAGS.NORMAL, (IntPtr)null);
                if (result != FMOD.RESULT.OK)
                {
                    throw(new Exception(result.ToString()));
                }

                // Set real-world effect scales.
                FMODExec(system.set3DSettings(
                             1.0f, // Doppler scale
                             1.0f, // Distance scale is meters
                             1.0f) // Rolloff factor
                         );

                soundSystemAvailable = true;
                Logger.Log("Initialized FMOD Ex: " + outputType, Helpers.LogLevel.Debug);
            }
            catch (Exception ex)
            {
                Logger.Log("Failed to initialize the sound system: " + ex, Helpers.LogLevel.Warning);
            }
        }
Example #3
0
        FMOD.RESULT Initialize()
        {
            #if UNITY_EDITOR
                #if UNITY_2017_2_OR_NEWER
            EditorApplication.playModeStateChanged += HandlePlayModeStateChange;
                #elif UNITY_2017_1_OR_NEWER
            EditorApplication.playmodeStateChanged += HandleOnPlayModeChanged;
                #endif // UNITY_2017_2_OR_NEWER
            #endif     // UNITY_EDITOR

            FMOD.RESULT result       = FMOD.RESULT.OK;
            FMOD.RESULT initResult   = FMOD.RESULT.OK;
            Settings    fmodSettings = Settings.Instance;
            fmodPlatform = RuntimeUtils.GetCurrentPlatform();

            int sampleRate               = fmodSettings.GetSampleRate(fmodPlatform);
            int realChannels             = Math.Min(fmodSettings.GetRealChannels(fmodPlatform), 256); // Prior to 1.08.10 we didn't clamp this properly in the settings screen
            int virtualChannels          = fmodSettings.GetVirtualChannels(fmodPlatform);
            FMOD.SPEAKERMODE speakerMode = (FMOD.SPEAKERMODE)fmodSettings.GetSpeakerMode(fmodPlatform);
            FMOD.OUTPUTTYPE  outputType  = FMOD.OUTPUTTYPE.AUTODETECT;

            FMOD.ADVANCEDSETTINGS advancedSettings = new FMOD.ADVANCEDSETTINGS();
            advancedSettings.randomSeed = (uint)DateTime.Now.Ticks;
            #if UNITY_EDITOR || UNITY_STANDALONE
            advancedSettings.maxVorbisCodecs = realChannels;
            #elif UNITY_XBOXONE
            advancedSettings.maxXMACodecs = realChannels;
            #elif UNITY_PS4
            advancedSettings.maxAT9Codecs = realChannels;
            #else
            advancedSettings.maxFADPCMCodecs = realChannels;
            #endif

            SetThreadAffinity();

            #if UNITY_EDITOR || ((UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX) && DEVELOPMENT_BUILD)
            result = FMOD.Debug.Initialize(FMOD.DEBUG_FLAGS.LOG, FMOD.DEBUG_MODE.FILE, null, RuntimeUtils.LogFileName);
            if (result == FMOD.RESULT.ERR_FILE_NOTFOUND)
            {
                UnityEngine.Debug.LogWarningFormat("FMOD Studio: Cannot open FMOD debug log file '{0}', logs will be missing for this session.", System.IO.Path.Combine(Application.dataPath, RuntimeUtils.LogFileName));
            }
            else
            {
                CheckInitResult(result, "FMOD.Debug.Initialize");
            }
            #endif

            FMOD.Studio.INITFLAGS studioInitFlags = FMOD.Studio.INITFLAGS.NORMAL | FMOD.Studio.INITFLAGS.DEFERRED_CALLBACKS;
            if (fmodSettings.IsLiveUpdateEnabled(fmodPlatform))
            {
                studioInitFlags |= FMOD.Studio.INITFLAGS.LIVEUPDATE;

                #if UNITY_5_0 || UNITY_5_1 // These versions of Unity shipped with FMOD4 profiling enabled consuming our port number.
                UnityEngine.Debug.LogWarning("FMOD Studio: Live Update port in-use by Unity, switching to port 9265");
                advancedSettings.profilePort = 9265;
                #endif
            }

retry:
            result = FMOD.Studio.System.create(out studioSystem);
            CheckInitResult(result, "FMOD.Studio.System.create");

            result = studioSystem.getLowLevelSystem(out lowlevelSystem);
            CheckInitResult(result, "FMOD.Studio.System.getLowLevelSystem");

            result = lowlevelSystem.setOutput(outputType);
            CheckInitResult(result, "FMOD.System.setOutput");

            result = lowlevelSystem.setSoftwareChannels(realChannels);
            CheckInitResult(result, "FMOD.System.setSoftwareChannels");

            result = lowlevelSystem.setSoftwareFormat(sampleRate, speakerMode, 0);
            CheckInitResult(result, "FMOD.System.setSoftwareFormat");

            result = lowlevelSystem.setAdvancedSettings(ref advancedSettings);
            CheckInitResult(result, "FMOD.System.setAdvancedSettings");

            result = studioSystem.initialize(virtualChannels, studioInitFlags, FMOD.INITFLAGS.NORMAL, IntPtr.Zero);
            if (result != FMOD.RESULT.OK && initResult == FMOD.RESULT.OK)
            {
                initResult = result; // Save this to throw at the end (we'll attempt NO SOUND to shield ourselves from unexpected device failures)
                outputType = FMOD.OUTPUTTYPE.NOSOUND;
                UnityEngine.Debug.LogErrorFormat("FMOD Studio: Studio::System::initialize returned {0}, defaulting to no-sound mode.", result.ToString());

                goto retry;
            }
            CheckInitResult(result, "Studio::System::initialize");

            // Test network functionality triggered during System::update
            if ((studioInitFlags & FMOD.Studio.INITFLAGS.LIVEUPDATE) != 0)
            {
                studioSystem.flushCommands(); // Any error will be returned through Studio.System.update

                result = studioSystem.update();
                if (result == FMOD.RESULT.ERR_NET_SOCKET_ERROR)
                {
                    studioInitFlags &= ~FMOD.Studio.INITFLAGS.LIVEUPDATE;
                    UnityEngine.Debug.LogWarning("FMOD Studio: Cannot open network port for Live Update (in-use), restarting with Live Update disabled.");

                    result = studioSystem.release();
                    CheckInitResult(result, "FMOD.Studio.System.Release");

                    goto retry;
                }
            }

            LoadPlugins(fmodSettings);
            LoadBanks(fmodSettings);

            return(initResult);
        }