2020-04-01 15:56:23 -04:00
|
|
|
import {
|
|
|
|
AbstractInput,
|
2020-09-24 16:06:52 -04:00
|
|
|
FilterMode,
|
2020-04-01 15:56:23 -04:00
|
|
|
InputConsumer,
|
2020-08-21 07:37:10 -04:00
|
|
|
InputConsumerType,
|
|
|
|
InputEvents,
|
|
|
|
InputStartResult,
|
|
|
|
InputState,
|
|
|
|
LevelMeter
|
2020-04-01 15:56:23 -04:00
|
|
|
} from "tc-shared/voice/RecorderBase";
|
|
|
|
import {audio} from "tc-native/connection";
|
|
|
|
import {tr} from "tc-shared/i18n/localize";
|
2020-08-21 07:37:10 -04:00
|
|
|
import {Registry} from "tc-shared/events";
|
|
|
|
import {Filter, FilterType, FilterTypeClass} from "tc-shared/voice/Filter";
|
|
|
|
import {NativeFilter, NStateFilter, NThresholdFilter, NVoiceLevelFilter} from "./AudioFilter";
|
|
|
|
import {IDevice} from "tc-shared/audio/recorder";
|
|
|
|
import {LogCategory, logWarn} from "tc-shared/log";
|
2020-09-24 16:06:52 -04:00
|
|
|
import NativeFilterMode = audio.record.FilterMode;
|
2020-04-01 15:56:23 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
export class NativeInput implements AbstractInput {
|
|
|
|
readonly events: Registry<InputEvents>;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
private nativeHandle: audio.record.AudioRecorder;
|
|
|
|
private nativeConsumer: audio.record.AudioConsumer;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
private state: InputState;
|
|
|
|
private deviceId: string | undefined;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
private registeredFilters: (Filter & NativeFilter)[] = [];
|
|
|
|
private filtered = false;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
constructor() {
|
|
|
|
this.events = new Registry<InputEvents>();
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
this.nativeHandle = audio.record.create_recorder();
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
this.nativeConsumer = this.nativeHandle.create_consumer();
|
|
|
|
this.nativeConsumer.callback_ended = () => {
|
|
|
|
this.filtered = true;
|
|
|
|
this.events.fire("notify_voice_end");
|
|
|
|
};
|
|
|
|
this.nativeConsumer.callback_started = () => {
|
|
|
|
this.filtered = false;
|
|
|
|
this.events.fire("notify_voice_start");
|
|
|
|
};
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
this.state = InputState.PAUSED;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
async start(): Promise<InputStartResult> {
|
|
|
|
if(this.state === InputState.RECORDING) {
|
|
|
|
logWarn(LogCategory.VOICE, tr("Tried to start an input recorder twice."));
|
|
|
|
return InputStartResult.EOK;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
this.state = InputState.INITIALIZING;
|
|
|
|
try {
|
|
|
|
const state = await new Promise<audio.record.DeviceSetResult>(resolve => this.nativeHandle.set_device(this.deviceId, resolve));
|
|
|
|
|
|
|
|
if(state !== "success") {
|
|
|
|
if(state === "invalid-device") {
|
|
|
|
return InputStartResult.EDEVICEUNKNOWN;
|
|
|
|
} else if(state === undefined) {
|
|
|
|
throw tr("invalid set device result state");
|
|
|
|
}
|
|
|
|
throw state;
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
await new Promise((resolve, reject) => this.nativeHandle.start(result => {
|
|
|
|
if(result === true) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(typeof result === "string" ? result : tr("failed to start input"));
|
|
|
|
}
|
|
|
|
}));
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
this.state = InputState.RECORDING;
|
|
|
|
return InputStartResult.EOK;
|
|
|
|
} finally {
|
|
|
|
if(this.state === InputState.INITIALIZING) {
|
|
|
|
this.state = InputState.PAUSED;
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
async stop(): Promise<void> {
|
|
|
|
if(this.state === InputState.PAUSED)
|
|
|
|
return;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
this.nativeHandle.stop();
|
|
|
|
this.state = InputState.PAUSED;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
async setDeviceId(device: string | undefined): Promise<void> {
|
2020-09-24 16:06:52 -04:00
|
|
|
if(this.deviceId === device) {
|
2020-04-01 15:56:23 -04:00
|
|
|
return;
|
2020-09-24 16:06:52 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
this.deviceId = device;
|
|
|
|
await this.stop();
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
currentDeviceId(): string | undefined {
|
|
|
|
return this.deviceId;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
isFiltered(): boolean {
|
|
|
|
return this.filtered;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
removeFilter(filter: Filter) {
|
|
|
|
const index = this.registeredFilters.indexOf(filter as any);
|
|
|
|
if(index === -1) return;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
const [ f ] = this.registeredFilters.splice(index, 1);
|
|
|
|
f.finalize();
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
createFilter<T extends FilterType>(type: T, priority: number): FilterTypeClass<T> {
|
|
|
|
let filter;
|
2020-04-01 15:56:23 -04:00
|
|
|
switch (type) {
|
2020-08-21 07:37:10 -04:00
|
|
|
case FilterType.STATE:
|
|
|
|
filter = new NStateFilter(this, priority);
|
2020-04-01 15:56:23 -04:00
|
|
|
break;
|
2020-08-21 07:37:10 -04:00
|
|
|
|
|
|
|
case FilterType.THRESHOLD:
|
|
|
|
filter = new NThresholdFilter(this, priority);
|
2020-04-01 15:56:23 -04:00
|
|
|
break;
|
2020-08-21 07:37:10 -04:00
|
|
|
|
|
|
|
case FilterType.VOICE_LEVEL:
|
|
|
|
filter = new NVoiceLevelFilter(this, priority);
|
2020-04-01 15:56:23 -04:00
|
|
|
break;
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-08-21 07:37:10 -04:00
|
|
|
|
|
|
|
this.registeredFilters.push(filter);
|
|
|
|
return filter;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
supportsFilter(type: FilterType): boolean {
|
2020-04-01 15:56:23 -04:00
|
|
|
switch (type) {
|
2020-08-21 07:37:10 -04:00
|
|
|
case FilterType.VOICE_LEVEL:
|
|
|
|
case FilterType.THRESHOLD:
|
|
|
|
case FilterType.STATE:
|
2020-04-01 15:56:23 -04:00
|
|
|
return true;
|
2020-08-21 07:37:10 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
default:
|
|
|
|
return false;
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
currentState(): InputState {
|
|
|
|
return this.state;
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
currentConsumer(): InputConsumer | undefined {
|
|
|
|
return {
|
|
|
|
type: InputConsumerType.NATIVE
|
|
|
|
};
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
getNativeConsumer() : audio.record.AudioConsumer {
|
|
|
|
return this.nativeConsumer;
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
async setConsumer(consumer: InputConsumer): Promise<void> {
|
2020-09-24 16:06:52 -04:00
|
|
|
if(typeof(consumer) !== "undefined") {
|
2020-08-21 07:37:10 -04:00
|
|
|
throw "we only support native consumers!"; // TODO: May create a general wrapper?
|
2020-09-24 16:06:52 -04:00
|
|
|
}
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
return;
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
setVolume(volume: number) {
|
|
|
|
this.nativeHandle.set_volume(volume);
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
getVolume(): number {
|
|
|
|
return this.nativeHandle.get_volume();
|
|
|
|
}
|
2020-09-24 16:06:52 -04:00
|
|
|
|
|
|
|
getFilterMode(): FilterMode {
|
|
|
|
const mode = this.nativeConsumer.get_filter_mode();
|
|
|
|
switch (mode) {
|
|
|
|
case NativeFilterMode.Block:
|
|
|
|
return FilterMode.Block;
|
|
|
|
|
|
|
|
case NativeFilterMode.Bypass:
|
|
|
|
return FilterMode.Bypass;
|
|
|
|
|
|
|
|
case NativeFilterMode.Filter:
|
|
|
|
default:
|
|
|
|
return FilterMode.Filter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setFilterMode(mode: FilterMode) {
|
|
|
|
let nativeMode: NativeFilterMode;
|
|
|
|
switch (mode) {
|
|
|
|
case FilterMode.Filter:
|
|
|
|
nativeMode = NativeFilterMode.Filter;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode.Bypass:
|
|
|
|
nativeMode = NativeFilterMode.Bypass;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode.Block:
|
|
|
|
nativeMode = NativeFilterMode.Block;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nativeConsumer.set_filter_mode(nativeMode);
|
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
export class NativeLevelMeter implements LevelMeter {
|
|
|
|
readonly _device: IDevice;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
private _callback: (num: number) => any;
|
|
|
|
private _recorder: audio.record.AudioRecorder;
|
|
|
|
private _consumer: audio.record.AudioConsumer;
|
|
|
|
private _filter: audio.record.ThresholdConsumeFilter;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
constructor(device: IDevice) {
|
2020-04-01 15:56:23 -04:00
|
|
|
this._device = device;
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
async initialize() {
|
|
|
|
try {
|
|
|
|
this._recorder = audio.record.create_recorder();
|
|
|
|
this._consumer = this._recorder.create_consumer();
|
|
|
|
|
|
|
|
this._filter = this._consumer.create_filter_threshold(.5);
|
|
|
|
this._filter.set_attack_smooth(.75);
|
|
|
|
this._filter.set_release_smooth(.75);
|
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
await new Promise(resolve => this._recorder.set_device(this._device.deviceId, resolve));
|
2020-04-01 15:56:23 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this._recorder.start(flag => {
|
|
|
|
if (typeof flag === "boolean" && flag)
|
|
|
|
resolve();
|
|
|
|
else
|
|
|
|
reject(typeof flag === "string" ? flag : "failed to start");
|
2019-10-25 19:51:40 -04:00
|
|
|
});
|
|
|
|
});
|
2020-04-01 15:56:23 -04:00
|
|
|
} catch (error) {
|
|
|
|
if (typeof (error) === "string")
|
|
|
|
throw error;
|
|
|
|
console.warn(tr("Failed to initialize levelmeter for device %o: %o"), this._device, error);
|
|
|
|
throw "initialize failed (lookup console)";
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
/* references this variable, needs a destory() call, else memory leak */
|
|
|
|
this._filter.set_analyze_filter(value => {
|
|
|
|
(this._callback || (() => {
|
|
|
|
}))(value);
|
|
|
|
});
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
destroy() {
|
2020-04-01 15:56:23 -04:00
|
|
|
if (this._filter) {
|
|
|
|
this._filter.set_analyze_filter(undefined);
|
|
|
|
this._consumer.unregister_filter(this._filter);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-08-21 07:37:10 -04:00
|
|
|
|
|
|
|
if (this._consumer) {
|
2020-04-01 15:56:23 -04:00
|
|
|
this._recorder.delete_consumer(this._consumer);
|
2020-08-21 07:37:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(this._recorder) {
|
|
|
|
this._recorder.stop();
|
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
this._recorder = undefined;
|
|
|
|
this._consumer = undefined;
|
|
|
|
this._filter = undefined;
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-09-24 16:06:52 -04:00
|
|
|
getDevice(): IDevice {
|
2020-04-01 15:56:23 -04:00
|
|
|
return this._device;
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:06:52 -04:00
|
|
|
setObserver(callback: (value: number) => any) {
|
2020-04-01 15:56:23 -04:00
|
|
|
this._callback = callback;
|
|
|
|
}
|
|
|
|
}
|