ruffle/web/packages/core/src/ruffle-imports.ts

73 lines
1.9 KiB
TypeScript
Raw Normal View History

/**
* Functions imported from JS into Ruffle.
*
* @ignore
2020-11-17 22:53:17 +00:00
* @internal
*/
/**
* Copies data into the given audio channel.
* This is necessary because Safari does not support `AudioBuffer.copyToChannel`.
2020-11-17 22:53:17 +00:00
*
* @internal
*/
export function copyToAudioBuffer(
2020-11-17 22:16:35 +00:00
audioBuffer: AudioBuffer,
leftData: ArrayLike<number>,
2023-07-20 11:19:39 +00:00
rightData: ArrayLike<number>,
): void {
2020-11-17 22:16:35 +00:00
if (leftData) {
const dstBuffer = audioBuffer.getChannelData(0);
dstBuffer.set(leftData);
}
2020-11-17 22:16:35 +00:00
if (rightData) {
const dstBuffer = audioBuffer.getChannelData(1);
dstBuffer.set(rightData);
}
}
/**
* Returns the estimated output timestamp for the audio context.
* This is necessary because web-sys does not export `AudioContext.baseLatency`.
*
* @internal
*/
export function getAudioOutputTimestamp(context: AudioContext): number {
// TODO: Ideally we'd use `context.getOutputTimestamp`, but this is broken as of Safari 15.4.
return context.currentTime - context.baseLatency;
}
/**
* Copies interleaved stereo audio data into an `AudioBuffer`.
*
* @internal
*/
export function copyToAudioBufferInterleaved(
audioBuffer: AudioBuffer,
2023-07-20 11:19:39 +00:00
interleavedData: ArrayLike<number>,
): void {
const numSamples = audioBuffer.length;
const leftBuffer = audioBuffer.getChannelData(0);
const rightBuffer = audioBuffer.getChannelData(1);
let i = 0;
let sample = 0;
while (sample < numSamples) {
2023-02-20 14:19:18 +00:00
leftBuffer[sample] = interleavedData[i]!;
rightBuffer[sample] = interleavedData[i + 1]!;
sample++;
i += 2;
}
}
/**
* Gets a property of an arbitrary JavaScript value.
* This is necessary because Reflect.get does not work for primitive targets.
*
* @internal
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getProperty(target: any, key: string): any {
return target[key];
}