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?etu1yk
Here’s the code:
void OnAudioFilterRead(float[] data, int channels) {
increment = (frequency * 2.0 * Mathf.PI / sampling_frequency);
if (sine == true) {
for (int i = 0; i < data.Length; i += channels) {
phase += increment;
//sine
data[i] = (float)(gain * (Mathf.Sin((float)phase * gain2)));
if (channels == 2) {
data[i + 1] = data[i];
}
if (phase > (Mathf.PI * 2)) {
phase = 0.0;
}
}
}
if (square == true) {
for (int i = 0; i < data.Length; i += channels) {
phase += increment;
//sqaure
if (gain * Mathf.Sin((float)phase) >= 0 * gain) {
data[i] = (float)gain * 0.6f;
}
else {
data[i] = (-(float)gain) * 0.6f;
}
if (channels == 2) {
data[i + 1] = data[i];
}
if (phase > (Mathf.PI * 2)) {
phase = 0.0;
}
}
}
if (triangle == true) {
for (int i = 0; i < data.Length; i += channels) {
phase += increment;
//triangle
data[i] = (float)(gain * (phase < gain2 ? Mathf.Lerp(-1, 1, Mathf.InverseLerp(0, gain2, (float)phase)) :
Mathf.Lerp(1, -1, Mathf.InverseLerp(gain2, 1, (float)phase))));
if (channels == 2) {
data[i + 1] = data[i];
}
if (phase > (Mathf.PI * 2)) {
phase = 0.0;
}
}
}
}
1 Like