nish/synth

Short sounds made from nothing: square, triangle and noise voices with a pitch slide and a decaying envelope, mixed into an array of f64 samples. Nothing is sampled or copied; a sound is arithmetic.

import {
  SYNTH_NOISE,
  SYNTH_SQUARE,
  SYNTH_TRIANGLE,
  synthSemitone,
  synthSilence,
  synthTone,
} from "nish/synth";
import { SYNTH_SQUARE, SYNTH_TRIANGLE, synthSemitone, synthSilence, synthTone } from "nish/synth";

const RATE: i32 = 22050;
const out: f64[] = synthSilence(0.2, RATE);
synthTone(out, RATE, 0.0, 0.2, 440.0, 220.0, SYNTH_SQUARE, 0.25, 8.0);
synthTone(out, RATE, 0.05, 0.1, synthSemitone(440.0, 7), synthSemitone(440.0, 7), SYNTH_TRIANGLE, 0.2, 12.0);

A sound starts as silence of its length and gains voices one at a time, each added into the samples already there:

The samples are not clipped: a voice stays inside -volume..volume, so voices whose volumes sum to at most 1 stay inside -1..1, and what plays them (a WAV writer, a page's audio buffer) clips the rest. The sample rate is the caller's, passed to every function, so one program can make sounds for a 22050 Hz file and a 48000 Hz page alike.

Pure: no capability, no I/O and no clock. The noise voice is a fixed xorshift sequence, so the same call gives the same samples every time.

Functions

synthSemitone

The frequency semitones equal-tempered steps above base.

const synthSemitone: (base: f64, semitones: i32) => f64
basef64required

the starting frequency in Hz

semitonesi32required

how many steps up; negative steps go down

returnsf64

base × 2^(semitones / 12)

synthSilence

seconds of silence: an array of zero samples, one at least.

const synthSilence: (seconds: f64, sampleRate: i32) => f64[]
secondsf64required

how long; zero, negative or NaN is one sample

sampleRatei32required

samples a second

returnsf64[]

seconds × sampleRate samples of 0.0, at least one

synthTone

Add a voice into out from start seconds for length seconds. Its pitch slides from f0 to f1 Hz on a log scale, and its loudness starts at volume and falls by a factor of e every 1 / decay seconds. A 2 ms attack and a 5 ms release keep the ends from clicking. The voice stops at the end of out; it never grows the array.

Nothing is added for a sample rate, start or length that is not positive (a start of 0 is fine), for a frequency that is not positive, or for a start or length too long to count in samples.

const synthTone: (
  out: f64[],
  sampleRate: i32,
  start: f64,
  length: f64,
  f0: f64,
  f1: f64,
  wave: i32,
  volume: f64,
  decay: f64,
) => void
outf64[]required

the samples to add into

sampleRatei32required

samples a second that out is at

startf64required

where the voice begins, in seconds from the first sample

lengthf64required

how long the voice lasts, in seconds

f0f64required

the starting pitch in Hz

f1f64required

the ending pitch in Hz

wavei32required

SYNTH_SQUARE, SYNTH_TRIANGLE or SYNTH_NOISE; any other value is noise

volumef64required

the starting peak level; a negative level inverts the wave

decayf64required

how fast the level falls, per second; 0 holds it

Constants

2

SYNTH_NOISE: i32

Noise: a fresh random level each period of the pitch, so a falling pitch rumbles.

0

SYNTH_SQUARE: i32

A square wave: +1 for the first half of each period, -1 for the second.

1

SYNTH_TRIANGLE: i32

A triangle wave: a straight rise from -1 to +1 and a straight fall back.