Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Piano frequency #23

Closed
fonsp opened this issue Jan 17, 2024 · 3 comments
Closed

Piano frequency #23

fonsp opened this issue Jan 17, 2024 · 3 comments

Comments

@fonsp
Copy link

fonsp commented Jan 17, 2024

Hey! Here is a little function to get the piano frequency of a Pitch, using the table https://upload.wikimedia.org/wikipedia/commons/a/ad/Piano_key_frequencies.png

const frequency_octave_4 = Dict(
	C => 261.626,
	C♯ => 277.183,
	D => 293.665,
	D♯ => 311.127,
	E => 329.628,
	F => 349.228,
	F♯ => 369.994,
	G => 391.995,
	G♯ => 415.305,
	A => 440,
	A♯ => 466.164,
	B => 493.883,
)

function frequency(pitch::Pitch)
	freq_at_4 = frequency_octave_4[pitch.class]
	transposition = pitch.octave - 4

	freq_at_4 * (2.0 ^ transposition)
end
@dpsanders
Copy link
Collaborator

dpsanders commented Jan 17, 2024

Thanks! Here's my version:

julia> using MusicTheory, MusicTheory.PitchNames

julia> abstract type Temperament end

julia> struct EqualTemperament
           base_pitch::Pitch
           base_frequency::Float64
       end

julia> temperament = EqualTemperament(A[4], 440)
EqualTemperament(A₄, 440.0)

julia> function frequency(temperament::EqualTemperament, pitch::Pitch)
           semitone_distance = semitone(pitch) - semitone(temperament.base_pitch)
           return 2.0^(semitone_distance // 12)
       end
frequency (generic function with 1 method)

julia> frequency(temperament, C[4])
0.5946035575013605

julia> function frequency(temperament::EqualTemperament, pitch::Pitch)
           semitone_distance = semitone(pitch) - semitone(temperament.base_pitch)
           return 2.0^(semitone_distance // 12) * temperament.base_frequency
       end
frequency (generic function with 1 method)

julia> frequency(temperament, C[4])
261.6255653005986

@dpsanders
Copy link
Collaborator

I'll add a frequency.jl file.

@dpsanders
Copy link
Collaborator

Done in #24.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants