devNotes 5-28-16 sound generation, audio processing, harmonizer

Sat One

Reveille

Jubilee

fdjofjodjf4

tubular

 

AGM_2a

AGM_2b

AGM_2c

AGM_2d

 

Argos Generative Music 2

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[RequireComponent(typeof(AudioSource))]

public class MicrophoneInput : MonoBehaviour
{
    public double bpm = 140.0F;
    public float gain = 0.5F;
    public int signatureHi = 4;
    public int signatureLo = 4;

    [Range(0.00f, 20f)]
    public float multInterval1 = 1f;

    [Range(0.00f, 20f)]
    public float multInterval2 = 1f;

    [Range(0.00f, 20f)]
    public float multInterval3 = 1f;

    [Range(0.01f, 0.02f)]
    public float phase1base = 0.01f;

    [Range(0.01f, 0.02f)]
    public float phase2base = 0.01f;

    [Range(0.01f, 0.02f)]
    public float phase3base = 0.01f;

    [Range(0.0f, 1f)]
    public float attack1 = 0.01f;

    [Range(0.0f, 1f)]
    public float decay1 = 0.01f;


    public bool bVoiceHigh = true;
    public bool bVoiceMid = true;
    public bool bVoiceLow = true;

    float phase1;
    float phase2;
    float phase3;

    float thisTime;
    float lastTime;

    float amp1;

    private double nextTick = 0.0F;
    private float amp = 0.0F;
    private float phase = 0.0F;
    private double sampleRate = 0.0F;
    private int accent;
    private bool running = false;

    void Start()
    {
        accent = signatureHi;
        double startTick = AudioSettings.dspTime;
        sampleRate = AudioSettings.outputSampleRate;
        nextTick = startTick * sampleRate;
        running = true;
    }

    int count1 = 0;
    int count2 = 0;
    int count3 = 0;

    float phasemult1 = 4.0f;
    float phasemult2 = 6.0f;
    float phasemult3 = 2.0f;

    bool switch1 = true;
    bool switch2 = true;
    bool switch3 = true;

    float time1;

    float accumTime;

    void OnAudioFilterRead(float[] data, int channels)
    {
        accumTime = (float)AudioSettings.dspTime;
        if(accumTime - time1 < attack1)
        {
            amp1 = CubicEaseIn(accumTime - time1, 0, 1, attack1);
        }
        else
        {
            amp1 = CubicEaseOut((accumTime - time1 - attack1), 1, -1, decay1);
        }
        amp = 1;
        if (count1++ > 10* multInterval1)
        {
            time1 = accumTime;
            if (switch1)
            {
                if (++phasemult1 > 4)
                {
                    switch1 = false;
                }
            }
            else
            {
                if (--phasemult1 < 3)
                {
                    switch1 = true;
                }
            }
            count1 = 0;
        }

        if (count2++ > 5*multInterval2)
        {
         if (switch2)
            {
                if (++phasemult2 > 7)
                {
                    switch2 = false;
                }
            }
            else
            {
                if (--phasemult2 < 6)
                {
                    switch2 = true;
                }
            }
            count2 = 0;
        }

        if (count3++ > 30*multInterval3)
        {
            //amp = 1.0f;

            if (switch3)
            {
                if (++phasemult3 > 3)
                {
                    switch3 = false;
                }
            }
            else
            {
                if (--phasemult3 < 2)
                {
                    switch3 = true;
                }
            }
            count3 = 0;
        }

        double samplesPerTick = sampleRate * 60.0F / bpm * 4.0F / signatureLo;
        double sample = AudioSettings.dspTime * sampleRate;
        int dataLen = data.Length / channels;
        int n = 0;

        float a, b, c;

        while (n < dataLen)
        {
            if (bVoiceHigh) a = gain * amp * Mathf.Sin(phase2); else a = 0f;
            if (bVoiceMid) b = gain * amp1 * Mathf.Sin(phase1); else b = 0f;
            if (bVoiceLow) c = gain * amp * Mathf.Sin(phase3); else c = 0f;


            float x = a + b + c;
            int i = 0;
            while (i < channels)
            {
                data[n * channels + i] += x;
                i++;
            }

            phase1 += phase1base * phasemult1;
            if (phase1 > Mathf.PI * 2f) phase1 = 0f;

            phase2 += phase2base * phasemult2;
            if (phase2 > Mathf.PI * 2f) phase2 = 0f;

            phase3 += phase3base * phasemult3;
            if (phase3 > Mathf.PI * 2f) phase3 = 0f;

            n++;
        }
    }


