Comments (2)
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.
Yeah, thanks a lot for a quick answer!
from pitchdetect.
Related Issues (20)
- Stream Generation Failed? HOT 1
- Stream generation failed HOT 3
- Stream generation failed? HOT 1
- error: failed to set the 'buffer' property
- "Stream Generation Failed Error" HOT 2
- Mic won't work first time HOT 1
- Thank you, this is awesome! HOT 2
- Not working on mobile HOT 2
- "Stream Generation Failed Error" on Safari
- How to get this to work with lower notes?
- Detecting the pitch of an AudioBuffer? Without playing the audio back. HOT 1
- get a fixed number of detected pitches for the same time interval?
- how to get value every changes frequency?
- getUserMedia threw exception :TypeError: navigator.getUserMedia is not a function HOT 2
- Sound Engineer Feedback
- Not working in live demo HOT 3
- getShadowHosts Error in Chrome
- Doubt about this code in function **autoCorrelate**:
- Live demo does not work in contemporary Chrome and Firefox
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pitchdetect.