How do I fix my music note system?

I am making a music note block for my building game (Like Minecraft note blocks.) But I am having trouble figuring out how to make it go from C2 to B7 without the notes sounding weird.

Right now I have the base sample as note A4. When the pitch is further away from the sample, it sounds weird.

function module:playNote(note, hold)
	if not note or typeof(note) ~= "number" then return end
	
	local newSound = script.NoteSample:Clone()
	
	local frequency = 440 * (2 ^ ((note - 69) / 12))
	newSound.Pitch = frequency / 440
	
	newSound.Parent = workspace
	newSound:Play()
	
	game.Debris:AddItem(newSound, typeof(hold) == "number" and hold or 0.4)
end

I make a loop playing the notes 0 to 100 and it sounds really quiet, barely able to hear, when its low. And when its a higher number, the pitch is right but it sounds weird still.

Any help is appreciated since I barely know how to go about stuff like this :blowfish:

Is it possible to use more then one base sample? The problem might be stretching your current base (A4) over the entire “pitch” (Cant find a better word). It might help to use 2,3,4 or even 5 samples, then move from there. (Increases accuracy to original note)

1 Like

There are two reasons that I can think of:

Every instrument has fundamentally different overtones for every single note (or “timbre”). Every note essentially has a unique set of overtones. For example, A2 may have the fundamental frequency be 50% of the sound, but A4 may only have it at 30%. When you pitch up or down a single A4 sample, you’re left playing something like G6 with an A4-like sound.

The algorithm for pitch shifting while keeping the same speed also changes the timbre, making lower pitches sound “muddy” and higher pitches sound “airy”.

The only way to solve this is to have more samples. 1 sample per octave is okay, 2-3 for “good” quality, and up to all 12 if you really want to have no issues with timbre.

1 Like

I should note that it’s only 71 half-steps from C2 to B7, so assuming you want C2 to be at 0,
then your loop should be from 0 to 71.
And then your offset for A4 would be 33, not 69.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.