File size: 6,449 Bytes
b0bfea8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
import { combineArrays, IndexedByteArray } from "../../../utils/indexed_array.js";
import { RiffChunk, writeRIFFChunk } from "../riff_chunk.js";
import { writeStringAsBytes } from "../../../utils/byte_functions/string.js";
import { consoleColors } from "../../../utils/other.js";
import { getIGEN } from "./igen.js";
import { getSDTA } from "./sdta.js";
import { getSHDR } from "./shdr.js";
import { getIMOD } from "./imod.js";
import { getIBAG } from "./ibag.js";
import { getINST } from "./inst.js";
import { getPGEN } from "./pgen.js";
import { getPMOD } from "./pmod.js";
import { getPBAG } from "./pbag.js";
import { getPHDR } from "./phdr.js";
import { writeWord } from "../../../utils/byte_functions/little_endian.js";
import { SpessaSynthGroupCollapsed, SpessaSynthGroupEnd, SpessaSynthInfo } from "../../../utils/loggin.js";
/**
* @typedef {Object} SoundFont2WriteOptions
* @property {boolean} compress - if the soundfont should be compressed with the Ogg Vorbis codec
* @property {number} compressionQuality - the vorbis compression quality, from -0.1 to 1
* @property {EncodeVorbisFunction|undefined} compressionFunction - the encode vorbis function.
* Can be undefined if not compressed.
*/
/**
* @type {SoundFont2WriteOptions}
*/
const DEFAULT_WRITE_OPTIONS = {
compress: false,
compressionQuality: 0.5,
compressionFunction: undefined
};
/**
* Write the soundfont as an .sf2 file. This method is DESTRUCTIVE
* @this {BasicSoundBank}
* @param {SoundFont2WriteOptions} options
* @returns {Uint8Array}
*/
export function write(options = DEFAULT_WRITE_OPTIONS)
{
if (options.compress)
{
if (typeof options.compressionFunction !== "function")
{
throw new TypeError("No compression function supplied but compression enabled.");
}
}
SpessaSynthGroupCollapsed(
"%cSaving soundfont...",
consoleColors.info
);
SpessaSynthInfo(
`%cCompression: %c${options?.compress || "false"}%c quality: %c${options?.compressionQuality || "none"}`,
consoleColors.info,
consoleColors.recognized,
consoleColors.info,
consoleColors.recognized
);
SpessaSynthInfo(
"%cWriting INFO...",
consoleColors.info
);
/**
* Write INFO
* @type {IndexedByteArray[]}
*/
const infoArrays = [];
this.soundFontInfo["ISFT"] = "SpessaSynth"; // ( ͡° ͜ʖ ͡°)
if (options?.compress)
{
this.soundFontInfo["ifil"] = "3.0"; // set version to 3
}
for (const [type, data] of Object.entries(this.soundFontInfo))
{
if (type === "ifil" || type === "iver")
{
const major = parseInt(data.split(".")[0]);
const minor = parseInt(data.split(".")[1]);
const ckdata = new IndexedByteArray(4);
writeWord(ckdata, major);
writeWord(ckdata, minor);
infoArrays.push(writeRIFFChunk(new RiffChunk(
type,
4,
ckdata
)));
}
else if (type === "DMOD")
{
infoArrays.push(writeRIFFChunk(new RiffChunk(
type,
data.length,
data
)));
}
else
{
const arr = new IndexedByteArray(data.length);
writeStringAsBytes(arr, data);
infoArrays.push(writeRIFFChunk(new RiffChunk(
type,
data.length,
arr
)));
}
}
const combined = combineArrays([
new IndexedByteArray([73, 78, 70, 79]), // INFO
...infoArrays
]);
const infoChunk = writeRIFFChunk(new RiffChunk("LIST", combined.length, combined));
SpessaSynthInfo(
"%cWriting SDTA...",
consoleColors.info
);
// write sdta
const smplStartOffsets = [];
const smplEndOffsets = [];
const sdtaChunk = getSDTA.call(
this,
smplStartOffsets,
smplEndOffsets,
options?.compress,
options?.compressionQuality ?? 0.5,
options.compressionFunction
);
SpessaSynthInfo(
"%cWriting PDTA...",
consoleColors.info
);
// write pdta
// go in reverse so the indexes are correct
// instruments
SpessaSynthInfo(
"%cWriting SHDR...",
consoleColors.info
);
const shdrChunk = getSHDR.call(this, smplStartOffsets, smplEndOffsets);
SpessaSynthInfo(
"%cWriting IGEN...",
consoleColors.info
);
const igenChunk = getIGEN.call(this);
SpessaSynthInfo(
"%cWriting IMOD...",
consoleColors.info
);
const imodChunk = getIMOD.call(this);
SpessaSynthInfo(
"%cWriting IBAG...",
consoleColors.info
);
const ibagChunk = getIBAG.call(this);
SpessaSynthInfo(
"%cWriting INST...",
consoleColors.info
);
const instChunk = getINST.call(this);
// presets
const pgenChunk = getPGEN.call(this);
SpessaSynthInfo(
"%cWriting PMOD...",
consoleColors.info
);
const pmodChunk = getPMOD.call(this);
SpessaSynthInfo(
"%cWriting PBAG...",
consoleColors.info
);
const pbagChunk = getPBAG.call(this);
SpessaSynthInfo(
"%cWriting PHDR...",
consoleColors.info
);
const phdrChunk = getPHDR.call(this);
// combine in the sfspec order
const pdtadata = combineArrays([
new IndexedByteArray([112, 100, 116, 97]), // "pdta"
phdrChunk,
pbagChunk,
pmodChunk,
pgenChunk,
instChunk,
ibagChunk,
imodChunk,
igenChunk,
shdrChunk
]);
const pdtaChunk = writeRIFFChunk(new RiffChunk(
"LIST",
pdtadata.length,
pdtadata
));
SpessaSynthInfo(
"%cWriting the output file...",
consoleColors.info
);
// finally, combine everything
const riffdata = combineArrays([
new IndexedByteArray([115, 102, 98, 107]), // "sfbk"
infoChunk,
sdtaChunk,
pdtaChunk
]);
const main = writeRIFFChunk(new RiffChunk(
"RIFF",
riffdata.length,
riffdata
));
SpessaSynthInfo(
`%cSaved succesfully! Final file size: %c${main.length}`,
consoleColors.info,
consoleColors.recognized
);
SpessaSynthGroupEnd();
return main;
} |