Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: drum8 kick and snare #16

Merged
merged 6 commits into from
Sep 2, 2024
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
8,714 changes: 1,156 additions & 7,558 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
],
"devDependencies": {
"@changesets/cli": "^2.27.7",
"@types/audioworklet": "^0.0.57",
"@types/audioworklet": "^0.0.60",
"@types/jest": "^29.5.12",
"esbuild": "^0.23.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.5",
"tsup": "^8.0.1",
"turbo": "^2.0.6",
"turbo": "^2.1.1",
"typescript": "^5.5.3"
},
"jest": {
Expand Down
77 changes: 77 additions & 0 deletions packages/drum8/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# @synthlet/drum8

> Drum8 generator module for [synthlet](https://github.com/danigb/synthlet)

```ts
import { registerDrum8WorkletOnce, createDrum8 } from "@synthlet/drum8";

const audioContext = new AudioContext();

await registerDrum8WorkletOnce(audioContext);

const kick = createDrum8(audioContext, "kick", {
decay: 0.2, // time in seconds
tone: 0.8, // number between [0-1]
snap: 0.2, // number between [0-1]
});

kick.connect(audioContext.destination);
```

Available instruments (wip): `kick`, `snare`

## Install

Full package:

```bash
npm i synthlet
```

Just this module:

```bash
npm i @synthlet/drum8
```

## Usage

You need to register the audio worklet before creating any instrument. See [/README.md#register] for details.

#### Create an instrument

Use the `createDrum8` function:

```ts
import { createDrum8 } from "synthlet"; // or "@synthlet/drum8"

const kick = createDrum8(audioContext, "kick", {
decay: 0.2, // time in seconds
tone: 0.8, // number between [0-1]
snap: 0.2, // number between [0-1]
});

kick.connect(audioContext.destination);
```

The second parameter is the instrument type. Available instruments (wip) are: `kick`, `snare`.

The third is optional parameters:

- `attack`: attack time in seconds
- `decay`: decay time in seconds
- `level`: volume of the synth (log scale in [0,1] interval where 0 is silence and 1 is 0dB)
- `tone`: tone of the synth ([0,1] interval)
- `snap`: snap sound of the synth ([0,1] interval)

## References

Some parts of this package are based on:

- https://github.com/vincentriemer/io-808
- https://paulbatchelor.github.io/sndkit/env
- https://github.com/nick-thompson/drumsynth

Thanks! :heart:

## Licenses
32 changes: 32 additions & 0 deletions packages/drum8/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@synthlet/drum8",
"version": "0.1.0",
"description": "Drum synth module for Synthlet",
"keywords": [
"noise"
],
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"author": "[email protected]",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"jest": "^29.7.0",
"ts-jest": "^29.1.1"
},
"jest": {
"preset": "ts-jest"
},
"scripts": {
"worklet": "esbuild src/worklet.ts --bundle --minify | sed -e 's/^/export const PROCESSOR = \\`/' -e 's/$/\\`;/' > src/processor.ts",
"lib": "tsup src/index.ts --sourcemap --dts --format esm,cjs",
"build": "npm run worklet && npm run lib"
}
}
55 changes: 55 additions & 0 deletions packages/drum8/src/drums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
createClick,
createEnvelope,
createFilter,
createFixedSin2,
createNoise,
decaySine,
} from "./dsp";

type Params = {
gate: number[];
attack: number[];
decay: number[];
tone: number[];
snap: number[];
};

export type Generator = (output: Float32Array, params: Params) => void;
type Instrument = (sampleRate: number) => Generator;

export const Kick: Instrument = (sampleRate) => {
const sin = decaySine(sampleRate, 90, 48);
const click = createClick(sampleRate, 1);
const env = createEnvelope(sampleRate, 0.1, 0.1);
const lpf = createFilter(sampleRate, 0, 1.0);
const softClip = Math.tanh;

return (output, params) => {
sin.update(params.gate[0], params.decay[0] * 0.5);
env.update(params.gate[0], params.attack[0], params.decay[0]);
lpf.update(200 + 200 * params.tone[0]);
for (let i = 0; i < output.length; i++) {
output[i] = softClip(lpf.process(sin.next() * env.next() + click.next()));
}
};
};

export const Snare: Instrument = (sampleRate) => {
const noise = createNoise(sampleRate);
const envNoise = createEnvelope(sampleRate, 0.1, 0.1);
const snap = createFixedSin2(sampleRate, 238, 1, 476, 1);
const snapEnv = createEnvelope(sampleRate, 0.01, 0.1);
const lpf = createFilter(sampleRate, 0, 1.0);

return (output, params) => {
envNoise.update(params.gate[0], params.attack[0], params.decay[0]);
snapEnv.update(params.gate[0], 0.01, params.snap[0]);
lpf.update(500 + 500 * params.tone[0]);
for (let i = 0; i < output.length; i++) {
output[i] = lpf.process(
noise.next() * envNoise.next() + snap.next() * snapEnv.next()
);
}
};
};
Loading
Loading