|
1 |
| -import { fetchResourceUrl } from '@/utils/tauri'; |
2 | 1 | import { ResetType } from 'solarxr-protocol';
|
| 2 | +import Xylophone from './xylophone'; |
3 | 3 |
|
4 |
| -const tapSetupSound1 = new Audio(await fetchResourceUrl('/sounds/first-tap.mp3')); |
5 |
| -const tapSetupSound2 = new Audio(await fetchResourceUrl('/sounds/second-tap.mp3')); |
6 |
| -const tapSetupSound3 = new Audio(await fetchResourceUrl('/sounds/third-tap.mp3')); |
7 |
| -const tapSetupSound4 = new Audio(await fetchResourceUrl('/sounds/fourth-tap.mp3')); |
8 |
| -const tapSetupSound5 = new Audio(await fetchResourceUrl('/sounds/fifth-tap.mp3')); |
9 |
| -const tapSetupSoundEnd = new Audio(await fetchResourceUrl('/sounds/end-tap.mp3')); |
10 |
| -const tapSetupExtraSound = new Audio( |
11 |
| - await fetchResourceUrl('/sounds/tapextrasetup.mp3') |
12 |
| -); |
| 4 | +const tones = [ |
| 5 | + ['C4', 'E4', 'G4'], |
| 6 | + ['E4', 'G4', 'C5'], |
| 7 | + ['G4', 'C5', 'E5'], |
| 8 | + ['C5', 'E5', 'G5'], |
| 9 | + ['E5', 'G5', 'C6'], |
| 10 | + ['G5', 'C6', 'E6'], |
| 11 | +]; |
13 | 12 |
|
14 |
| -function restartAndPlay(audio: HTMLAudioElement, volume: number) { |
15 |
| - audio.volume = Math.min(1, Math.pow(volume, Math.E) + 0.05); |
16 |
| - if (audio.paused) { |
17 |
| - audio.play(); |
18 |
| - } else { |
19 |
| - audio.currentTime = 0; |
20 |
| - } |
21 |
| -} |
| 13 | +const xylophone = new Xylophone(); |
22 | 14 |
|
23 |
| -export function playSoundOnResetEnded(resetType: ResetType, volume = 1) { |
| 15 | +export async function playSoundOnResetEnded(resetType: ResetType, volume = 1) { |
24 | 16 | switch (resetType) {
|
25 | 17 | case ResetType.Yaw: {
|
26 |
| - restartAndPlay(tapSetupSound2, volume); |
| 18 | + xylophone.play({ |
| 19 | + notes: ['C4'], |
| 20 | + offset: 0.15, |
| 21 | + type: 'square', |
| 22 | + volume, |
| 23 | + }); |
27 | 24 | break;
|
28 | 25 | }
|
29 | 26 | case ResetType.Full: {
|
30 |
| - restartAndPlay(tapSetupSound3, volume); |
| 27 | + xylophone.play({ |
| 28 | + notes: ['C4', 'E4'], |
| 29 | + offset: 0.15, |
| 30 | + type: 'square', |
| 31 | + volume, |
| 32 | + }); |
31 | 33 | break;
|
32 | 34 | }
|
33 | 35 | case ResetType.Mounting: {
|
34 |
| - restartAndPlay(tapSetupSound4, volume); |
| 36 | + xylophone.play({ |
| 37 | + notes: ['C4', 'E4', 'G4'], |
| 38 | + offset: 0.15, |
| 39 | + type: 'square', |
| 40 | + volume, |
| 41 | + }); |
35 | 42 | break;
|
36 | 43 | }
|
37 | 44 | }
|
38 | 45 | }
|
39 | 46 |
|
40 |
| -export function playSoundOnResetStarted(volume = 1) { |
41 |
| - restartAndPlay(tapSetupSound1, volume); |
| 47 | +export async function playSoundOnResetStarted(volume = 1) { |
| 48 | + await xylophone.play({ |
| 49 | + notes: ['A4'], |
| 50 | + offset: 0.4, |
| 51 | + type: 'square', |
| 52 | + volume, |
| 53 | + }); |
42 | 54 | }
|
43 | 55 |
|
44 |
| -let lastKnownVolume = 1; |
45 |
| -/* Easter egg */ |
46 |
| -tapSetupSoundEnd.onended = () => { |
47 |
| - if (Math.floor(Math.random() * 12000) !== 0) return; |
48 |
| - restartAndPlay(tapSetupExtraSound, lastKnownVolume); |
49 |
| -}; |
50 |
| - |
51 |
| -const order = [ |
52 |
| - tapSetupSound1, |
53 |
| - tapSetupSound2, |
54 |
| - tapSetupSound3, |
55 |
| - tapSetupSound4, |
56 |
| - tapSetupSound5, |
57 |
| - tapSetupSoundEnd, |
58 |
| - tapSetupSoundEnd, |
59 |
| - tapSetupSoundEnd, |
60 |
| -]; |
61 | 56 | let lastTap = 0;
|
62 |
| -export function playTapSetupSound(volume = 1) { |
63 |
| - lastKnownVolume = volume; |
64 |
| - restartAndPlay(order[lastTap++], volume); |
65 |
| - if (lastTap >= order.length) { |
| 57 | +export async function playTapSetupSound(volume = 1) { |
| 58 | + if (Math.floor(Math.random() * 12000) !== 0) { |
| 59 | + xylophone.play({ |
| 60 | + notes: tones[lastTap], |
| 61 | + offset: 0.15, |
| 62 | + type: 'square', |
| 63 | + volume, |
| 64 | + }); |
| 65 | + } else { |
| 66 | + xylophone.play({ |
| 67 | + notes: ['D4', 'E4', 'G4', 'E4', 'B4', 'B4', 'A4'], |
| 68 | + offset: 0.15, |
| 69 | + length: 1, |
| 70 | + type: 'sawtooth', |
| 71 | + volume, |
| 72 | + }); |
| 73 | + } |
| 74 | + lastTap++; |
| 75 | + if (lastTap >= tones.length) { |
66 | 76 | lastTap = 0;
|
67 | 77 | }
|
68 | 78 | }
|
0 commit comments