create-cecile-module 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -2
- package/create.ts +91 -0
- package/package.json +25 -4
- package/template/README.md +41 -0
- package/template/_gitignore +3 -0
- package/template/package.json +39 -0
- package/template/scripts/dev.ts +90 -0
- package/template/src/module.test.ts +14 -0
- package/template/src/module.ts +11 -0
- package/template/src/processor.test.ts +95 -0
- package/template/src/processor.ts +42 -0
- package/template/src/worklet.d.ts +14 -0
- package/template/tsconfig.json +13 -0
package/README.md
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
-
#
|
|
1
|
+
# create-cecile-module
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Creates a Cécile module: an AudioWorklet processor built with Bun, with a dev server that
|
|
4
|
+
serves it to Cécile on localhost, and tests that run the built bundle the way Cécile does.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
bun create cecile-module cecile-foo-bar
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
(or `bunx create-cecile-module cecile-foo-bar`, `npm init cecile-module cecile-foo-bar`)
|
|
11
|
+
|
|
12
|
+
The folder name is the module name: `cecile-foo-bar` (or `foo-bar`) makes the package
|
|
13
|
+
`@t4sty/cecile-foo-bar`, the processor `foo-bar` (also the alias `bun run dev` prints for
|
|
14
|
+
`#import`), and the repository `github.com/t4sty-it/cecile-foo-bar`. `CECILE_SCOPE=@me`
|
|
15
|
+
and `CECILE_GITHUB=me` change the scope and the GitHub owner. It then installs and makes
|
|
16
|
+
the first commit; `--no-install` and `--no-git` skip those.
|
|
17
|
+
|
|
18
|
+
The new module is a working example, `scale` (`input * amount + offset`). To make it
|
|
19
|
+
yours:
|
|
20
|
+
|
|
21
|
+
1. In `package.json`, write the `description`, the `keywords`, the `category` and the
|
|
22
|
+
`params`: the inlets and outlets Cécile shows, in the order `process` gets them.
|
|
23
|
+
2. Replace `src/module.ts` with the module's logic, and its tests.
|
|
24
|
+
3. Update `src/processor.ts` to read the inputs and params and write the outputs, and add
|
|
25
|
+
the module's own tests at the end of `src/processor.test.ts` (the generic ones above
|
|
26
|
+
them read the manifest and work as they are).
|
|
27
|
+
4. Rewrite `README.md`: what the module does, an example patch, its inlets and outlets.
|
|
28
|
+
5. Publish with `npm publish`, which typechecks, tests and builds first.
|
|
29
|
+
|
|
30
|
+
The new module comes with no CI: a GitHub Actions workflow runs on the repository it's
|
|
31
|
+
pushed to, so adding one is left to the module's author.
|
|
32
|
+
|
|
33
|
+
## Working on the template
|
|
34
|
+
|
|
35
|
+
`template/` is the module every new one starts from, named `module-template`: `create.ts`
|
|
36
|
+
copies it and replaces `@t4sty/cecile-module-template`, `cecile-module-template` and
|
|
37
|
+
`module-template` with the new names in `package.json`, `README.md` and
|
|
38
|
+
`src/processor.ts`. Its `.gitignore` is `_gitignore`, because npm leaves `.gitignore`
|
|
39
|
+
files out of packages.
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
bun install
|
|
43
|
+
bun run typecheck # create.ts, and the template
|
|
44
|
+
bun run test # creates a module in a temp folder, then typechecks and tests it
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
To try the template's code in place: `cd template && bun install && bun test` (or
|
|
48
|
+
`bun run dev`).
|
|
49
|
+
|
|
50
|
+
To publish `create-cecile-module`, push a tag `v<version>` matching `package.json`:
|
|
51
|
+
`.github/workflows/publish.yml` publishes it through npm trusted publishing. That workflow
|
|
52
|
+
belongs to this repository only; `template/` has none.
|
package/create.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// `bun create cecile-module <folder>` (or `bunx create-cecile-module <folder>`):
|
|
3
|
+
// copies template/ into <folder> as a new module named after it, installs
|
|
4
|
+
// and makes the first commit.
|
|
5
|
+
//
|
|
6
|
+
// A folder `cecile-foo-bar` (or `foo-bar`) makes the package
|
|
7
|
+
// `@t4sty/cecile-foo-bar`, the processor `foo-bar` and the repository
|
|
8
|
+
// github.com/t4sty-it/cecile-foo-bar. CECILE_SCOPE and CECILE_GITHUB change
|
|
9
|
+
// the scope and the GitHub owner.
|
|
10
|
+
import { $ } from 'bun'
|
|
11
|
+
import { cp, readdir, rename } from 'node:fs/promises'
|
|
12
|
+
import { basename, join, relative, resolve } from 'node:path'
|
|
13
|
+
|
|
14
|
+
// the names the template uses, replaced in package.json, src/ and README.md
|
|
15
|
+
const TEMPLATE = 'module-template'
|
|
16
|
+
const TEMPLATE_SCOPE = '@t4sty'
|
|
17
|
+
const TEMPLATE_OWNER = 't4sty-it'
|
|
18
|
+
|
|
19
|
+
const args = process.argv.slice(2)
|
|
20
|
+
const flags = new Set(args.filter(a => a.startsWith('--')))
|
|
21
|
+
const target = args.find(a => !a.startsWith('--'))
|
|
22
|
+
|
|
23
|
+
if (!target || flags.has('--help')) {
|
|
24
|
+
console.log('usage: bun create cecile-module <folder> [--no-install] [--no-git]\n' +
|
|
25
|
+
' e.g. bun create cecile-module cecile-clock-divider')
|
|
26
|
+
process.exit(target ? 0 : 1)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const scope = process.env.CECILE_SCOPE ?? TEMPLATE_SCOPE
|
|
30
|
+
const owner = process.env.CECILE_GITHUB ?? TEMPLATE_OWNER
|
|
31
|
+
const dest = resolve(target)
|
|
32
|
+
const short = basename(dest).toLowerCase().replace(/^cecile-/, '')
|
|
33
|
+
const repo = `cecile-${short}`
|
|
34
|
+
const pkg = `${scope}/${repo}`
|
|
35
|
+
|
|
36
|
+
function fail(message: string): never {
|
|
37
|
+
console.error(message)
|
|
38
|
+
process.exit(1)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// the processor name is also the default #import alias: keep it simple
|
|
42
|
+
if (!/^[a-z][a-z0-9-]*$/.test(short) || short == TEMPLATE)
|
|
43
|
+
fail(`"${basename(dest)}" isn't a usable module name: use lowercase letters, digits and dashes, e.g. cecile-clock-divider`)
|
|
44
|
+
if (!/^@[a-z0-9][a-z0-9._-]*$/.test(scope))
|
|
45
|
+
fail(`CECILE_SCOPE "${scope}" isn't an npm scope, e.g. @me`)
|
|
46
|
+
if ((await readdir(dest).catch(() => [])).length > 0)
|
|
47
|
+
fail(`${dest} already exists and isn't empty`)
|
|
48
|
+
|
|
49
|
+
const template = join(import.meta.dir, 'template')
|
|
50
|
+
await cp(template, dest, {
|
|
51
|
+
recursive: true,
|
|
52
|
+
// left over from working on the template in place
|
|
53
|
+
filter: src => !/^(node_modules|dist|bun\.lock)(\/|$)/.test(relative(template, src)),
|
|
54
|
+
})
|
|
55
|
+
// npm leaves .gitignore files out of packages
|
|
56
|
+
await rename(join(dest, '_gitignore'), join(dest, '.gitignore'))
|
|
57
|
+
|
|
58
|
+
const renamed = (text: string) => text
|
|
59
|
+
.replaceAll(`${TEMPLATE_SCOPE}/cecile-${TEMPLATE}`, pkg)
|
|
60
|
+
.replaceAll(`github.com/${TEMPLATE_OWNER}/cecile-${TEMPLATE}`, `github.com/${owner}/${repo}`)
|
|
61
|
+
.replaceAll(`cecile-${TEMPLATE}`, repo)
|
|
62
|
+
.replaceAll(TEMPLATE, short)
|
|
63
|
+
|
|
64
|
+
for (const path of ['package.json', 'README.md', 'src/processor.ts']) {
|
|
65
|
+
const file = Bun.file(join(dest, path))
|
|
66
|
+
await Bun.write(file, renamed(await file.text()))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const where = relative(process.cwd(), dest) || '.'
|
|
70
|
+
console.log(`created ${pkg} in ${where}`)
|
|
71
|
+
|
|
72
|
+
if (!flags.has('--no-install')) {
|
|
73
|
+
const install = await $`bun install`.cwd(dest).nothrow()
|
|
74
|
+
if (install.exitCode != 0) console.error('bun install failed: run it again in the new folder')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!flags.has('--no-git')) {
|
|
78
|
+
const git = await $`git init -q && git add -A && git commit -q -m ${`feat: ${short} module for Cécile, from create-cecile-module`}`
|
|
79
|
+
.cwd(dest).nothrow().quiet()
|
|
80
|
+
if (git.exitCode != 0) console.error(`git: couldn't make the first commit (${git.stderr.toString().trim()})`)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
console.log(`
|
|
84
|
+
next:
|
|
85
|
+
cd ${where}
|
|
86
|
+
- package.json: the description, keywords, category and the params (inlets and outlets)
|
|
87
|
+
- src/module.ts: the module's logic, and its tests
|
|
88
|
+
- src/processor.ts: read the inputs and params, write the outputs
|
|
89
|
+
- README.md: what it does, an example patch, the inlets and outlets
|
|
90
|
+
bun run dev # serves it to Cécile on localhost
|
|
91
|
+
bun test`)
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-cecile-module",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Creates a Cécile module: bun create cecile-module <folder>",
|
|
5
|
+
"keywords": ["cecile", "cecile-module", "audioworklet", "template", "create"],
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/t4sty-it/cecile-module-template.git"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"bin": {
|
|
15
|
+
"create-cecile-module": "create.ts"
|
|
16
|
+
},
|
|
17
|
+
"files": ["create.ts", "template"],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "bun test ./create.test.ts",
|
|
20
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p template",
|
|
21
|
+
"prepublishOnly": "bun run typecheck && bun run test"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/bun": "^1.2.13",
|
|
25
|
+
"typescript": "^5.8.3"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# module-template
|
|
2
|
+
|
|
3
|
+
A Cécile module: it scales and offsets a signal, outputting `input * amount + offset`.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
#import @t4sty/cecile-module-template as module-template
|
|
7
|
+
oscillator @frequency=220 > gain:vca @gain=0 > out
|
|
8
|
+
oscillator @frequency=0.5 > module-template @amount=0.5 @offset=0.5 > gain{ $gain:vca
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Inlets
|
|
12
|
+
|
|
13
|
+
| Name | What it does |
|
|
14
|
+
|------|--------------|
|
|
15
|
+
| `input` | The signal to scale. |
|
|
16
|
+
| `amount` | What the input is multiplied by, -10 to 10. |
|
|
17
|
+
| `offset` | What's added after, -10 to 10. |
|
|
18
|
+
|
|
19
|
+
## Outlets
|
|
20
|
+
|
|
21
|
+
| Name | What it does |
|
|
22
|
+
|------|--------------|
|
|
23
|
+
| `output` | `input * amount + offset`, or 0 where that isn't a finite number. |
|
|
24
|
+
|
|
25
|
+
## Development
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
bun install
|
|
29
|
+
bun run dev # rebuilds on change and serves the module on localhost, from port 5174
|
|
30
|
+
bun test # the module, and the built processor run as Cécile runs it
|
|
31
|
+
bun run build # src/ → dist/processor.js, the single file Cécile loads
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
With `bun run dev` running, `#import http://localhost:5174 as module-template` in Cécile
|
|
35
|
+
loads the local build; run the import again after a change to reload it. If 5174 is taken,
|
|
36
|
+
e.g. by another module's dev server, it picks the next free port and prints the `#import`
|
|
37
|
+
to use (`PORT=…` forces one).
|
|
38
|
+
|
|
39
|
+
To publish, `npm publish`: `prepublishOnly` typechecks, tests and builds first. `dist/`
|
|
40
|
+
isn't committed: Cécile fetches modules from npm (through jsDelivr or unpkg), and
|
|
41
|
+
`prepublishOnly` builds it into the package.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@t4sty/cecile-module-template",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A Cécile module: scales and offsets a signal",
|
|
5
|
+
"keywords": ["cecile-module", "audioworklet"],
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/t4sty-it/cecile-module-template.git"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": ["dist/processor.js"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "bun scripts/dev.ts",
|
|
17
|
+
"build": "bun build src/processor.ts --outfile dist/processor.js --format esm --target browser",
|
|
18
|
+
"test": "bun test",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"prepublishOnly": "bun run typecheck && bun test && bun run build"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/bun": "^1.2.13",
|
|
24
|
+
"typescript": "^5.8.3"
|
|
25
|
+
},
|
|
26
|
+
"cecile": {
|
|
27
|
+
"api": 1,
|
|
28
|
+
"kind": "worklet",
|
|
29
|
+
"main": "dist/processor.js",
|
|
30
|
+
"processor": "module-template",
|
|
31
|
+
"category": "Control",
|
|
32
|
+
"params": [
|
|
33
|
+
{ "name": "input", "type": "input", "dataType": "signal" },
|
|
34
|
+
{ "name": "amount", "type": "input", "dataType": "number", "value": 1, "min": -10, "max": 10 },
|
|
35
|
+
{ "name": "offset", "type": "input", "dataType": "number", "value": 0, "min": -10, "max": 10 },
|
|
36
|
+
{ "name": "output", "type": "output", "dataType": "signal" }
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Dev loop: rebuilds on every change under src/ and serves the module to
|
|
2
|
+
// Cécile, which imports it with `#import http://localhost:<port> as <alias>`
|
|
3
|
+
// (the first free port from 5174, printed at start).
|
|
4
|
+
// Running the import again loads the new code into the patch.
|
|
5
|
+
import { $ } from 'bun'
|
|
6
|
+
import { watch } from 'node:fs'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
|
|
9
|
+
const root = join(import.meta.dir, '..')
|
|
10
|
+
const port = Number(process.env.PORT ?? 5174)
|
|
11
|
+
|
|
12
|
+
const headers = {
|
|
13
|
+
// Cécile runs on another origin, and must always get the latest build
|
|
14
|
+
'Access-Control-Allow-Origin': '*',
|
|
15
|
+
// Chrome's preflight for a public site reading from localhost
|
|
16
|
+
'Access-Control-Allow-Private-Network': 'true',
|
|
17
|
+
'Cache-Control': 'no-store',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function build() {
|
|
21
|
+
const result = await $`bun run build`.cwd(root).nothrow().quiet()
|
|
22
|
+
const time = new Date().toLocaleTimeString()
|
|
23
|
+
if (result.exitCode == 0) console.log(`${time} built, run the #import again in Cécile to reload`)
|
|
24
|
+
else console.error(`${time} build failed:\n${result.stderr}`)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const packageJson = () => Bun.file(join(root, 'package.json')).json()
|
|
28
|
+
|
|
29
|
+
// only what Cécile asks for: package.json and the manifest's main file
|
|
30
|
+
async function servable(path: string): Promise<boolean> {
|
|
31
|
+
return path == 'package.json' || path == (await packageJson()).cecile?.main
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await build()
|
|
35
|
+
|
|
36
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
37
|
+
watch(join(root, 'src'), { recursive: true }, (_event, file) => {
|
|
38
|
+
if (file?.endsWith('.test.ts')) return
|
|
39
|
+
clearTimeout(timer)
|
|
40
|
+
timer = setTimeout(build, 100)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
async function handle(req: Request): Promise<Response> {
|
|
44
|
+
if (req.method == 'OPTIONS')
|
|
45
|
+
return new Response(null, { status: 204, headers: { ...headers, 'Access-Control-Allow-Methods': 'GET' } })
|
|
46
|
+
|
|
47
|
+
const path = decodeURIComponent(new URL(req.url).pathname).replace(/^\/+/, '')
|
|
48
|
+
const file = Bun.file(join(root, path))
|
|
49
|
+
if (!(await servable(path)) || !(await file.exists()))
|
|
50
|
+
return new Response('Not found', { status: 404, headers })
|
|
51
|
+
return new Response(file, { headers })
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const inUse = (e: unknown) => (e as { code?: string })?.code == 'EADDRINUSE'
|
|
55
|
+
|
|
56
|
+
// `localhost` is both 127.0.0.1 and ::1, and browsers try ::1 first: listen
|
|
57
|
+
// on both, or another module's dev server on the other one would answer
|
|
58
|
+
// `#import http://localhost:<port>` instead of this one. False if the port is
|
|
59
|
+
// taken on either.
|
|
60
|
+
function listen(port: number): boolean {
|
|
61
|
+
const servers = []
|
|
62
|
+
for (const hostname of ['127.0.0.1', '::1']) {
|
|
63
|
+
try {
|
|
64
|
+
servers.push(Bun.serve({ hostname, port, fetch: handle }))
|
|
65
|
+
} catch (e) {
|
|
66
|
+
// a machine without IPv6 only has 127.0.0.1
|
|
67
|
+
if (hostname == '::1' && !inUse(e)) break
|
|
68
|
+
servers.forEach(s => s.stop(true))
|
|
69
|
+
if (inUse(e)) return false
|
|
70
|
+
throw e
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// an explicit PORT or nothing; otherwise the first free one from 5174, so
|
|
77
|
+
// several modules can be served at once
|
|
78
|
+
const candidates = process.env.PORT ? [port] : Array.from({ length: 20 }, (_, i) => port + i)
|
|
79
|
+
const served = candidates.find(listen)
|
|
80
|
+
if (served === undefined) {
|
|
81
|
+
console.error(process.env.PORT
|
|
82
|
+
? `port ${port} is already in use on localhost: pick another with PORT=…`
|
|
83
|
+
: `ports ${port}-${port + 19} are all in use on localhost: pick one with PORT=…`)
|
|
84
|
+
process.exit(1)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// a valid alias: the package name without scope, as a Cécile identifier
|
|
88
|
+
const alias = String((await packageJson()).name).split('/').at(-1)!
|
|
89
|
+
.replace(/^cecile-/, '').replace(/[^a-zA-Z0-9_-]/g, '_').replace(/^(?=\d)/, '_')
|
|
90
|
+
console.log(`serving on http://localhost:${served}, import it in Cécile with:\n #import http://localhost:${served} as ${alias}`)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { Module } from './module'
|
|
3
|
+
|
|
4
|
+
test('scales then offsets the input', () => {
|
|
5
|
+
const m = new Module()
|
|
6
|
+
expect(m.tick(0.5, 2, 0.25)).toBe(1.25)
|
|
7
|
+
expect(m.tick(1, -1, 0)).toBe(-1)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('outputs 0 instead of NaN or infinities', () => {
|
|
11
|
+
const m = new Module()
|
|
12
|
+
expect(m.tick(1, NaN, 0)).toBe(0)
|
|
13
|
+
expect(m.tick(1e308, 10, 0)).toBe(0)
|
|
14
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The module's logic, one sample at a time and free of AudioWorklet globals, so
|
|
2
|
+
// it can be tested on its own. Replace it with yours; keep any state here (a
|
|
3
|
+
// counter, the last input for edge detection...), not in the processor.
|
|
4
|
+
|
|
5
|
+
export class Module {
|
|
6
|
+
tick(input: number, amount: number, offset: number): number {
|
|
7
|
+
const value = input * amount + offset
|
|
8
|
+
// a NaN or an infinity would spread through the whole patch
|
|
9
|
+
return Number.isFinite(value) ? value : 0
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { beforeAll, expect, test } from 'bun:test'
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
|
|
5
|
+
// Runs the built bundle the way Cécile does: as plain source (no imports or
|
|
6
|
+
// exports), in a scope that provides AudioWorkletProcessor and
|
|
7
|
+
// registerProcessor. Generic checks first; add the module's own below.
|
|
8
|
+
|
|
9
|
+
type Processor = {
|
|
10
|
+
process(i: Float32Array[][], o: Float32Array[][], p: Record<string, Float32Array>): boolean
|
|
11
|
+
onParam?(name: string, value: string): void
|
|
12
|
+
}
|
|
13
|
+
type Param = { name: string, type: string, dataType: string, value?: number | string }
|
|
14
|
+
|
|
15
|
+
const root = join(import.meta.dir, '..')
|
|
16
|
+
const manifest = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')).cecile as {
|
|
17
|
+
processor: string, main: string, params: Param[]
|
|
18
|
+
}
|
|
19
|
+
const signals = (type: string) => manifest.params.filter(p => p.type == type && p.dataType == 'signal')
|
|
20
|
+
const numbers = manifest.params.filter(p => p.dataType == 'number')
|
|
21
|
+
|
|
22
|
+
let Ctor: new () => Processor
|
|
23
|
+
let name = ''
|
|
24
|
+
|
|
25
|
+
beforeAll(async () => {
|
|
26
|
+
await Bun.$`bun run build`.cwd(root).quiet()
|
|
27
|
+
const source = readFileSync(join(root, manifest.main), 'utf8')
|
|
28
|
+
expect(source).not.toMatch(/^\s*(import|export)\b/m)
|
|
29
|
+
new Function('AudioWorkletProcessor', 'registerProcessor', source)(
|
|
30
|
+
class {},
|
|
31
|
+
(n: string, C: typeof Ctor) => { name = n; Ctor = C },
|
|
32
|
+
)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const BLOCK = 128
|
|
36
|
+
|
|
37
|
+
// one block: `inputs` by inlet index, padded with zeros; stereo outputs
|
|
38
|
+
function block(inputs: number[][] = []) {
|
|
39
|
+
const pad = (xs: number[] = []) => Float32Array.from({ length: BLOCK }, (_, i) => xs[i] ?? 0)
|
|
40
|
+
return {
|
|
41
|
+
inputs: signals('input').map((_, k) => [pad(inputs[k])]),
|
|
42
|
+
outputs: signals('output').map(() => [new Float32Array(BLOCK), new Float32Array(BLOCK)]),
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// the manifest's defaults, or the given values, as k-rate params
|
|
47
|
+
const params = (values: Record<string, number> = {}) => Object.fromEntries(
|
|
48
|
+
numbers.map(p => [p.name, Float32Array.of(values[p.name] ?? Number(p.value ?? 0))]))
|
|
49
|
+
|
|
50
|
+
test('registers under the manifest name', () => {
|
|
51
|
+
expect(name).toBe(manifest.processor)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('runs with nothing connected', () => {
|
|
55
|
+
const p = new Ctor()
|
|
56
|
+
const outputs = signals('output').map(() => [new Float32Array(BLOCK)])
|
|
57
|
+
expect(p.process(signals('input').map(() => []), outputs, params())).toBe(true)
|
|
58
|
+
expect(outputs.every(o => o[0].every(Number.isFinite))).toBe(true)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('writes every channel the same, and finite values', () => {
|
|
62
|
+
const p = new Ctor()
|
|
63
|
+
const { inputs, outputs } = block(signals('input').map(() => Array.from({ length: BLOCK }, (_, i) => Math.sin(i / 8))))
|
|
64
|
+
p.process(inputs, outputs, params())
|
|
65
|
+
for (const [left, right] of outputs) {
|
|
66
|
+
expect(Array.from(right)).toEqual(Array.from(left))
|
|
67
|
+
expect(left.every(Number.isFinite)).toBe(true)
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('survives edge values of its params', () => {
|
|
72
|
+
for (const v of [0, -1, 1e6, NaN]) {
|
|
73
|
+
const p = new Ctor()
|
|
74
|
+
const { inputs, outputs } = block()
|
|
75
|
+
expect(p.process(inputs, outputs, params(Object.fromEntries(numbers.map(n => [n.name, v]))))).toBe(true)
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
// module-specific tests: drive `process` over a few blocks and check outputs
|
|
80
|
+
|
|
81
|
+
test('scales and offsets the input', () => {
|
|
82
|
+
const p = new Ctor()
|
|
83
|
+
const { inputs, outputs } = block([[0, 0.5, 1, -1]])
|
|
84
|
+
p.process(inputs, outputs, params({ amount: 2, offset: 0.5 }))
|
|
85
|
+
expect(Array.from(outputs[0][0].slice(0, 4))).toEqual([0.5, 1.5, 2.5, -1.5])
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('reads its params at audio rate when patched', () => {
|
|
89
|
+
const p = new Ctor()
|
|
90
|
+
const { inputs, outputs } = block([Array(BLOCK).fill(1)])
|
|
91
|
+
const amount = Float32Array.from({ length: BLOCK }, (_, i) => (i < 64 ? 1 : 3))
|
|
92
|
+
p.process(inputs, outputs, { amount, offset: Float32Array.of(0) })
|
|
93
|
+
expect(outputs[0][0][0]).toBe(1)
|
|
94
|
+
expect(outputs[0][0][64]).toBe(3)
|
|
95
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Module } from './module'
|
|
2
|
+
|
|
3
|
+
// Inputs and outputs by index, in the order of the `cecile` field of
|
|
4
|
+
// package.json: inputs 0 input; outputs 0 output. Number params arrive in
|
|
5
|
+
// `parameters` by name. The parameter descriptors come from the manifest:
|
|
6
|
+
// Cécile declares them.
|
|
7
|
+
class ModuleProcessor extends AudioWorkletProcessor {
|
|
8
|
+
private module = new Module()
|
|
9
|
+
|
|
10
|
+
process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Record<string, Float32Array>): boolean {
|
|
11
|
+
// a disconnected input is an empty array
|
|
12
|
+
const input = inputs[0]?.[0]
|
|
13
|
+
const [out] = outputs
|
|
14
|
+
const { amount, offset } = parameters
|
|
15
|
+
const length = out?.[0]?.length ?? 128
|
|
16
|
+
|
|
17
|
+
for (let i = 0; i < length; i++) {
|
|
18
|
+
const value = this.module.tick(
|
|
19
|
+
input?.[i] ?? 0,
|
|
20
|
+
at(amount, i),
|
|
21
|
+
at(offset, i),
|
|
22
|
+
)
|
|
23
|
+
write(out, i, value)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Cécile stops the processor when the node is deleted
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// a param is one value per block (k-rate) or one per sample (a-rate, when patched)
|
|
32
|
+
function at(param: Float32Array, i: number): number {
|
|
33
|
+
return param.length > 1 ? param[i] : param[0]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function write(output: Float32Array[] | undefined, i: number, value: number) {
|
|
37
|
+
if (!output) return
|
|
38
|
+
for (let ch = 0; ch < output.length; ch++) output[ch][i] = value
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// the same name as `processor` in the manifest
|
|
42
|
+
registerProcessor('module-template', ModuleProcessor)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// The AudioWorkletGlobalScope, which TypeScript's DOM lib doesn't describe.
|
|
2
|
+
|
|
3
|
+
declare class AudioWorkletProcessor {
|
|
4
|
+
readonly port: MessagePort
|
|
5
|
+
constructor(options?: AudioWorkletNodeOptions)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare function registerProcessor(
|
|
9
|
+
name: string,
|
|
10
|
+
processor: new (options?: AudioWorkletNodeOptions) => AudioWorkletProcessor,
|
|
11
|
+
): void
|
|
12
|
+
|
|
13
|
+
declare const sampleRate: number
|
|
14
|
+
declare const currentTime: number
|