2019-08-26 17:55:04 +00:00
|
|
|
import {
|
|
|
|
AudioContext as stdAudioContext,
|
2019-09-27 21:52:37 +00:00
|
|
|
AudioWorkletNode as stdAudioWorkletNode,
|
2024-05-03 18:31:14 +00:00
|
|
|
OfflineAudioContext as stdOfflineAudioContext,
|
2019-08-26 17:55:04 +00:00
|
|
|
} from "standardized-audio-context";
|
2024-05-03 18:31:14 +00:00
|
|
|
import { assert } from "../util/Debug.js";
|
|
|
|
import { isDefined } from "../util/TypeCheck.js";
|
2019-08-19 18:15:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new AudioContext
|
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
export function createAudioContext(
|
|
|
|
options?: AudioContextOptions
|
|
|
|
): AudioContext {
|
2020-05-12 16:31:17 +00:00
|
|
|
return new stdAudioContext(options) as unknown as AudioContext;
|
2019-08-19 18:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new OfflineAudioContext
|
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
export function createOfflineAudioContext(
|
|
|
|
channels: number,
|
|
|
|
length: number,
|
|
|
|
sampleRate: number
|
|
|
|
): OfflineAudioContext {
|
|
|
|
return new stdOfflineAudioContext(
|
|
|
|
channels,
|
|
|
|
length,
|
|
|
|
sampleRate
|
|
|
|
) as unknown as OfflineAudioContext;
|
2019-08-19 18:15:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 16:49:04 +00:00
|
|
|
/**
|
|
|
|
* Either the online or offline audio context
|
|
|
|
*/
|
|
|
|
export type AnyAudioContext = AudioContext | OfflineAudioContext;
|
|
|
|
|
2019-07-25 22:28:17 +00:00
|
|
|
/**
|
|
|
|
* Interface for things that Tone.js adds to the window
|
|
|
|
*/
|
|
|
|
interface ToneWindow extends Window {
|
|
|
|
TONE_SILENCE_LOGGING?: boolean;
|
2019-08-08 21:00:42 +00:00
|
|
|
TONE_DEBUG_CLASS?: string;
|
2019-07-25 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the window object
|
2019-08-26 17:55:04 +00:00
|
|
|
* @hidden
|
2019-07-25 22:28:17 +00:00
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
export const theWindow: ToneWindow | null =
|
|
|
|
typeof self === "object" ? self : null;
|
2019-07-25 22:28:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the browser has a window object which has an AudioContext
|
2019-08-26 17:55:04 +00:00
|
|
|
* @hidden
|
2019-07-25 22:28:17 +00:00
|
|
|
*/
|
2024-05-03 18:31:14 +00:00
|
|
|
export const hasAudioContext =
|
|
|
|
theWindow &&
|
|
|
|
(theWindow.hasOwnProperty("AudioContext") ||
|
|
|
|
theWindow.hasOwnProperty("webkitAudioContext"));
|
2019-07-25 22:28:17 +00:00
|
|
|
|
2024-05-03 18:31:14 +00:00
|
|
|
export function createAudioWorkletNode(
|
|
|
|
context: AnyAudioContext,
|
|
|
|
name: string,
|
|
|
|
options?: Partial<AudioWorkletNodeOptions>
|
|
|
|
): AudioWorkletNode {
|
|
|
|
assert(
|
|
|
|
isDefined(stdAudioWorkletNode),
|
|
|
|
"This node only works in a secure context (https or localhost)"
|
|
|
|
);
|
2019-09-27 21:52:37 +00:00
|
|
|
// @ts-ignore
|
|
|
|
return new stdAudioWorkletNode(context, name, options);
|
|
|
|
}
|
2020-04-16 18:44:48 +00:00
|
|
|
|
|
|
|
/**
|
2024-05-03 18:31:14 +00:00
|
|
|
* This promise resolves to a boolean which indicates if the
|
|
|
|
* functionality is supported within the currently used browse.
|
2020-04-16 18:44:48 +00:00
|
|
|
* Taken from [standardized-audio-context](https://github.com/chrisguttandin/standardized-audio-context#issupported)
|
|
|
|
*/
|
|
|
|
export { isSupported as supported } from "standardized-audio-context";
|