Skip to content

Commit

Permalink
rev v1
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 committed Aug 18, 2022
1 parent 6ef122d commit c183537
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "yarn run http-server -a 0.0.0.0 -p 5000"
"start": "yarn run http-server -a localhost -p 5000"
},
"keywords": [
"pcm",
Expand Down
2 changes: 1 addition & 1 deletion package/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 45,7 @@ module.exports = {
//=========================================================================

// disallow trailing commas in object literals
'comma-dangle': [0, 'always-multiline'], //TODO: need discussion
'comma-dangle': [0, 'always-multiline'],
// disallow assignment in conditional expressions
'no-cond-assign': [2, 'always'],
// disallow use of console
Expand Down
2 changes: 1 addition & 1 deletion package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 53,7 @@ window.WebVoiceProcessor.browserCompatibilityCheck();
- 'mediaDevices' (basis for microphone enumeration / access)
- 'WebAssembly' (required for all Picovoice engines)
- 'webKitGetUserMedia' (legacy predecessor to getUserMedia)
- 'Worker' (required for downsampling and for all engine processing)
- 'Worker' (required for downsampler and for all engine processing)

## Installation

Expand Down
31 changes: 15 additions & 16 deletions package/src/downsampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 17,10 @@ import { wasiSnapshotPreview1Emulator } from './wasi_snapshot';
const PV_STATUS_SUCCESS = 10000;

type pv_downsampler_convert_num_samples_to_input_sample_rate_type = (objectAddress: number, frameLength: number) => number;
type pv_downsampler_init = (inputFrequency: number, outputFrequency: number, order: number, objectAddressAddress: number) => number;
type pv_downsampler_process = (objectAddress: number, inputBufferAddress: number, inputBufferSize: number, outputBufferAddress: number) => number;
type pv_downsampler_reset = (objectAddress: number) => void;
type pv_downsampler_delete = (objectAddress: number) => number;
type pv_downsampler_init_type = (inputFrequency: number, outputFrequency: number, order: number, objectAddressAddress: number) => number;
type pv_downsampler_process_type = (objectAddress: number, inputBufferAddress: number, inputBufferSize: number, outputBufferAddress: number) => number;
type pv_downsampler_reset_type = (objectAddress: number) => void;
type pv_downsampler_delete_type = (objectAddress: number) => number;

type DownsamplerWasmOutput = {
inputBufferAddress: number;
Expand All @@ -29,19 29,19 @@ type DownsamplerWasmOutput = {
objectAddress: number;
outputBufferAddress: number;
pvDownsamplerConvertNumSamplesToInputSampleRate: pv_downsampler_convert_num_samples_to_input_sample_rate_type;
pvDownsamplerInit: pv_downsampler_init;
pvDownsamplerProcess: pv_downsampler_process;
pvDownsamplerReset: pv_downsampler_reset;
pvDownsamplerDelete: pv_downsampler_delete;
pvDownsamplerInit: pv_downsampler_init_type;
pvDownsamplerProcess: pv_downsampler_process_type;
pvDownsamplerReset: pv_downsampler_reset_type;
pvDownsamplerDelete: pv_downsampler_delete_type;
frameLength: number;
version: string;
};

class Downsampler {
private readonly _pvDownsamplerConvertNumSamplesToInputSampleRate: pv_downsampler_convert_num_samples_to_input_sample_rate_type;
private readonly _pvDownsamplerDelete: pv_downsampler_delete;
private readonly _pvDownsamplerProcess: pv_downsampler_process;
private readonly _pvDownsamplerReset: pv_downsampler_reset;
private readonly _pvDownsamplerDelete: pv_downsampler_delete_type;
private readonly _pvDownsamplerProcess: pv_downsampler_process_type;
private readonly _pvDownsamplerReset: pv_downsampler_reset_type;

private readonly _inputBufferAddress: number;
private readonly _objectAddress: number;
Expand Down Expand Up @@ -148,7 148,7 @@ class Downsampler {
);

const alignedAlloc = instance.exports.aligned_alloc as CallableFunction;
const pvDownsamplerInit = instance.exports.pv_downsampler_init as pv_downsampler_init;
const pvDownsamplerInit = instance.exports.pv_downsampler_init as pv_downsampler_init_type;
const pvDownsamplerConvertNumSamplesToInputSampleRate =
instance.exports.pv_downsampler_convert_num_samples_to_input_sample_rate as
pv_downsampler_convert_num_samples_to_input_sample_rate_type;
Expand Down Expand Up @@ -200,19 200,18 @@ class Downsampler {
}

const pvDownsamplerReset = instance.exports
.pv_downsampler_reset as pv_downsampler_reset;
.pv_downsampler_reset as pv_downsampler_reset_type;
const pvDownsamplerProcess = instance.exports
.pv_downsampler_process as pv_downsampler_process;
.pv_downsampler_process as pv_downsampler_process_type;
const pvDownsamplerDelete = instance.exports
.pv_downsampler_delete as pv_downsampler_delete;
.pv_downsampler_delete as pv_downsampler_delete_type;

return {
inputBufferAddress: inputBufferAddress,
inputFrameLength: inputFrameLength,
memory: memory,
objectAddress: objectAddress,
outputBufferAddress: outputBufferAddress,
pvDownsamplerConvertNumSamplesToInputSampleRate:
pvDownsamplerConvertNumSamplesToInputSampleRate,
pvDownsamplerInit: pvDownsamplerInit,
pvDownsamplerProcess: pvDownsamplerProcess,
Expand Down
3 changes: 2 additions & 1 deletion package/src/downsampler_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,8 @@
import DsWorker from 'web-worker:./downsampler_worker_handler.ts';

import {
DownsamplerWorkerInitResponse, DownsamplerWorkerNumRequiredInputSamplesResponse,
DownsamplerWorkerInitResponse,
DownsamplerWorkerNumRequiredInputSamplesResponse,
DownsamplerWorkerProcessResponse,
DownsamplerWorkerReleaseResponse,
DownsamplerWorkerResetResponse,
Expand Down
6 changes: 4 additions & 2 deletions package/src/downsampler_worker_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,9 @@
specific language governing permissions and limitations under the License.
*/

// @ts-ignore
declare const self: ServiceWorkerGlobalScope;

import { DownsamplerWorkerRequest } from './types';
import Downsampler from './downsampler';

Expand Down Expand Up @@ -62,7 65,6 @@ onmessage = async function(event: MessageEvent<DownsamplerWorkerRequest>): Promi
self.postMessage({
command: 'ok',
result: outputBuffer,
// @ts-ignore
}, [outputBuffer.buffer]);
} catch (e: any) {
self.postMessage({
Expand Down Expand Up @@ -121,7 123,7 @@ onmessage = async function(event: MessageEvent<DownsamplerWorkerRequest>): Promi
default:
// @ts-ignore
// eslint-disable-next-line no-console
console.warn(`Unhandled message in downsampling_worker.ts: ${event.data.command}`);
console.warn(`Unhandled message in downsampler_worker.ts: ${event.data.command}`);
break;
}
};
4 changes: 2 additions & 2 deletions package/src/engines/vu_meter_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 19,8 @@ const process = (frames: Int16Array): number => {
(accumulator, frame) => accumulator frame ** 2,
0,
);
const rms = Math.sqrt(sum / frames.length) / INT_16_MAX;
return 20 * Math.log10(Math.max(rms, EPSILON));
const rms = (sum / frames.length) / INT_16_MAX / INT_16_MAX;
return 10 * Math.log10(Math.max(rms, EPSILON));
};

onmessage = (e: MessageEvent<WvpMessageEvent>): void => {
Expand Down
1 change: 1 addition & 0 deletions package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 18,5 @@ export {
WebVoiceProcessorOptions,
browserCompatibilityCheck,
WebVoiceProcessor,
DownsamplerWorker,
};

0 comments on commit c183537

Please sign in to comment.