I am trying to recreate the entire range of the guitar using only 6 sound clips.
I thought there was a way to set the audio clip frequency, but audio.frequency returns only the audio frequency based on the compression format, not the actual tone.
I know that I can read GetSpectrumData, but this solution is rather complicated and will require some analysis of the Fourier transform or something like that.
Having influenced the pitch, it's easy to change the tone so I can go up and down, but there is a way to figure out which steps to use.
void Update () { CheckAudio(KeyCode.Q, 1.0f); CheckAudio(KeyCode.W, 1.1f); CheckAudio(KeyCode.E, 1.2f); CheckAudio(KeyCode.R, 1.3f); CheckAudio(KeyCode.T, 1.4f); } void CheckAudio(KeyCode key, float pitch) { if (Input.GetKeyDown (key)) { audio.pitch = pitch; audio.Play (); } }
I hear that doesnβt sound like that.
Knowing the initial tone of E4 329.63Hz in steps of 1, is there any equation that affects the pitch, would I get the following key F4 349.23Hz (or close enough)?
You should also consider that Unity AudioSource limits the pitch within the -3/3 range (which, in my opinion, is more than necessary).
EDIT: adding some personal research. Step 1 seems to be the starting note, and tuning to 2 gives the same key an octave higher.
Since the chromatic scale (all black and white notes on the piano) is 12 keys, I believe that using 1/12 for each step should do this.
It sounds close, but I fell, it's not quite right. Here is the new code:
[SerializeField] private AudioSource audio; float step = 1f/12f; KeyCode[]keys = new KeyCode[]{ KeyCode.Q, KeyCode.W,KeyCode.E,KeyCode.R,KeyCode.T, KeyCode.Y, KeyCode.U, KeyCode.I, KeyCode.O, KeyCode.P, KeyCode.A, KeyCode.S, KeyCode.D }; void Update () { float f = 0.0f; foreach (KeyCode key in keys) { CheckAudio(key, f); f += 1f; } } void CheckAudio(KeyCode key, float pitch) { if (Input.GetKeyDown (key)) { audio.pitch = 1f + pitch * step; audio.Play (); } }