var samplingRate = 44100;
function canvasDrawLine(oPosX, oPosY, fPosX, fPosY) { var ctx = getCanvas().getContext('2d'); ctx.beginPath(); ctx.moveTo(oPosX, oPosY); ctx.lineTo(fPosX, fPosY); ctx.stroke(); }
function canvasInitialize(width, height) { var c = getCanvas(); // Set canvas parameters c.width = width; c.height = height;
// Outline var ctx = c.getContext('2d'); ctx.strokeStyle='#000000'; ctx.clearRect(0,0,width,height); ctx.strokeRect(0,0,width-1,height);
}
function plotSpectrum(array){ //calculate the number of freq bins needed to get to 8000 khz var eachbin = samplingRate / 1024; var nbins = 8000/eachbin; var scale = 374/nbins;
canvasInitialize(374,260);
var ctx = getCanvas().getContext('2d'); ctx.fillStyle='#00ff00'; for (var i = 0; i < nbins; i++) { ctx.fillRect(i * scale, 259, scale, -array[i]); } //draw the lines at 300 hz and 3 KHz ctx.strokeStyle='#ff0000'; var pos3000Hz = (3000 * 1024)/samplingRate; var pos300Hz = (300 * 1024)/samplingRate; canvasDrawLine(pos300Hz * scale, 1, pos300Hz * scale, 259); canvasDrawLine(pos3000Hz * scale, 1, pos3000Hz * scale, 259); } function onSuccess(stream) { var context = new AudioContext(); var mediaStreamSource = context.createMediaStreamSource(stream); var analyser = context.createAnalyser(); analyser.smoothingTimeConstant = 0.3; samplingRate = analyser.context.sampleRate; document.getElementById("sampling_rate").innerHTML = analyser.context.sampleRate + "Hz"; analyser.fftSize = 1024; var javascriptNode = context.createScriptProcessor(2048, 1, 1); javascriptNode.onaudioprocess = function(e) { var array = new Uint8Array(analyser.frequencyBinCount); samplingRate = analyser.context.sampleRate; analyser.getByteFrequencyData(array); plotSpectrum(array); }; //stream -> mediaSource -> analyser -> javascriptNode -> destination mediaStreamSource.connect(analyser); analyser.connect(javascriptNode); javascriptNode.connect(context.destination); }
function onError(err) { alert('Error: You must allow access to the microphone.\n('+err+')'); console.log("error: "+err); }
function log(logVal) {
getLog().innerHTML = logVal + '\n
';
}
function getCanvas() { return document.getElementById('mycanvas'); }
function getLog() { return document.getElementById('mylog'); }
/* this code rewritten by Reed, thanks! */ function documentReady() { console.log("documentReady called"); if('getUserMedia' in navigator) { console.log("found navigator.getUserMedia"); navigator.getUserMedia({video: false, audio: true}, onSuccess, onError); }else if('webkitGetUserMedia' in navigator) { console.log("found navigator.webkitGetUserMedia"); navigator.webkitGetUserMedia({video: false, audio: true}, onSuccess, onError); } else if('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices){ console.log("found navigator.mediaDevices.getUserMedia"); navigator.mediaDevices.getUserMedia({video: false, audio: true}).then(onSuccess).catch(onError); } else { console.log("No suitable input found"); } } documentReady();
- Attach your hands free to your phone or computer
- Keep the mic an inch away from the radio’s speaker.
- In the settings, choose “Setup BFO”. Tune the knob until the noise graph is centered apporximately between the two red lines of 300 Hz and 3 KHz
- Press the encoder button again to save the settings
Sampling Rate: