Skip to content

Commit d8784af

Browse files
committed
structure cache, update libraries, add speaker to devdeps, etc.
1 parent 21aee8a commit d8784af

File tree

6 files changed

+219
-17
lines changed

6 files changed

+219
-17
lines changed

lib/cache.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,24 @@ export default class CacheManager {
2828
} catch (err) {}
2929
}
3030

31-
private getCachedSourceFilename(identifier: string) {
31+
private async getCachedSourceFilename(identifier: string) {
3232
const filename = createHash('sha256').update(identifier).digest('hex');
33-
return join(this.directory, SOURCES_CACHE_DIRECTORY, filename + '.json');
33+
const folder = join(this.directory, SOURCES_CACHE_DIRECTORY, filename.slice(0, 2), filename.slice(2, 4));
34+
35+
if (!existsSync(folder))
36+
await promises.mkdir(folder, { recursive: true });
37+
38+
return join(folder, filename + '.json');
3439
}
3540

36-
private getCachedSoundFilename(url: string) {
41+
private async getCachedSoundFilename(url: string) {
3742
const filename = createHash('sha256').update(url).digest('hex');
38-
return join(this.directory, SOUNDS_CACHE_DIRECTORY, filename + '.ogg');
43+
const folder = join(this.directory, SOUNDS_CACHE_DIRECTORY, filename.slice(0, 2), filename.slice(2, 4));
44+
45+
if (!existsSync(folder))
46+
await promises.mkdir(folder, { recursive: true });
47+
48+
return join(folder, filename + '.ogg');
3949
}
4050

4151
private get cachedDurationsFile() {
@@ -118,12 +128,10 @@ export default class CacheManager {
118128
});
119129
}
120130

121-
public async getSound(url: string) {
122-
await this.createNeededDirectories();
123-
124-
const path = this.getCachedSoundFilename(url);
131+
public async getSound(url: string, download = true) {
132+
const path = await this.getCachedSoundFilename(url);
125133

126-
if (!existsSync(path)) {
134+
if (!existsSync(path) && download) {
127135
const buffer = await this.convertToSuitableFormat(url);
128136
await promises.writeFile(path, buffer);
129137
}
@@ -134,7 +142,7 @@ export default class CacheManager {
134142
public async compareLocalList(hash: string, identifier: string) {
135143
await this.createNeededDirectories();
136144

137-
const path = this.getCachedSourceFilename(identifier);
145+
const path = await this.getCachedSourceFilename(identifier);
138146

139147
if (existsSync(path)) {
140148
try {
@@ -155,7 +163,7 @@ export default class CacheManager {
155163
) {
156164
await this.createNeededDirectories();
157165

158-
const path = this.getCachedSourceFilename(identifier);
166+
const path = await this.getCachedSourceFilename(identifier);
159167
await promises.writeFile(
160168
path,
161169
JSON.stringify({ sounds, hash } as CachedSource)

lib/context.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ export default class Context {
4141
if (!this.flattened || this.flattened.length === 0) return;
4242
let last: Chatsound | undefined;
4343

44+
const downloading: string[] = [];
4445
let paths = await Promise.all(
4546
this.flattened.map(scope => {
4647
const sound = this.chatsounds.getRequiredSound(scope, last);
4748
last = sound;
4849

4950
if (!sound) return;
5051

51-
// if parser found it, this should be able to too
52-
return this.chatsounds.cache.getSound((sound as Chatsound).url);
52+
const used = downloading.includes(sound.url);
53+
if (!used) downloading.push(sound.url);
54+
55+
return this.chatsounds.cache.getSound(sound.url, !used);
5356
})
5457
);
5558

package-lock.json

+139
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"devDependencies": {
2929
"@types/node": "^18.11.17",
30+
"speaker": "^0.5.5",
3031
"typescript": "^4.9.4"
3132
}
3233
}

test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ async function update() {
1616
async function run(data) {
1717
data = data.toString().trim();
1818

19-
const context = sh.new('stream', data);
19+
const context = sh.new(data);
2020
const speaker = new Speaker({
2121
channels: 2, // 2 channels
2222
bitDepth: 16, // 16-bit samples
23-
sampleRate: 44100, // 48,000 Hz sample rate
23+
sampleRate: 44100, // 44,100 Hz sample rate
2424
});
2525
speaker.on("end", () => speaker.close());
26-
console.log(context.scope.children[0].children)
2726

28-
const audio = await context.audio({
27+
const audio = await context.stream({
2928
sampleRate: 44100,
3029
audioChannels: 2,
3130
format: 's16le'
3231
});
32+
if (!audio) return console.log('no audio :(');
3333
audio.pipe(speaker);
3434
}
3535

yarn.lock

+51
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,57 @@
1212
resolved "https://registry.npmjs.org/@types/node/-/node-18.11.17.tgz"
1313
integrity sha512-HJSUJmni4BeDHhfzn6nF0sVmd1SMezP7/4F0Lq+aXzmp2xm9O7WXrUtHW/CHlYVtZUbByEvWidHqRtcJXGF2Ng==
1414

15+
bindings@^1.3.0:
16+
version "1.5.0"
17+
resolved "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz"
18+
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
19+
dependencies:
20+
file-uri-to-path "1.0.0"
21+
22+
buffer-alloc-unsafe@^1.1.0:
23+
version "1.1.0"
24+
resolved "https://registry.npmjs.org/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz"
25+
integrity sha512-TEM2iMIEQdJ2yjPJoSIsldnleVaAk1oW3DBVUykyOLsEsFmEc9kn+SFFPz+gl54KQNxlDnAwCXosOS9Okx2xAg==
26+
27+
buffer-alloc@^1.1.0:
28+
version "1.2.0"
29+
resolved "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz"
30+
integrity sha512-CFsHQgjtW1UChdXgbyJGtnm+O/uLQeZdtbDo8mfUgYXCHSM1wgrVxXm6bSyrUuErEb+4sYVGCzASBRot7zyrow==
31+
dependencies:
32+
buffer-alloc-unsafe "^1.1.0"
33+
buffer-fill "^1.0.0"
34+
35+
buffer-fill@^1.0.0:
36+
version "1.0.0"
37+
resolved "https://registry.npmjs.org/buffer-fill/-/buffer-fill-1.0.0.tgz"
38+
integrity sha512-T7zexNBwiiaCOGDg9xNX9PBmjrubblRkENuptryuI64URkXDFum9il/JGL8Lm8wYfAXpredVXXZz7eMHilimiQ==
39+
40+
debug@^4.0.0:
41+
version "4.3.7"
42+
resolved "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz"
43+
integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==
44+
dependencies:
45+
ms "^2.1.3"
46+
47+
48+
version "1.0.0"
49+
resolved "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz"
50+
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
51+
52+
ms@^2.1.3:
53+
version "2.1.3"
54+
resolved "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz"
55+
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
56+
57+
speaker@^0.5.5:
58+
version "0.5.5"
59+
resolved "https://registry.npmjs.org/speaker/-/speaker-0.5.5.tgz"
60+
integrity sha512-IBeMZUITigYBO139h0+1MAgBHNZF55GFJN4U/Box35Sg49cfqYkbCO92TXoCUy22Ast08zfqKuXLvPxq9CWwLw==
61+
dependencies:
62+
bindings "^1.3.0"
63+
buffer-alloc "^1.1.0"
64+
debug "^4.0.0"
65+
1566
typescript@^4.9.4:
1667
version "4.9.4"
1768
resolved "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz"

0 commit comments

Comments
 (0)