2019-12-04 06:55:58 +00:00
|
|
|
/**
|
2020-11-16 23:01:25 +00:00
|
|
|
* Functions imported from JS into Ruffle.
|
|
|
|
*
|
|
|
|
* @ignore
|
2020-11-17 22:53:17 +00:00
|
|
|
* @internal
|
2019-12-04 06:55:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2020-11-16 23:01:25 +00:00
|
|
|
* @internal
|
2019-12-04 06:55:58 +00:00
|
|
|
*/
|
2020-11-16 22:59:51 +00:00
|
|
|
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>,
|
2020-11-16 23:01:25 +00:00
|
|
|
): void {
|
2020-11-17 22:16:35 +00:00
|
|
|
if (leftData) {
|
|
|
|
const dstBuffer = audioBuffer.getChannelData(0);
|
|
|
|
dstBuffer.set(leftData);
|
2019-12-04 06:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 22:16:35 +00:00
|
|
|
if (rightData) {
|
|
|
|
const dstBuffer = audioBuffer.getChannelData(1);
|
|
|
|
dstBuffer.set(rightData);
|
2019-12-04 06:55:58 +00:00
|
|
|
}
|
2020-11-12 22:32:53 +00:00
|
|
|
}
|
2021-10-16 08:25:31 +00:00
|
|
|
|
|
|
|
/**
|
2022-03-19 19:58:47 +00:00
|
|
|
* Returns the estimated output timestamp for the audio context.
|
|
|
|
* This is necessary because web-sys does not export `AudioContext.baseLatency`.
|
2021-10-16 08:25:31 +00:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function getAudioOutputTimestamp(context: AudioContext): number {
|
2022-03-19 19:58:47 +00:00
|
|
|
// TODO: Ideally we'd use `context.getOutputTimestamp`, but this is broken as of Safari 15.4.
|
|
|
|
return context.currentTime - context.baseLatency;
|
2021-10-16 08:25:31 +00:00
|
|
|
}
|
2022-05-10 01:01:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>,
|
2022-05-10 01:01:13 +00:00
|
|
|
): 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]!;
|
2022-05-10 01:01:13 +00:00
|
|
|
sample++;
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
2023-03-19 02:47:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
}
|