Skip to content

Commit

Permalink
Create basic autocorellation function in assemblyscript and configure…
Browse files Browse the repository at this point in the history
… scripts
  • Loading branch information
Odisseuss committed Jan 27, 2021
1 parent 745da9d commit 31746d7
Show file tree
Hide file tree
Showing 20 changed files with 7,147 additions and 129 deletions.
30 changes: 23 additions & 7 deletions asconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
{
"targets": {
"debug": {
"binaryFile": "public/untouched.wasm",
"textFile": "public/untouched.wat",
"yin-debug": {
"binaryFile": "public/yin-untouched.wasm",
"textFile": "public/yin-untouched.wat",
"sourceMap": true,
"debug": true
},
"release": {
"binaryFile": "public/optimized.wasm",
"textFile": "public/optimized.wat",
"tsdFile": "src/asTypes.ts",
"yin-release": {
"binaryFile": "public/yin-optimized.wasm",
"textFile": "public/yin-optimized.wat",
"tsdFile": "src/types/yin-asTypes.d.ts",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": true
},
"ac-debug": {
"binaryFile": "public/ac-untouched.wasm",
"textFile": "public/ac-untouched.wat",
"sourceMap": true,
"debug": true
},
"ac-release": {
"binaryFile": "public/ac-optimized.wasm",
"textFile": "public/ac-optimized.wat",
"tsdFile": "src/types/ac-asTypes.d.ts",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
Expand Down
60 changes: 60 additions & 0 deletions assembly/autocorellation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export const Float32AudioBuffer_ID = idof<Float32Array>();
export function autocorellation(
float32AudioBuffer: Float32Array,
sampleRate: i32
): f32 {
let rms: f32 = 0;
let size: i32 = float32AudioBuffer.length;
for (let index = 0; index < size; index++) {
const val: f32 = unchecked(float32AudioBuffer[index]);
rms += val * val;
}
rms = f32(Math.sqrt(rms / f32(size)));
if (rms < 0.01) return -1;

let r1: i32 = 0,
r2: i32 = size - 1,
thresh: f32 = 0.2;

for (let index = 0; index < size / 2; index++) {
if (Math.abs(unchecked(float32AudioBuffer[index])) < thresh) {
r1 = index;
break;
}
}
for (let index = 0; index < size / 2; index++) {
if (Math.abs(unchecked(float32AudioBuffer[size - 1])) < thresh) {
r2 = size - index;
break;
}
}
float32AudioBuffer = float32AudioBuffer.slice(r1, r2);
size = float32AudioBuffer.length;

let c = new Float32Array(size).fill(0);
for (let i = 0; i < size; i++) {
for (let j = 0; j < size - i; j++) {
c[i] =
unchecked(c[i]) +
unchecked(float32AudioBuffer[j]) * unchecked(float32AudioBuffer[j + i]);
}
}
let d: i32 = 0;
while (unchecked(c[d]) > unchecked(c[d + 1])) d++;
let maxval: f32 = -1,
maxpos: i32 = -1;
for (let index = d; index < size; index++) {
if (c[index] > maxval) {
maxval = unchecked(c[index]);
maxpos = index;
}
}
let t0: i32 = maxpos;
let x1: f32 = unchecked(c[t0 - 1]),
x2: f32 = unchecked(c[t0]),
x3: f32 = unchecked(c[t0 + 1]);
let a: f32 = (x1 + x3 - 2 * x2) / 2,
b: f32 = (x3 - x1) / 2;
if (a) t0 = i32(f32(t0) - b / (2 * a));
return f32(sampleRate / t0);
}
117 changes: 0 additions & 117 deletions assembly/index.ts

This file was deleted.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
"build": "react-app-rewired build",
"test": "jest --watchAll --detectOpenHandles --config=jest.config.js",
"eject": "react-scripts eject",
"asbuild:untouched": "asc assembly/yin.ts --target debug",
"asbuild:optimized": "asc assembly/yin.ts --target release",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"asbuild:yin-untouched": "asc assembly/yin.ts --target yin-debug",
"asbuild:yin-optimized": "asc assembly/yin.ts --target yin-release",
"asbuild:ac-untouched": "asc assembly/autocorellation.ts --target ac-debug",
"asbuild:ac-optimized": "asc assembly/autocorellation.ts --target ac-release",
"asbuild": "npm run asbuild:yin-untouched && npm run asbuild:yin-optimized && npm run asbuild:ac-untouched && npm run asbuild:ac-optimized",
"asbuild:watch": "onchange -i 'assembly/**/*' -- npm run asbuild"
},
"config-overrides-path": "react-app-rewired.config.js",
Expand Down
Binary file added public/ac-optimized.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions public/ac-optimized.wasm.map

Large diffs are not rendered by default.

Loading

0 comments on commit 31746d7

Please sign in to comment.