diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2024-01-06 20:15:28 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2024-01-06 20:15:28 +0900 |
| commit | 746367004e99ca9c5da3de03a2bbcc545cf0c986 (patch) | |
| tree | 053c7e3f6908fe9e4a98de8b0a18c5eebbbe4ab1 /packages/frontend/src/scripts/sound.ts | |
| parent | feat: Add support for mCaptcha (#12905) (diff) | |
| download | sharkey-746367004e99ca9c5da3de03a2bbcc545cf0c986.tar.gz sharkey-746367004e99ca9c5da3de03a2bbcc545cf0c986.tar.bz2 sharkey-746367004e99ca9c5da3de03a2bbcc545cf0c986.zip | |
feat(frontend): add new game
Diffstat (limited to 'packages/frontend/src/scripts/sound.ts')
| -rw-r--r-- | packages/frontend/src/scripts/sound.ts | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/packages/frontend/src/scripts/sound.ts b/packages/frontend/src/scripts/sound.ts index 0b966ff199..acde78f5fd 100644 --- a/packages/frontend/src/scripts/sound.ts +++ b/packages/frontend/src/scripts/sound.ts @@ -92,7 +92,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_' && !soundStore.fileUrl)) { @@ -179,18 +185,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; } |