-
Notifications
You must be signed in to change notification settings - Fork 992
/
AudioContext.ts
85 lines (77 loc) · 2.13 KB
/
AudioContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import {
AudioContext as stdAudioContext,
AudioWorkletNode as stdAudioWorkletNode,
OfflineAudioContext as stdOfflineAudioContext,
} from "standardized-audio-context";
import { assert } from "../util/Debug.js";
import { isDefined } from "../util/TypeCheck.js";
/**
* Create a new AudioContext
*/
export function createAudioContext(
options?: AudioContextOptions
): AudioContext {
return new stdAudioContext(options) as unknown as AudioContext;
}
/**
* Create a new OfflineAudioContext
*/
export function createOfflineAudioContext(
channels: number,
length: number,
sampleRate: number
): OfflineAudioContext {
return new stdOfflineAudioContext(
channels,
length,
sampleRate
) as unknown as OfflineAudioContext;
}
/**
* Either the online or offline audio context
*/
export type AnyAudioContext = AudioContext | OfflineAudioContext;
/**
* Interface for things that Tone.js adds to the window
*/
interface ToneWindow extends Window {
TONE_SILENCE_LOGGING?: boolean;
TONE_DEBUG_CLASS?: string;
BaseAudioContext: any;
AudioWorkletNode: any;
}
/**
* A reference to the window object
* @hidden
*/
export const theWindow: ToneWindow | null =
typeof self === "object" ? self : null;
/**
* If the browser has a window object which has an AudioContext
* @hidden
*/
export const hasAudioContext =
theWindow &&
(theWindow.hasOwnProperty("AudioContext") ||
theWindow.hasOwnProperty("webkitAudioContext"));
export function createAudioWorkletNode(
context: AnyAudioContext,
name: string,
options?: Partial<AudioWorkletNodeOptions>
): AudioWorkletNode {
assert(
isDefined(stdAudioWorkletNode),
"AudioWorkletNode only works in a secure context (https or localhost)"
);
return new (
context instanceof theWindow?.BaseAudioContext
? theWindow?.AudioWorkletNode
: stdAudioWorkletNode
)(context, name, options);
}
/**
* This promise resolves to a boolean which indicates if the
* functionality is supported within the currently used browse.
* Taken from [standardized-audio-context](https://github.com/chrisguttandin/standardized-audio-context#issupported)
*/
export { isSupported as supported } from "standardized-audio-context";