Frequency to Tone Ratio for AudioClip - Unity3D

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 (); } } 
+6
source share
1 answer

What you are trying to do will not work, just changing the pitch . By changing the pitch, you will encounter other problems , such as processing the sound too fast or take more time to complete, and the sound will not be good either.

The first solution is to create a plugin ( Synthesizer ) in C ++ that reads the Unity audio file and changes the frequency. It must also perform other actions to resolve performance issues. It is very difficult if you are not a sound engineer with excellent math skills. And to try it on a mobile device is a completely different story. OnAudioFilterRead is a function that you should use if you decide to go with this method.

The second and recommended solution is to make an audio file for each key of the guitar and then put them in an audioClip array. This solves any other problems. the downside is that you will have more files .

EDIT:

If you don't care about being perfect, you can use something lower from this pretty guy on the Internet .

 void playSound(){ float transpose = -4; float note = -1; if (Input.GetKeyDown("a")) note = 0; // C if (Input.GetKeyDown("s")) note = 2; // D if (Input.GetKeyDown("d")) note = 4; // E if (Input.GetKeyDown("f")) note = 5; // F if (Input.GetKeyDown("g")) note = 7; // G if (Input.GetKeyDown("h")) note = 9; // A if (Input.GetKeyDown("j")) note = 11; // B if (Input.GetKeyDown("k")) note = 12; // C if (Input.GetKeyDown("l")) note = 14; // D if (note>=0){ // if some key pressed... audio.pitch = Mathf.Pow(2, (note+transpose)/12.0); audio.Play(); } 

EDIT: for those of you who are wondering why the Mathf.Pow equation is used and working, read the following: https://en.wikipedia.org/wiki/Twelfth_root_of_two p>

+6
source

All Articles