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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* MikMod Web Audio library
(c) 2021 Carlos Rafael Gimenes das Neves.
https://github.com/sezero/mikmod
https://github.com/carlosrafaelgn/mikmod/tree/master/libmikmod/webaudio
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
interface LibMikModCLib {
HEAP8: Uint8Array;
HEAPF32: Float32Array;
_getVersion(): number;
_init(): number;
_freeModule(): void;
_terminate(): void;
_preLoadModule(length: number): number;
_loadModule(mixfreq: number, reverb: number, hqMixer: number, interpolation: number, noiseReduction: number, wrap: number, loop: number, fadeout: number): number;
_changeGeneralOptions(reverb: number, interpolation: number, noiseReduction: number): void;
_update(): number;
_getErrno(): number;
_getStrerr(code: number): number;
_getSongName(): number;
_getModType(): number;
_getComment(): number;
_getAudioBuffer(): number;
_getAudioBufferMaxLength(): number;
_getAudioBufferUsedLength(): number;
}
class LibMikMod {
private static cLib: LibMikModCLib | null = null;
private static tmpBuffer: Float32Array | null = null;
private static audioBufferPtr = 0;
private static audioBufferUsedLength = 0;
private static lastReverb = 0;
private static lastHqMixer = 1;
private static lastInterpolation = 1;
private static lastNoiseReduction = 1;
private static lastWrap = 0;
private static lastLoop = 0;
private static lastFadeout = 1;
public static loading = false;
public static loaded = false;
public static loadErrorStr: string | null = null;
public static init(wasmBinary: ArrayBuffer): Promise<void> {
return (LibMikMod.loaded ? Promise.resolve() : new Promise((resolve, reject) => {
if (LibMikMod.loading) {
reject(LibMikMod.loadErrorStr = "The library was still loading");
return;
}
if (LibMikMod.loadErrorStr) {
reject(LibMikMod.loadErrorStr);
return;
}
LibMikMod.loading = true;
LibMikModCLib({ wasmBinary }).then((value) => {
LibMikMod.cLib = value;
const r = value._init();
if (!r) {
LibMikMod.loading = false;
LibMikMod.loaded = true;
LibMikMod.loadErrorStr = null;
resolve();
} else {
LibMikMod.loading = false;
reject(LibMikMod.loadErrorStr = LibMikMod.getStrerr(r));
}
}, (reason) => {
LibMikMod.loading = false;
reject(LibMikMod.loadErrorStr = ((reason ? (reason.message || reason.toString()) : null) || "Unknown error while loading the library"));
});
}));
}
public static terminate(): void {
if (LibMikMod.cLib) {
LibMikMod.cLib._terminate();
LibMikMod.cLib = null;
LibMikMod.audioBufferPtr = 0;
LibMikMod.audioBufferUsedLength = 0;
}
}
public static getVersion(): number {
return (LibMikMod.cLib ? LibMikMod.cLib._getVersion() : 0);
}
public static loadModule(destinationSampleRate: number, srcBuffer?: ArrayBuffer | Uint8Array, options?: LibMikModInitialOptions | null): number {
if (!LibMikMod.cLib)
return 3; // MMERR_DYNAMIC_LINKING
if (!srcBuffer)
return 2; // MMERR_OUT_OF_MEMORY
const ptr = LibMikMod.cLib._preLoadModule(srcBuffer.byteLength);
if (!ptr)
return 2; // MMERR_OUT_OF_MEMORY
const dstBuffer = new Uint8Array(LibMikMod.cLib.HEAP8.buffer, ptr, srcBuffer.byteLength);
if ("set" in srcBuffer)
dstBuffer.set(srcBuffer, 0);
else
dstBuffer.set(new Uint8Array(srcBuffer, 0, srcBuffer.byteLength), 0);
LibMikMod.audioBufferPtr = 0;
LibMikMod.audioBufferUsedLength = 0;
if (options) {
if (options.reverb !== undefined && options.reverb >= 0 && options.reverb <= 15)
LibMikMod.lastReverb = options.reverb;
if (options.hqMixer !== undefined)
LibMikMod.lastHqMixer = (options.hqMixer ? 1 : 0);
if (options.interpolation !== undefined)
LibMikMod.lastInterpolation = (options.interpolation ? 1 : 0);
if (options.noiseReduction !== undefined)
LibMikMod.lastNoiseReduction = (options.noiseReduction ? 1 : 0);
if (options.wrap !== undefined)
LibMikMod.lastWrap = (options.wrap ? 1 : 0);
if (options.loop !== undefined)
LibMikMod.lastLoop = (options.loop ? 1 : 0);
if (options.fadeout !== undefined)
LibMikMod.lastFadeout = (options.fadeout ? 1 : 0);
}
const r = LibMikMod.cLib._loadModule(destinationSampleRate, LibMikMod.lastReverb, LibMikMod.lastHqMixer, LibMikMod.lastInterpolation, LibMikMod.lastNoiseReduction, LibMikMod.lastWrap, LibMikMod.lastLoop, LibMikMod.lastFadeout);
if (!r) {
const audioBufferPtr = LibMikMod.cLib._getAudioBuffer();
if (!audioBufferPtr)
return 2; // MMERR_OUT_OF_MEMORY
}
return r;
}
public static changeGeneralOptions(options?: LibMikModGeneralOptions | null): void {
if (options) {
if (options.reverb !== undefined && options.reverb >= 0 && options.reverb <= 15)
LibMikMod.lastReverb = options.reverb;
if (options.interpolation !== undefined)
LibMikMod.lastInterpolation = (options.interpolation ? 1 : 0);
if (options.noiseReduction !== undefined)
LibMikMod.lastNoiseReduction = (options.noiseReduction ? 1 : 0);
if (LibMikMod.cLib)
LibMikMod.cLib._changeGeneralOptions(LibMikMod.lastReverb, LibMikMod.lastInterpolation, LibMikMod.lastNoiseReduction);
}
}
public static stopModule(): void {
if (LibMikMod.cLib) {
LibMikMod.cLib._freeModule();
LibMikMod.audioBufferPtr = 0;
LibMikMod.audioBufferUsedLength = 0;
}
}
private static getString(ptr: number): string | null {
if (LibMikMod.cLib && ptr) {
const heap = LibMikMod.cLib.HEAP8;
let len = 0;
for (let i = ptr; heap[i] && len < 1024; i++)
len++;
if (!len)
return "";
const arr: number[] = new Array(len);
while (len-- > 0)
arr[len] = heap[ptr + len];
return String.fromCharCode.apply(String, arr);
}
return null;
}
public static getSongName(): string | null {
return (LibMikMod.cLib ? LibMikMod.getString(LibMikMod.cLib._getSongName()) : null);
}
public static getModType(): string | null {
return (LibMikMod.cLib ? LibMikMod.getString(LibMikMod.cLib._getModType()) : null);
}
public static getComment(): string | null {
return (LibMikMod.cLib ? LibMikMod.getString(LibMikMod.cLib._getComment()) : null);
}
public static getErrno(): number {
return (LibMikMod.cLib ? LibMikMod.cLib._getErrno() : 0);
}
public static getStrerr(code: number): string | null {
return (LibMikMod.cLib ? LibMikMod.getString(LibMikMod.cLib._getStrerr(code)) : null);
}
public static process(outputs: Float32Array[][]): boolean {
if (!LibMikMod.cLib)
return false;
let audioBufferUsedLength = LibMikMod.audioBufferUsedLength;
if (!audioBufferUsedLength) {
for (let attempts = 0; attempts < 3; attempts++) {
audioBufferUsedLength = LibMikMod.cLib._update();
if (audioBufferUsedLength < 0)
return false;
if (audioBufferUsedLength) {
LibMikMod.audioBufferPtr = LibMikMod.cLib._getAudioBuffer();
LibMikMod.audioBufferUsedLength = audioBufferUsedLength;
break;
}
}
// Output silence if we cannot fill the buffer
if (!audioBufferUsedLength)
return true;
}
let tmpBuffer: Float32Array | null = null;
for (let o = outputs.length - 1; o >= 0; o--) {
const channels = outputs[o];
for (let c = channels.length - 1; c >= 0; c--) {
const channel = channels[c];
if (!tmpBuffer) {
// Convert mono 32-bit float samples into bytes
const maxBytes = channel.length << 2;
if (audioBufferUsedLength >= maxBytes) {
audioBufferUsedLength = maxBytes;
// Convert bytes into mono 32-bit float samples
tmpBuffer = new Float32Array(LibMikMod.cLib.HEAP8.buffer, LibMikMod.audioBufferPtr, audioBufferUsedLength >> 2);
LibMikMod.audioBufferPtr += audioBufferUsedLength;
LibMikMod.audioBufferUsedLength -= audioBufferUsedLength;
} else {
// We had data, but not enough to fill the buffer... This should not
// happen as long as BUFFERSIZE in drv_webaudio.c is a multiple of maxBytes,
// which is currently true, since BUFFERSIZE is 32768, and maxBytes is (128 * 4) = 512
if (!LibMikMod.tmpBuffer || LibMikMod.tmpBuffer.byteLength !== maxBytes)
LibMikMod.tmpBuffer = new Float32Array(channel.length);
// Convert bytes into mono 32-bit float samples
let samplesRead = audioBufferUsedLength >> 2;
tmpBuffer = new Float32Array(LibMikMod.cLib.HEAP8.buffer, LibMikMod.audioBufferPtr, samplesRead);
LibMikMod.audioBufferPtr = 0;
LibMikMod.audioBufferUsedLength = 0;
audioBufferUsedLength = 0;
LibMikMod.tmpBuffer.set(tmpBuffer);
let remainingSamples = channel.length - samplesRead;
while (remainingSamples > 0) {
if (!audioBufferUsedLength) {
for (let attempts = 0; attempts < 3; attempts++) {
audioBufferUsedLength = LibMikMod.cLib._update();
if (audioBufferUsedLength < 0) {
if (LibMikMod.getErrno())
return false;
audioBufferUsedLength = 0;
break;
}
if (audioBufferUsedLength) {
LibMikMod.audioBufferPtr = LibMikMod.cLib._getAudioBuffer();
LibMikMod.audioBufferUsedLength = audioBufferUsedLength;
break;
}
}
// Output silence if we cannot fill the buffer
if (!audioBufferUsedLength) {
LibMikMod.tmpBuffer.fill(0, samplesRead);
break;
}
}
// Convert mono 32-bit float samples into bytes
let remainingBytes = remainingSamples << 2;
if (remainingBytes > audioBufferUsedLength)
remainingBytes = audioBufferUsedLength;
// Convert bytes into mono 32-bit float samples
const samplesToReadNow = remainingBytes >> 2;
tmpBuffer = new Float32Array(LibMikMod.cLib.HEAP8.buffer, LibMikMod.audioBufferPtr, samplesToReadNow);
LibMikMod.audioBufferPtr += remainingBytes;
LibMikMod.audioBufferUsedLength -= remainingBytes;
LibMikMod.tmpBuffer.set(tmpBuffer, samplesRead);
samplesRead += samplesToReadNow;
remainingSamples -= samplesToReadNow;
}
tmpBuffer = LibMikMod.tmpBuffer;
}
}
channel.set(tmpBuffer);
}
}
return true;
}
}
|