-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d3f1fc
commit 1f4b0c5
Showing
5 changed files
with
299 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
examples/sound-effects/video-to-sfx/app/state/orchestrator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { action, autorun, computed, makeObservable, observable } from "mobx"; | ||
import { AudioPlayer } from "./player"; | ||
|
||
export class Orchestrator { | ||
sfxPlayers: AudioPlayer[] = []; | ||
activeIndex = -1; | ||
caption: string; | ||
playing: boolean; | ||
|
||
constructor({ | ||
caption, | ||
soundEffects, | ||
}: { | ||
caption: string; | ||
soundEffects: string[]; | ||
}) { | ||
this.playing = false; | ||
this.caption = caption; | ||
this.sfxPlayers = soundEffects.map(data => new AudioPlayer(data)); | ||
makeObservable(this, { | ||
caption: observable.ref, | ||
sfxPlayers: observable.shallow, | ||
activeIndex: observable.ref, | ||
playing: observable.ref, | ||
activePlayer: computed, | ||
play: action, | ||
stop: action, | ||
}); | ||
autorun(() => { | ||
if (this.playing) { | ||
this.sfxPlayers.forEach((player, index) => { | ||
if (index === this.activeIndex) { | ||
player.start(0, Infinity); | ||
} else { | ||
player.stop(); | ||
} | ||
}); | ||
} else { | ||
this.sfxPlayers.forEach(player => { | ||
player.stop(); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
get activePlayer(): AudioPlayer | null { | ||
return this.sfxPlayers[this.activeIndex] || null; | ||
} | ||
|
||
play(index: number) { | ||
this.activeIndex = index; | ||
this.playing = true; | ||
} | ||
|
||
stop() { | ||
this.playing = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as Tone from "tone"; | ||
import { action, makeObservable, observable } from "mobx"; | ||
|
||
export class AudioPlayer { | ||
_player: Tone.Player; | ||
loading: boolean; | ||
|
||
constructor(data: string) { | ||
this.loading = true; | ||
this._player = new Tone.Player( | ||
data, | ||
action(() => { | ||
this.loading = false; | ||
}) | ||
).toDestination(); | ||
makeObservable(this, { | ||
loading: observable.ref, | ||
}); | ||
} | ||
|
||
start(offset: number, duration: number) { | ||
try { | ||
this._player.start(Tone.now(), offset, duration); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
stop() { | ||
if (this._player.state === "started") { | ||
try { | ||
this._player.stop(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.