Skip to content

Commit

Permalink
Merge pull request #726 from hyoo-ru/audio-fix
Browse files Browse the repository at this point in the history
$mol_audio_instrument error status, fail safe, idempotency fix in act…
  • Loading branch information
zerkalica authored Jan 15, 2025
2 parents d8640f6 + 2eb140e commit 5c8fd59
Show file tree
Hide file tree
Showing 4 changed files with 27 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
1 change: 1 addition & 0 deletions audio/status/status.view.tree
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$mol_audio_status $mol_view
icons *
closed <= Closed $mol_icon_power_sleep
error <= Error $mol_icon_alert
suspended <= Suspended $mol_icon_sleep
playing <= Playing $mol_icon_play
running <= Running $mol_icon_pause
Expand Down

0 comments on commit 5c8fd59

Please sign in to comment.