Tidied up and added some comments.
This commit is contained in:
parent
605c44de79
commit
f101de695b
@ -1,3 +1,8 @@
|
||||
//This is adapted from https://github.com/Snack-X/node-ms-adpcm
|
||||
//I tried to find a fast decoder for MSADPCM in nodejs and came up short.
|
||||
//Maybe I didn't look hard enough.
|
||||
//With some work, this did the job well for me.
|
||||
|
||||
const ADAPTATION_TABLE = [
|
||||
230, 230, 230, 230, 307, 409, 512, 614,
|
||||
768, 614, 512, 409, 307, 230, 230, 230,
|
||||
|
24
package.json
Normal file
24
package.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "popntowav",
|
||||
"version": "1.0.0",
|
||||
"description": "Tool for rendering pop'n music IFS/chart files to 16-bit PCM wav.",
|
||||
"main": "popntowav.js",
|
||||
"dependencies": {
|
||||
"node-libsamplerate": "^1.0.0",
|
||||
"wav": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Gi-z/popntowav.git"
|
||||
},
|
||||
"author": "Giz",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/Gi-z/popntowav/issues"
|
||||
},
|
||||
"homepage": "https://github.com/Gi-z/popntowav#readme"
|
||||
}
|
40
popnchart.js
40
popnchart.js
@ -2,21 +2,22 @@ const fs = require("fs");
|
||||
|
||||
class PopnChart {
|
||||
|
||||
//offsetKeysounds indicates that any keysound index references
|
||||
//within the chart may need to be decremented by one to account
|
||||
//for the bgtrack not being at the start of the 2dx container.
|
||||
|
||||
constructor(filename, offsetKeysounds=false) {
|
||||
this.filename = filename;
|
||||
this.data = fs.readFileSync(filename);
|
||||
|
||||
let newFormat = false;
|
||||
if (this.data.readInt8(16) == 69) {
|
||||
newFormat = true;
|
||||
} else if (this.data.readInt8(12) == 69) {
|
||||
newFormat = false;
|
||||
} else {
|
||||
throw "Chart format not supported.";
|
||||
}
|
||||
//Check if the chart is newstyle or old>
|
||||
//Needed to know event lengths.
|
||||
let newFormat = this.checkFormat();
|
||||
|
||||
this.events = [];
|
||||
|
||||
//This loop reads through the entire file,
|
||||
//rather than ending on an endofsong event.
|
||||
let offset = 0;
|
||||
while (offset < this.data.length) {
|
||||
const eventOffset = this.data.readInt32LE(offset);
|
||||
@ -27,9 +28,15 @@ class PopnChart {
|
||||
let eventParam = 0;
|
||||
let eventValue = 0;
|
||||
|
||||
//In regular events, param and value are 1 byte.
|
||||
//However on keysound events, the first 4 bits
|
||||
//are used for the param, while the proceeding
|
||||
//12 bits are used for the value.
|
||||
let joined = this.data.slice(offset, offset+2);
|
||||
offset += 2;
|
||||
if (eventFlag === 2 || eventFlag === 7) {
|
||||
//Endianness needs flipped.
|
||||
//This is a terrible way of doing this, I think.
|
||||
joined.swap16();
|
||||
const hx = joined.toString("hex");
|
||||
|
||||
@ -40,6 +47,7 @@ class PopnChart {
|
||||
eventValue = joined.readUInt8(1);
|
||||
}
|
||||
|
||||
//Long note data isn't needed for GSTs, however it's here.
|
||||
if (newFormat) {
|
||||
const longNoteData = this.data.readInt32LE(offset);
|
||||
offset += 4;
|
||||
@ -69,25 +77,31 @@ class PopnChart {
|
||||
|
||||
switch (eventType) {
|
||||
case 1:
|
||||
//Playable note event.
|
||||
//This if is overzealous, just trying to stop BG tracks from being played twice.
|
||||
if (sampleColumns[param] != 0) {
|
||||
this.playEvents.push([offset, sampleColumns[param]]);
|
||||
}
|
||||
this.notecount += 1;
|
||||
break;
|
||||
case 2:
|
||||
//Sample change event.
|
||||
if (offsetKeysounds) {
|
||||
param -= 1;
|
||||
}
|
||||
sampleColumns[value] = param;
|
||||
break;
|
||||
case 3:
|
||||
//BG track start event.
|
||||
this.playEvents.push([offset, 0]);
|
||||
break;
|
||||
case 4:
|
||||
//BPM change event.
|
||||
this.bpm = param;
|
||||
this.bpmTransitions.push(param);
|
||||
break;
|
||||
case 7:
|
||||
//BG sample event.
|
||||
if (offsetKeysounds) {
|
||||
param -= 1;
|
||||
}
|
||||
@ -95,6 +109,16 @@ class PopnChart {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkFormat() {
|
||||
if (this.data.readInt8(16) == 69) {
|
||||
return true;
|
||||
} else if (this.data.readInt8(12) == 69) {
|
||||
return false;
|
||||
} else {
|
||||
throw "Chart format not supported.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PopnChart;
|
18
popntowav.js
18
popntowav.js
@ -40,8 +40,11 @@ const channels = 2;
|
||||
const samplingRate = 44100;
|
||||
//Because Int32.
|
||||
const bytes = 4;
|
||||
let lowestVolume = 100;
|
||||
|
||||
//After loading in all the keysounds, we need to find ones that
|
||||
//aren't 44.1KHz, since they'll mess everything up.
|
||||
//Best resampling option I could find was node-libsamplerate.
|
||||
//I'm sure other people have better suggestions.
|
||||
for (var i = 0; i<decodedKeysounds.length; i++) {
|
||||
let keysound = decodedKeysounds[i];
|
||||
if (keysound.samplingRate != samplingRate) {
|
||||
@ -58,7 +61,6 @@ for (var i = 0; i<decodedKeysounds.length; i++) {
|
||||
resample.write(keysound.data);
|
||||
keysound.data = Buffer.from(resample.read());
|
||||
}
|
||||
lowestVolume = keysound.volume < lowestVolume ? keysound.volume : lowestVolume;
|
||||
decodedKeysounds[i] = keysound;
|
||||
}
|
||||
|
||||
@ -82,8 +84,7 @@ for (const event of chart.playEvents) {
|
||||
//This is overcompensating to deal with overflow from digital summing.
|
||||
//Final Timestamp in milliseconds * sampling rate * 2 channels * 4 bytes.
|
||||
const finalBuffer = Buffer.alloc(buffSize);
|
||||
|
||||
chart.playEvents.forEach((event) => {
|
||||
for (const event of chart.playEvents) {
|
||||
const [offset, keysoundNo] = event;
|
||||
//Grabbing the relevant offset for the buffer.
|
||||
const convertedOffset = parseInt((offset*samplingRate)/1000)*channels*bytes;
|
||||
@ -100,12 +101,12 @@ chart.playEvents.forEach((event) => {
|
||||
finalBuffer.writeInt32LE(mixedBytes, convertedOffset+(i*2));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//We've got summed 16bit values, but they need normalising so we can hear them,
|
||||
//from a 32bit buffer.
|
||||
//We've got summed 16bit values, which means they won't fit into a 16bit buffer.
|
||||
//We also can't just shove them into a 32bit buffer, since they're 16bit scale.
|
||||
//Instead, we'll have to normalise them first using the peak observed volume.
|
||||
//2147483647 is just so I don't have to import a MAX_INT32 module.
|
||||
//We're normaslising against the highest volume seen.
|
||||
//After normalising, these values will be scaled correctly from 16bit to 32bit.
|
||||
const normaliseFactor = parseInt(2147483647/highestSample);
|
||||
for (var i = 0; i<finalBuffer.length; i += 4) {
|
||||
@ -117,5 +118,6 @@ for (var i = 0; i<finalBuffer.length; i += 4) {
|
||||
let filename = soundContainer.name;
|
||||
filename = filename.slice(0, filename.indexOf("\u0000"));
|
||||
|
||||
//I could manually generate a wav header, but I don't because I'm lazy.
|
||||
let writer = new wav.FileWriter("output\\"+outputFilename+".wav", {bitDepth: 32});
|
||||
writer.write(finalBuffer);
|
58
twodx.js
58
twodx.js
@ -1,6 +1,9 @@
|
||||
const fs = require("fs");
|
||||
|
||||
class Keysound {
|
||||
//This is adapted from https://github.com/mon/SDVX-Song-Extractor/blob/master/bm2dx.py.
|
||||
//I'm personally not very good at data exploration.
|
||||
|
||||
class Sample {
|
||||
constructor(data, offset, key_no) {
|
||||
const header = data.toString("ascii", offset, offset+4);
|
||||
offset += 4;
|
||||
@ -13,50 +16,71 @@ class Keysound {
|
||||
const size = data.readUInt32LE(offset);
|
||||
offset += 6;
|
||||
|
||||
//Previously the keysound number was stored here.
|
||||
//In popn files this doesn't appear to be the case.
|
||||
//So we assume keysounds are sequential in these files.
|
||||
this.key_no = key_no;
|
||||
|
||||
//These two bytes are set to 0000 on keysounds which are
|
||||
//used as background tracks. This keysound needs to be
|
||||
//identified as it should be at the start of the container.
|
||||
this.is_bg = data.toString("hex", offset, offset+2) == "0000";
|
||||
offset += 2;
|
||||
|
||||
//These values were for attenuation and loop point in SDVX 2dxs.
|
||||
//I have no clue how to make use of these.
|
||||
this.unk1 = data.readUInt16LE(offset);
|
||||
offset += 2;
|
||||
this.unk2 = data.readUInt16LE(offset);
|
||||
offset += 6;
|
||||
|
||||
|
||||
this.data = data.slice(offset, offset+size);
|
||||
}
|
||||
}
|
||||
|
||||
class Twodx {
|
||||
|
||||
constructor(path) {
|
||||
this.path = path;
|
||||
const data = fs.readFileSync(path);
|
||||
this.data = fs.readFileSync(path);
|
||||
|
||||
let offset = 0;
|
||||
this.offset = 0;
|
||||
|
||||
this.name = data.toString("ascii", 0, 16);
|
||||
offset += 16;
|
||||
this.header_len = data.readUInt32LE(offset);
|
||||
offset += 4;
|
||||
this.file_count = data.readUInt32LE(offset);
|
||||
offset += 52;
|
||||
this.name = this.data.toString("ascii", 0, 16);
|
||||
this.offset += 16;
|
||||
this.header_len = this.data.readUInt32LE(this.offset);
|
||||
this.offset += 4;
|
||||
this.file_count = this.data.readUInt32LE(this.offset);
|
||||
this.offset += 52;
|
||||
|
||||
this.keysounds = [];
|
||||
const offsets = this.generateOffsets();
|
||||
this.keysounds = this.generateSamples(offsets);
|
||||
}
|
||||
|
||||
let trackOffsets = [...Array(this.file_count).keys()].map((_) => {
|
||||
const ind = data.readUInt32LE(offset);
|
||||
offset += 4;
|
||||
generateOffsets() {
|
||||
return [...Array(this.file_count).keys()].map((_) => {
|
||||
const ind = this.data.readUInt32LE(this.offset);
|
||||
this.offset += 4;
|
||||
return ind;
|
||||
});
|
||||
}
|
||||
|
||||
for (let i = 0; i<trackOffsets.length; i++) {
|
||||
const keysound = new Keysound(data, trackOffsets[i]);
|
||||
generateSamples(offsets) {
|
||||
const keysounds = [];
|
||||
for (let i = 0; i<offsets.length; i++) {
|
||||
const keysound = new Sample(this.data, offsets[i]);
|
||||
if (keysound.is_bg) {
|
||||
//BG tracks are placed at the start of the list
|
||||
//as this makes it easier to deal with keysound
|
||||
//indices in chart files.
|
||||
this.late_bg = i != 0;
|
||||
this.keysounds.unshift(keysound);
|
||||
keysounds.unshift(keysound);
|
||||
} else {
|
||||
this.keysounds.push(keysound);
|
||||
keysounds.push(keysound);
|
||||
}
|
||||
}
|
||||
return keysounds;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user