|
11 | 11 |
|
12 | 12 | import * as tf from '@tensorflow/tfjs';
|
13 | 13 |
|
14 |
| -class Crepe { |
15 |
| - constructor(audioContext, stream) { |
| 14 | +class PitchDetection { |
| 15 | + constructor(modelName, audioContext, stream) { |
| 16 | + this.modelName = modelName; |
16 | 17 | this.audioContext = audioContext;
|
17 | 18 | this.stream = stream;
|
18 | 19 | this.loadModel();
|
@@ -76,7 +77,7 @@ class Crepe {
|
76 | 77 | processMicrophoneBuffer(event) {
|
77 | 78 | this.results = {};
|
78 | 79 | const centMapping = tf.add(tf.linspace(0, 7180, 360), tf.tensor(1997.3794084376191));
|
79 |
| - Crepe.resample(event.inputBuffer, (resampled) => { |
| 80 | + PitchDetection.resample(event.inputBuffer, (resampled) => { |
80 | 81 | tf.tidy(() => {
|
81 | 82 | this.running = true;
|
82 | 83 | const frame = tf.tensor(resampled.slice(0, 1024));
|
@@ -111,6 +112,18 @@ class Crepe {
|
111 | 112 | }
|
112 | 113 | }
|
113 | 114 |
|
114 |
| -const crepe = (context, stream) => new Crepe(context, stream); |
| 115 | +const pitchDetection = (modelName, context, stream) => { |
| 116 | + let model; |
| 117 | + if (typeof modelName === 'string') { |
| 118 | + model = modelName.toLowerCase(); |
| 119 | + } else { |
| 120 | + throw new Error('Please specify a model to use. E.g: "Crepe"'); |
| 121 | + } |
| 122 | + |
| 123 | + if (model === 'crepe') { |
| 124 | + return new PitchDetection(model, context, stream); |
| 125 | + } |
| 126 | + throw new Error(`${model} is not a valid model to use in pitchDetection()`); |
| 127 | +}; |
115 | 128 |
|
116 |
| -export default crepe; |
| 129 | +export default pitchDetection; |
0 commit comments