Example #1
0
    // Update is called once per frame
    void Update()
    {
        //reset counting variables
        totalEnergy1 = 0;
        totalEnergy2 = 0;
        totalEnergy3 = 0;

        totalRotationalMomentum1 = 0;
        totalRotationalMomentum2 = 0;
        totalRotationalMomentum3 = 0;

        netVelocity1 = Vector3.zero;
        netVelocity2 = Vector3.zero;
        netVelocity3 = Vector3.zero;

        foreach (Transform child in transform)
        {
            if (Vector3.SqrMagnitude(child.position - basinAxis1.position) <= maxCalcDistanceSqr)                                                        //if it's in the first basin
            {
                Rigidbody body = child.GetComponent <Rigidbody>();                                                                                       //get rigidbody component
                if (body != null)                                                                                                                        //verify it's not null
                {
                    totalEnergy1             += Vector3.SqrMagnitude(body.velocity) * body.mass;                                                         //calculate total particle energy where E = m * |v|^2
                    totalRotationalMomentum1 += Vector3.Dot(Vector3.Cross(child.position - basinAxis1.position, body.mass * body.velocity), Vector3.up); //calculate total angular momentum where L = R x mV
                    netVelocity1             += body.velocity;                                                                                           //calculate net velocity in x,y,z
                }
            }
            else if (Vector3.SqrMagnitude(child.position - basinAxis2.position) <= maxCalcDistanceSqr)                                                   //if it's in the second basin
            {
                Rigidbody body = child.GetComponent <Rigidbody>();                                                                                       //get rigidbody component
                if (body != null)                                                                                                                        //verify it's not null
                {
                    totalEnergy2             += Vector3.SqrMagnitude(body.velocity) * body.mass;                                                         //calculate total particle energy where E = m * |v|^2
                    totalRotationalMomentum2 += Vector3.Dot(Vector3.Cross(child.position - basinAxis2.position, body.mass * body.velocity), Vector3.up); //calculate total angular momentum where L = R x mV
                    netVelocity2             += body.velocity;                                                                                           //calculate net velocity in x,y,z
                }
            }
            else if (Vector3.SqrMagnitude(child.position - basinAxis3.position) <= maxCalcDistanceSqr)                                                   //if it's in the third basin
            {
                Rigidbody body = child.GetComponent <Rigidbody>();                                                                                       //get rigidbody component
                if (body != null)                                                                                                                        //verify it's not null
                {
                    totalEnergy3             += Vector3.SqrMagnitude(body.velocity) * body.mass;                                                         //calculate total particle energy where E = m * |v|^2
                    totalRotationalMomentum3 += Vector3.Dot(Vector3.Cross(child.position - basinAxis3.position, body.mass * body.velocity), Vector3.up); //calculate total angular momentum where L = R x mV
                    netVelocity3             += body.velocity;                                                                                           //calculate net velocity in x,y,z
                }
            }
        }

        //update parameters on synthesizers based on inputs
        Hv_simple_sin_synth_AudioLib sinSynth = basinAxis1.GetComponent <Hv_simple_sin_synth_AudioLib>();

        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, totalEnergy1 / energyMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Oscfreq, Mathf.Lerp(oscMin, oscMax, Mathf.Abs(totalRotationalMomentum1) / momentumMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Freqcutoff, Mathf.Lerp(frequencyMin, frequencyMax, Mathf.Abs(netVelocity1.x) / netVelocityMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Ringmodfreq, Mathf.Lerp(ringFreqMin, ringFreqMax, Mathf.Abs(netVelocity1.y) / netVelocityMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Ringmodmultiplier, Mathf.Lerp(ringMultMin, ringMultMax, Mathf.Abs(netVelocity1.z) / netVelocityMax));

        Hv_simple_saw_synth_AudioLib sawSynth = basinAxis2.GetComponent <Hv_simple_saw_synth_AudioLib>();

        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, totalEnergy2 / energyMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Oscfreq, Mathf.Lerp(oscMin, oscMax, Mathf.Abs(totalRotationalMomentum2) / momentumMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Freqcutoff, Mathf.Lerp(frequencyMin, frequencyMax, Mathf.Abs(netVelocity2.x) / netVelocityMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Ringmodfreq, Mathf.Lerp(ringFreqMin, ringFreqMax, Mathf.Abs(netVelocity2.y) / netVelocityMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Ringmodmultiplier, Mathf.Lerp(ringMultMin, ringMultMax, Mathf.Abs(netVelocity2.z) / netVelocityMax));

        Hv_simple_square_synth_AudioLib squareSynth = basinAxis3.GetComponent <Hv_simple_square_synth_AudioLib>();

        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, totalEnergy3 / energyMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Oscfreq, Mathf.Lerp(oscMin, oscMax, Mathf.Abs(totalRotationalMomentum3) / momentumMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Freqcutoff, Mathf.Lerp(frequencyMin, frequencyMax, Mathf.Abs(netVelocity3.x) / netVelocityMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodfreq, Mathf.Lerp(ringFreqMin, ringFreqMax, Mathf.Abs(netVelocity3.y) / netVelocityMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodmultiplier, Mathf.Lerp(ringMultMin, ringMultMax, Mathf.Abs(netVelocity3.z) / netVelocityMax));
    }
    public override void OnInspectorGUI()
    {
        bool isEnabled = _dsp.IsInstantiated();

        if (!isEnabled)
        {
            EditorGUILayout.LabelField("Press Play!", EditorStyles.centeredGreyMiniLabel);
        }
        GUILayout.EndVertical();

        // parameters
        GUI.enabled = true;
        GUILayout.BeginVertical();
        EditorGUILayout.Space();
        EditorGUI.indentLevel++;

        // freqCutoff
        GUILayout.BeginHorizontal();
        float freqCutoff    = _dsp.GetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Freqcutoff);
        float newFreqcutoff = EditorGUILayout.Slider("freqCutoff", freqCutoff, 0.0f, 20000.0f);

        if (freqCutoff != newFreqcutoff)
        {
            _dsp.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Freqcutoff, newFreqcutoff);
        }
        GUILayout.EndHorizontal();

        // gain
        GUILayout.BeginHorizontal();
        float gain    = _dsp.GetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Gain);
        float newGain = EditorGUILayout.Slider("gain", gain, 0.0f, 1.0f);

        if (gain != newGain)
        {
            _dsp.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Gain, newGain);
        }
        GUILayout.EndHorizontal();

        // oscFreq
        GUILayout.BeginHorizontal();
        float oscFreq    = _dsp.GetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Oscfreq);
        float newOscfreq = EditorGUILayout.Slider("oscFreq", oscFreq, 0.0f, 20000.0f);

        if (oscFreq != newOscfreq)
        {
            _dsp.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Oscfreq, newOscfreq);
        }
        GUILayout.EndHorizontal();

        // ringModFreq
        GUILayout.BeginHorizontal();
        float ringModFreq    = _dsp.GetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodfreq);
        float newRingmodfreq = EditorGUILayout.Slider("ringModFreq", ringModFreq, 0.0f, 20000.0f);

        if (ringModFreq != newRingmodfreq)
        {
            _dsp.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodfreq, newRingmodfreq);
        }
        GUILayout.EndHorizontal();

        // ringModMultiplier
        GUILayout.BeginHorizontal();
        float ringModMultiplier    = _dsp.GetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodmultiplier);
        float newRingmodmultiplier = EditorGUILayout.Slider("ringModMultiplier", ringModMultiplier, 0.0f, 1.0f);

        if (ringModMultiplier != newRingmodmultiplier)
        {
            _dsp.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodmultiplier, newRingmodmultiplier);
        }
        GUILayout.EndHorizontal();
        EditorGUI.indentLevel--;
    }