Example #1
0
        public static void Update()
        {
            if (system == null)
            {
                return;
            }

            // Update sound
            if (channel != null)
            {
                FMOD.MODE mode = FMOD.MODE.DEFAULT;
                channel.getMode(ref mode);

                if ((mode & FMOD.MODE._3D) == FMOD.MODE._3D)
                {
                    FMOD.VECTOR pos; pos.x = 0.0f; pos.y = 0.0f; pos.z = 0.0f;
                    FMOD.VECTOR vel; vel.x = 0.0f; vel.y = 0.0f; vel.z = 0.0f;
                    channel.get3DAttributes(ref pos, ref vel);

                    float distance = (float)Math.Sqrt((double)(pos.x * pos.x + pos.y * pos.y));

                    float minDist = 0.0f, maxDist = 1000.0f;
                    channel.get3DMinMaxDistance(ref minDist, ref maxDist);

                    float distRatio = distance / maxDist;

                    if (ignorefod == false)
                    {
                        // Set direct and reverb occlusion
                        float direct = 1.0f - GetDistanceRatioValue(CONTROLTYPE.DIRECT, distRatio);
                        float reverb = 1.0f - GetDistanceRatioValue(CONTROLTYPE.REVERB, distRatio);
                        channel.set3DOcclusion(direct, reverb);

                        // Set lowpass
                        FMOD.DSP head = null;
                        channel.getDSPHead(ref head);
                        if (head != null)
                        {
                            int inputs = 0;
                            head.getNumInputs(ref inputs);
                            for (int i = 0; i < inputs; i++)
                            {
                                FMOD.DSP           dsp    = null;
                                FMOD.DSPConnection dspcon = null;
                                head.getInput(i, ref dsp, ref dspcon);
                                if (dsp == null)
                                {
                                    continue;
                                }

                                FMOD.DSP_TYPE type = FMOD.DSP_TYPE.UNKNOWN;
                                dsp.getType(ref type);
                                if (type == FMOD.DSP_TYPE.LOWPASS_SIMPLE)
                                {
                                    float hrtf = 1.0f;

                                    FMOD.VECTOR dir;
                                    dir.x = pos.x;
                                    dir.y = pos.y;
                                    dir.z = 0.0f;

                                    float length = (float)Math.Sqrt((double)(dir.x * dir.x + dir.y * dir.y));
                                    if (length > 0.0001f)
                                    {
                                        length = 1.0f / length;
                                        dir.x *= length;
                                        dir.y *= length;
                                        float _angle = (float)Math.Acos((double)dir.y);
                                        hrtf = 1.0f - HRTFDATA.Scale * Math.Min(1.0f, Math.Max(0.0f, (_angle - HRTFDATA.MinAngle) / (HRTFDATA.MaxAngle - HRTFDATA.MinAngle)));
                                    }

                                    float lowpass = 21990.0f * GetDistanceRatioValue(CONTROLTYPE.LOWPASS, distRatio) * hrtf + 10.0f;
                                    dsp.setParameter((int)FMOD.DSP_LOWPASS_SIMPLE.CUTOFF, lowpass);

                                    break;
                                }
                            }
                        }

                        // Set pan level
                        float panlevel = GetDistanceRatioValue(CONTROLTYPE.PANLEVEL, distRatio);
                        channel.set3DPanLevel(panlevel);
                    }

                    else
                    {
                        channel.set3DPanLevel(0.0f);
                    }
                }
            }

            system.update();
        }