Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement OscillatorNode for OfflineAudioContext
- Loading branch information
1 parent
251116b
commit 09dd6e6
Showing
11 changed files
with
1,050 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Injector } from '@angular/core'; | ||
import { assignNativeAudioNodeOptions } from '../helpers/assign-native-audio-node-options'; | ||
import { cacheTestResult } from '../helpers/cache-test-result'; | ||
import { IOscillatorOptions } from '../interfaces'; | ||
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 { TNativeOscillatorNode, TUnpatchedAudioContext, TUnpatchedOfflineAudioContext } from '../types'; | ||
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_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 | ||
] | ||
}); | ||
|
||
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 createNativeOscillatorNode = ( | ||
nativeContext: TUnpatchedAudioContext | TUnpatchedOfflineAudioContext, | ||
options: Partial<IOscillatorOptions> = { } | ||
): TNativeOscillatorNode => { | ||
const nativeNode = nativeContext.createOscillator(); | ||
|
||
assignNativeAudioNodeOptions(nativeNode, options); | ||
|
||
if (options.detune !== undefined) { | ||
nativeNode.detune.value = options.detune; | ||
} | ||
|
||
if (options.frequency !== undefined) { | ||
nativeNode.frequency.value = options.frequency; | ||
} | ||
|
||
// @todo periodicWave | ||
|
||
if (options.type !== undefined) { | ||
nativeNode.type = options.type; | ||
} | ||
|
||
// 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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { AUDIO_PARAM_STORE } from '../globals'; | ||
import { connectAudioParam } from '../helpers/connect-audio-param'; | ||
import { createNativeOscillatorNode } from '../helpers/create-native-oscillator-node'; | ||
import { getNativeNode } from '../helpers/get-native-node'; | ||
import { isOwnedByContext } from '../helpers/is-owned-by-context'; | ||
import { renderAutomation } from '../helpers/render-automation'; | ||
import { IAudioParam, IOscillatorNode } from '../interfaces'; | ||
import { TNativeAudioNode, TNativeAudioParam, TNativeOscillatorNode, TUnpatchedOfflineAudioContext } from '../types'; | ||
import { AudioNodeRenderer } from './audio-node'; | ||
|
||
export class OscillatorNodeRenderer extends AudioNodeRenderer { | ||
|
||
private _nativeNode: null | TNativeOscillatorNode; | ||
|
||
private _proxy: IOscillatorNode; | ||
|
||
constructor (proxy: IOscillatorNode) { | ||
super(); | ||
|
||
this._nativeNode = null; | ||
this._proxy = proxy; | ||
} | ||
|
||
public async render (offlineAudioContext: TUnpatchedOfflineAudioContext): Promise<TNativeAudioNode> { | ||
if (this._nativeNode !== null) { | ||
return this._nativeNode; | ||
} | ||
|
||
this._nativeNode = <TNativeOscillatorNode> getNativeNode(this._proxy); | ||
|
||
// If the initially used nativeNode was not constructed on the same OfflineAudioContext it needs to be created again. | ||
if (!isOwnedByContext(this._nativeNode, offlineAudioContext)) { | ||
const detuneAudioParam = <IAudioParam> (<any> this._nativeNode.detune); | ||
const frequencyAudioParam = <IAudioParam> (<any> this._nativeNode.frequency); | ||
|
||
this._nativeNode = createNativeOscillatorNode(offlineAudioContext); | ||
|
||
await renderAutomation(offlineAudioContext, detuneAudioParam, this._nativeNode.detune); | ||
await renderAutomation(offlineAudioContext, frequencyAudioParam, this._nativeNode.frequency); | ||
} else { | ||
const detuneNativeAudioParam = <TNativeAudioParam> AUDIO_PARAM_STORE.get(this._proxy.detune); | ||
const frequencyNativeAudioParam = <TNativeAudioParam> AUDIO_PARAM_STORE.get(this._proxy.frequency); | ||
|
||
await connectAudioParam(offlineAudioContext, <IAudioParam> (<any> this._nativeNode.detune), detuneNativeAudioParam); | ||
await connectAudioParam(offlineAudioContext, <IAudioParam> (<any> this._nativeNode.frequency), frequencyNativeAudioParam); | ||
} | ||
|
||
await this._connectSources(offlineAudioContext, <TNativeAudioNode> this._nativeNode); | ||
|
||
return <TNativeAudioNode> this._nativeNode; | ||
} | ||
|
||
} |
Oops, something went wrong.