Skip to content

Commit

Permalink
build: remove next-pow-2 dependency (#21)
Browse files Browse the repository at this point in the history
This is such a small function that it's not worth having an external dependency.
  • Loading branch information
Ian Johnson authored Dec 27, 2021
1 parent 7662e7e commit d24a176
Show file tree
Hide file tree
Showing 8 changed files with 26,478 additions and 11,392 deletions.
13 changes: 1 addition & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
},
"homepage": "https://github.com/ianprime0509/pitchy#readme",
"dependencies": {
"fft.js": "^4.0.4",
"next-pow-2": "^1.0.0"
"fft.js": "^4.0.4"
},
"devDependencies": {
"@types/jest": "^27.0.3",
Expand Down
5 changes: 0 additions & 5 deletions src/@types/next-pow-2/index.d.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FFT from "fft.js";
import np2 from "next-pow-2";

import { ceilPow2 } from "./util";

/**
* One of the supported buffer types. Other numeric array types may not work
Expand Down Expand Up @@ -85,7 +86,7 @@ export class Autocorrelator<T extends Buffer> {
this._inputLength = inputLength;
// We need to double the input length to get correct results, and the FFT
// algorithm we use requires a length that's a power of 2
this._fft = new FFT(np2(2 * inputLength));
this._fft = new FFT(ceilPow2(2 * inputLength));
this._bufferSupplier = bufferSupplier;
this._paddedInputBuffer = this._bufferSupplier(this._fft.size);
this._transformBuffer = this._bufferSupplier(2 * this._fft.size);
Expand Down
11 changes: 11 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-env jest */
import { ceilPow2 } from "./util";

describe("ceilPow2", () => {
test("returns the next power of 2 at least as large as the input", () => {
expect(ceilPow2(1)).toBe(1);
expect(ceilPow2(2)).toBe(2);
expect(ceilPow2(5)).toBe(8);
expect(ceilPow2(255)).toBe(256);
});
});
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function ceilPow2(v: number): number {
// https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
3 changes: 1 addition & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ export default defineConfig({
},
sourcemap: true,
rollupOptions: {
external: ["fft.js", "next-pow-2"],
external: ["fft.js"],
output: {
globals: {
"fft.js": "fft",
"next-pow-2": "np2",
},
},
},
Expand Down
Loading

0 comments on commit d24a176

Please sign in to comment.