| 1 |
/* eslint-disable unicorn/prevent-abbreviations */ |
| 2 |
import { uint2String } from "../utils/text-encoders.js"; |
| 3 |
/* |
| 4 |
csound table module from <csound.h> |
| 5 |
https://csound.com/docs/api/group___t_a_b_l_e.html |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns the length of a function table |
| 10 |
* (not including the guard point), |
| 11 |
* or -1 if the table does not exist. |
| 12 |
* @function |
| 13 |
*/ |
| 14 |
export const csoundTableLength = (wasm) => (csound /* CsoundInst */, tableNumber /* string */) => |
| 15 |
wasm.exports.csoundTableLength(csound, tableNumber); |
| 16 |
|
| 17 |
csoundTableLength.toString = () => "tableLength = async (tableNum) => Number;"; |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns the value of a slot in a function table. |
| 21 |
* The table number and index are assumed to be valid. |
| 22 |
* @function |
| 23 |
*/ |
| 24 |
export const csoundTableGet = |
| 25 |
(wasm) => (csound /* CsoundInst */, tableNumber /* string */, tableIndex /* string */) => |
| 26 |
wasm.exports.csoundTableGet(csound, tableNumber, tableIndex); |
| 27 |
|
| 28 |
csoundTableGet.toString = () => "tableGet = async (tableNum, tableIndex) => Number;"; |
| 29 |
|
| 30 |
/** |
| 31 |
* Sets the value of a slot in a function table. |
| 32 |
* The table number and index are assumed to be valid. |
| 33 |
* @function |
| 34 |
*/ |
| 35 |
export const csoundTableSet = |
| 36 |
(wasm) => |
| 37 |
( |
| 38 |
csound /* CsoundInst */, |
| 39 |
tableNumber /* string */, |
| 40 |
tableIndex /* string */, |
| 41 |
value /* string */, |
| 42 |
) => |
| 43 |
wasm.exports.csoundTableSet(csound, tableNumber, tableIndex, value); |
| 44 |
|
| 45 |
csoundTableSet.toString = () => "tableSet = async (tableNum, tableIndex, value) => undefined;"; |
| 46 |
|
| 47 |
/** |
| 48 |
* Copy the contents of an Array or TypedArray from javascript into a given csound function table. |
| 49 |
* The table number is assumed to be valid, and the table needs to have sufficient space |
| 50 |
* to receive all the array contents. |
| 51 |
* The table number and index are assumed to be valid. |
| 52 |
*/ |
| 53 |
export const csoundTableCopyIn = |
| 54 |
(wasm) => (csound /* CsoundInst */, tableNumber /* string */, array /* ArrayLike<number> */) => { |
| 55 |
const arrayPtr = wasm.exports.allocFloatArray(array.length); |
| 56 |
const buffer = new Float64Array(wasm.wasi.memory.buffer, arrayPtr, array.length); |
| 57 |
buffer.set(array); |
| 58 |
wasm.exports.csoundTableCopyIn(csound, tableNumber, arrayPtr); |
| 59 |
wasm.exports.freeFloatArrayMem(arrayPtr); |
| 60 |
}; |
| 61 |
|
| 62 |
csoundTableCopyIn.toString = () => "tableCopyIn = async (tableNum, float64Array) => undefined;"; |
| 63 |
|
| 64 |
/** |
| 65 |
* Copies the contents of a function table from csound into Float64Array. |
| 66 |
* The function returns a Float64Array if the table exists, otherwise |
| 67 |
* it returns undefined. |
| 68 |
* @function |
| 69 |
*/ |
| 70 |
export const csoundTableCopyOut = (wasm) => (csound /* CsoundInst */, tableNumber /* string */) => { |
| 71 |
const tableLength = wasm.exports.csoundTableLength(csound, tableNumber); |
| 72 |
if (tableLength > 0) { |
| 73 |
const arrayPtr = wasm.exports.allocFloatArray(tableLength); |
| 74 |
wasm.exports.csoundTableCopyOut(csound, tableNumber, arrayPtr); |
| 75 |
const { buffer } = wasm.wasi.memory; |
| 76 |
const jsArray = new Float64Array(buffer, arrayPtr, tableLength); |
| 77 |
wasm.exports.freeFloatArrayMem(arrayPtr); |
| 78 |
return Float64Array.from(jsArray); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
csoundTableCopyOut.toString = () => "tableCopyOut = async (tableNum) => ?Float64Array;"; |
| 83 |
|
| 84 |
/** |
| 85 |
* @name getTable |
| 86 |
* @alias csoundTableCopyOut |
| 87 |
* @async |
| 88 |
* @function |
| 89 |
*/ |
| 90 |
export const csoundGetTable = csoundTableCopyOut; |
| 91 |
csoundGetTable.toString = csoundTableCopyOut.toString; |
| 92 |
|
| 93 |
/** |
| 94 |
* Copies the contents of a function table from csound into Float64Array. |
| 95 |
* The function returns a Float64Array if the table exists, otherwise |
| 96 |
* it returns undefined. |
| 97 |
*/ |
| 98 |
export const csoundGetTableArgs = (wasm) => (csound /* CsoundInst */, tableNumber /* string */) => { |
| 99 |
const arrayPtr = wasm.exports.allocFloatArray(1024); |
| 100 |
wasm.exports.csoundGetTableArgs(csound, arrayPtr, tableNumber); |
| 101 |
const { buffer } = wasm.wasi.memory; |
| 102 |
const jsArray = new Float64Array(buffer, arrayPtr, 1024); |
| 103 |
wasm.exports.freeFloatArrayMem(arrayPtr); |
| 104 |
return jsArray; |
| 105 |
}; |
| 106 |
|
| 107 |
csoundGetTableArgs.toString = () => "getTableArgs = async (tableNum) => ?Float64Array;"; |
| 108 |
|
| 109 |
/** |
| 110 |
* Checks if a given GEN number num is a named GEN if so, |
| 111 |
* it returns the string length (excluding terminating NULL char). |
| 112 |
* Otherwise it returns 0. |
| 113 |
*/ |
| 114 |
export const csoundIsNamedGEN = (wasm) => (csound /* CsoundInst */, tableNumber /* string */) => |
| 115 |
wasm.exports.csoundIsNamedGEN(csound, tableNumber); |
| 116 |
|
| 117 |
csoundIsNamedGEN.toString = () => "isNamedGEN = async (tableNum) => number;"; |
| 118 |
|
| 119 |
/** |
| 120 |
* Gets the GEN name from a number num, if this is a named GEN. |
| 121 |
* If the table number doesn't represent a named GEN, it will |
| 122 |
* return undefined. |
| 123 |
*/ |
| 124 |
export const csoundGetNamedGEN = (wasm) => (csound /* CsoundInst */, tableNumber /* string */) => { |
| 125 |
const stringLength = wasm.exports.csoundIsNamedGEN(csound, tableNumber); |
| 126 |
if (stringLength > 0) { |
| 127 |
const offset = wasm.exports.allocStringMem(stringLength); |
| 128 |
wasm.exports.csoundGetNamedGEN(csound, offset, tableNumber, stringLength); |
| 129 |
const { buffer } = wasm.wasi.memory; |
| 130 |
const stringBuffer = new Uint8Array(buffer, offset, stringLength); |
| 131 |
const result = uint2String(stringBuffer); |
| 132 |
return result; |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
csoundGetNamedGEN.toString = () => "getNamedGEN = async (tableNum) => ?string;"; |