Skip to content

Instantly share code, notes, and snippets.

@quimbs
quimbs / autocorrelation.js
Last active April 1, 2024 08:32
Sample implementation of Autocorrelation using Web Audio
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;
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;
};
@quimbs
quimbs / frequencyBinarySearch.js
Last active August 29, 2015 14:17
Binary search function to find the note in an array sorted by frequency that is closest to the one passed as a parameter
// '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 {
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()){
@quimbs
quimbs / analyserNode.js
Last active August 29, 2015 14:17
Sample of usage of MediaStream as an input to an AnalyserNode in WebAudio API
var analyserAudioNode, sourceAudioNode, micStream;
var streamReceived = function(stream) {
micStream = stream;
analyserAudioNode = audioContext.createAnalyser();
analyserAudioNode.fftSize = 2048;
sourceAudioNode = audioContext.createMediaStreamSource(micStream);
sourceAudioNode.connect(analyserAudioNode);
// 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.
@quimbs
quimbs / notes.json
Created March 13, 2015 20:50
Sample of usage of OscillatorNode in WebAudio API given a set of frequencies that represent different musical notes.
{
"432": [
{
"note":"C0",
"frequency":16.05
},
{
"note":"C#0",
"frequency":17.01
},
@quimbs
quimbs / audioContext.js
Last active December 8, 2020 14:04
Check if AudioContext is supported and instance it
var isAudioContextSupported = function () {
// This feature is still prefixed in Safari
window.AudioContext = window.AudioContext || window.webkitAudioContext;
if(window.AudioContext){
return true;
}
else {
return false;
}
};