|
1 | | -# bluesharp-pitch-detection |
| 1 | +# Bluesharp Pitch Detection |
2 | 2 |
|
3 | | -High-accuracy pitch detection algorithms for musical applications. |
| 3 | +[](https://www.npmjs.com/package/bluesharp-pitch-detection) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | + |
| 6 | +High-accuracy pitch detection algorithms for musical applications, with a focus on harmonica and other musical instruments. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- Multiple pitch detection algorithms: |
| 11 | + - **YIN**: Excellent for vocal and monophonic instrument detection |
| 12 | + - **MPM**: Modified Phase Matching for improved accuracy |
| 13 | + - **FFT**: Fast Fourier Transform-based detection |
| 14 | + - **Hybrid**: Combines multiple algorithms for optimal results |
| 15 | +- Chord detection capabilities |
| 16 | +- Note utilities for frequency-to-note conversion |
| 17 | +- Web Audio API integration with AudioWorklet support |
| 18 | +- Optimized for real-time applications |
4 | 19 |
|
5 | 20 | ## Installation |
6 | 21 |
|
7 | 22 | ```bash |
8 | 23 | npm install bluesharp-pitch-detection |
9 | 24 | ``` |
10 | 25 |
|
11 | | -## Usage |
| 26 | +## Basic Usage |
12 | 27 |
|
13 | | -```js |
| 28 | +```javascript |
14 | 29 | import { YINPitchDetector } from 'bluesharp-pitch-detection'; |
| 30 | + |
| 31 | +// Configure the detector (optional) |
| 32 | +YINPitchDetector.setMinFrequency(80); // Set minimum frequency in Hz |
| 33 | +YINPitchDetector.setMaxFrequency(1000); // Set maximum frequency in Hz |
| 34 | + |
| 35 | +// Detect pitch from audio data |
| 36 | +function processMicrophoneData(audioData, sampleRate) { |
| 37 | + const result = YINPitchDetector.detectPitch(audioData, sampleRate); |
| 38 | + |
| 39 | + if (result.pitch !== YINPitchDetector.NO_DETECTED_PITCH) { |
| 40 | + console.log(`Detected pitch: ${result.pitch.toFixed(2)} Hz`); |
| 41 | + console.log(`Confidence: ${(result.confidence * 100).toFixed(2)}%`); |
| 42 | + } else { |
| 43 | + console.log('No pitch detected'); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Module Structure |
| 49 | + |
| 50 | +The package provides several modules that can be imported separately: |
| 51 | + |
| 52 | +```javascript |
| 53 | +// Import everything |
| 54 | +import * as PitchDetection from 'bluesharp-pitch-detection'; |
| 55 | + |
| 56 | +// Import specific modules |
| 57 | +import { YINPitchDetector, MPMPitchDetector } from 'bluesharp-pitch-detection/pitch-detectors'; |
| 58 | +import { NoteUtils, NoteLookup } from 'bluesharp-pitch-detection/utils'; |
| 59 | +import { ChordDetector, ChordDetectionResult } from 'bluesharp-pitch-detection/chord-detection'; |
| 60 | +import { PitchProcessor } from 'bluesharp-pitch-detection/audio-worklet'; |
| 61 | +``` |
| 62 | + |
| 63 | +## API Documentation |
| 64 | + |
| 65 | +### Pitch Detectors |
| 66 | + |
| 67 | +#### YINPitchDetector |
| 68 | + |
| 69 | +Implementation of the YIN algorithm for pitch detection. |
| 70 | + |
| 71 | +```javascript |
| 72 | +import { YINPitchDetector } from 'bluesharp-pitch-detection/pitch-detectors'; |
| 73 | + |
| 74 | +// Configure frequency range |
| 75 | +YINPitchDetector.setMinFrequency(80); // Default: 80 Hz |
| 76 | +YINPitchDetector.setMaxFrequency(4835); // Default: 4835 Hz |
| 77 | + |
| 78 | +// Detect pitch |
| 79 | +const result = YINPitchDetector.detectPitch(audioData, sampleRate); |
| 80 | +// result = { pitch: 440.0, confidence: 0.95 } |
| 81 | +``` |
| 82 | + |
| 83 | +#### MPMPitchDetector |
| 84 | + |
| 85 | +Implementation of the McLeod Pitch Method (MPM) algorithm. |
| 86 | + |
| 87 | +```javascript |
| 88 | +import { MPMPitchDetector } from 'bluesharp-pitch-detection/pitch-detectors'; |
| 89 | + |
| 90 | +// Configure frequency range |
| 91 | +MPMPitchDetector.setMinFrequency(80); |
| 92 | +MPMPitchDetector.setMaxFrequency(4835); |
| 93 | + |
| 94 | +// Detect pitch |
| 95 | +const result = MPMPitchDetector.detectPitch(audioData, sampleRate); |
| 96 | +``` |
| 97 | + |
| 98 | +#### FFTDetector |
| 99 | + |
| 100 | +Fast Fourier Transform-based pitch detection. |
| 101 | + |
| 102 | +```javascript |
| 103 | +import { FFTDetector } from 'bluesharp-pitch-detection/pitch-detectors'; |
| 104 | + |
| 105 | +// Detect pitch |
| 106 | +const result = FFTDetector.detectPitch(audioData, sampleRate); |
| 107 | +``` |
| 108 | + |
| 109 | +#### HybridPitchDetector |
| 110 | + |
| 111 | +Combines multiple algorithms for optimal results. |
| 112 | + |
| 113 | +```javascript |
| 114 | +import { HybridPitchDetector } from 'bluesharp-pitch-detection/pitch-detectors'; |
| 115 | + |
| 116 | +// Detect pitch |
| 117 | +const result = HybridPitchDetector.detectPitch(audioData, sampleRate); |
| 118 | +``` |
| 119 | + |
| 120 | +### Utilities |
| 121 | + |
| 122 | +#### NoteUtils |
| 123 | + |
| 124 | +Utilities for working with musical notes and frequencies. |
| 125 | + |
| 126 | +```javascript |
| 127 | +import { NoteUtils } from 'bluesharp-pitch-detection/utils'; |
| 128 | + |
| 129 | +// Calculate cents between two frequencies |
| 130 | +const cents = NoteUtils.getCents(440, 466.16); // ~100 cents (a semitone) |
| 131 | + |
| 132 | +// Add cents to a frequency |
| 133 | +const newFreq = NoteUtils.addCentsToFrequency(50, 440); // 50 cents higher than A4 |
| 134 | + |
| 135 | +// Round a value to a specific decimal precision |
| 136 | +const roundedValue = NoteUtils.round(123.4567); // 123.456 |
| 137 | +``` |
| 138 | + |
| 139 | +#### NoteLookup |
| 140 | + |
| 141 | +Lookup table for note frequencies and names. |
| 142 | + |
| 143 | +```javascript |
| 144 | +import { NoteLookup } from 'bluesharp-pitch-detection/utils'; |
| 145 | + |
| 146 | +// Get note name from frequency |
| 147 | +const noteName = NoteLookup.getNoteName(440); // "A4" |
| 148 | + |
| 149 | +// Get frequency from note name |
| 150 | +const frequency = NoteLookup.getFrequency("A4"); // 440 |
| 151 | +``` |
| 152 | + |
| 153 | +### Chord Detection |
| 154 | + |
| 155 | +#### ChordDetector |
| 156 | + |
| 157 | +Detects chords from frequency data. |
| 158 | + |
| 159 | +```javascript |
| 160 | +import { ChordDetector } from 'bluesharp-pitch-detection/chord-detection'; |
| 161 | + |
| 162 | +// Detect chord from audio data |
| 163 | +const result = ChordDetector.detectChord(audioData, sampleRate); |
| 164 | + |
| 165 | +// Get detected pitches |
| 166 | +if (result.hasPitches()) { |
| 167 | + console.log(`Detected ${result.getPitchCount()} pitches`); |
| 168 | + console.log(`First pitch: ${result.getPitch(0)} Hz`); |
| 169 | + console.log(`Confidence: ${result.confidence}`); |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +#### ChordDetectionResult |
| 174 | + |
| 175 | +Represents the result of chord detection. |
| 176 | + |
| 177 | +```javascript |
| 178 | +import { ChordDetectionResult } from 'bluesharp-pitch-detection/chord-detection'; |
| 179 | + |
| 180 | +// Create a chord detection result with an array of pitches and a confidence value |
| 181 | +const pitches = [261.63, 329.63, 392.00]; // C, E, G (C major chord) |
| 182 | +const confidence = 0.95; |
| 183 | +const result = new ChordDetectionResult(pitches, confidence); |
| 184 | + |
| 185 | +// Alternative: use the static factory method |
| 186 | +const result2 = ChordDetectionResult.of(pitches, confidence); |
| 187 | + |
| 188 | +// Convert from a single pitch detection result |
| 189 | +const pitchResult = { pitch: 440, confidence: 0.9 }; |
| 190 | +const chordResult = ChordDetectionResult.fromPitchDetectionResult(pitchResult); |
| 191 | +``` |
| 192 | + |
| 193 | +### Audio Worklet |
| 194 | + |
| 195 | +#### PitchProcessor |
| 196 | + |
| 197 | +AudioWorklet processor for real-time pitch detection. |
| 198 | + |
| 199 | +```javascript |
| 200 | +import { PitchProcessor } from 'bluesharp-pitch-detection/audio-worklet'; |
| 201 | + |
| 202 | +// Register the processor |
| 203 | +const audioContext = new AudioContext(); |
| 204 | +await audioContext.audioWorklet.addModule('node_modules/bluesharp-pitch-detection/audio-worklet.js'); |
| 205 | + |
| 206 | +// Create and connect the processor |
| 207 | +const pitchNode = new AudioWorkletNode(audioContext, 'pitch-processor'); |
| 208 | +pitchNode.port.onmessage = (event) => { |
| 209 | + const { pitch, confidence } = event.data; |
| 210 | + console.log(`Detected pitch: ${pitch} Hz with confidence ${confidence}`); |
| 211 | +}; |
| 212 | + |
| 213 | +// Connect to audio source |
| 214 | +microphoneSource.connect(pitchNode); |
15 | 215 | ``` |
16 | 216 |
|
17 | 217 | ## License |
18 | 218 |
|
19 | | -MIT |
| 219 | +MIT © Christian Kierdorf |
0 commit comments