/ tutorials / wave-acoustics
Sign in
tutorials / wave-acoustics
wave-acoustics / src / chapters / shared.ts
29 lines · 1.1 KBCodeBlameHistory
d8098edAn interactive tutorial on the wave equation in acoustics: strings, drums and rooms, measured by earJeremy Magland 1// Settings reused across chapters, so that "the audio rate" and "A4" mean the
2// same thing everywhere.
3import type { Control } from '../ui/types.ts'
5/** Every sound in the tutorial that is not a simulation's own output is at CD rate. */
6export const FS = 44100
8const NAMES = ['C', 'C♯', 'D', 'E♭', 'E', 'F', 'F♯', 'G', 'A♭', 'A', 'B♭', 'B']
10/** MIDI note number to a name like A4, with A4 = 69. */
11export function noteName(m: number): string {
12 const r = Math.round(m)
13 return `${NAMES[((r % 12) + 12) % 12]}${Math.floor(r / 12) - 1}`
16/** Equal-tempered frequency of a MIDI note, A4 = 440 Hz. */
17export function noteHz(m: number): number {
18 return 440 * Math.pow(2, (m - 69) / 12)
21export function noteLabel(m: number): string {
22 const f = noteHz(m)
23 return `${noteName(m)}, ${f >= 1000 ? f.toFixed(0) : f.toFixed(1)} Hz`
26/** A slider over a list of MIDI notes, showing the name and the frequency. */
27export function noteControl(key: string, label: string, notes: number[]): Control {
28 return { kind: 'slider', key, label, min: 0, max: 0, values: notes, format: noteLabel }
↑↓ move↵openescclose