    public static float CubicEaseOut(float t, float b, float c, float d)
    {
        if (t < d)
        {
            return c * ((t = t / d - 1) * t * t + 1) + b;
        }
        else
        {
            return b + c;
        }
    }

    public static float CubicEaseIn(float t, float b, float c, float d)
    {
        if (t < d)
        {
            return c * (t /= d) * t * t + b;
        }
        else
        {
            return b + c;
        }
    }
}

Argos Generative Music 1:

    int count1 = 0;
    int count2 = 0;
    int count3 = 0;

    float phasemult1 = 4.0f;
    float phasemult2 = 6.0f;
    float phasemult3 = 2.0f;

    float phase1 = 0.01f;
    float phase2 = 0.01f;
    float phase3 = 0.01f;

    bool switch1 = true;
    bool switch2 = true;
    bool switch3 = true;

    void OnAudioFilterRead(float[] data, int channels)
    {
        if (count1++ > 10* multInterval)
        {
            amp = 1.0f;
            if (switch1)
            {
                if (++phasemult1 > 4)
                {
                    switch1 = false;
                }
            }
            else
            {

                if (--phasemult1 < 3)
                {
                    switch1 = true;
                }
            }
            count1 = 0;
        }

        if (count2++ > 5*multInterval)
        {
            if (switch2)
            {
                if (++phasemult2 > 7)
                {
                    switch2 = false;
                }
            }
            else
            {
                if (--phasemult2 < 6)
                {
                    switch2 = true;
                }
            }
            count2 = 0;
        }

        if (count3++ > 30*multInterval)
        {
            if (switch3)
            {
                if (++phasemult3 > 3)
                {
                    switch3 = false;
                }
            }
            else
            {
                if (--phasemult3 < 2)
                {
                    switch3 = true;
                }
            }
            count3 = 0;
        }


        if (!running)
            return;

        double samplesPerTick = sampleRate * 60.0F / bpm * 4.0F / signatureLo;
        double sample = AudioSettings.dspTime * sampleRate;
        int dataLen = data.Length / channels;
        int n = 0;
        while (n < dataLen)
        {
            float x = gain * amp * Mathf.Sin(phase1) + gain * amp * Mathf.Sin(phase2) + gain * amp * Mathf.Sin(phase3);
            int i = 0;
            while (i < channels)
            {
                data[n * channels + i] += x;
                i++;
            }

            phase1 += 0.01F * phasemult1;
            if (phase1 > Mathf.PI * 2f) phase1 = 0f;

            phase2 += 0.01F * phasemult2;
            if (phase2 > Mathf.PI * 2f) phase2 = 0f;

            phase3 += 0.01F * phasemult3;
            if (phase3 > Mathf.PI * 2f) phase3 = 0f;

            n++;
        }
    }
}

 

tone_envelope

 

Unity Manual Metronome:

[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour
{
	public double bpm = 140.0F;
	public float gain = 0.5F;
	public int signatureHi = 4;
	public int signatureLo = 4;
	private double nextTick = 0.0F;
	private float amp = 0.0F;
	private float phase = 0.0F;
	private double sampleRate = 0.0F;
	private int accent;
	private bool running = false;
	void Start()
	{
		accent = signatureHi;
		double startTick = AudioSettings.dspTime;
		sampleRate = AudioSettings.outputSampleRate;
		nextTick = startTick * sampleRate;
		running = true;
	}
	void OnAudioFilterRead(float[] data, int channels)
	{
		if (!running)
			return;

		double samplesPerTick = sampleRate * 60.0F / bpm * 4.0F / signatureLo;
		double sample = AudioSettings.dspTime * sampleRate;
		int dataLen = data.Length / channels;
		int n = 0;
		while (n < dataLen)
		{
			float x = gain * amp * Mathf.Sin(phase);
			int i = 0;
			while (i < channels)
			{
				data[n * channels + i] += x;
				i++;
			}
			while (sample + n >= nextTick)
			{
				nextTick += samplesPerTick;
				amp = 1.0F;
				if (++accent > signatureHi)
				{
					accent = 1;
					amp *= 2.0F;
				}
				Debug.Log("Tick: " + accent + "/" + signatureHi);
			}
			phase += amp * 0.3F;
			amp *= 0.993F;
			n++;
		}
	}
}