Skip to content

Commit

Permalink
$mol_audio_instrument error status, fail safe, idempotency fix in act…
Browse files Browse the repository at this point in the history
…ive api
  • Loading branch information
zerkalica committed Jan 15, 2025
1 parent d8640f6 commit 5b867eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
12 changes: 7 additions & 5 deletions audio/instrument/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,24 @@ namespace $ {

@ $mol_mem
override active(next?: boolean) {
this.node()
const start_at = this.start_at()
this.stop_at()

if (next) {
this.context().active(true)
this.start_at(0)
this.stop_at(-1)
return true
}

if (next === false) {
this.start_at(-1)
this.stop_at(0)
return false
}

this.node()
this.start_at()
this.stop_at()

return this.start_at() !== -1
return start_at !== -1
}

@ $mol_action
Expand Down
20 changes: 17 additions & 3 deletions audio/room/room.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace $ {

export type $mol_audio_room_status = AudioContextState | 'playing'
export type $mol_audio_room_status = AudioContextState | 'playing' | 'error'

/**
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_audio_demo
Expand Down Expand Up @@ -34,13 +34,27 @@ namespace $ {
return new this.$.$mol_after_timeout(time * 1000, () => $mol_wire_async(this).active(false))
}

@ $mol_mem
error() {
try {
this.output()
} catch (e) {
if (! $mol_promise_like(e)) return { value: e as Error }
}

return null
}

@ $mol_mem
status(next?: $mol_audio_room_status | null): $mol_audio_room_status {
const state = this.context().state(next === 'playing' ? 'running' : next)
if (next === 'playing') next = 'running'
if (next === 'error') next = 'closed'

const state = this.context().state(next)

if (state === 'closed') return state

this.output()
if ( this.error() ) return 'error'

if (this.inputs_active() && state === 'running') return 'playing'

Expand Down
4 changes: 2 additions & 2 deletions audio/sample/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ namespace $ {

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

const prev = super.active(next)
if (this.node_started()) {
if (next) this.context().active(true)
this.rate(next ? null : 0)

return next ?? false
}

return super.active(next)
return prev
}

@ $mol_mem
Expand Down

0 comments on commit 5b867eb

Please sign in to comment.