Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: init bfstm (wip) #41

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules/
*.log
*.brstm
*.bfstm
node_modules
.DS_Store
dist
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"build-gh-pages": "vite build --base \"/nikku/\""
},
"dependencies": {
"bfstm": "^1.0.0",
"brstm": "^1.6.1",
"comlink": "^4.3.1",
"lit": "^2.4.0"
Expand Down
54 changes: 27 additions & 27 deletions app/src/audio-decoder/worker.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { Brstm, Metadata } from 'brstm';
import { transfer } from 'comlink';
import { Bfstm, Metadata } from 'bfstm';
// import { transfer } from 'comlink';

let brstm: Brstm | null = null;
let instance: Bfstm | null = null;
export function init(receivedBuffer: ArrayBuffer) {
brstm = new Brstm(receivedBuffer);
instance = new Bfstm(receivedBuffer);
}
export function destroy() {
brstm = null;
instance = null;
}
export function getMetadata(): Metadata | undefined {
if (!brstm) {
if (!instance) {
return;
}
return brstm.metadata;
return instance.metadata;
}

export function getAllSamples() {
if (!brstm) {
return;
}
const allSamples = brstm.getAllSamples();
return transfer(
allSamples,
allSamples.map((allSamplesPerChannel) => allSamplesPerChannel.buffer)
);
}
// export function getAllSamples() {
// if (!brstm) {
// return;
// }
// const allSamples = brstm.getAllSamples();
// return transfer(
// allSamples,
// allSamples.map((allSamplesPerChannel) => allSamplesPerChannel.buffer)
// );
// }

export function getSamples(offset: number, size: number) {
if (!brstm) {
return;
}
const allSamples = brstm.getSamples(offset, size).map(convertToFloat32);
return transfer(
allSamples,
allSamples.map((allSamplesPerChannel) => allSamplesPerChannel.buffer)
);
}
// export function getSamples(offset: number, size: number) {
// if (!brstm) {
// return;
// }
// const allSamples = brstm.getSamples(offset, size).map(convertToFloat32);
// return transfer(
// allSamples,
// allSamples.map((allSamplesPerChannel) => allSamplesPerChannel.buffer)
// );
// }


function convertToFloat32(pcmSamples: Int16Array): Float32Array {
Expand Down
4 changes: 3 additions & 1 deletion app/src/elements/nikku-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class NikkuMain extends LitElement {
<input
type="file"
id="controls-select-file"
accept=".brstm"
accept=".brstm,.bfstm"
@change=${this.#handleFileInputChange}
/>
<span id="controls-select-file-custom"></span>
Expand Down Expand Up @@ -210,6 +210,8 @@ export class NikkuMain extends LitElement {
try {
await this.workerInstance.init(transfer(buffer, [buffer]));
const metadata = await this.workerInstance.getMetadata();
console.log('metadata', metadata);
return;

if (this.audioPlayer) {
await this.audioPlayer.destroy();
Expand Down
3 changes: 3 additions & 0 deletions packages/bfstm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# BFSTM

https://mk8.tockdom.com/wiki/BFSTM_(File_Format)
55 changes: 55 additions & 0 deletions packages/bfstm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "bfstm",
"version": "1.0.0",
"description": "BFSTM Decoder",
"keywords": [
"bfstm"
],
"sideEffects": false,
"source": "src/index.ts",
"main": "./dist/bfstm.js",
"module": "./dist/bfstm.mjs",
"umd:main": "./dist/bfstm.umd.js",
"unpkg": "./dist/bfstm.umd.js",
"exports": {
".": {
"import": {
"nikku:source": "./src/index.ts",
"default": "./dist/bfstm.mjs"
},
"require": "./dist/bfstm.js"
}
},
"types": "./types/index.d.ts",
"files": [
"dist/",
"types/",
"src/"
],
"author": {
"name": "Kenrick",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/kenrick95/nikku.git",
"directory": "packages/bfstm"
},
"homepage": "https://github.com/kenrick95/nikku",
"bugs": {
"url": "https://github.com/kenrick95/nikku/issues"
},
"license": "MIT",
"scripts": {
"prepublishOnly": "pnpm run build",
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
"build": "pnpm run build-declaration && pnpm build-lib",
"build-declaration": "tsc",
"build-lib": "vite build"
},
"devDependencies": {
"@nikku/utils": "workspace:*",
"typescript": "^4.8.4",
"vite": "^3.1.8"
}
}
193 changes: 193 additions & 0 deletions packages/bfstm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import {
getSliceAsString,
getSliceAsNumber,
getInt16,
clamp,
getEndianness,
} from '@nikku/utils';
import type { Endianness } from '@nikku/utils';
import type {
ChannelInfo,
CodecType,
Metadata,
TrackDescription,
} from './types';

export * from './types';

declare var console: any;

/**
* @class
*/
export class Bfstm {
rawData: Uint8Array;
endianness: Endianness;
versionNumber: number;
metadata: Metadata;

#offsetToInfo: number;
#offsetToSeek: number;
#offsetToData: number;

constructor(arrayBuffer: ArrayBuffer) {
/**
* @type {Uint8Array} rawData
*/
this.rawData = new Uint8Array(arrayBuffer);

if (getSliceAsString(this.rawData, 0, 4) !== 'FSTM') {
throw new Error('Not a valid BFSTM file');
}

this.endianness = getEndianness(this.rawData);
this.versionNumber = getSliceAsNumber(
this.rawData,
0x08,
4,
this.endianness
);
this.#offsetToInfo = getSliceAsNumber(
this.rawData,
0x18,
4,
this.endianness
);
this.#offsetToSeek = getSliceAsNumber(
this.rawData,
0x24,
4,
this.endianness
);
this.#offsetToData = getSliceAsNumber(
this.rawData,
0x30,
4,
this.endianness
);

this.metadata = this.#getMetadata();
}

#getMetadata() {
const offsetToStreamInfo =
this.#offsetToInfo +
getSliceAsNumber(
this.rawData,
this.#offsetToInfo + 0x0c,
4,
this.endianness
) +
0x08;
const offsetToTrackInfo =
this.#offsetToInfo +
getSliceAsNumber(
this.rawData,
this.#offsetToInfo + 0x14,
4,
this.endianness
) +
0x08;
const offsetToChannelInfo =
this.#offsetToInfo +
getSliceAsNumber(
this.rawData,
this.#offsetToInfo + 0x1c,
4,
this.endianness
) +
0x08;

/**
* @type {Metadata}
*/
const metadata: Metadata = {

offsetToTrackInfo,
offsetToChannelInfo,

fileSize: getSliceAsNumber(this.rawData, 0x0c, 4, this.endianness),
endianness: this.endianness,
codec: getSliceAsNumber(
this.rawData,
offsetToStreamInfo,
1,
this.endianness
) as CodecType,
loopFlag: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x01,
1,
this.endianness
),
numberChannels: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x02,
1,
this.endianness
),
numberRegions: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x03,
1,
this.endianness
),
sampleRate: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x04,
4,
this.endianness
),
loopStartSample: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x08,
4,
this.endianness
),
totalSamples: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x0c,
4,
this.endianness
),
totalBlocks: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x10,
4,
this.endianness
),
blockSize: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x14,
4,
this.endianness
),
samplesPerBlock: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x18,
4,
this.endianness
),
finalBlockSize: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x1c,
4,
this.endianness
),
totalSamplesInFinalBlock: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x20,
4,
this.endianness
),
finalBlockSizeWithPadding: getSliceAsNumber(
this.rawData,
offsetToStreamInfo + 0x24,
4,
this.endianness
),
};

return metadata;
}
}
Loading