This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findFundamentalFreq = function(buffer, sampleRate) { | |
// We use Autocorrelation to find the fundamental frequency. | |
// In order to correlate the signal with itself (hence the name of the algorithm), we will check two points 'k' frames away. | |
// The autocorrelation index will be the average of these products. At the same time, we normalize the values. | |
// Source: http://www.phy.mty.edu/~suits/autocorrelation.html | |
// Assuming the sample rate is 48000Hz, a 'k' equal to 1000 would correspond to a 48Hz signal (48000/1000 = 48), | |
// while a 'k' equal to 8 would correspond to a 6000Hz one, which is enough to cover most (if not all) | |
// the notes we have in the notes.json file. | |
var n = 1024, bestR = 0, bestK = -1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findCentsOffPitch = function(freq, refFreq) { | |
// We need to find how far freq is from baseFreq in cents | |
var log2 = 0.6931471805599453; // Math.log(2) | |
var multiplicativeFactor = freq / refFreq; | |
// We use Math.floor to get the integer part and ignore decimals | |
var cents = Math.floor(1200 * (Math.log(multiplicativeFactor) / log2)); | |
return cents; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 'notes' is an array of objects like { note: 'A4', frequency: 440 }. | |
// See initialization in the source code of the demo | |
var findClosestNote = function(freq, notes) { | |
// Use binary search to find the closest note | |
var low = -1, high = notes.length; | |
while (high - low > 1) { | |
var pivot = Math.round((low + high) / 2); | |
if (notes[pivot].frequency <= freq) { | |
low = pivot; | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isGetUserMediaSupported = function(){ | |
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; | |
if((navigator.mediaDevices && navigator.mediaDevices.getUserMedia) || navigator.getUserMedia){ | |
return true; | |
} | |
return false; | |
}; | |
if(isGetUserMediaSupported()){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var analyserAudioNode, sourceAudioNode, micStream; | |
var streamReceived = function(stream) { | |
micStream = stream; | |
analyserAudioNode = audioContext.createAnalyser(); | |
analyserAudioNode.fftSize = 2048; | |
sourceAudioNode = audioContext.createMediaStreamSource(micStream); | |
sourceAudioNode.connect(analyserAudioNode); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// micStream is the MediaStream object we get from the Media Stream API | |
var sourceAudioNode = audioContext.createMediaStreamSource(micStream); | |
sourceAudioNode.connect(analyserAudioNode); // See initialization in the AnalyserNode section of the demo. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"432": [ | |
{ | |
"note":"C0", | |
"frequency":16.05 | |
}, | |
{ | |
"note":"C#0", | |
"frequency":17.01 | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isAudioContextSupported = function () { | |
// This feature is still prefixed in Safari | |
window.AudioContext = window.AudioContext || window.webkitAudioContext; | |
if(window.AudioContext){ | |
return true; | |
} | |
else { | |
return false; | |
} | |
}; |