mirror of
				https://github.com/monkeytypegame/monkeytype.git
				synced 2025-10-31 11:16:08 +08:00 
			
		
		
		
	moved sound into a module
This commit is contained in:
		
							parent
							
								
									3c5f0f646d
								
							
						
					
					
						commit
						c3f2aa0ff4
					
				
					 7 changed files with 156 additions and 156 deletions
				
			
		|  | @ -95,6 +95,7 @@ const refactoredSrc = [ | |||
|   "./src/js/result-filters.js", | ||||
|   "./src/js/notification-center.js", | ||||
|   "./src/js/leaderboards.js", | ||||
|   "./src/js/sound.js", | ||||
| ]; | ||||
| 
 | ||||
| //legacy files
 | ||||
|  |  | |||
|  | @ -828,7 +828,7 @@ let commandsSoundOnClick = { | |||
|       display: "1", | ||||
|       exec: () => { | ||||
|         setPlaySoundOnClick("1"); | ||||
|         playClickSound(); | ||||
|         Sound.playClick(config.playSoundOnClick); | ||||
|       }, | ||||
|     }, | ||||
|     { | ||||
|  | @ -836,7 +836,7 @@ let commandsSoundOnClick = { | |||
|       display: "2", | ||||
|       exec: () => { | ||||
|         setPlaySoundOnClick("2"); | ||||
|         playClickSound(); | ||||
|         Sound.playClick(config.playSoundOnClick); | ||||
|       }, | ||||
|     }, | ||||
|     { | ||||
|  | @ -844,7 +844,7 @@ let commandsSoundOnClick = { | |||
|       display: "3", | ||||
|       exec: () => { | ||||
|         setPlaySoundOnClick("3"); | ||||
|         playClickSound(); | ||||
|         Sound.playClick(config.playSoundOnClick); | ||||
|       }, | ||||
|     }, | ||||
|     { | ||||
|  | @ -852,7 +852,7 @@ let commandsSoundOnClick = { | |||
|       display: "4", | ||||
|       exec: () => { | ||||
|         setPlaySoundOnClick("4"); | ||||
|         playClickSound(); | ||||
|         Sound.playClick(config.playSoundOnClick); | ||||
|       }, | ||||
|     }, | ||||
|   ], | ||||
|  |  | |||
|  | @ -28,3 +28,4 @@ import * as Monkey from "./monkey"; | |||
| import * as Notifications from "./notification-center"; | ||||
| import * as ResultFilters from "./result-filters"; | ||||
| import * as Leaderboards from "./leaderboards"; | ||||
| import * as Sound from "./sound"; | ||||
|  |  | |||
							
								
								
									
										157
									
								
								src/js/script.js
									
										
									
									
									
								
							
							
						
						
									
										157
									
								
								src/js/script.js
									
										
									
									
									
								
							|  | @ -79,129 +79,8 @@ let keypressStats = { | |||
|   }, | ||||
| }; | ||||
| 
 | ||||
| let errorSound = new Audio("../sound/error.wav"); | ||||
| let clickSounds = null; | ||||
| 
 | ||||
| let isPreviewingTheme = false; | ||||
| 
 | ||||
| function initClickSounds() { | ||||
|   clickSounds = { | ||||
|     1: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click1/click1_1.wav"), | ||||
|           new Audio("../sound/click1/click1_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click1/click1_2.wav"), | ||||
|           new Audio("../sound/click1/click1_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click1/click1_3.wav"), | ||||
|           new Audio("../sound/click1/click1_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|     2: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click2/click2_1.wav"), | ||||
|           new Audio("../sound/click2/click2_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click2/click2_2.wav"), | ||||
|           new Audio("../sound/click2/click2_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click2/click2_3.wav"), | ||||
|           new Audio("../sound/click2/click2_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|     3: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click3/click3_1.wav"), | ||||
|           new Audio("../sound/click3/click3_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click3/click3_2.wav"), | ||||
|           new Audio("../sound/click3/click3_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click3/click3_3.wav"), | ||||
|           new Audio("../sound/click3/click3_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|     4: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_1.wav"), | ||||
|           new Audio("../sound/click4/click4_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_2.wav"), | ||||
|           new Audio("../sound/click4/click4_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_3.wav"), | ||||
|           new Audio("../sound/click4/click4_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_4.wav"), | ||||
|           new Audio("../sound/click4/click4_4.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_5.wav"), | ||||
|           new Audio("../sound/click4/click4_5.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_6.wav"), | ||||
|           new Audio("../sound/click4/click4_6.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|   }; | ||||
| } | ||||
| 
 | ||||
| let customText = { | ||||
|   text: "The quick brown fox jumps over the lazy dog".split(" "), | ||||
|   isWordRandom: false, | ||||
|  | @ -4324,26 +4203,6 @@ function hideCustomMode2Popup() { | |||
|   } | ||||
| } | ||||
| 
 | ||||
| function playClickSound() { | ||||
|   if (config.playSoundOnClick === "off") return; | ||||
|   if (clickSounds === null) initClickSounds(); | ||||
| 
 | ||||
|   let rand = Math.floor( | ||||
|     Math.random() * clickSounds[config.playSoundOnClick].length | ||||
|   ); | ||||
|   let randomSound = clickSounds[config.playSoundOnClick][rand]; | ||||
|   randomSound.counter++; | ||||
|   if (randomSound.counter === 2) randomSound.counter = 0; | ||||
|   randomSound.sounds[randomSound.counter].currentTime = 0; | ||||
|   randomSound.sounds[randomSound.counter].play(); | ||||
| } | ||||
| 
 | ||||
| function playErrorSound() { | ||||
|   if (!config.playSoundOnError) return; | ||||
|   errorSound.currentTime = 0; | ||||
|   errorSound.play(); | ||||
| } | ||||
| 
 | ||||
| async function initPaceCaret() { | ||||
|   let mode2 = ""; | ||||
|   if (config.mode === "time") { | ||||
|  | @ -5328,7 +5187,7 @@ function handleBackspace(event) { | |||
|     } | ||||
|     updateWordElement(!config.blindMode); | ||||
|   } | ||||
|   playClickSound(); | ||||
|   Sound.playClick(config.playSoundOnClick); | ||||
|   if (config.keymapMode === "react") { | ||||
|     flashPressedKeymapKey(event.code, true); | ||||
|   } else if (config.keymapMode === "next") { | ||||
|  | @ -5385,7 +5244,7 @@ function handleSpace(event, isEnter) { | |||
|     currentKeypress.count++; | ||||
|     currentKeypress.words.push(currentWordIndex); | ||||
|     if (activeFunBox !== "nospace") { | ||||
|       playClickSound(); | ||||
|       Sound.playClick(config.playSoundOnClick); | ||||
|     } | ||||
|   } else { | ||||
|     //incorrect word
 | ||||
|  | @ -5399,9 +5258,9 @@ function handleSpace(event, isEnter) { | |||
|     } | ||||
|     if (activeFunBox !== "nospace") { | ||||
|       if (!config.playSoundOnError || config.blindMode) { | ||||
|         playClickSound(); | ||||
|         Sound.playClick(config.playSoundOnClick); | ||||
|       } else { | ||||
|         playErrorSound(); | ||||
|         Sound.playError(config.playSoundOnError); | ||||
|       } | ||||
|     } | ||||
|     accuracyStats.incorrect++; | ||||
|  | @ -5613,7 +5472,7 @@ function handleAlpha(event) { | |||
| 
 | ||||
|   //show dead keys
 | ||||
|   if (event.key === "Dead") { | ||||
|     playClickSound(); | ||||
|     Sound.playClick(config.playSoundOnClick); | ||||
|     $( | ||||
|       document.querySelector("#words .word.active").querySelectorAll("letter")[ | ||||
|         currentInput.length | ||||
|  | @ -5703,12 +5562,12 @@ function handleAlpha(event) { | |||
|   } | ||||
| 
 | ||||
|   if (thisCharCorrect) { | ||||
|     playClickSound(); | ||||
|     Sound.playClick(config.playSoundOnClick); | ||||
|   } else { | ||||
|     if (!config.playSoundOnError || config.blindMode) { | ||||
|       playClickSound(); | ||||
|       Sound.playClick(config.playSoundOnClick); | ||||
|     } else { | ||||
|       playErrorSound(); | ||||
|       Sound.playError(config.playSoundOnError); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|  |  | |||
|  | @ -201,7 +201,8 @@ settingsGroups.playSoundOnClick = new SettingsGroup( | |||
|   "playSoundOnClick", | ||||
|   setPlaySoundOnClick, | ||||
|   () => { | ||||
|     if (config.playSoundOnClick !== "off") playClickSound(); | ||||
|     if (config.playSoundOnClick !== "off") | ||||
|       Sound.playClick(config.playSoundOnClick); | ||||
|   } | ||||
| ); | ||||
| settingsGroups.showAllLines = new SettingsGroup( | ||||
|  |  | |||
							
								
								
									
										139
									
								
								src/js/sound.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										139
									
								
								src/js/sound.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,139 @@ | |||
| let errorSound = new Audio("../sound/error.wav"); | ||||
| let clickSounds = null; | ||||
| 
 | ||||
| export function init() { | ||||
|   if (clickSounds !== null) return; | ||||
|   clickSounds = { | ||||
|     1: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click1/click1_1.wav"), | ||||
|           new Audio("../sound/click1/click1_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click1/click1_2.wav"), | ||||
|           new Audio("../sound/click1/click1_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click1/click1_3.wav"), | ||||
|           new Audio("../sound/click1/click1_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|     2: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click2/click2_1.wav"), | ||||
|           new Audio("../sound/click2/click2_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click2/click2_2.wav"), | ||||
|           new Audio("../sound/click2/click2_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click2/click2_3.wav"), | ||||
|           new Audio("../sound/click2/click2_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|     3: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click3/click3_1.wav"), | ||||
|           new Audio("../sound/click3/click3_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click3/click3_2.wav"), | ||||
|           new Audio("../sound/click3/click3_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click3/click3_3.wav"), | ||||
|           new Audio("../sound/click3/click3_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|     4: [ | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_1.wav"), | ||||
|           new Audio("../sound/click4/click4_1.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_2.wav"), | ||||
|           new Audio("../sound/click4/click4_2.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_3.wav"), | ||||
|           new Audio("../sound/click4/click4_3.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_4.wav"), | ||||
|           new Audio("../sound/click4/click4_4.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_5.wav"), | ||||
|           new Audio("../sound/click4/click4_5.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|       { | ||||
|         sounds: [ | ||||
|           new Audio("../sound/click4/click4_6.wav"), | ||||
|           new Audio("../sound/click4/click4_6.wav"), | ||||
|         ], | ||||
|         counter: 0, | ||||
|       }, | ||||
|     ], | ||||
|   }; | ||||
| } | ||||
| 
 | ||||
| export function playClick(config) { | ||||
|   if (config === "off") return; | ||||
|   if (clickSounds === null) init(); | ||||
| 
 | ||||
|   let rand = Math.floor(Math.random() * clickSounds[config].length); | ||||
|   let randomSound = clickSounds[config][rand]; | ||||
|   randomSound.counter++; | ||||
|   if (randomSound.counter === 2) randomSound.counter = 0; | ||||
|   randomSound.sounds[randomSound.counter].currentTime = 0; | ||||
|   randomSound.sounds[randomSound.counter].play(); | ||||
| } | ||||
| 
 | ||||
| export function playError(config) { | ||||
|   if (!config) return; | ||||
|   errorSound.currentTime = 0; | ||||
|   errorSound.play(); | ||||
| } | ||||
|  | @ -205,8 +205,7 @@ function setPlaySoundOnClick(val, nosave) { | |||
|     val = "off"; | ||||
|   } | ||||
|   config.playSoundOnClick = val; | ||||
|   if (clickSounds === null && config.playSoundOnClick !== "off") | ||||
|     initClickSounds(); | ||||
|   if (config.playSoundOnClick !== "off") Sound.init(); | ||||
|   if (!nosave) saveConfigToCookie(); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue