From 1945903ca466284ea1394c35aa85ec6ba55ec56f Mon Sep 17 00:00:00 2001 From: Sergey Yuferev Date: Fri, 29 Dec 2023 19:54:48 +0300 Subject: [PATCH] $mol_audio_scheduled fixes 1 --- audio/context/context.node.ts | 13 ++++++------ audio/context/context.ts | 6 +++++- audio/context/context.web.ts | 8 +++++++- audio/gain/gain.ts | 4 ++-- .../scheduled.ts => instrument/instrument.ts} | 20 ++++++++++--------- audio/node/node.ts | 14 ++++++++----- audio/room/room.ts | 2 +- audio/sample/sample.ts | 12 +++++------ audio/vibe/vibe.ts | 8 ++++---- 9 files changed, 52 insertions(+), 35 deletions(-) rename audio/{scheduled/scheduled.ts => instrument/instrument.ts} (74%) diff --git a/audio/context/context.node.ts b/audio/context/context.node.ts index 8f7501917be..eed40341192 100644 --- a/audio/context/context.node.ts +++ b/audio/context/context.node.ts @@ -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 } diff --git a/audio/context/context.ts b/audio/context/context.ts index 625eccc49f9..e25bdc66292 100644 --- a/audio/context/context.ts +++ b/audio/context/context.ts @@ -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') + } + } } diff --git a/audio/context/context.web.ts b/audio/context/context.web.ts index fb9742f5bfa..98ce70c756a 100644 --- a/audio/context/context.web.ts +++ b/audio/context/context.web.ts @@ -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 } diff --git a/audio/gain/gain.ts b/audio/gain/gain.ts index 7db9f17c278..6e18e14a163 100644 --- a/audio/gain/gain.ts +++ b/audio/gain/gain.ts @@ -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() } diff --git a/audio/scheduled/scheduled.ts b/audio/instrument/instrument.ts similarity index 74% rename from audio/scheduled/scheduled.ts rename to audio/instrument/instrument.ts index 06a38d087a0..ddf96d01952 100644 --- a/audio/scheduled/scheduled.ts +++ b/audio/instrument/instrument.ts @@ -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() @@ -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 diff --git a/audio/node/node.ts b/audio/node/node.ts index 55ae9e2af94..56a7c38447f 100644 --- a/audio/node/node.ts +++ b/audio/node/node.ts @@ -1,9 +1,13 @@ 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 } @@ -11,7 +15,7 @@ namespace $ { @ $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() @@ -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 ) diff --git a/audio/room/room.ts b/audio/room/room.ts index 1a258518b6e..b9d45d2aead 100644 --- a/audio/room/room.ts +++ b/audio/room/room.ts @@ -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() } diff --git a/audio/sample/sample.ts b/audio/sample/sample.ts index 25e112adfde..e4288ac0245 100644 --- a/audio/sample/sample.ts +++ b/audio/sample/sample.ts @@ -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() { @@ -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 } diff --git a/audio/vibe/vibe.ts b/audio/vibe/vibe.ts index 22cb2f1f430..726bd7e543c 100644 --- a/audio/vibe/vibe.ts +++ b/audio/vibe/vibe.ts @@ -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 } @@ -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()