|
| 1 | +import { computed, Directive, effect, inject, input, model, untracked } from '@angular/core'; |
| 2 | +import { type ISequence, onChange, val } from '@theatre/core'; |
| 3 | +import { omit, pick } from 'angular-three'; |
| 4 | +import { mergeInputs } from 'ngxtension/inject-inputs'; |
| 5 | +import { TheatreProject } from './project'; |
| 6 | +import { TheatreSheet } from './sheet'; |
| 7 | + |
| 8 | +export interface AttachAudioOptions { |
| 9 | + /** |
| 10 | + * Either a URL to the audio file (eg "http://localhost:3000/audio.mp3") or an instance of AudioBuffer |
| 11 | + */ |
| 12 | + source: string | AudioBuffer; |
| 13 | + /** |
| 14 | + * An optional AudioContext. If not provided, one will be created. |
| 15 | + */ |
| 16 | + audioContext?: AudioContext; |
| 17 | + /** |
| 18 | + * An AudioNode to feed the audio into. Will use audioContext.destination if not provided. |
| 19 | + */ |
| 20 | + destinationNode?: AudioNode; |
| 21 | +} |
| 22 | + |
| 23 | +export type TheatreSequenceOptions = Parameters<ISequence['play']>[0] & { |
| 24 | + autoplay: boolean; |
| 25 | + autopause: boolean; |
| 26 | + delay: number; |
| 27 | + autoreset?: 'init' | 'destroy' | 'always'; |
| 28 | +}; |
| 29 | + |
| 30 | +const defaultOptions: TheatreSequenceOptions = { |
| 31 | + rate: 1, |
| 32 | + autoplay: false, |
| 33 | + autopause: false, |
| 34 | + delay: 0, |
| 35 | +}; |
| 36 | + |
| 37 | +@Directive({ selector: 'theatre-sheet[sequence]' }) |
| 38 | +export class TheatreSequence { |
| 39 | + options = input(defaultOptions, { alias: 'sequence', transform: mergeInputs(defaultOptions) }); |
| 40 | + audioOptions = input<AttachAudioOptions | undefined>(undefined, { alias: 'sequenceAudio' }); |
| 41 | + |
| 42 | + position = model<number>(0); |
| 43 | + playing = model<boolean>(false); |
| 44 | + length = model<number>(0); |
| 45 | + |
| 46 | + private playOptions = omit(this.options, ['autoplay', 'autopause', 'delay', 'autoreset']); |
| 47 | + private autoplay = pick(this.options, 'autoplay'); |
| 48 | + private autopause = pick(this.options, 'autopause'); |
| 49 | + private autoreset = pick(this.options, 'autoreset'); |
| 50 | + private delay = pick(this.options, 'delay'); |
| 51 | + |
| 52 | + private project = inject(TheatreProject); |
| 53 | + private sheet = inject(TheatreSheet, { host: true }); |
| 54 | + sequence = computed(() => this.sheet.sheet().sequence); |
| 55 | + |
| 56 | + constructor() { |
| 57 | + effect((onCleanup) => { |
| 58 | + const autoplay = untracked(this.autoplay); |
| 59 | + if (!autoplay) return; |
| 60 | + |
| 61 | + const delay = untracked(this.delay); |
| 62 | + const id = setTimeout(() => { |
| 63 | + untracked(() => this.play()); |
| 64 | + }, delay); |
| 65 | + |
| 66 | + onCleanup(() => { |
| 67 | + clearTimeout(id); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + effect((onCleanup) => { |
| 72 | + const autopause = untracked(this.autopause); |
| 73 | + onCleanup(() => { |
| 74 | + if (autopause) { |
| 75 | + this.pause(); |
| 76 | + } |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + effect((onCleanup) => { |
| 81 | + const autoreset = untracked(this.autoreset); |
| 82 | + if (autoreset === 'init' || autoreset === 'always') { |
| 83 | + untracked(() => this.reset()); |
| 84 | + } |
| 85 | + |
| 86 | + onCleanup(() => { |
| 87 | + if (autoreset === 'destroy' || autoreset === 'always') { |
| 88 | + untracked(() => this.reset()); |
| 89 | + } |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + effect(() => { |
| 94 | + const [audioOptions, sequence] = [this.audioOptions(), untracked(this.sequence)]; |
| 95 | + if (audioOptions) sequence.attachAudio(audioOptions); |
| 96 | + }); |
| 97 | + |
| 98 | + effect(() => { |
| 99 | + const [playOptions, sequence] = [this.playOptions(), untracked(this.sequence)]; |
| 100 | + const isPlaying = val(sequence.pointer.playing); |
| 101 | + if (isPlaying) { |
| 102 | + this.pause(); |
| 103 | + this.play(playOptions); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + effect((onCleanup) => { |
| 108 | + const sequence = this.sequence(); |
| 109 | + |
| 110 | + const cleanups: Array<() => void> = []; |
| 111 | + |
| 112 | + cleanups.push( |
| 113 | + onChange(sequence.pointer.position, (value) => this.position.set(value)), |
| 114 | + onChange(sequence.pointer.playing, (value) => this.playing.set(value)), |
| 115 | + onChange(sequence.pointer.length, (value) => this.length.set(value)), |
| 116 | + ); |
| 117 | + |
| 118 | + onCleanup(() => { |
| 119 | + cleanups.forEach((cleanup) => cleanup()); |
| 120 | + }); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + pause() { |
| 125 | + const sequence = this.sequence(); |
| 126 | + sequence.pause(); |
| 127 | + } |
| 128 | + |
| 129 | + play(options: Parameters<ISequence['play']>[0] = {}) { |
| 130 | + const sequence = this.sequence(); |
| 131 | + const project = this.project.project(); |
| 132 | + |
| 133 | + project.ready.then(() => { |
| 134 | + sequence.play({ ...this.playOptions(), ...options }); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + reset() { |
| 139 | + const sequence = this.sequence(); |
| 140 | + const isPlaying = val(sequence.pointer.playing); |
| 141 | + sequence.position = 0; |
| 142 | + if (isPlaying) this.play(); |
| 143 | + } |
| 144 | +} |
0 commit comments