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

Question How Does Pitch Relate to Frequency?

Discussion in 'Audio & Video' started by John-B, Aug 20, 2023.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    If, for example, I have a recording of a middle C, which is 262 Hz, and I change the pitch from 1 to 2, what happens to the frequency of the note? Does the frequency double to 524 Hz, that is, go up an octave? If I divide the pitch in half, does it go down an octave? Or is there some other relationship? I can hear the difference in pitch, but would like to know exactly how the frequency is changing.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    Yes and yes.
     
    John-B likes this.
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    To be specific, it's just sampling at half or double the rate, duplicating or skipping samples as necessary to fill twice the time or half the time. It cannot change the pitch without changing the overall amount of time of the audioclip by the same factor. Audio processors like Audacity can adjust the pitch without affecting the total play time, but it's a heavier compute task so Unity skips it in favor of what can be done in realtime.

    For nice moderate frequencies, that ends up being a good approximation of a one octave shift. Clean music should be fine. For noises, you might be able to make out some artifacts. Think of it like scaling an image up or down, the quality of some images can visibly suffer if you choose a bad sampling method.
     
    John-B and AcidArrow like this.
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    I find that for clean waveforms (a simple sine / triangle etc) it's more than acceptable to pitch it up and down a lot, you are not losing much fidelity and you're not introducing a lot of noise (especially if you have like some reverb on top and it is mixed with other normally sampled stuff, they sound fine in a mix).

    The problem is with natural sounds, say a piano, but even then I wouldn't say the primary issue is the loss of "fidelity" it's that the the characteristics of the sound change (the decay and like the additional harmonics that evolve as the note is held down).
     
    John-B and halley like this.
  5. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    Speaking of Audacity, I compared Audacity's Change Pitch effect and changing the pitch value in Unity. In Audacity, I set the frequency percent change to 100%. In Unity, I changed the pitch from 1 to 2. The clip is a musical bell ringing, middle C (stereo, 44,100 Hz). The result is much better in Unity. In Unity, it sounds like a higher-pitched bell ringing, no distortion, no change in quality. In Audacity, it sounds very tinny, a little scratchy at first, and a lot of vibrato – really bad. Unity wins hands down (in this particular case).

    I'm not sure why Unity would have to do the calculations in real time. Don't those calculations only need to be done once when the pitch value changes?
     
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,300
    Unity doesn't do anything when you change the pitch, other than play the clip faster or slower, which affects the length of the clip. The drawbacks are very obvious if you try to significantly change the pitch of dialogue, because now they are talking very quickly or very slowly just so you can get a higher pitched or lower pitched voice, which isn't what you want.

    Unity does have an audio effect that can resample audio to play it at a different pitch without affecting the length, and it too will sound tinny and bad if you drastically change the pitch of audio like you did in audacity. It's also expensive to do in real time. You could use that to repitch dialogue and maintain the length/speed, but it won't sound great.

    What you are talking about is making an instrument, though.
    For making instruments, the cheap method of changing the pitch of an audiosource may be used to save disk/ram space so you don't have to have every semitone of an instrument as its own audio clip. Obviously exporting every single semitone will sound better, but if you're trying to save space you might only export every 4th semitone for example, and repitch the 3 semitones above it while using the 4th semitone sample under the hood. That will sound much more convincing than having a single semitone clip repitched across an entire octave, although the latter would save you more space. Basically it's a balancing act between saving space and having it sound good.

    If you want it to sound as good as your DAW, you'd need to export every semitone you use, and you wouldn't have to repitch the audiosources to emulate missing semitones and the lengths wouldn't be affected either.
     
    Last edited: Aug 26, 2023
    halley likes this.
  7. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    Basically, yes. I have one octave's worth of recorded notes, and I'd like to have three, one octave above and one below what I have. Recording additional notes may not be an option, at least not right now, so I'm trying to figure out alternatives. I assume it's possible to generate these notes, but I don't know how.
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    It's probably a better idea to do the pitching up and down in a dedicated audio program that has more advanced and expensive algorithms and then just save the results and use that instead of doing pitch = 2f; in Unity.
     
  9. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    Such as?
     
  10. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    Audacity, Reaper, Soundforge, Wavelab, Audition
     
  11. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    I can't speak to the others, but Audacity is not such a dedicated audio program, as I explained in a previous post.