Published
Edited
Jun 3, 2021
1 fork
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
yin = require('yinjs')
Insert cell
function successCallback(stream) {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext();

var analyser = audioContext.createAnalyser();
analyser.fftSize = Math.pow(2, 13);

var sampleRate = audioContext.sampleRate;
var data = new Float32Array(analyser.fftSize);

function step() {
requestAnimationFrame(step);
analyser.getFloatTimeDomainData(data);
var frequency = window.yin(data, sampleRate, 0.1);
// console.log(frequency);
frequencies.push(frequency);
if (frequencies.length > maxValuesToKeep) {
frequencies.shift();
}
// Store offsets from note
const midi = freqToApproxMidiNr(lastFreq);
offsets.push(Math.round((midi - Math.round(midi)) * 100));
if (offsets.length > 20) {
offsets.shift();
}
}

var mediaStreamSource = audioContext.createMediaStreamSource(stream);
mediaStreamSource.connect(analyser);

requestAnimationFrame(step);
}
Insert cell
Insert cell
// navigator.mediaDevices.getUserMedia({ audio: true}, successCallback, errorCallback)
navigator.getUserMedia({ audio: true}, successCallback, errorCallback)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more