-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavdft.js
More file actions
executable file
·58 lines (49 loc) · 1.82 KB
/
wavdft.js
File metadata and controls
executable file
·58 lines (49 loc) · 1.82 KB
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
import { WaveFile } from "https://code4fukui.github.io/wavefile-es/index.js";
import { CBOR } from "https://js.sabae.cc/CBOR.js";
import { dft, idft, makeLen } from "./DFT.js";
import { Int16 } from "./Int16.js";
import { Float32 } from "./Float32.js";
const fn = "sekaideichiban.wav";
const dsec = 10;
const all = true;
const dftinjs = false;
const wav = new WaveFile();
wav.fromBuffer(await Deno.readFile(fn));
console.log(wav);
const data = wav.data.samples;
console.log(data.length);
const nlen = all ? data.length / 4 : Math.floor(dsec * wav.fmt.sampleRate);
console.log(nlen);
const right = new Int16Array(nlen);
const left = new Int16Array(nlen);
for (let i = 0; i < nlen; i++) {
const n = i * 4;
right[i] = data[n] + (data[n + 1] << 8);
left[i] = data[n + 2] + (data[n + 3] << 8);
}
const toArray = (ar) => {
const res = new Array(ar.length);
for (let i = 0; i < ar.length; i++) {
res[i] = ar[i];
}
return res;
};
//await Deno.writeTextFile(fn + "-r.json", JSON.stringify(right));
//await Deno.writeTextFile(fn + "-l.json", JSON.stringify(left));
//await Deno.writeFile(fn + "-r.cbor", CBOR.encode(toArray(right)));
//await Deno.writeFile(fn + "-l.cbor", CBOR.encode(toArray(left)));
await Deno.writeFile(fn + "-r.i16.bin", Int16.encode(right));
await Deno.writeFile(fn + "-l.i16.bin", Int16.encode(left));
if (!dftinjs) Deno.exit();
let freq;
{
const now = performance.now();
freq = dft(right);
const dt = performance.now() - now;
console.log("dft time: " + dt / 1000 + "sec"); // 44100samples time: 33.18sec
await Deno.writeFile(fn + "-re.f32.bin", Float32.encode(freq[0]));
await Deno.writeFile(fn + "-im.f32.bin", Float32.encode(freq[1]));
const power = makeLen(freq[0], freq[1]);
//await Deno.writeFile(fn + "-p.cbor", CBOR.encode(power));
await Deno.writeFile(fn + "-p.f32.bin", Float32.encode(power));
}