Skip to content

Commit

Permalink
$mol_audio_scheduled fixes 1
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Dec 29, 2023
1 parent b5dbf19 commit 1945903
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 35 deletions.
13 changes: 7 additions & 6 deletions audio/context/context.node.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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')
export class $mol_audio_context_node extends $mol_audio_context {

@ $mol_memo.method
static override context() {
const AudioContext = this.$.$mol_dom_context.AudioContext || this.$.$node['web-audio-api'].AudioContext
return new AudioContext()
}
})
}

$.$mol_audio_context = $mol_audio_context_node
}
6 changes: 5 additions & 1 deletion audio/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
namespace $ {
export let $mol_audio_context = undefined as unknown as AudioContext
export class $mol_audio_context extends $mol_object2 {
static context(): AudioContext {
throw new Error('implement')
}
}
}
8 changes: 7 additions & 1 deletion audio/context/context.web.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
namespace $ {
export const $mol_audio_context_web = new AudioContext()
export class $mol_audio_context_web extends $mol_audio_context {

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

$.$mol_audio_context = $mol_audio_context_web
}
4 changes: 2 additions & 2 deletions audio/gain/gain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace $ {
export class $mol_audio_gain extends $mol_audio_node {

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

@ $mol_mem
gain( next = 1 ) { return next }

@ $mol_mem
output() {
this.node().gain.setValueAtTime( this.gain(), this.time() )
this.node_raw().gain.setValueAtTime( this.gain(), this.time() )
return super.output()
}

Expand Down
20 changes: 11 additions & 9 deletions audio/scheduled/scheduled.ts → audio/instrument/instrument.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
namespace $ {
export class $mol_audio_scheduled extends $mol_audio_node {
@ $mol_mem
override node(): AudioScheduledSourceNode {
export class $mol_audio_instrument extends $mol_audio_node {
override node_raw(): AudioScheduledSourceNode {
throw new Error('implement')
}

duration() {
return 1000
override node() {
return this.node_raw()
}

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

promise = $mol_promise<void>()
Expand All @@ -21,12 +19,16 @@ namespace $ {
return this.promise
}

end() {
this.active( false )
}

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

$mol_wire_solid()

const node = this.node_configured()
const node = this.node()

const prev = $mol_wire_probe( ()=> this.active() )
if( prev === next ) return next ?? false
Expand Down
14 changes: 9 additions & 5 deletions audio/node/node.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
namespace $ {
export class $mol_audio_node extends $mol_object2 {
context() { return this.$.$mol_audio_context }
context() { return this.$.$mol_audio_context.context() }

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

node() {
return this.node_raw()
}

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

@ $mol_mem
input_connected() {

const node = this.node()
const node = this.node_raw()

const prev = $mol_mem_cached( ()=> this.input_connected() ) ?? []
const next = this.input()
Expand All @@ -31,14 +35,14 @@ namespace $ {
@ $mol_mem
output() {
this.input_connected()
return this.node()
return this.node_raw()
}

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

destructor() {

const node = this.node()
const node = this.node_raw()

for( const src of this.input() ) {
src.output().disconnect( node )
Expand Down
2 changes: 1 addition & 1 deletion audio/room/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace $ {
let duration = 0
for (const input of this.input_connected()) {
if (input instanceof $mol_audio_room) duration += input.duration()
if (input instanceof $mol_audio_scheduled) duration += input.duration()
if (input instanceof $mol_audio_instrument) duration += input.duration()
}
return duration || this.duration_default()
}
Expand Down
12 changes: 6 additions & 6 deletions audio/sample/sample.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace $ {
export class $mol_audio_sample extends $mol_audio_scheduled {
export class $mol_audio_sample extends $mol_audio_instrument {
@ $mol_mem
override node() { return this.context().createBufferSource() }
override node_raw() { return this.context().createBufferSource() }

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

buffer() {
Expand All @@ -17,10 +17,10 @@ namespace $ {
}

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

return node
}
Expand Down
8 changes: 4 additions & 4 deletions audio/vibe/vibe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace $ {
/**
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_audio_demo_vibe
*/
export class $mol_audio_vibe extends $mol_audio_scheduled {
export class $mol_audio_vibe extends $mol_audio_instrument {

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

@ $mol_mem
freq( next = 440 ) { return next }
Expand All @@ -22,8 +22,8 @@ namespace $ {
shape( next: $mol_audio_vibe_shape = 'sine' ) { return next }

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

Expand Down

0 comments on commit 1945903

Please sign in to comment.