Skip to content

Commit

Permalink
$mol_audio refactor, added sample and scheduled nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Dec 29, 2023
1 parent 1a1f913 commit 8f48ee3
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 83 deletions.
12 changes: 12 additions & 0 deletions audio/context/context.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace $ {
// const api = require('web-audio-api') as { AudioContext: new() => AudioContext }

// export const $mol_audio_context_node = new api.AudioContext()
export const $mol_audio_context_node = new Proxy({} as AudioContext, {
get() {
throw new Error('Not implemented')
}
})

$.$mol_audio_context = $mol_audio_context_node
}
3 changes: 3 additions & 0 deletions audio/context/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace $ {
export let $mol_audio_context = undefined as unknown as AudioContext
}
5 changes: 5 additions & 0 deletions audio/context/context.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace $ {
export const $mol_audio_context_web = new AudioContext()

$.$mol_audio_context = $mol_audio_context_web
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion audio/gain/gain.web.ts → audio/gain/gain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace $ {
export class $mol_audio_gain extends $mol_audio_node {

@ $mol_mem
node() { return $mol_audio_node.context().createGain() }
node() { return this.context().createGain() }

@ $mol_mem
gain( next = 1 ) { return next }
Expand Down
12 changes: 4 additions & 8 deletions audio/node/node.web.ts → audio/node/node.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
namespace $ {
export class $mol_audio_node extends $mol_object2 {

@ $mol_memo.method
static context() {
return new AudioContext
}
context() { return this.$.$mol_audio_context }

@ $mol_mem
node() { return $mol_audio_node.context().destination as AudioNode }
node() { return this.context().destination as AudioNode }

@ $mol_mem
input( next = [] as readonly $mol_audio_node[] ) { return next }

Expand Down Expand Up @@ -38,7 +34,7 @@ namespace $ {
return this.node()
}

time() { return $mol_audio_node.context().currentTime }
time() { return this.context().currentTime }

destructor() {

Expand Down
28 changes: 28 additions & 0 deletions audio/room/room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace $ {

/**
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_audio_demo
*/
export class $mol_audio_room extends $mol_audio_node {

duration_default() {
return 1000
}

duration() {
let duration = 0
for (const input of this.input_connected()) {
if (input instanceof $mol_audio_room) duration += input.duration()
if (input instanceof $mol_audio_sample) duration += input.duration()
}
return duration || this.duration_default()
}

@ $mol_action
play() {
this.output()
this.$.$mol_wait_timeout( this.duration() )
}

}
}
19 changes: 0 additions & 19 deletions audio/room/room.web.ts

This file was deleted.

29 changes: 29 additions & 0 deletions audio/sample/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace $ {
export class $mol_audio_sample extends $mol_audio_scheduled {
@ $mol_mem
override node() { return this.context().createBufferSource() }

duration() {
return this.audio_buffer().duration * 1000
}

buffer() {
return new ArrayBuffer(0)
}

@ $mol_mem
audio_buffer() {
return $mol_wire_sync(this.context()).decodeAudioData(this.buffer())
}

@ $mol_mem
override node_configured() {
const node = this.node()
node.buffer = this.audio_buffer()
node.onended = $mol_wire_async(() => this.active(false))

return node
}

}
}
53 changes: 53 additions & 0 deletions audio/scheduled/scheduled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace $ {
export class $mol_audio_scheduled extends $mol_audio_node {
@ $mol_mem
override node(): AudioScheduledSourceNode {
throw new Error('implement')
}

@ $mol_mem
node_configured() {
return this.node()
}

promise = $mol_promise<void>()

@ $mol_mem
wait() {
return this.promise
}

@ $mol_mem
active( next?: boolean ): boolean {

$mol_wire_solid()

const node = this.node_configured()

const prev = $mol_wire_probe( ()=> this.active() )
if( prev === next ) return next ?? false

if( next === true ) {
node.start()
} else if( prev === true ) {
node.stop()
this.promise.done()
this.promise = $mol_promise()
}

return next ?? false
}

override destructor() {
this.active( false )
super.destructor()
}

@ $mol_mem
override output() {
this.active( true )
return super.output()
}

}
}
34 changes: 34 additions & 0 deletions audio/vibe/vibe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace $ {

export type $mol_audio_vibe_shape =
| 'sine'
| 'square'
| 'sawtooth'
| 'triangle'
| 'custom'

/**
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_audio_demo_vibe
*/
export class $mol_audio_vibe extends $mol_audio_scheduled {

@ $mol_mem
override node() { return this.context().createOscillator() }

@ $mol_mem
freq( next = 440 ) { return next }

@ $mol_mem
shape( next: $mol_audio_vibe_shape = 'sine' ) { return next }

@ $mol_mem
override node_configured(): AudioScheduledSourceNode {
const node = this.node()
node.frequency.setValueAtTime( this.freq(), this.time() )
node.type = this.shape()

return node
}

}
}
55 changes: 0 additions & 55 deletions audio/vibe/vibe.web.ts

This file was deleted.

0 comments on commit 8f48ee3

Please sign in to comment.