Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement AnalyserNode for OfflineAudioContext
- Loading branch information
1 parent
c8392cb
commit 983f4ae
Showing
10 changed files
with
613 additions
and
178 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,65 @@ | ||
import { Injector } from '@angular/core'; | ||
import { assignNativeAudioNodeOptions } from '../helpers/assign-native-audio-node-options'; | ||
import { cacheTestResult } from '../helpers/cache-test-result'; | ||
import { IAnalyserOptions } from '../interfaces'; | ||
import { | ||
ANALYSER_NODE_GET_FLOAT_TIME_DOMAIN_DATA_SUPPORT_TESTER_PROVIDER, | ||
AnalyserNodeGetFloatTimeDomainDataSupportTester | ||
} from '../support-testers/analyser-node-get-float-time-domain-data'; | ||
import { TNativeAnalyserNode, TUnpatchedAudioContext, TUnpatchedOfflineAudioContext } from '../types'; | ||
import { | ||
ANALYSER_NODE_GET_FLOAT_TIME_DOMAIN_DATA_METHOD_WRAPPER_PROVIDER, | ||
AnalyserNodeGetFloatTimeDomainDataMethodWrapper | ||
} from '../wrappers/analyser-node-get-float-time-domain-data-method'; | ||
|
||
const injector = Injector.create({ | ||
providers: [ | ||
ANALYSER_NODE_GET_FLOAT_TIME_DOMAIN_DATA_METHOD_WRAPPER_PROVIDER, | ||
ANALYSER_NODE_GET_FLOAT_TIME_DOMAIN_DATA_SUPPORT_TESTER_PROVIDER | ||
] | ||
}); | ||
|
||
const analyserNodeGetFloatTimeDomainDataMethodWrapper = injector.get(AnalyserNodeGetFloatTimeDomainDataMethodWrapper); | ||
const analyserNodeGetFloatTimeDomainDataSupportTester = injector.get(AnalyserNodeGetFloatTimeDomainDataSupportTester); | ||
|
||
const isSupportingAnalyserNodeGetFloatTimeDomainData = (context: TUnpatchedAudioContext | TUnpatchedOfflineAudioContext) => cacheTestResult( | ||
AnalyserNodeGetFloatTimeDomainDataSupportTester, | ||
() => analyserNodeGetFloatTimeDomainDataSupportTester.test(context) | ||
); | ||
|
||
export const createNativeAnalyserNode = ( | ||
nativeContext: TUnpatchedAudioContext | TUnpatchedOfflineAudioContext, | ||
options: Partial<IAnalyserOptions> = { } | ||
): TNativeAnalyserNode => { | ||
const nativeNode = nativeContext.createAnalyser(); | ||
|
||
assignNativeAudioNodeOptions(nativeNode, options); | ||
|
||
if (options.fftSize !== undefined) { | ||
nativeNode.fftSize = options.fftSize; | ||
} | ||
|
||
if (options.maxDecibels !== undefined) { | ||
nativeNode.maxDecibels = options.maxDecibels; | ||
} | ||
|
||
if (options.minDecibels !== undefined) { | ||
nativeNode.minDecibels = options.minDecibels; | ||
} | ||
|
||
if (options.smoothingTimeConstant !== undefined) { | ||
nativeNode.smoothingTimeConstant = options.smoothingTimeConstant; | ||
} | ||
|
||
// Bug #37: Only Edge and Safari create an AnalyserNode with the default properties. | ||
if (nativeNode.channelCount === 1) { | ||
nativeNode.channelCount = 2; | ||
} | ||
|
||
// Bug #36: Safari does not support getFloatTimeDomainData() yet. | ||
if (!isSupportingAnalyserNodeGetFloatTimeDomainData(nativeContext)) { | ||
analyserNodeGetFloatTimeDomainDataMethodWrapper.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,38 @@ | ||
import { createNativeAnalyserNode } from '../helpers/create-native-analyser-node'; | ||
import { getNativeNode } from '../helpers/get-native-node'; | ||
import { isOwnedByContext } from '../helpers/is-owned-by-context'; | ||
import { IAnalyserNode } from '../interfaces'; | ||
import { TNativeAnalyserNode, TNativeAudioNode, TUnpatchedOfflineAudioContext } from '../types'; | ||
import { AudioNodeRenderer } from './audio-node'; | ||
|
||
export class AnalyserNodeRenderer extends AudioNodeRenderer { | ||
|
||
private _nativeNode: null | TNativeAnalyserNode; | ||
|
||
private _proxy: IAnalyserNode; | ||
|
||
constructor (proxy: IAnalyserNode) { | ||
super(); | ||
|
||
this._nativeNode = null; | ||
this._proxy = proxy; | ||
} | ||
|
||
public async render (offlineAudioContext: TUnpatchedOfflineAudioContext): Promise<TNativeAudioNode> { | ||
if (this._nativeNode !== null) { | ||
return this._nativeNode; | ||
} | ||
|
||
this._nativeNode = <TNativeAnalyserNode> 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)) { | ||
this._nativeNode = createNativeAnalyserNode(offlineAudioContext); | ||
} | ||
|
||
await this._connectSources(offlineAudioContext, <TNativeAudioNode> this._nativeNode); | ||
|
||
return <TNativeAudioNode> this._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
Oops, something went wrong.