52 lines
1.9 KiB
TypeScript
Raw Normal View History

import {audio as naudio} from "tc-native/connection";
import {SoundBackend, SoundFile} from "tc-shared/audio/Sounds";
import * as paths from "path";
2021-03-25 13:05:58 +01:00
import * as loader from "tc-loader";
import {Stage} from "tc-loader";
import {ipcRenderer} from "electron";
import {LogCategory, logDebug, logInfo} from "tc-shared/log";
2021-03-25 13:05:58 +01:00
let uiFilePath;
export class NativeSoundBackend implements SoundBackend {
playSound(sound: SoundFile): Promise<void> {
return new Promise((resolve, reject) => {
2021-03-25 13:05:58 +01:00
if(!uiFilePath) {
reject("Mussing UI file path");
return;
}
2021-03-25 13:05:58 +01:00
const path = paths.join(uiFilePath, sound.path);
console.log("replaying %s (volume: %f) from %s", sound.path, sound.volume, path);
naudio.sounds.playback_sound({
callback: (result, message) => {
2021-03-25 13:05:58 +01:00
if(result == naudio.sounds.PlaybackResult.SUCCEEDED) {
resolve();
2021-03-25 13:05:58 +01:00
} else {
reject(naudio.sounds.PlaybackResult[result].toLowerCase() + ": " + message);
2021-03-25 13:05:58 +01:00
}
},
file: path,
volume: typeof sound.volume === "number" ? sound.volume : 1
});
});
}
2021-03-25 13:05:58 +01:00
}
2021-03-25 13:05:58 +01:00
//tc-get-ui-path
loader.register_task(Stage.JAVASCRIPT_INITIALIZING, {
name: "sound initialize",
priority: 50,
function: async () => {
uiFilePath = await ipcRenderer.invoke("tc-get-ui-path");
if(!uiFilePath) {
logInfo(LogCategory.AUDIO, tr("Missing UI path. App sounds will not work."));
} else {
if(uiFilePath[0] === '/' && uiFilePath[2] === ':') {
//e.g.: /C:/test...
uiFilePath = uiFilePath.substr(1);
}
logDebug(LogCategory.AUDIO, tr("Received UI path: %s"), uiFilePath);
}
}
})