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

73 lines
2.0 KiB
TypeScript

import * as native from "tc-native/connection";
//FIXME: Native audio initialize handle!
export interface Device {
device_id: string;
name: string;
}
let _initialized_callbacks: (() => any)[] = [];
export let _initialized = false;
export let _initializing = false;
export let _current_device: native.audio.AudioDevice;
export function initialized() : boolean {
return _initialized;
}
export function on_ready(cb: () => any) {
if(_initialized)
cb();
else
_initialized_callbacks.push(cb);
}
export function initialize() {
if(_initializing) return;
_initializing = true;
native.audio.initialize(() => {
_initialized = true;
for(const callback of _initialized_callbacks)
callback();
_initialized_callbacks = [];
});
return true;
}
export async function available_devices() : Promise<Device[]> {
return native.audio.available_devices().filter(e => e.output_supported || e.output_default);
}
export async function set_device(device_id?: string) : Promise<void> {
const dev = native.audio.available_devices().filter(e => e.device_id == device_id);
if(dev.length == 0) {
console.warn("Missing audio device with is %s", device_id);
throw "invalid device id";
}
try {
native.audio.playback.set_device(dev[0].device_id);
} catch(error) {
if(error instanceof Error)
throw error.message;
throw error;
}
_current_device = dev[0];
}
export function current_device() : Device {
if(_current_device)
return _current_device;
const dev = native.audio.available_devices().filter(e => e.output_default);
if(dev.length > 0)
return dev[0];
return {device_id: "default", name: "default"} as Device;
}
export function get_master_volume() : number {
return native.audio.playback.get_master_volume();
}
export function set_master_volume(volume: number) {
native.audio.playback.set_master_volume(volume);
}