Skip to content

Commit

Permalink
add audio worklet polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 authored Aug 19, 2022
1 parent ab54dac commit fd02a9b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 17,7 @@
"author": "Picovoice Inc",
"license": "Apache-2.0",
"dependencies": {
"@picovoice/web-voice-processor": "^3.0.0",
"@picovoice/web-voice-processor": "^3.0.1",
"http-server": "^14.0.0",
"wavefile": "^11.0.0"
}
Expand Down
8 changes: 4 additions & 4 deletions demo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 2,10 @@
# yarn lockfile v1


"@picovoice/web-voice-processor@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@picovoice/web-voice-processor/-/web-voice-processor-3.0.0.tgz#0dbdbcfa79022fc54f6b51ee4294efb847fd736f"
integrity sha512-EtkCvUOklmWESabT2rVfrHsUGugg6zj1JBV9Jl9h7nzaGVXPMhDVuiwtZpT8TFVSLKXNOV31QsoSHnzN2CYjvQ==
"@picovoice/web-voice-processor@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@picovoice/web-voice-processor/-/web-voice-processor-3.0.1.tgz#e2adf41efe31971a58fa5f5ac09f34daab7dff72"
integrity sha512- B23Yj leSm6Kk Pv2K94X2yZF2ccapFrCOMZ90G4uJmZqeLpTkSrz7UI43vJgdxMc0dp lS0lP2zMtpzxW5HA==

async@^2.6.2:
version "2.6.3"
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
{
"name": "@picovoice/web-voice-processor",
"version": "3.0.0",
"version": "3.0.1",
"description": "Real-time audio processing for voice, in web browsers",
"entry": "src/index.ts",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -48,7 48,6 @@
"@babel/plugin-transform-runtime": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/runtime": "^7.18.3",
"@free-side/audioworklet-polyfill": "^1.1.3-beta.4",
"@picovoice/web-utils": "=1.2.3",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^18.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
import '@free-side/audioworklet-polyfill';
import './polyfill/audioworklet_polyfill';

import { WvpMessageEvent, WebVoiceProcessorOptions } from './types';

Expand Down
51 changes: 51 additions & 0 deletions src/polyfill/audioworklet_polyfill.ts
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;
};
}
8 changes: 3 additions & 5 deletions src/web_voice_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 19,6 @@ import { PvEngine, WebVoiceProcessorOptions } from './types';
import { AudioDumpEngine } from './engines/audio_dump_engine';
import VuMeterWorker from 'web-worker:./engines/vu_meter_worker.ts';

// @ts-ignore window.webkitAudioContext
window.AudioContext = window.AudioContext || window.webkitAudioContext;

/**
* Obtain microphone permission and audio stream;
* Down sample audio into 16kHz single-channel PCM for speech recognition (via DownsamplerWorker).
Expand Down Expand Up @@ -53,9 50,9 @@ export class WebVoiceProcessor {
* @param options Startup options.
* @return WebVoiceProcessor singleton.
*/
public static async instance(
public static instance(
options: WebVoiceProcessorOptions = {},
): Promise<WebVoiceProcessor> {
): WebVoiceProcessor {
if (!this._instance) {
this._instance = new WebVoiceProcessor(options);
} else {
Expand Down Expand Up @@ -266,6 263,7 @@ export class WebVoiceProcessor {
'recorder-processor',
{
processorOptions: {
frameLength,
numberOfChannels
}
}
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1231,11 1231,6 @@
minimatch "^3.1.2"
strip-json-comments "^3.1.1"

"@free-side/audioworklet-polyfill@^1.1.3-beta.4":
version "1.1.3-beta.4"
resolved "https://registry.yarnpkg.com/@free-side/audioworklet-polyfill/-/audioworklet-polyfill-1.1.3-beta.4.tgz#97143ccfd1ef96082f6849767bf4939fc2fb8927"
integrity sha512-w6Rub/Mu9BAdWvJaCsATqR0L/SEiRM6neQ73g8fcjxBzRfjC90C4e0TZ7nQ0qNzufh2tu83b14kI/j0JzlgPzQ==

"@humanwhocodes/config-array@^0.10.4":
version "0.10.4"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.10.4.tgz#01e7366e57d2ad104feea63e72248f22015c520c"
Expand Down

0 comments on commit fd02a9b

Please sign in to comment.