First off I'm trying to implement this chord detection algorithm: http://www.music.mcgill.ca/~jason/mumt621/papers5/fujishima_1999.pdf
I originally implemented the algorithm to use my microphone, but it didn't work. As a test I created three oscillators to make a c chord, but the algorithm still does not work. I think I should only be seeing a higher number for C,E and G but I see numbers for all the notes. Is there a problem with my implementation of the algorithm? or is it my N, fref, or fs value?
Here is a snippet of code with the important parts:
// Set audio Context
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var mediaStreamSource = null;
var analyser = null;
var N = 4096;//8192;//2048; // Samples of Sound
var bufferLen = null;
var buffer = null;
var PCP = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // Pitch Class Profiles
var fref = 261.63; // Reference frequency middle C (C4)
// fref = 65.4; // Reference frequency C2
// fref = 440.0; // Reference frequency A4
var audioContext = new AudioContext();
var fs = audioContext.sampleRate; // Retrieve sampling rate. Usually 48KHz
var useMicrophone = false;
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
// Create an analyzer node to process the audio
analyser = audioContext.createAnalyser();
analyser.fftSize = N;
bufferLen = N / 2;
//bufferLen = analyser.frequencyBinCount;
console.log( 'bufferLen = ' + bufferLen );
buffer = new Float32Array(bufferLen);
if ( useMicrophone ) {
// Create an AudioNode from the stream.
mediaStreamSource = audioContext.createMediaStreamSource(stream);
// Connect it to the destination.
mediaStreamSource.connect(analyser);
}
else {
// As a test, feed a C chord directly into the analyzer
// C4, E4, G4
var freqs = [261.63, 329.63, 392.00];
for( var i=0; i < freqs.length; i++) {
var o = audioContext.createOscillator();
var g = audioContext.createGain(); //Create Gain Node
o.frequency.value = freqs[i];
o.connect(g);
g.gain.value = 0.25;
g.connect( audioContext.destination );
g.connect( analyser );
o.start(0);
//setTimeout(function(s) {s.stop(0)}, 1000, o);
}
}
// Call algorithm every 50 ms
setInterval(function() {
pcpAlg();
}, 50);
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
});
function pcpAlg() {
analyser.getFloatTimeDomainData(buffer);
//analyser.getFloatFrequencyData( buffer );
// Reset PCP
PCP = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// M(0)=-1 so we don't have to start at 0
for (var l = 1; l < bufferLen; l++) { // l = 0,1,...,[(N/2) - 1]
// Calculate M(l)
var ML = Math.round(12 * Math.log2( (fs * (l / N) ) / fref ) ) % 12; //
//console.log( ML );
if (ML >= 0 && ML <= 11) {
PCP[ML] += Math.pow( Math.abs( buffer[l] ), 2 );
}
}
// Display Data on UI and also try to determine if the sound is a C or F chord
displayAndCategorize();
}
Here is my full codepen if you want to try running it yourself. Warning I have useMicrophone set to false so it will be making a c chord sound: https://codepen.io/mapmaps/pen/ONQPpw