Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Audio Modifying sine, square and triangle waves?

Discussion in 'Audio & Video' started by FMmutingMode, Feb 28, 2021.

  1. FMmutingMode

    FMmutingMode

    Joined:
    Feb 4, 2019
    Posts:
    5
    Hey guys,
    I'm currently working on a synth component to my drum machine for Android. I have basic Sine, Square, and Triangle waves set. I need help putting in variables that will modify the sound using sliders. Can anyone help with this?
     
  2. FMmutingMode

    FMmutingMode

    Joined:
    Feb 4, 2019
    Posts:
    5
    Here's the code:


    Code (CSharp):
    1.     void OnAudioFilterRead(float[] data, int channels) {
    2.         increment = (frequency * 2.0 * Mathf.PI / sampling_frequency);
    3.        
    4.         if (sine == true) {
    5.             for (int i = 0; i < data.Length; i += channels) {
    6.                 phase += increment;
    7.                
    8.                 //sine
    9.                 data[i] = (float)(gain * (Mathf.Sin((float)phase * gain2)));
    10.  
    11.                 if (channels == 2) {
    12.                     data[i + 1] = data[i];
    13.                 }
    14.  
    15.                 if (phase > (Mathf.PI * 2)) {
    16.                     phase = 0.0;
    17.                 }
    18.             }
    19.         }
    20.  
    21.         if (square == true) {
    22.             for (int i = 0; i < data.Length; i += channels) {
    23.                 phase += increment;
    24.  
    25.                 //sqaure
    26.                 if (gain * Mathf.Sin((float)phase) >= 0 * gain) {
    27.                     data[i] = (float)gain * 0.6f;
    28.                 }
    29.                 else {
    30.                     data[i] = (-(float)gain) * 0.6f;
    31.                 }
    32.  
    33.                 if (channels == 2) {
    34.                     data[i + 1] = data[i];
    35.                 }
    36.  
    37.                 if (phase > (Mathf.PI * 2)) {
    38.                     phase = 0.0;
    39.                 }
    40.             }          
    41.         }  
    42.  
    43.         if (triangle == true) {
    44.             for (int i = 0; i < data.Length; i += channels) {
    45.                 phase += increment;
    46.  
    47.                 //triangle
    48.                 data[i] = (float)(gain * (phase < gain2 ? Mathf.Lerp(-1, 1, Mathf.InverseLerp(0, gain2, (float)phase)) :
    49.                             Mathf.Lerp(1, -1, Mathf.InverseLerp(gain2, 1, (float)phase))));
    50.  
    51.                 if (channels == 2) {
    52.                     data[i + 1] = data[i];
    53.                 }
    54.  
    55.                 if (phase > (Mathf.PI * 2)) {
    56.                     phase = 0.0;
    57.                 }
    58.             }          
    59.         }        
    60.     }
     
    MasterNyborg likes this.