-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
61 additions
and
18 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
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,51 @@ | ||
/* | ||
Copyright 2021 Picovoice Inc. | ||
You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" | ||
file accompanying this source. | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
type ProcessorPolyfill = { | ||
port?: { | ||
onmessage?: (event: MessageEvent<{ buffer: Float32Array[] }>) => void; | ||
} | ||
}; | ||
|
||
// @ts-ignore window.webkitAudioContext | ||
window.AudioContext = window.AudioContext || window.webkitAudioContext; | ||
|
||
if (typeof AudioWorkletNode !== 'function' || !('audioWorklet' in AudioContext.prototype)) { | ||
// @ts-ignore | ||
AudioContext.prototype.audioWorklet = { | ||
addModule: async function(moduleURL: string | URL, options?: WorkletOptions): Promise<void> { | ||
return; | ||
}, | ||
}; | ||
|
||
// @ts-ignore | ||
// eslint-disable-next-line no-native-reassign | ||
window.AudioWorkletNode = function(context: AudioContext, processorName: string, options: any): ScriptProcessorNode { | ||
const { numberOfChannels = 1, frameLength = 512 } = options && options.processorOptions; | ||
const scriptProcessor: ScriptProcessorNode & ProcessorPolyfill = context.createScriptProcessor(frameLength, numberOfChannels, numberOfChannels); | ||
|
||
if (!scriptProcessor.port) { | ||
scriptProcessor.port = {}; | ||
} | ||
|
||
scriptProcessor.onaudioprocess = (event: AudioProcessingEvent): void => { | ||
if (scriptProcessor.port && scriptProcessor.port.onmessage) { | ||
const buffer = []; | ||
for (let i = 0; i < event.inputBuffer.numberOfChannels; i ) { | ||
buffer.push(event.inputBuffer.getChannelData(i)); | ||
} | ||
scriptProcessor.port.onmessage({ data: { buffer } } as MessageEvent); | ||
} | ||
}; | ||
|
||
return scriptProcessor; | ||
}; | ||
} |
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