# How to produce the natural(diatonic) scale by changing pitch?

I’m trying to code a piano that uses one key audio by changing the pitch of the audio using PitchShiftSoundEffect.

I’ve made 12 key parts to start with, and have named them 1-12. I’ve made code that gives each key part audio a different pitch, however they are off-key using my current equation. With each progressive note it becomes more off-key basically. I need help figuring out how to give each key the right pitch to make the natural note scale.

The code is as follows:

``````local count = tonumber(note.Name)

note.pitch.Octave = basepitch + (math.log(2) * (count/8))
``````

Assume the first key is C or something. How do I make the next notes produce the sound of DEFG etc.?

1 Like

Unfortunately, sounds are more complicated than you think: Equal temperament - Wikipedia

The (middle) A is usually tuned to 440!

Have fun!

2 Likes

Okay

Music is complicated, which I know because I often research music theory, but for the purposes of this script all I need is the diatonic scale. Each note is double the frequency of the last on instruments such as the piano(under 12-tone equal temperament). However, natural notes are specifically what I’m trying to translate into to code.

Any help with this would be appreciated.

1 Like

There is a mathematical formula within that page that explains that the frequency difference between each note is around the twelfth root of 2. 12 notes after each equals to double the frequency which introduces its first harmonic(or the same note of the next octave). Also you need the frequency for an A note, not a C(it is rarely calibrated from the note C).

The scale is not a tuning. The tuning is the physical element while the scale is just the idea applied on top of it. Please understand the distinction between those two.

1 Like

Thank you

Well I’ve been using an audio of C5 from my piano because the piano in my game is actually a bridge the player walks on where each step produces a note. Basically it doesn’t need to be a 1:1 replica of a real life piano in terms of which key it’s calculated in (pitch shift).

When I used 1/12 as an increment it produced the sound of ALL the notes, black and white keys.

(math.log(2) * (count/12))

And using 1/6 sounds very nice as it’s merely 1/12 * 2, but I’m a bit stuck you see. This is not what I need. Is there a formula that produces natural keys only, no sharps or flats? I’ve heard the natural keys under 12-tone equal temperament are maximum entropy, so does this mean there is no consistent fraction to add to the octave to transform it into the next natural key in the sequence?

Also I’ve read the Wikipedia page but I didn’t find a formula for producing sounds of the white keys only.

If I’m asking too many questions I would 100% understand if you couldn’t assist me further.

It’s simple, just use the first, third, fifth, sixth, eighth, tenth and twelfth note. Use the same formula, and only specify the numbers you need, excluding the other tones.

With that said, concentrate on the table containing `{1, 3, 5, 6, 8, 10, 12}`

1 Like

Oh yeah…

Alright I’ll test this out soon

As tedious as it sounds, I think I would literally just manually write each pitch into an table.

Alternatively, if you know how semi-tones and octaves work you can also write a function that just auto-corrects the pitch to the nearest semi-tone but make a array/table of semi-tones that are NOT in the scale since it’s often not that much.

You know you can just check if it is equal to it by using `% 12` math operation? We don’t intend to create a whole table full of them. Nuh-uh!

`% 12` results in numbers of `0, 1, 2, 3 ... 11`. Just add 1 to shift it.

Division by 12 can get the octave number.

1 Like