Skip to content

Commit f35dcad

Browse files
authored
Merge pull request #132 from ml5js/crepe-linting
Linting errors for Crepe class
2 parents 2f54953 + 7d2c4da commit f35dcad

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/Crepe/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ class Crepe {
3030

3131
// perform resampling the audio to 16000 Hz, on which the model is trained.
3232
// setting a sample rate in AudioContext is not supported by most browsers at the moment.
33-
resample(audioBuffer, onComplete) {
33+
static resample(audioBuffer, onComplete) {
3434
const interpolate = (audioBuffer.sampleRate % 16000 !== 0);
3535
const multiplier = audioBuffer.sampleRate / 16000;
3636
const original = audioBuffer.getChannelData(0);
3737
const subsamples = new Float32Array(1024);
38-
for (let i = 0; i < 1024; i++) {
38+
for (let i = 0; i < 1024; i += 1) {
3939
if (!interpolate) {
4040
subsamples[i] = original[i * multiplier];
4141
} else {
4242
// simplistic, linear resampling
4343
const left = Math.floor(i * multiplier);
4444
const right = left + 1;
45-
const p = i * multiplier - left;
45+
const p = (i * multiplier) - left;
4646
subsamples[i] = (((1 - p) * original[left]) + (p * original[right]));
4747
}
4848
}
@@ -53,14 +53,14 @@ class Crepe {
5353
this.results = {};
5454
// bin number -> cent value mapping
5555
const centMapping = tf.add(tf.linspace(0, 7180, 360), tf.tensor(1997.3794084376191));
56-
this.resample(event.inputBuffer, (resampled) => {
56+
Crepe.resample(event.inputBuffer, (resampled) => {
5757
tf.tidy(() => {
5858
this.running = true;
5959

6060
// run the prediction on the model
6161
const frame = tf.tensor(resampled.slice(0, 1024));
6262
const zeromean = tf.sub(frame, tf.mean(frame));
63-
const framestd = tf.tensor(tf.norm(zeromean).dataSync()/Math.sqrt(1024));
63+
const framestd = tf.tensor(tf.norm(zeromean).dataSync() / Math.sqrt(1024));
6464
const normalized = tf.div(zeromean, framestd);
6565
const input = normalized.reshape([1, 1024]);
6666
const activation = this.model.predict([input]).reshape([360]);
@@ -81,12 +81,12 @@ class Crepe {
8181
const productSum = products.dataSync().reduce((a, b) => a + b, 0);
8282
const weightSum = weights.dataSync().reduce((a, b) => a + b, 0);
8383
const predictedCent = productSum / weightSum;
84-
const predictedHz = 10 * Math.pow(2, predictedCent / 1200.0);
84+
const predictedHz = 10 * ((predictedCent / 1200.0) ** 2);
8585

8686
// update
87-
let result = (confidence > 0.5) ? predictedHz.toFixed(3) + ' Hz' : 'no voice';
88-
const strlen = result.length;
89-
for (let i = 0; i < 11 - strlen; i++) result = result;
87+
const result = (confidence > 0.5) ? `${predictedHz.toFixed(3)} + Hz` : 'no voice';
88+
// const strlen = result.length;
89+
// for (let i = 0; i < 11 - strlen; i += 1) result = result;
9090
this.results.result = result;
9191
});
9292
});
@@ -98,16 +98,17 @@ class Crepe {
9898

9999
processStream(stream) {
100100
console.log('Setting up AudioContext ...');
101-
console.log('Audio context sample rate = ' + this.audioContext.sampleRate);
101+
console.log(`Audio context sample rate = + ${this.audioContext.sampleRate}`);
102102
const mic = this.audioContext.createMediaStreamSource(stream);
103103

104104
// We need the buffer size that is a power of two
105105
// and is longer than 1024 samples when resampled to 16000 Hz.
106106
// In most platforms where the sample rate is 44.1 kHz or 48 kHz,
107107
// this will be 4096, giving 10-12 updates/sec.
108-
const minBufferSize = this.audioContext.sampleRate / 16000 * 1024;
109-
for (var bufferSize = 4; bufferSize < minBufferSize; bufferSize *= 2);
110-
console.log('Buffer size = ' + bufferSize);
108+
const minBufferSize = (this.audioContext.sampleRate / 16000) * 1024;
109+
let bufferSize = 4;
110+
while (bufferSize < minBufferSize) bufferSize *= 2;
111+
console.log(`Buffer size = ${bufferSize}`);
111112
const scriptNode = this.audioContext.createScriptProcessor(bufferSize, 1, 1);
112113
scriptNode.onaudioprocess = this.processMicrophoneBuffer.bind(this);
113114
// It seems necessary to connect the stream to a sink

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// https://opensource.org/licenses/MIT
55

66
import * as tf from '@tensorflow/tfjs';
7+
import Crepe from './Crepe/';
78
import ImageClassifier from './ImageClassifier/';
89
import Word2Vec from './Word2vec/';
910
import YOLO from './YOLO';
@@ -14,6 +15,7 @@ import LSTMGenerator from './LSTM/';
1415

1516
module.exports = {
1617
ImageClassifier,
18+
Crepe,
1719
YOLO,
1820
Word2Vec,
1921
StyleTransfer,

0 commit comments

Comments
 (0)