-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
227 lines (194 loc) · 6.3 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
import { join } from "path";
interface TypingSoundsPluginSettings {
muted: boolean;
volume: number;
}
const SOUND_RELATIVE_PATH = "sounds";
const AUDIO_PLAYER_COUNT = 20;
const AUDIO_PITCH_VARIATION = 0.05;
const VOLUME_STEP = 0.1;
const DEFAULT_SETTINGS: TypingSoundsPluginSettings = {
muted: false,
volume: 1.0,
};
function getPluginFilePath(plugin: Plugin, filename: string) {
return plugin.app.vault.adapter.getResourcePath(
join(plugin.app.vault.configDir, "plugins", `obsidian-${plugin.manifest.id}`, filename)
);
}
class SeedableRandomGenerator {
m_w: number;
m_z: number;
mask: number;
constructor() {
this.m_w = 123456789;
this.m_z = 987654321;
this.mask = 0xffffffff;
}
setSeed(seed: number) {
this.m_w = (123456789 + seed) & this.mask;
this.m_z = (987654321 - seed) & this.mask;
}
nextValue() {
this.m_z = (36969 * (this.m_z & 65535) + (this.m_z >> 16)) & this.mask;
this.m_w = (18000 * (this.m_w & 65535) + (this.m_w >> 16)) & this.mask;
let result = ((this.m_z << 16) + (this.m_w & 65535)) >>> 0;
result /= 4294967296;
return result;
}
}
const randomGenerator = new SeedableRandomGenerator();
function getRandomPlaybackRate(seed = 1.0) {
randomGenerator.setSeed(seed);
const random_playback_rate = 1.0 + AUDIO_PITCH_VARIATION * (2.0 * randomGenerator.nextValue() - 1.0);
return random_playback_rate;
}
class SoundPlayer {
available: HTMLAudioElement[];
constructor(audioFileName: string) {
this.available = [];
for (let i = 0; i < AUDIO_PLAYER_COUNT; ++i) {
const player = new Audio(audioFileName);
// @ts-expect-error: preservesPitch is not a standard property for player
player.preservesPitch = false;
this.available.push(player);
player.addEventListener("play", () => {
this.available.remove(player);
});
player.addEventListener("ended", () => {
this.available.push(player);
});
}
}
play(volume: number, varyPitch: boolean, seed = 0.0): void {
const player = this.available.pop();
if (player) {
player.volume = volume;
if (varyPitch) {
player.playbackRate = getRandomPlaybackRate(seed);
} else {
player.playbackRate = 1.0;
}
player.play();
}
}
}
export default class TypingSoundsPlugin extends Plugin {
settings: TypingSoundsPluginSettings;
keyPlayer: SoundPlayer;
spacePlayer: SoundPlayer;
enterPlayer: SoundPlayer;
async onload() {
this.addSettingTab(new TypingSoundsSettingTab(this.app, this));
this.keyPlayer = new SoundPlayer(getPluginFilePath(this, `${SOUND_RELATIVE_PATH}/key.wav`));
this.spacePlayer = new SoundPlayer(getPluginFilePath(this, `${SOUND_RELATIVE_PATH}/space.wav`));
this.enterPlayer = new SoundPlayer(getPluginFilePath(this, `${SOUND_RELATIVE_PATH}/enter.wav`));
this.addCommand({
id: "toggle-typing-sounds",
name: "Toggle typing sounds",
callback: async () => {
this.settings.muted = !this.settings.muted;
await this.saveSettings();
},
});
this.addCommand({
id: "mute-typing-sounds",
name: "Mute typing sounds",
callback: async () => {
this.settings.muted = true;
await this.saveSettings();
},
});
this.addCommand({
id: "unmute-typing-sounds",
name: "Unmute typing sounds",
callback: async () => {
this.settings.muted = false;
await this.saveSettings();
},
});
this.addCommand({
id: "volume-up-typing-sounds",
name: "Volume up for typing sounds",
callback: async () => {
this.settings.volume += VOLUME_STEP;
if (this.settings.volume > 1.0) this.settings.volume = 1.0;
await this.saveSettings();
this.keyPlayer.play(this.settings.volume, false);
},
});
this.addCommand({
id: "volume-down-typing-sounds",
name: "Volume down for typing sounds",
callback: async () => {
this.settings.volume -= VOLUME_STEP;
if (this.settings.volume < 0.0) this.settings.volume = 0.0;
await this.saveSettings();
this.keyPlayer.play(this.settings.volume, false);
},
});
this.registerDomEvent(document, "keydown", (event: KeyboardEvent) => {
if (this.settings.muted) {
return;
}
const isSuggestionContainerVisible = document.getElementsByClassName("suggestion-container").length > 0;
const isMarkdownEditor = this.app.workspace.activeEditor?.editor?.hasFocus() && !isSuggestionContainerVisible;
if (isMarkdownEditor) {
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
return;
}
if (event.code === "Enter") {
this.enterPlayer.play(this.settings.volume, false);
} else if (event.code === "Space" || event.code === "Backspace") {
this.spacePlayer.play(this.settings.volume, false);
} else {
const seed = parseInt(event.code.toUpperCase(), 36);
this.keyPlayer.play(this.settings.volume, true, seed);
}
}
});
await this.loadSettings();
}
async onunload() {
// ...
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class TypingSoundsSettingTab extends PluginSettingTab {
plugin: TypingSoundsPlugin;
constructor(app: App, plugin: TypingSoundsPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Typing sounds muted")
.setDesc("Mute typewriter sounds.")
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.muted).onChange(async (value) => {
this.plugin.settings.muted = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Volume")
.setDesc("Typewriter sounds volume.")
.addSlider((slider) =>
slider
.setLimits(0.0, 1.0, 0.05)
.setValue(this.plugin.settings.volume)
.onChange(async (value) => {
this.plugin.settings.volume = value;
await this.plugin.saveSettings();
})
);
}
}