@ddlab/tuner
A guitar tuner in a browser from @ddlab
For detailed explanation how it works see "The Tuning Algorithm" section.
To embed it in your javascript project read the following section.
Package Usage
npm i @ddlab/tuner
;
Initialize a tuner widget:
import {initTuner} from '@ddlab/tuner';
initTuner(document.body);
Other Examples
Analyzing static data:
import {getTuningInfo} from '@ddlab/tuner';
const deltaFreq = sampleRate / fftSize;
const {noteStr, cents, isInTune} = getTuningInfo(freqData, deltaFreq);
Continuously analyzing mic stream
import {getTuningInfo, initAudio} from '@ddlab/tuner';
const {getFreqData, deltaFreq} = await initAudio(); // this must go after some user event
const loop = () => {
const {noteStr, cents, isInTune} = getTuningInfo(getFreqData(), deltaFreq);
// ...
requestAnimationFrame(loop);
};
Warning: initialization (
initAudio
) has to occur after some user event due to browsers' autoplay policies. For example:addEventListener('keypress', () => initTuner(document.body));
The Tuning Algorithm
To find out how many cents a played note is out-of-tune, the following steps has to be made:
- Get a frequency spectrum
- Find fundamental peak freq. (in 50-400Hz range) in a spectrum
- Find harmonic peaks frequencies in a spectrum
- Calculate the harmonic average distance to nearest in-tune note
- Convert the diff to cents and represent it
References
- http://amath.colorado.edu/pub/matlab/music/MathMusic.pdf
- http://www.tedknowlton.com/resume/CCPPT.htm
- https://jakevdp.github.io/blog/2013/08/28/understanding-the-fft/
- ./docs/misc/GuitarFretboardFrequencies.md
License
© Dominykas Dukštas (@ddlab) 2019