TeaSpeak-Client/modules/renderer/audio/AudioRecorder.ts

500 lines
15 KiB
TypeScript
Raw Normal View History

import {
filter,
AbstractInput,
InputDevice,
InputState,
InputConsumer,
InputConsumerType, InputStartResult, LevelMeter
} from "tc-shared/voice/RecorderBase";
import {audio} from "tc-native/connection";
import {tr} from "tc-shared/i18n/localize";
interface NativeDevice extends InputDevice {
device_index: number;
native: any;
}
2019-10-25 19:51:40 -04:00
let _device_cache: NativeDevice[] = undefined;
export function devices() : InputDevice[] {
//TODO: Handle device updates!
if(!audio.initialized()) return [];
return _device_cache || (_device_cache = audio.available_devices().filter(e => e.input_supported || e.input_default).map(e => {
return {
unique_id: e.device_id,
channels: 2, /* TODO */
default_input: e.input_default,
supported: e.input_supported,
name: e.name,
driver: e.driver,
sample_rate: 48000, /* TODO! */
native: e
} as NativeDevice
}));
}
2019-10-25 19:51:40 -04:00
export function device_refresh_available() : boolean { return false; }
export function refresh_devices() : Promise<void> { throw "not supported yet!"; }
2019-10-25 19:51:40 -04:00
export function create_input() : AbstractInput {
return new NativeInput();
}
2019-10-25 19:51:40 -04:00
namespace filters {
export abstract class NativeFilter implements filter.Filter {
type: filter.Type;
handle: NativeInput;
enabled: boolean = false;
2019-10-25 19:51:40 -04:00
protected constructor(handle, type) { this.handle = handle; this.type = type; }
2019-10-25 19:51:40 -04:00
abstract initialize();
abstract finalize();
2019-10-25 19:51:40 -04:00
is_enabled(): boolean { return this.enabled; }
}
2019-10-25 19:51:40 -04:00
export class NThresholdFilter extends NativeFilter implements filter.ThresholdFilter {
2020-04-19 19:09:52 -04:00
static readonly frames_per_second = 1 / (960 / 48000);
private filter: audio.record.ThresholdConsumeFilter;
2019-10-25 19:51:40 -04:00
2020-04-19 19:09:52 -04:00
private _margin_frames: number = 25; /* 120ms */
private _threshold: number = 50;
private _callback_level: any;
2019-10-25 19:51:40 -04:00
private _attack_smooth = 0;
private _release_smooth = 0;
2019-10-25 19:51:40 -04:00
callback_level: (level: number) => any;
2019-10-25 19:51:40 -04:00
constructor(handle) {
super(handle, filter.Type.THRESHOLD);
2019-10-25 19:51:40 -04:00
Object.defineProperty(this, 'callback_level', {
get(): any {
return this._callback_level;
}, set(v: any): void {
if(v === this._callback_level)
return;
2019-10-25 19:51:40 -04:00
this._callback_level = v;
if(this.filter)
this.filter.set_analyze_filter(v);
},
enumerable: true,
configurable: false,
})
}
2019-10-25 19:51:40 -04:00
get_margin_frames(): number {
2020-04-19 19:09:52 -04:00
return this.filter ? this.filter.get_margin_time() * NThresholdFilter.frames_per_second : this._margin_frames;
}
2019-10-25 19:51:40 -04:00
get_threshold(): number {
return this.filter ? this.filter.get_threshold() : this._threshold;
}
2019-10-25 19:51:40 -04:00
set_margin_frames(value: number) {
this._margin_frames = value;
if(this.filter)
2020-04-19 19:09:52 -04:00
this.filter.set_margin_time(value / 960 / 1000);
}
2019-10-25 19:51:40 -04:00
get_attack_smooth(): number {
return this.filter ? this.filter.get_attack_smooth() : this._attack_smooth;
}
2019-10-25 19:51:40 -04:00
get_release_smooth(): number {
return this.filter ? this.filter.get_release_smooth() : this._release_smooth;
}
2019-10-25 19:51:40 -04:00
set_attack_smooth(value: number) {
this._attack_smooth = value;
if(this.filter)
this.filter.set_attack_smooth(value);
}
2019-10-25 19:51:40 -04:00
set_release_smooth(value: number) {
this._release_smooth = value;
if(this.filter)
this.filter.set_release_smooth(value);
}
2019-10-25 19:51:40 -04:00
set_threshold(value: number): Promise<void> {
if(typeof(value) === "string")
value = parseInt(value); /* yes... this happens */
this._threshold = value;
if(this.filter)
this.filter.set_threshold(value);
return Promise.resolve();
}
2019-10-25 19:51:40 -04:00
finalize() {
if(this.filter) {
if(this.handle.consumer)
this.handle.consumer.unregister_filter(this.filter);
this.filter = undefined;
2019-10-25 19:51:40 -04:00
}
}
2019-10-25 19:51:40 -04:00
initialize() {
if(!this.handle.consumer)
return;
2019-10-25 19:51:40 -04:00
this.finalize();
this.filter = this.handle.consumer.create_filter_threshold(this._threshold);
if(this._callback_level)
this.filter.set_analyze_filter(this._callback_level);
2020-04-19 19:09:52 -04:00
this.filter.set_margin_time(this._margin_frames / NThresholdFilter.frames_per_second);
this.filter.set_attack_smooth(this._attack_smooth);
this.filter.set_release_smooth(this._release_smooth);
}
}
2019-10-25 19:51:40 -04:00
export class NStateFilter extends NativeFilter implements filter.StateFilter {
private filter: audio.record.StateConsumeFilter;
private active = false;
2019-10-25 19:51:40 -04:00
constructor(handle) {
super(handle, filter.Type.STATE);
}
2019-10-25 19:51:40 -04:00
finalize() {
if(this.filter) {
if(this.handle.consumer)
this.handle.consumer.unregister_filter(this.filter);
this.filter = undefined;
2019-10-25 19:51:40 -04:00
}
}
initialize() {
if(!this.handle.consumer)
return;
2019-10-25 19:51:40 -04:00
this.finalize();
this.filter = this.handle.consumer.create_filter_state();
this.filter.set_consuming(this.active);
}
2019-10-25 19:51:40 -04:00
is_active(): boolean {
return this.active;
}
2019-10-25 19:51:40 -04:00
async set_state(state: boolean): Promise<void> {
if(this.active === state)
return;
this.active = state;
if(this.filter)
this.filter.set_consuming(state);
}
}
2019-10-25 19:51:40 -04:00
export class NVoiceLevelFilter extends NativeFilter implements filter.VoiceLevelFilter {
2020-04-19 19:09:52 -04:00
static readonly frames_per_second = 1 / (960 / 48000);
private filter: audio.record.VADConsumeFilter;
private level = 3;
2020-04-19 19:09:52 -04:00
private _margin_frames = 6;
2019-10-25 19:51:40 -04:00
constructor(handle) {
super(handle, filter.Type.VOICE_LEVEL);
}
2019-10-25 19:51:40 -04:00
finalize() {
if(this.filter) {
if(this.handle.consumer)
this.handle.consumer.unregister_filter(this.filter);
this.filter = undefined;
2019-10-25 19:51:40 -04:00
}
}
initialize() {
if(!this.handle.consumer)
return;
2019-10-25 19:51:40 -04:00
this.finalize();
this.filter = this.handle.consumer.create_filter_vad(this.level);
2020-04-19 19:09:52 -04:00
this.filter.set_margin_time(this._margin_frames / NVoiceLevelFilter.frames_per_second);
}
2019-10-25 19:51:40 -04:00
get_level(): number {
return this.level;
}
2019-10-25 19:51:40 -04:00
set_level(value: number) {
if(this.level === value)
return;
2019-10-25 19:51:40 -04:00
this.level = value;
if(this.filter) {
2019-10-25 19:51:40 -04:00
this.finalize();
this.initialize();
2019-10-25 19:51:40 -04:00
}
}
2019-10-25 19:51:40 -04:00
set_margin_frames(value: number) {
this._margin_frames = value;
if(this.filter)
2020-04-19 19:09:52 -04:00
this.filter.set_margin_time(value / NVoiceLevelFilter.frames_per_second);
}
2019-10-25 19:51:40 -04:00
get_margin_frames(): number {
2020-04-19 19:09:52 -04:00
return this.filter ? this.filter.get_margin_time() * NVoiceLevelFilter.frames_per_second : this._margin_frames;
2019-10-25 19:51:40 -04:00
}
}
}
2019-10-25 19:51:40 -04:00
export class NativeInput implements AbstractInput {
private handle: audio.record.AudioRecorder;
consumer: audio.record.AudioConsumer;
2019-10-25 19:51:40 -04:00
private _current_device: InputDevice;
private _current_state: InputState = InputState.PAUSED;
2019-10-25 19:51:40 -04:00
callback_begin: () => any;
callback_end: () => any;
2019-10-25 19:51:40 -04:00
private filters: filters.NativeFilter[] = [];
2019-10-25 19:51:40 -04:00
constructor() {
this.handle = audio.record.create_recorder();
2019-10-25 19:51:40 -04:00
this.consumer = this.handle.create_consumer();
this.consumer.callback_ended = () => {
if(this._current_state !== InputState.RECORDING)
return;
2019-10-25 19:51:40 -04:00
this._current_state = InputState.DRY;
if(this.callback_end)
this.callback_end();
};
this.consumer.callback_started = () => {
if(this._current_state !== InputState.DRY)
return;
2019-10-25 19:51:40 -04:00
this._current_state = InputState.RECORDING;
if(this.callback_begin)
this.callback_begin();
};
2019-10-25 19:51:40 -04:00
this._current_state = InputState.PAUSED;
}
2019-10-25 19:51:40 -04:00
/* TODO: some kind of finalize? */
current_consumer(): InputConsumer | undefined {
return {
type: InputConsumerType.NATIVE
};
}
2019-10-25 19:51:40 -04:00
async set_consumer(consumer: InputConsumer): Promise<void> {
if(typeof(consumer) !== "undefined")
throw "we only support native consumers!"; /* TODO: May create a general wrapper? */
return;
}
2019-10-25 19:51:40 -04:00
async set_device(_device: InputDevice | undefined): Promise<void> {
if(_device === this._current_device)
return;
2019-10-25 19:51:40 -04:00
this._current_device = _device;
try {
await new Promise(resolve => this.handle.set_device(this._current_device ? this._current_device.unique_id : undefined, resolve));
if(this._current_state !== InputState.PAUSED && this._current_device)
await new Promise((resolve, reject) => {
this.handle.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
});
});
} catch(error) {
console.warn(tr("Failed to start playback on new input device (%o)"), error);
throw error;
2019-10-25 19:51:40 -04:00
}
}
2019-10-25 19:51:40 -04:00
current_device(): InputDevice | undefined {
return this._current_device;
}
current_state(): InputState {
return this._current_state;
}
disable_filter(type: filter.Type) {
const filter = this.get_filter(type) as filters.NativeFilter;
if(filter.is_enabled())
filter.enabled = false;
filter.finalize();
}
2019-10-25 19:51:40 -04:00
enable_filter(type: filter.Type) {
const filter = this.get_filter(type) as filters.NativeFilter;
if(!filter.is_enabled()) {
filter.enabled = true;
filter.initialize();
2019-10-25 19:51:40 -04:00
}
}
2019-10-25 19:51:40 -04:00
clear_filter() {
for(const filter of this.filters) {
filter.enabled = false;
2019-10-25 19:51:40 -04:00
filter.finalize();
}
}
2019-10-25 19:51:40 -04:00
get_filter(type: filter.Type): filter.Filter | undefined {
for(const filter of this.filters)
if(filter.type === type)
return filter;
let _filter: filters.NativeFilter;
switch (type) {
case filter.Type.THRESHOLD:
_filter = new filters.NThresholdFilter(this);
break;
case filter.Type.STATE:
_filter = new filters.NStateFilter(this);
break;
case filter.Type.VOICE_LEVEL:
_filter = new filters.NVoiceLevelFilter(this);
break;
default:
throw "this filter isn't supported!";
2019-10-25 19:51:40 -04:00
}
this.filters.push(_filter);
return _filter;
}
2019-10-25 19:51:40 -04:00
supports_filter(type: filter.Type) : boolean {
switch (type) {
case filter.Type.THRESHOLD:
case filter.Type.STATE:
case filter.Type.VOICE_LEVEL:
return true;
default:
return false;
2019-10-25 19:51:40 -04:00
}
}
2019-10-25 19:51:40 -04:00
async start(): Promise<InputStartResult> {
try {
await this.stop();
} catch(error) {
console.warn(tr("Failed to stop old record session before start (%o)"), error);
2019-10-25 19:51:40 -04:00
}
this._current_state = InputState.DRY;
try {
if(this._current_device)
await new Promise((resolve, reject) => {
this.handle.start(flag => {
if(flag)
resolve();
else
reject("start failed");
2019-10-25 19:51:40 -04:00
});
});
2019-10-25 19:51:40 -04:00
for(const filter of this.filters)
if(filter.is_enabled())
filter.initialize();
return InputStartResult.EOK;
} catch(error) {
this._current_state = InputState.PAUSED;
throw error;
2019-10-25 19:51:40 -04:00
}
}
2019-10-25 19:51:40 -04:00
async stop(): Promise<void> {
this.handle.stop();
for(const filter of this.filters)
filter.finalize();
if(this.callback_end)
this.callback_end();
this._current_state = InputState.PAUSED;
2019-10-25 19:51:40 -04:00
}
get_volume(): number {
return this.handle.get_volume();
2019-10-25 19:51:40 -04:00
}
set_volume(volume: number) {
this.handle.set_volume(volume);
}
}
2019-10-25 19:51:40 -04:00
export async function create_levelmeter(device: InputDevice) : Promise<LevelMeter> {
const meter = new NativeLevelmenter(device as any);
await meter.initialize();
return meter;
}
2019-10-25 19:51:40 -04:00
class NativeLevelmenter implements LevelMeter {
readonly _device: NativeDevice;
2019-10-25 19:51:40 -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
constructor(device: NativeDevice) {
this._device = device;
}
2019-10-25 19:51:40 -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);
await new Promise(resolve => this._recorder.set_device(this._device ? this._device.unique_id : undefined, resolve));
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
});
});
} 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
}
/* 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
destory() {
if (this._filter) {
this._filter.set_analyze_filter(undefined);
this._consumer.unregister_filter(this._filter);
2019-10-25 19:51:40 -04:00
}
if (this._consumer)
this._recorder.delete_consumer(this._consumer);
this._recorder.stop();
this._recorder.set_device(undefined, () => {
}); /* -1 := No device */
this._recorder = undefined;
this._consumer = undefined;
this._filter = undefined;
}
2019-10-25 19:51:40 -04:00
device(): InputDevice {
return this._device;
2019-10-25 19:51:40 -04:00
}
set_observer(callback: (value: number) => any) {
this._callback = callback;
}
}