-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
14 changed files
with
122 additions
and
4 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
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
Git LFS file not shown
Git LFS file not shown
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
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,7 @@ | ||
|
||
// This file is generated | ||
|
||
export const GeneratedMusicData = { | ||
"Test2": { ogg: require("./ogg/test2.ogg") }, | ||
"Test1": { ogg: require("./ogg/test1.ogg") } | ||
} |
Git LFS file not shown
Git LFS file not shown
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,8 @@ | ||
import { GeneratedMusicData } from "./generated/music"; | ||
|
||
type MusicId = keyof typeof GeneratedMusicData; | ||
|
||
export const Mzk: Record<MusicId, string> = <any>{}; | ||
|
||
for (const key in GeneratedMusicData) | ||
Mzk[key] = GeneratedMusicData[key].ogg; |
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,14 @@ | ||
/** | ||
@param {import("smooch/template-api").TemplateContext.AudioConvert} context; | ||
@param {import("smooch/template-api").Utils} utils; | ||
*/ | ||
module.exports = function ({ files }, { pascal, noext }) { | ||
return ` | ||
// This file is generated | ||
export const GeneratedMusicData = { | ||
${files.map(file => | ||
` "${pascal(noext(file.path))}": { ogg: require("./ogg/${file.convertedPaths.ogg}") }`).join(`, | ||
`)} | ||
}`; | ||
} |
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
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
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,51 @@ | ||
import { intervalWait } from "../../browser/interval-wait"; | ||
import { Sound, SoundInstance } from "./sound"; | ||
|
||
type Url = string; | ||
type MusicTrack = Url; | ||
|
||
// TODO generics? | ||
export class AsshatJukebox { | ||
private readonly _loader: MusicTrackLoader; | ||
private _current?: SoundInstance; | ||
|
||
constructor(private readonly _destination: AudioNode) { | ||
this._loader = new MusicTrackLoader(_destination); | ||
} | ||
|
||
play(track: MusicTrack) { | ||
setTimeout(() => this.playAsync(track)); | ||
} | ||
|
||
async playAsync(track: MusicTrack) { | ||
const sound = await this._loader.load(track); | ||
this._current?.stop(); | ||
this._current = sound.with.loop(true).playInstance(); | ||
} | ||
} | ||
|
||
class MusicTrackLoader { | ||
private readonly _loaded: Record<Url, Sound> = {}; | ||
private readonly _loading = new Set<Url>(); | ||
|
||
constructor(private readonly _destination: AudioNode) { | ||
|
||
} | ||
|
||
async load(track: Url) { | ||
if (this._loaded[track]) | ||
return this._loaded[track]; | ||
|
||
if (!this._loading.has(track)) { | ||
this._loading.add(track); | ||
const arrayBuffer = await fetch(track).then(x => x.arrayBuffer()); | ||
const audioBuffer = await this._destination.context.decodeAudioData(arrayBuffer); | ||
this._loading.delete(track); | ||
const sound = new Sound(audioBuffer, this._destination); | ||
return this._loaded[track] = sound; | ||
} | ||
|
||
await intervalWait(() => !!this._loaded[track]); | ||
return this._loaded[track]; | ||
} | ||
} |
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