feat(frontend): add BubbleGame

Cherry-picked from 746367004e
Cherry-picked from 0815a5235d
Cherry-picked from 4ea030d669
Cherry-picked from 00e195f50b
Cherry-picked from c6a4caa8be

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
This commit is contained in:
syuilo 2024-01-07 14:32:57 +09:00 committed by まっちゃとーにゅ
parent 078cfbcd19
commit 73ee6b455a
No known key found for this signature in database
GPG key ID: 6AFBBF529601C1DB
31 changed files with 1094 additions and 4 deletions

View file

@ -154,7 +154,13 @@ export type OperationType = typeof operationTypes[number];
* @param soundStore
* @param options `useCache`: `true`
*/
export async function loadAudio(soundStore: SoundStore, options?: { useCache?: boolean; }) {
export async function loadAudio(soundStore: {
type: Exclude<SoundType, '_driveFile_'>;
} | {
type: '_driveFile_';
fileId: string;
fileUrl: string;
}, options?: { useCache?: boolean; }) {
if (_DEV_) console.log('loading audio. opts:', options);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (soundStore.type === null || (soundStore.type === '_driveFile_' && (!$i?.policies.canUseDriveFileInSoundSettings || !soundStore.fileUrl))) {
@ -241,18 +247,31 @@ export async function playFile(soundStore: SoundStore) {
createSourceNode(buffer, soundStore.volume)?.start();
}
export function createSourceNode(buffer: AudioBuffer, volume: number) : AudioBufferSourceNode | null {
export async function playRaw(type: Exclude<SoundType, '_driveFile_'>, volume = 1, pan = 0, playbackRate = 1) {
const buffer = await loadAudio({ type });
if (!buffer) return;
createSourceNode(buffer, volume, pan, playbackRate)?.start();
}
export function createSourceNode(buffer: AudioBuffer, volume: number, pan = 0, playbackRate = 1) : AudioBufferSourceNode | null {
const masterVolume = defaultStore.state.sound_masterVolume;
if (isMute() || masterVolume === 0 || volume === 0) {
return null;
}
const panNode = ctx.createStereoPanner();
panNode.pan.value = pan;
const gainNode = ctx.createGain();
gainNode.gain.value = masterVolume * volume;
const soundSource = ctx.createBufferSource();
soundSource.buffer = buffer;
soundSource.connect(gainNode).connect(ctx.destination);
soundSource.playbackRate.value = playbackRate;
soundSource
.connect(panNode)
.connect(gainNode)
.connect(ctx.destination);
return soundSource;
}