Skip to content

Commit

Permalink
Upgrade to Elementary Audio v3 (#8)
Browse files Browse the repository at this point in the history
* Upgrade Elementary audio to v3

* Instantiate AudioContext directly

* Set noEmit to avoid file overwrite warnings
  • Loading branch information
bgins authored Nov 19, 2023
1 parent c9678d1 commit 63ea352
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 70 deletions.
61 changes: 18 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
},
"type": "module",
"dependencies": {
"@elemaudio/core": "^1.0.8",
"@elemaudio/web-renderer": "^1.0.16",
"@elemaudio/core": "^3.0.0",
"@elemaudio/web-renderer": "^3.0.1",
"rxjs": "^7.5.4",
"webmidi": "^3.0.20"
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Engine {
const core = new WebRenderer()

if (!store.context) {
const context = new window.AudioContext()
const context = new AudioContext()
void context.suspend()

engineStore.update(store => ({
Expand Down
21 changes: 6 additions & 15 deletions src/lib/instruments/base-synth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createNode, el, resolve } from '@elemaudio/core'
import { el, resolve } from '@elemaudio/core'

import { updateVoices } from '$lib/instruments'

import type { NodeRepr_t } from '@elemaudio/core'
import type { ElemNode } from '@elemaudio/core'
import type { Channels, Voice } from '$lib/instruments'


Expand Down Expand Up @@ -74,22 +74,13 @@ export class BaseSynth {
}

const baseSynth = (voices: Voice[], gainValue: number, panValue: number): Channels => {
const node = el.add(...voices.map(voice => {
return createNode(synthVoice, { voice }, [])
}))
const node = el.add(...voices.map(voice => synthVoice(voice)))
const gainOut = gain(node, gainValue)

return pan(gainOut, panValue)
}


type SynthVoiceProps = {
voice: Voice
}

const synthVoice = ({ props }): NodeRepr_t => {
const { voice } = props as SynthVoiceProps

const synthVoice = (voice: Voice): ElemNode => {
return resolve(
el.mul(
el.const({ key: `${voice.key}:gate`, value: voice.gate }),
Expand All @@ -113,7 +104,7 @@ const silence = (): Channels => {
* @param gainValue gain value between 0 and 1
* @returns node with gain applied
*/
const gain = (node: NodeRepr_t | number, gainValue: number): NodeRepr_t | number => {
const gain = (node: ElemNode, gainValue: number): ElemNode => {
return el.mul(
node,
el.div(
Expand All @@ -130,7 +121,7 @@ const gain = (node: NodeRepr_t | number, gainValue: number): NodeRepr_t | number
* @param panVal pan value between 0 and 1
* @returns node with pan applied
*/
const pan = (node: NodeRepr_t | number, panVal: number): Channels => {
const pan = (node: ElemNode, panVal: number): Channels => {
const left = el.mul(el.sm(el.const({ key: 'leftPanValue', value: 1 - panVal })), node)
const right = el.mul(el.sm(el.const({ key: 'rightPanValue', value: panVal })), node)

Expand Down
5 changes: 2 additions & 3 deletions src/lib/instruments/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NodeRepr_t } from '@elemaudio/core'
import type { ElemNode } from '@elemaudio/core'

import { tune } from '$lib/tuning'

Expand All @@ -7,11 +7,10 @@ export type Config = {
keyboardStatus: 'playing' | 'typing'
}

export type Channels = { left: NodeRepr_t | number; right: NodeRepr_t | number }
export type Channels = { left: ElemNode; right: ElemNode }

export type Voice = { gate: number; freq: number; key: string }


export const updateVoices = (voices: Voice[], midiNote: number): Voice[] => {
const key = `v${midiNote}`
const freq = tune(midiNote)
Expand Down
8 changes: 2 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"lib": ["es2020"],
"lib": ["es2020", "dom"],
"target": "es2019",
/**
svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
to enforce using \`import type\` instead of \`import\` for Types.
*/
"importsNotUsedAsValues": "error",
"noEmit": true,
"isolatedModules": true,
"resolveJsonModule": true,
/**
Expand Down

0 comments on commit 63ea352

Please sign in to comment.