Debian Codegraph @

wasm/browser/src/utils.js

raw
1 export const appendBuffers = (buffer1, buffer2) => {
2 const temporary = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
3 temporary.set(new Uint8Array(buffer1), 0);
4 temporary.set(new Uint8Array(buffer2), buffer1.byteLength);
5 return temporary.buffer;
6 };
7
8 export const nearestPowerOf2 = (n) => {
9 return 1 << (31 - Math.clz32(n));
10 };
11
12 export const isIos = () => /iPhone|iPad|iPod/.test(navigator.userAgent);
13
14 const isFirefox = () => navigator.userAgent.toLowerCase().includes("firefox");
15
16 export const isSafari = () =>
17 typeof navigator.vendor === "string" && navigator.vendor.includes("Apple");
18
19 export const isSabSupported = () =>
20 !isFirefox() &&
21 typeof window.Atomics !== "undefined" &&
22 typeof window.SharedArrayBuffer !== "undefined";
23
24 export const areWorkletsSupported = () =>
25 typeof AudioNode !== "undefined" && typeof AudioWorkletNode !== "undefined";
26
27 export const WebkitAudioContext = () => {
28 if (typeof window.webkitAudioContext !== "undefined") {
29 return window.webkitAudioContext;
30 } else if (typeof window.AudioContext !== "undefined") {
31 return window.AudioContext;
32 }
33 };
34
35 export const isScriptProcessorNodeSupported = () => {
36 const audioContext = WebkitAudioContext();
37 return (
38 typeof audioContext !== "undefined" &&
39 typeof audioContext.prototype.createScriptProcessor !== "undefined"
40 );
41 };
42
43 export const csoundApiRename = (apiName) => {
44 let minusCsound = apiName.replace(/^csound/i, "");
45 if (apiName === "csoundPushMidiMessage") {
46 minusCsound = "midiMessage";
47 }
48 return minusCsound.charAt(0).toLowerCase() + minusCsound.slice(1);
49 };
50
51 export const stopableStates = new Set([
52 "realtimePerformanceStarted",
53 "realtimePerformancePaused",
54 "realtimePerformanceResumed",
55 "renderStarted",
56 ]);
57
58 export const makeProxyCallback =
59 (proxyPort, csoundInstance, apiK, playState) =>
60 async (...arguments_) => {
61 if (!playState || !stopableStates.has(playState)) {
62 const modifiedFs = {}; // getModifiedPersistentStorage();
63 Object.values(modifiedFs).length > 0 &&
64 (await proxyPort.callUncloned("syncWorkerFs", [csoundInstance, modifiedFs]));
65 }
66 return await proxyPort.callUncloned(apiK, [csoundInstance, ...arguments_]);
67 };
68
69 export const makeSingleThreadCallback =
70 (csoundInstance, apiCallback) =>
71 async (...arguments_) => {
72 return await apiCallback.apply({}, [csoundInstance, ...arguments_]);
73 };
74
75 export const fetchPlugins = async (withPlugins) => {
76 return await Promise.all(
77 withPlugins.map(async (url) => {
78 const response = await fetch(url);
79 return response.arrayBuffer();
80 }),
81 );
82 };