---
title: Synthesis
order: 2
summary: Make sound from nothing: oscillators, noise, envelopes, sweeps and drums.
---

<!-- Generated by tools/generate-docs.bxs from the BIF classes. Do not edit; change the Javadoc and run it again. -->

Make sound from nothing: oscillators, noise, envelopes, sweeps and drums.

## audioFreq

Convert a note name or MIDI number to a frequency in Hz

```js
audioFreq( note )
```

| Argument | Type | Default | |
|---|---|---|---|
| `note` | any | required | A note name such as C4, F#3 or Bb2 (C4 is middle C), or a MIDI note number where 69 is A4. Fractions detune, 60.1 is ten cents above C4. |

**Returns** The frequency in Hz with A4 tuned to 440.

## audioOsc

Generate a tone with an oscillator

```js
audioOsc( shape, freq, seconds, sampleRate=44100, harmonics=16, partials, width=0.5, phase=0 )
```

| Argument | Type | Default | |
|---|---|---|---|
| `shape` | string | required | sine, saw, square, triangle, pulse or additive. Saw, square and triangle are band limited so they do not alias. Pulse is the naive digital kind used by chiptune hardware. |
| `freq` | double | required | The frequency in Hz, see audioFreq() for note names. |
| `seconds` | double | required | The length in seconds. |
| `sampleRate` | integer | `44100` | The sample rate in Hz. Defaults to 44100. |
| `harmonics` | integer | `16` | For saw, square and triangle: the most harmonics to add, fewer are used near the Nyquist frequency. Fewer harmonics sound duller. Defaults to 16. |
| `partials` | array | optional | For additive: an array of [ frequency multiple, amplitude ] pairs, for example [ [ 1, 1.0 ], [ 2, 0.5 ] ]. Amplitudes are used as given. |
| `width` | double | `0.5` | For pulse: the fraction of the cycle spent high, 0.5 is a square. NES style duties are 0.125, 0.25, 0.5 and 0.75. Defaults to 0.5. |
| `phase` | double | `0` | Where in the cycle to start, 0 to 1. Defaults to 0. |

**Returns** A mono audio buffer with a peak of about 1.

## audioSweep

Generate a sine that glides from one pitch to another

```js
audioSweep( startHz, endHz, seconds, speed=28, sampleRate=44100 )
```

| Argument | Type | Default | |
|---|---|---|---|
| `startHz` | double | required | The frequency at the start, in Hz. |
| `endHz` | double | required | The frequency it settles towards, in Hz. |
| `seconds` | double | required | The length in seconds. |
| `speed` | double | `28` | How fast it settles, in 1/seconds. Higher is quicker. Defaults to 28. |
| `sampleRate` | integer | `44100` | The sample rate in Hz. Defaults to 44100. |

**Returns** A mono audio buffer. Shape it with audioEnvelope() to make a kick or a laser.

## audioNoise

Generate repeatable noise

```js
audioNoise( seconds, sampleRate=44100, seed=1, color="white" )
```

| Argument | Type | Default | |
|---|---|---|---|
| `seconds` | double | required | The length in seconds. |
| `sampleRate` | integer | `44100` | The sample rate in Hz. Defaults to 44100. |
| `seed` | long | `1` | Seed for the random generator. The same seed always gives the same noise. Defaults to 1. |
| `color` | string | `"white"` | white (flat), pink (softer highs), brown (rumble) or blue (hissy highs). Defaults to white. |

**Returns** A mono audio buffer at an RMS level of 0.25.

## audioEnvelope

Shape the volume of a sound with an attack and an exponential decay

```js
audioEnvelope( audio, attack=0.005, decay=0 )
```

| Argument | Type | Default | |
|---|---|---|---|
| `audio` | any | required | The audio buffer. |
| `attack` | double | `0.005` | Seconds to rise from silence to full volume in a straight line. 0 starts at full volume. Defaults to 0.005. |
| `decay` | double | `0` | The decay time constant in seconds, the level falls to 37% after one decay time. 0 means no decay. Defaults to 0. |

**Returns** A new audio buffer.

## audioDrum

Synthesise a drum hit

```js
audioDrum( type, sampleRate=44100, seed=1, pitch=1, decay=1, level=1, seconds=1 )
```

| Argument | Type | Default | |
|---|---|---|---|
| `type` | string | required | kick, snare, hat, openhat, clap, tom, crash or riser. |
| `sampleRate` | integer | `44100` | The sample rate in Hz. Defaults to 44100. |
| `seed` | long | `1` | Seed for the noise, the same seed always gives the same hit. Defaults to 1. |
| `pitch` | double | `1` | Multiplier for the tuned part of kick, snare and tom. 1 is the default tuning, 0.8 is lower. |
| `decay` | double | `1` | Multiplier for how long the hit rings. 1 is the default, 2 is twice as long. |
| `level` | double | `1` | The peak level of the result, 0 to 1. Defaults to 1. |
| `seconds` | double | `1` | The length of a riser. Ignored by the other types. |

**Returns** A mono audio buffer.

## audioFilter

Filter an audio buffer with a low-pass, high-pass, band-pass or notch filter

```js
audioFilter( audio, type, cutoff, q=0.7071 )
```

| Argument | Type | Default | |
|---|---|---|---|
| `audio` | any | required | The audio buffer. |
| `type` | string | required | lowpass, highpass, bandpass or notch. |
| `cutoff` | double | required | The corner or center frequency in Hz. |
| `q` | double | `0.7071` | Resonance. 0.7071 is flat, higher peaks at the cutoff. For bandpass and notch it sets how narrow they are. Defaults to 0.7071. |

**Returns** A new audio buffer.

