Git Product home page Git Product logo

Comments (2)

cwilso avatar cwilso commented on August 23, 2024

Hi Olga!

For both of the first two functions, it's important to know that the note A4 is MIDI note number 69. This corresponds to 440Hz, also known as "A above middle C", or A440. That's where the "440" and the "69" in both of those functions comes from - the functions calculate pitch frequency and note numbers from a known point (note 69 = 440Hz).

It's also important that 12 is the number of semitones in an octave (12 notes, spread evenly on a log scale), and the ratio of frequency from one octave to the next is 2. (A above middle C (440Hz) is exactly twice the frequency as A below middle C (220Hz), for example.). Finally, for the final function, you'll need to know that there are 100 cents in a semitone - that is, each semitone is 100 cents higher or lower than the semitone below or above it.

So, the first function is calculating the closest MIDI note from a known pitch. It does this by calculating a note offset from the known pitch of A440 - by dividing the frequency parameter by 440, you'll get a ratio from A440. You want this as a logarithmic ratio - even temperament pitches are spaced logarithmically - and log(n)/log(2) gives you the base two log because of the mathematics rule that "logarithm of x base b = log(x)/log(b)"

So, "(Math.log( frequency / 440 )/Math.log(2))" gives you the offset, but it's in octaves. The "12 *" part means it gives you the offset in notes. (noteNum is actually the OFFSET from A440. If you ran A220 - one octave below A440 - through this algorithm, noteNum would be -12.)

The Math.round() gives you the CLOSEST note - because the pitch might be 445Hz, for example, which should return A440 (note 69), but you need to round off. The "+69" is to give an absolute MIDI note number answer, rather than an offset.

The second function is the same thing, but in reverse - for a given note, calculate how much higher or lower it is than A440 in terms of note numbers, what ratio it has to A440's pitch, and then multiple that by 440Hz to get that note number's pitch.

The final function just tells you how far off the pitch is from the note number, in cents - that's frequently used for tuning purposes.

A useful reference for all this is https://newt.phys.unsw.edu.au/jw/notes.html.

Does that help?

from pitchdetect.

olga-sinepalnikova avatar olga-sinepalnikova commented on August 23, 2024

Yeah, thanks a lot for a quick answer!

from pitchdetect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.