Skip to content

Commit

Permalink
feat: cache OfflineAudioContext used to create AudioBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin committed Mar 28, 2018
1 parent 2a2e18c commit 5d8b395
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/audio-buffer.ts
Expand Up @@ -11,6 +11,7 @@ import {
AUDIO_BUFFER_COPY_CHANNEL_METHODS_SUPPORT_TESTER_PROVIDER,
AudioBufferCopyChannelMethodsSupportTester
} from './support-testers/audio-buffer-copy-channel-methods';
import { TUnpatchedOfflineAudioContext } from './types';
import { AUDIO_BUFFER_WRAPPER_PROVIDER, AudioBufferWrapper } from './wrappers/audio-buffer';
import {
AUDIO_BUFFER_COPY_CHANNEL_METHODS_WRAPPER_PROVIDER,
Expand All @@ -37,6 +38,8 @@ const audioBufferCopyChannelMethodsSupportTester = injector.get(AudioBufferCopyC
const audioBufferCopyChannelMethodsWrapper = injector.get(AudioBufferCopyChannelMethodsWrapper);
const unpatchedOfflineAudioContextConstructor = injector.get(nptchdFflnDCntxtCnstrctr);

let unpatchedOfflineAudioContext: null | TUnpatchedOfflineAudioContext = null;

export class AudioBuffer implements IAudioBuffer {

public duration: number;
Expand All @@ -54,7 +57,10 @@ export class AudioBuffer implements IAudioBuffer {

const { length, numberOfChannels, sampleRate } = <typeof DEFAULT_OPTIONS & IAudioBufferOptions> { ...DEFAULT_OPTIONS, ...options };

const unpatchedOfflineAudioContext = new unpatchedOfflineAudioContextConstructor(1, 1, 44100);
if (unpatchedOfflineAudioContext === null) {
unpatchedOfflineAudioContext = new unpatchedOfflineAudioContextConstructor(1, 1, 44100);
}

const audioBuffer = unpatchedOfflineAudioContext.createBuffer(numberOfChannels, length, sampleRate);

// Bug #5: Safari does not support copyFromChannel() and copyToChannel().
Expand All @@ -64,7 +70,10 @@ export class AudioBuffer implements IAudioBuffer {
} else if (
!cacheTestResult(
AudioBufferCopyChannelMethodsSupportTester,
() => audioBufferCopyChannelMethodsSupportTester.test(unpatchedOfflineAudioContext)
() => {
return (unpatchedOfflineAudioContext !== null &&
audioBufferCopyChannelMethodsSupportTester.test(unpatchedOfflineAudioContext));
}
)
) {
audioBufferCopyChannelMethodsWrapper.wrap(audioBuffer);
Expand Down

0 comments on commit 5d8b395

Please sign in to comment.