I’m using AudioClip.Create() to create procedural tones. Using the example provided it is functional and I’ve been able to implement my own tones, but I want to take it further and I’m not understanding what the callback functions are doing or why I should be using this function at all!
My first question is in regards to the ‘frequency’ argument within Create(). Why does it accept a frequency if you make the frequency yourself inside the OnAudioRead() callback function? What baffles me further is that even though I’m overriding the frequency (or so I thought) within the OnAudioRead() using a standard sine wave input for the audio data, the frequency argument seems to affect the pitch.
OnAudioRead() appears to be called 12 times when using AudioClip.Create() with the following parameters: (“Sine Tone”, 44100, 1, 44100, false, false, OnAudioRead, OnAudioSetPosition)
Is there any reason that it needs to be called 12 times? I think there’s something I don’t understand about the callbacks in general. The ‘position’ integer also makes little sense to me. What is this meant to be doing!?
Here’s the code for reference:
using UnityEngine;
using System.Collections;
public class ToneGenerator : MonoBehaviour {
private AudioClip[] tones;
private AudioSource audioSource;
private int position = 0;
private int sampleRate = 0;
private float frequency = 0;
void Start ()
{
//Audio properties
audioSource = gameObject.AddComponent<AudioSource>();
audio.volume = 0;
audio.loop = true;
GenerateTones();
}
void GenerateTones ()
{
tones = new AudioClip[4];
//Generate the tones
sampleRate = 44100;
frequency = 440f;
tones[0] = AudioClip.Create("Saw Tone", 44100, 1, 44100, false, false, OnAudioRead, OnAudioSetPosition);
frequency = 300f;
tones[1] = AudioClip.Create("Saw Tone", 44100, 2, 44100, false, false, OnAudioRead, OnAudioSetPosition);
frequency = 200f;
tones[2] = AudioClip.Create("Saw Tone", 44100, 2, 44100, false, false, OnAudioRead, OnAudioSetPosition);
frequency = 500f;
tones[3] = AudioClip.Create("Saw Tone", 44100, 2, 44100, false, false, OnAudioRead, OnAudioSetPosition);
}
void OnAudioRead(float[] data)
{
for(int i = 0; i < data.Length; i++)
{
switch (wave)
{
case waveType.Sine:
data _= Mathf.Sin(2 * Mathf.PI * frequency * position / sampleRate);_
-
break;*
-
case waveType.Sawtooth:*
data = (Mathf.PingPong(frequency * position / sampleRate, 0.5f));
* break;*
* case waveType.Square:*
data = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * frequency * position / sampleRate)) * 0.5f;
* break;*
* case waveType.Noise:*
data = Mathf.PerlinNoise(frequency * position / sampleRate, 0);
* break;*
* }*
position++;
}
}
void OnAudioSetPosition(int newPosition)
* {*
position = newPosition;
}
}
Thanks for any advice or resources!