Coder Social home page Coder Social logo

abbijamal / nitro-fs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danielpxl/nitro-fs

0.0 0.0 0.0 151 KB

Library for reading the Nitro SDK Filesystem, used on most NDS games, and parsing files like textures and sound

License: GNU Lesser General Public License v3.0

JavaScript 0.30% TypeScript 99.70%

nitro-fs's Introduction

nitro-fs

About

nitro-fs is a work-in-progress TypeScript library for reading files on the Nintendo DS ROM Filesystem. It is also able to parse and convert some popular file formats used on the Nintendo DS (mainly SDAT as of right now).

Note that this library is still in development and things may change. If you find any bugs or have any suggestions, feel free to open an issue or pull request.

If you need help with something, feel free to ask me on Discord (DanielPXL#9770).

Supported Formats

  • SDAT (Sound Data)
    • SSEQ (Sound Sequence)
    • STRM (Sound Stream)
    • SWAR (Sound Wave Archive)
    • SBNK (Sound Bank)
    • SSAR (Sound Sequence Archive) support is planned
  • BTX (3D Model Texture)

Installation

There is no npm package yet. You can build the library yourself by cloning the repository and running npm run build. The output will be in the dist folder.

Usage

Initializing NitroFS

import { NitroFS } from "nitro-fs";

// File input element for selecting a ROM
const fileInput = document.getElementById("fileInput") as HTMLInputElement;

fileInput.addEventListener("change", async () => {
	// Initialize NitroFS with the ROM buffer
	const rom = await fileInput.files.item(0).arrayBuffer();
	const nitroFS = NitroFS.fromRom(rom);
});

Reading the ROM header

// Read the ROM header
const header = nitroFS.cartridgeHeader;
console.log(header.gameTitle);

Reading a file as an ArrayBuffer

// Read a file
const buffer = nitroFS.readFile("data/sound/sound_data.sdat");
// Do something with the buffer

Parsing an SDAT file

// Add imports at the top
import { Audio, BufferReader } from "nitro-fs";
// Read the file
const file = nitroFS.readFile("data/sound/sound_data.sdat");

// Convert ArrayBuffer to BufferReader (makes it easier to read the file)
const reader = BufferReader.new(file);

// Parse the SDAT file
const sdat = new Audio.SDAT(reader);

console.log(sdat);

Converting an SSEQ file to PCM

// Assuming you have already parsed the SDAT file
// Find the sequence file by name
const sequenceFile = sdat.fs.sequences.find(seq => seq.name === "SEQ_BA_AKAGI");
// Parse the sequence file
const sequence = new Audio.SSEQ(sequenceFile.buffer);

// Desired sample rate and number of seconds to render
const sampleRate = 48000;
const numSeconds = 10;

// Create a (stereo) buffer to store the audio data
const audioBuffer: Float32Array[] = new Array(2);
for (let i = 0; i < 2; i++) {
	// Create a buffer for each channel
	audioBuffer[i] = new Float32Array(sampleRate * numSeconds);
}

// Create a sink function to handle the audio data
let offset = 0;
let done = false;
const sink = (chunk: Float32Array[]) => {
	// If the offset is greater than the length of the buffer, we're done
	if (offset + chunk[0].length > chunk[0].length) {
		done = true;
		return;
	}

	// Copy the audio data into the buffer
	for (let i = 0; i < 2; i++) {
		audioBuffer[i].set(chunk[i], offset);
	}

	offset += chunk[0].length;
}

// Initialize the sequence renderer
const renderer = new Audio.SequenceRenderer(sequence, sequenceFile.fileInfo, sdat, sampleRate, sink);

// Render the sequence until it's done
while (!done) {
	renderer.tick();
}

// Do something with the audio buffer

Known Issues

  • The SSEQ command parser parses the "programming" commands (like variable, random, if, etc.) incorrectly. Not many games use these commands though, so far I have only found them in The Legend of Zelda: Spirit Tracks.
  • The SequenceRenderer only supports a small subset of the available commands (see here for more info). The most important ones are supported though, and many games already work fine.
  • Something is wrong with envelopes (decay and release phase is not really accurate, I think the other phases are fine though?).

Thanks to:

nitro-fs's People

Contributors

danielpxl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.