-
Notifications
You must be signed in to change notification settings - Fork 36
/
create-native-audio-buffer-source-node.ts
130 lines (115 loc) · 6.55 KB
/
create-native-audio-buffer-source-node.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Injector } from '@angular/core';
import { INVALID_STATE_ERROR_FACTORY_PROVIDER } from '../factories/invalid-state-error';
import { assignNativeAudioNodeOptions } from '../helpers/assign-native-audio-node-options';
import { cacheTestResult } from '../helpers/cache-test-result';
import { IAudioBufferSourceOptions } from '../interfaces';
import {
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_CONSECUTIVE_CALLS_SUPPORT_TESTER_PROVIDER,
AudioScheduledSourceNodeStartMethodConsecutiveCallsSupportTester
} from '../support-testers/audio-scheduled-source-node-start-methods-consecutive-calls';
import {
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_NEGATIVE_PARAMETERS_SUPPORT_TESTER_PROVIDER,
AudioScheduledSourceNodeStartMethodNegativeParametersSupportTester
} from '../support-testers/audio-scheduled-source-node-start-methods-negative-parameters';
import {
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_CONSECUTIVE_CALLS_SUPPORT_TESTER_PROVIDER,
AudioScheduledSourceNodeStopMethodConsecutiveCallsSupportTester
} from '../support-testers/audio-scheduled-source-node-stop-methods-consecutive-calls';
import {
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_NEGATIVE_PARAMETERS_SUPPORT_TESTER_PROVIDER,
AudioScheduledSourceNodeStopMethodNegativeParametersSupportTester
} from '../support-testers/audio-scheduled-source-node-stop-methods-negative-parameters';
import { TNativeAudioBufferSourceNode, TUnpatchedAudioContext, TUnpatchedOfflineAudioContext } from '../types';
import {
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_CONSECUTIVE_CALLS_WRAPPER_PROVIDER,
AudioScheduledSourceNodeStartMethodConsecutiveCallsWrapper
} from '../wrappers/audio-scheduled-source-node-start-method-consecutive-calls';
import {
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_NEGATIVE_PARAMETERS_WRAPPER_PROVIDER,
AudioScheduledSourceNodeStartMethodNegativeParametersWrapper
} from '../wrappers/audio-scheduled-source-node-start-method-negative-parameters';
import {
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_CONSECUTIVE_CALLS_WRAPPER_PROVIDER,
AudioScheduledSourceNodeStopMethodConsecutiveCallsWrapper
} from '../wrappers/audio-scheduled-source-node-stop-method-consecutive-calls';
import {
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_NEGATIVE_PARAMETERS_WRAPPER_PROVIDER,
AudioScheduledSourceNodeStopMethodNegativeParametersWrapper
} from '../wrappers/audio-scheduled-source-node-stop-method-negative-parameters';
const injector = Injector.create({
providers: [
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_CONSECUTIVE_CALLS_SUPPORT_TESTER_PROVIDER,
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_CONSECUTIVE_CALLS_WRAPPER_PROVIDER,
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_NEGATIVE_PARAMETERS_SUPPORT_TESTER_PROVIDER,
AUDIO_SCHEDULED_SOURCE_NODE_START_METHOD_NEGATIVE_PARAMETERS_WRAPPER_PROVIDER,
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_CONSECUTIVE_CALLS_SUPPORT_TESTER_PROVIDER,
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_CONSECUTIVE_CALLS_WRAPPER_PROVIDER,
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_NEGATIVE_PARAMETERS_SUPPORT_TESTER_PROVIDER,
AUDIO_SCHEDULED_SOURCE_NODE_STOP_METHOD_NEGATIVE_PARAMETERS_WRAPPER_PROVIDER,
INVALID_STATE_ERROR_FACTORY_PROVIDER
]
});
const startMethodConsecutiveCallsSupportTester = injector.get(AudioScheduledSourceNodeStartMethodConsecutiveCallsSupportTester);
const startMethodConsecutiveCallsWrapper = injector
.get<AudioScheduledSourceNodeStartMethodConsecutiveCallsWrapper>(AudioScheduledSourceNodeStartMethodConsecutiveCallsWrapper);
const startMethodNegativeParametersSupportTester = injector.get(AudioScheduledSourceNodeStartMethodNegativeParametersSupportTester);
const startMethodNegativeParametersWrapper = injector.get(AudioScheduledSourceNodeStartMethodNegativeParametersWrapper);
const stopMethodConsecutiveCallsSupportTester = injector.get(AudioScheduledSourceNodeStopMethodConsecutiveCallsSupportTester);
const stopMethodConsecutiveCallsWrapper = injector.get(AudioScheduledSourceNodeStopMethodConsecutiveCallsWrapper);
const stopMethodNegativeParametersSupportTester = injector.get(AudioScheduledSourceNodeStopMethodNegativeParametersSupportTester);
const stopMethodNegativeParametersWrapper = injector.get(AudioScheduledSourceNodeStopMethodNegativeParametersWrapper);
export const createNativeAudioBufferSourceNode = (
nativeContext: TUnpatchedAudioContext | TUnpatchedOfflineAudioContext,
options: Partial<IAudioBufferSourceOptions> = { }
): TNativeAudioBufferSourceNode => {
const nativeNode = nativeContext.createBufferSource();
assignNativeAudioNodeOptions(nativeNode, options);
// Bug #71: Edge does not allow to set the buffer to null.
if (options.buffer !== undefined && options.buffer !== null) {
nativeNode.buffer = options.buffer;
}
// @todo if (options.detune !== undefined) {
// @todo nativeNode.detune.value = options.detune;
// @todo }
if (options.loop !== undefined) {
nativeNode.loop = options.loop;
}
if (options.loopEnd !== undefined) {
nativeNode.loopEnd = options.loopEnd;
}
if (options.loopStart !== undefined) {
nativeNode.loopStart = options.loopStart;
}
if (options.playbackRate !== undefined) {
nativeNode.playbackRate.value = options.playbackRate;
}
// Bug #69: Safari does allow calls to start() of an already scheduled AudioBufferSourceNode.
if (!cacheTestResult(
AudioScheduledSourceNodeStartMethodConsecutiveCallsSupportTester,
() => startMethodConsecutiveCallsSupportTester.test(nativeContext)
)) {
startMethodConsecutiveCallsWrapper.wrap(nativeNode);
}
// Bug #44: Only Chrome & Opera throw a RangeError yet.
if (!cacheTestResult(
AudioScheduledSourceNodeStartMethodNegativeParametersSupportTester,
() => startMethodNegativeParametersSupportTester.test(nativeContext)
)) {
startMethodNegativeParametersWrapper.wrap(nativeNode);
}
// Bug #19: Safari does not ignore calls to stop() of an already stopped AudioBufferSourceNode.
if (!cacheTestResult(
AudioScheduledSourceNodeStopMethodConsecutiveCallsSupportTester,
() => stopMethodConsecutiveCallsSupportTester.test(nativeContext)
)) {
stopMethodConsecutiveCallsWrapper.wrap(nativeNode, nativeContext);
}
// Bug #44: No browser does throw a RangeError yet.
if (!cacheTestResult(
AudioScheduledSourceNodeStopMethodNegativeParametersSupportTester,
() => stopMethodNegativeParametersSupportTester.test(nativeContext)
)) {
stopMethodNegativeParametersWrapper.wrap(nativeNode);
}
return nativeNode;
};