Esempio n. 1
0
        public bool Activate(
            ActivationParams ap, Parameters.EnvelopeParams eo
#if UNITY_EDITOR
            , Patch patch
#endif
            )
        {
            bool delayed = !Mathf.Approximately(ap.delay, 0f);

            if (delayed || (!randomize && !increment) ||
                lastFrame != Time.frameCount || !Application.isPlaying)
            {
                if (!delayed)
                {
                    lastFrame = Time.frameCount;
                }
                return(Synthesizer.Activate(
                           this, eo, ap
#if UNITY_EDITOR
                           , patch
#endif
                           ));
            }
            return(false);
        }
Esempio n. 2
0
        internal static bool Activate(
            AudioProgram a, Parameters.EnvelopeParams ep, ActivationParams ap
#if UNITY_EDITOR
            , Patch patch
#endif
            )
        {
            var p = a.parameters;

            p.envelope = ep;
            float gain;
            var   clip = a.GetClip(out gain);

#if UNITY_EDITOR
            if (!clip)
            {
                Debug.LogError("Activate: Null AudioClip from patch: " + patch, patch);
            }
#endif
            ap.volume *= 1f + gain;
            bool looping = Activate(
                a.mixerGroup, clip, p, ap
#if UNITY_EDITOR
                , patch
#endif
                );
            return(looping);
        }
Esempio n. 3
0
        internal static bool Activate(AudioSequence s, ActivationParams ap)
        {
            bool looping = false;

            for (int i = 0, n = s.timing.Length; i < n; ++i)
            {
                var g = s.timing[i];
                if (Randomizer.zeroToOne <= g.randomization.chance)
                {
                    var ap2 = ap;
                    ap2.delay += g.GetDelay();
                    if (g.patch != null)
                    {
                        looping |= g.patch.Activate(ap2);
                    }
                    else
                    {
                        looping |= Activate(s.patch.program, ap2
#if UNITY_EDITOR
                                            , s.patch
#endif
                                            );
                    }
                }
            }
            return(looping);
        }
Esempio n. 4
0
        public bool Activate(ActivationParams ap, Parameters.EnvelopeParams ep)
        {
            return(program.Activate(ap, ep
#if UNITY_EDITOR
                                    , this
#endif
                                    ));
        }
Esempio n. 5
0
 public bool Activate(ActivationParams ap)
 {
     if (lastHandle != ap.handle || !Application.isPlaying)
     {
         lastHandle = ap.handle;
         return(Synthesizer.Activate(this, ap));
     }
     return(false);
 }
Esempio n. 6
0
        public bool Activate(ActivationParams ap)
        {
            return(hasTimings ?
                   sequence.Activate(ap) :
                   program.Activate(ap
#if UNITY_EDITOR
                                    , this
#endif
                                    ));
        }
Esempio n. 7
0
        internal static bool ActivateInternal(
            AudioMixerGroup g, AudioClip c, Parameters p,
            ActivationParams ap, Vector3 pos2, float pitch
#if UNITY_EDITOR
            , Patch patch
#endif
            )
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                UnityEditor.EditorApplication.CallbackFunction f = null;
                float n = Time.realtimeSinceStartup;
                f = () => {
                    if (Time.realtimeSinceStartup - n >= ap.delay)
                    {
                        UnityEditor.EditorApplication.update -= f;
                        PlayClip(c, p.loop);
                    }
                };
                UnityEditor.EditorApplication.update += f;
                return(false);
            }
#endif

            var i = freeSources.Count > 0 ? freeSources.Pop() : CreateSource();
            i.transform.position = pos2;
            i.Enable(p);

            var z = new ActiveSource {
                handle = ap.handle,
                keyOn  = ap.delay,
                keyOff = false,
#if UNITY_EDITOR
                patch = patch,
#endif
                info          = i,
                target        = (ap.transform == null || ap.transform.gameObject.isStatic) ? null : ap.transform,
                localPosition = ap.position,
                volume        = p.GetVolume() * Mathf.Clamp01(ap.volume),
                modVolume     = ap.modVolume,
                envelope      = Envelope.instant
            };

#if SYNTHESIZER_PARANOIA
            Debug.LogFormat(
                Time.frameCount.ToString("X4") +
                " Synthesizer.ActivateInternal: {0} {1} ({2})",
                g, c.name, z.target);
#endif

            if (p.envelope.attack > Mathf.Epsilon)
            {
                z.envelope.SetAttack(p.envelope.attack);
            }
            if (p.envelope.release > Mathf.Epsilon)
            {
                z.envelope.SetRelease(p.envelope.release);
            }

            ActivateStatic(g, c, p, i.audioSource, ap.delay, z.GetVolume(), pitch);
            activeSources0.Add(z);
            return(p.loop);
        }
Esempio n. 8
0
        internal static bool Activate(
            AudioMixerGroup g, AudioClip c, Parameters p, ActivationParams ap
#if UNITY_EDITOR
            , Patch patch
#endif
            )
        {
            float r = p.randomization.distance.GetRandomValue();

            if (!Mathf.Approximately(r, 0f))
            {
                float a = Randomizer.plusMinusOne * Mathf.PI;
                r             *= (p.spatial.distance.max - p.spatial.distance.min);
                r             += p.spatial.distance.min;
                ap.position.x += Mathf.Sin(a) * r;
                ap.position.z += Mathf.Cos(a) * r;
            }

            var pos2 = ap.position;

            if (ap.transform != null)
            {
                pos2 += ap.transform.position;
            }

            bool looping = ActivateInternal(
                g, c, p, ap, pos2, 1f
#if UNITY_EDITOR
                , patch
#endif
                );

            if (!looping && p.occlusion.function != OcclusionFunction.None && p.slapback.patch != null)
            {
                AudioSlapback s;
                Vector3       pos3, d3;
                if ((bool)(s = AudioSlapback.FindClosest(pos2, out pos3, out d3)))
                {
                    var patch2 = p.slapback.patch;
                    var p2     = patch2.program.parameters;

                    var   lpos = Heartbeat.listenerTransform.position;
                    var   d    = pos3 - lpos;
                    var   pos4 = pos3 + d.normalized * s.radius;
                    float dt   = s.radius / OcclusionSettings.instance.speedOfSound;

                    p2.randomization.distance.min = 0f;
                    p2.randomization.distance.max = 0f;
                    p2.occlusion.function         = OcclusionFunction.Slapback;

                    float gain;
                    var   clip = patch2.program.GetClip(out gain);
                    if (!clip)
                    {
                        Debug.LogError("Activate: Null AudioClip from patch: " + patch2, patch2);
                    }
                    Activate(
                        patch2.program.mixerGroup, clip, p2,
                        new ActivationParams {
                        transform = null,
                        position  = pos4,
                        delay     = ap.delay + dt,
                        volume    = ap.volume * (1f + gain),
                        modVolume = ap.modVolume,
                        handle    = ap.handle
                    }
#if UNITY_EDITOR
                        , patch2
#endif
                        );
                }
            }

            return(looping);
        }