Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 41 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A chromatic instrument tuner that runs in the browser and on Android. It listens

## Features

- Real-time pitch detection using the [pitchy](https://github.com/ianprime0509/pitchy) library (McLeod Pitch Method)
- Pitch detection built for tuning strings: accurate to a fraction of a cent (pedal tuners are rated ±1 cent), in tune within ±3 cents, and not thrown off by the string you tuned before still ringing
- Multiple instrument categories: guitar, bass, ukulele, and violin — each with tailored signal-processing parameters
- Preset tunings (Standard, Drop D, Half Step Down, Open tunings, scordatura variants, and more)
- Sequential string-by-string tuning flow — starts on the lowest/thickest string, confirms in tune, then auto-advances; tap any chip to jump to a specific string
Expand Down Expand Up @@ -45,7 +45,6 @@ A chromatic instrument tuner that runs in the browser and on Android. It listens
- [Vite](https://vitejs.dev/)
- [Capacitor](https://capacitorjs.com/) for Android packaging
- [vue-i18n](https://vue-i18n.intlify.dev/) for localisation
- [pitchy](https://github.com/ianprime0509/pitchy) for pitch detection
- [Nunito](https://fonts.google.com/specimen/Nunito) (SIL Open Font License), bundled with the app
- [Vitest](https://vitest.dev/) and [ESLint](https://eslint.org/), run on every pull request

Expand All @@ -70,99 +69,53 @@ The large files are stored with [Git LFS](https://git-lfs.com/). Run `git lfs in

## Signal Processing Architecture

The audio pipeline runs at two rates:
The tuner always knows which string it is listening for, and every stage is aimed at it.

| Layer | Rate | Responsibility |
|---|---|---|
| Engine | ~60fps (16ms) | RMS gate, transient gate, pitchy, deliver `AudioFrame` |
| Presentation | ~30fps (33ms) | Write Vue reactive refs, update needle/ring/hint |
| Layer | Where | Rate | Responsibility |
|---|---|---|---|
| Capture | `useAudioCapture` | ~60fps (16ms) | Microphone, low-pass filter, noise gate, deliver `AudioFrame` |
| Detector | `src/tuner/detector.ts` | per frame | Pitch of the string to a fraction of a cent, its clarity, its share of the sound |
| Engine | `src/tuner/engine.ts` | per frame | Reading, needle, in tune, confirm, next string |
| Presentation | `useTuner` | ~30fps (33ms) | Write Vue reactive refs, update needle/ring/hint |

### Per-instrument configuration

Each instrument category has its own profile in `INSTRUMENT_CONFIGS`.
The **decay gate** applies when string RMS drops below 0.04 (quiet tail). It must always be
≤ the normal clarity gate — decaying strings produce noisier signals, so the gate must be
*more* permissive, not stricter.

| Instrument | FFT window | Stable time | Clarity gate | Decay gate |
|---|---|---|---|---|
| Bass | 8192 (~185ms) | 1500ms | 0.93 | 0.82 |
| Guitar | 2048 (~46ms) | 1100ms | 0.92 | 0.80 |
| Ukulele | 2048 | 800ms | 0.85 | 0.75 |
| Violin | 2048 | 600ms | 0.80 | 0.70 |

### Signal chain (per tick)

**Every tick (useAudioCapture):**
1. **RMS noise gate** — frames below 0.01 RMS are discarded (silence / room noise)
2. **Attack transient gate** — 80ms suppression window after a >3× RMS spike (pick attack)
3. **Low-pass filter** — always active as an audio graph node; cutoff = 3× locked string fundamental, clamped 280–2000Hz; updated per string
4. **Pitchy (primary)** — McLeod pitch detection on the primary FFT window
5. **Large-window fallback** — if primary clarity < 0.92, a secondary 8192-sample analyser is tried (guitar/ukulele/violin only; bass uses 8192 as primary)

**Happy path — pitch passes frequency window and clarity gate (useTuner):**

6. **Frequency window + clarity gate** — pitch must be within ±15% of the locked fundamental and above the instrument clarity threshold
7. **Median filter** — 22-frame rolling median (~350ms) removes transient octave errors
8. **Octave-fold correction** — if dividing by N=2–6 brings the pitch within 300¢ and improves by >400¢, use the folded value
9. **Pitch continuity gate** — discard if EMA would jump >500¢ in one tick
10. **EMA smoothing** — α=0.04 at 60fps ≈ 500ms settling time for the cents needle
11. **Hint + stability ring** — advances `stabilityProgress`; confirms the string after `stableMs` hold time

**No-signal path — pitch fails step 6 (useTuner):**

12. **Overtone fold-and-route** — if the pitch is a harmonic (N×2–6) of the locked fundamental, attempt to fold it down to the fundamental; if the folded pitch lands in the ±15% window, route it through the full stability pipeline (steps 7–11); otherwise just refresh the latch timer
13. **Coarse hint (wide window)** — active only before the string has ever entered the ±15% fine-tuning window; if pitch is within ±1 octave of the fundamental and passes the clarity gate, show a directional too-high/too-low hint without advancing stability. Suppressed if the pitch is a harmonic of an already-tuned string (sympathetic resonance)
14. **Latch-and-Fade** — display held for 500ms after the last valid frame; needle position preserved across the gap

## Running Locally

**Prerequisites:** Node.js 20.19+ or 22.12+
### Capture (useAudioCapture)

```bash
# Install dependencies
npm install
1. **Raw microphone** — echo cancellation, noise suppression and automatic gain are turned off. They are tuned for speech: they fade out a sustained note as if it were noise and keep changing the level.
2. **Low-pass filter** — a Butterworth biquad at 3× the string (280–2000 Hz): the fundamental and the first harmonics pass, the upper harmonics that mislead period detection are damped.
3. **Noise gate** — a frame is analysed only above −60 dBFS and 6 dB above the room's own noise level, which the gate keeps learning from the unpitched frames.

# Start the dev server
npm run dev
```
### Detector (per frame)

Open [http://localhost:5173/String-Tuner/](http://localhost:5173/String-Tuner/) in your browser. The app will ask for microphone permission on first use.
4. **Decimation** — the filtered input is averaged down to a rate that still gives ≥ 64 samples per period, so a 31 Hz bass B costs as little as a violin E.
5. **Coarse pitch: McLeod Pitch Method** — the normalised square difference function of a 60 ms (at least 4 periods) span, the same span at every lag, up to a fifth below the string, in double precision through one FFT pair; the first key maximum ≥ 0.9 of the highest, refined by parabolic interpolation.
6. **Fine pitch: phase measurement** — as a strobe tuner does: the phase advance of the fundamental and of the 2nd harmonic between two Hann-windowed frames of 12 periods. A period method reads a blend of everything that rings (with the previous string still sounding, the new one several cents flat); in these narrow bands a string a third or a fourth away hardly counts. Only this fine pitch is ever shown: in the first moments of a note, before both frames hold it, there is no reading at all.
7. **Fundamental share** — how much of the sound is at the pitch and its octave. B3 and E4 ringing together repeat at the period of E2 with no E2 in them: that phantom has none.
8. **Harmonic spread** — the cents between the pitch the fundamental gives and the one the 2nd harmonic gives: within a cent for a string read cleanly, wide when noise in the bands pulls each its own way.

```bash
# Run the tests (including a virtual instrument played through the whole audio chain)
npm test
### Engine (per frame)

# Check the code
npm run lint
9. **Clarity gate** — 0.85 to start a reading, 0.75 to carry one on through the tail of the note (violin: 0.80 / 0.70). Another string ringing as loud lowers the clarity of the whole sound without spoiling the narrow-band pitch, so a frame down to 0.55 still counts when its harmonics agree within a cent.
10. **Range** — up to a fifth either way reads as this string (direction to turn); a pitch an octave up (±300¢) is its 2nd harmonic. Anything else, a tuned string still ringing, or a phantom is ignored.
11. **Median of 5 readings** — a single wrong frame never shows.
12. **Needle smoothing** — 60 ms when steady, 25 ms while the pitch moves (a peg being turned); slower for readings whose harmonics disagree (noise).
13. **In tune** — within ±3 cents after at least 3 readings, staying in until ±4 cents (no flicker at the edge); confirmed after a hold time in tune.
14. **Latch-and-fade** — the reading stays 500 ms after the sound stops; the needle stays where it was.

# Build for production
npm run build

# Preview the production build
npm run preview
```

## Android

**Prerequisites:** Android Studio with the Android SDK installed.

```bash
# Build the web assets and sync to the Android project
npm run build
npx cap sync android

# Open in Android Studio
npx cap open android
```

The app declares the `RECORD_AUDIO` permission in `AndroidManifest.xml`, which Android requires for microphone access. The permission prompt will appear on first launch.

**Min SDK:** Android 7.0 (API 24) — **Target SDK:** API 36

## License

This project is licensed under the **MIT License** — you are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software, provided that the original copyright notice and this permission notice are included in all copies or substantial portions of the software.
### Per-instrument configuration

See the [LICENSE](LICENSE) file for the full text.
| Instrument | Hold time in tune | Clarity to start | Clarity to carry on |
|---|---|---|---|
| Guitar | 1000ms | 0.85 | 0.75 |
| Bass | 1200ms | 0.85 | 0.75 |
| Ukulele | 700ms | 0.85 | 0.75 |
| Violin | 600ms | 0.80 | 0.70 |

### Measured on the virtual instrument (`npm test`)

| | |
|---|---|
| Error on a held note | < 0.03 cent on guitar; < 0.2 cent in a noisy room (< 0.5 on ukulele) |
| First reading after a pluck | 80–220 ms on guitar, already within half a cent; 0.15–0.36 s on bass |
| Needle behind a peg being turned | 130–220 ms |
| Previous string ringing 6 dB down | < 1 cent of error (a period method alone: 15–22 cents) |

2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- The WebView's audio capture (getUserMedia) needs it to set up the microphone route -->
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />
</manifest>
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@capacitor/android": "^8.3.1",
"@capacitor/core": "^8.3.1",
"@fontsource-variable/nunito": "^5.3.0",
"pitchy": "^4.1.0",
"vue": "^3.5.32",
"vue-i18n": "^11.4.0"
},
Expand Down
Loading
Loading