Skip to content

Commit

Permalink
add try catch to ingore tts errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stCarolas committed Jan 26, 2024
1 parent 805fb79 commit 2d90016
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/components/ConfigurationPage/WidgetConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ interface WidgetConfigurationProps {

function getSettingsWidget(id: string, type: string, onChange: Function) {
switch (type) {
case "payment-alerts":
return <PaymentAlertSettings id={id} onChange={onChange} />;
case "payments":
return <BaseSettings id={id} onChange={onChange} />;
case "media":
Expand All @@ -33,6 +31,8 @@ function getSettingsWidget(id: string, type: string, onChange: Function) {
return <BaseSettings id={id} onChange={onChange} />;
case "player-control":
return <BaseSettings id={id} onChange={onChange} />;
case "payment-alerts":
return <PaymentAlertSettings id={id} onChange={onChange} />;
case "donaters-top-list":
return <DonatersTopListSettings id={id} onChange={onChange} />;
case "donation-timer":
Expand Down
1 change: 1 addition & 0 deletions src/logic/alert/AlertController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class AlertController {
this.voiceController.playAudio(alert, () => {
this.voiceController.pronounceTitle(alert, data, () =>
this.voiceController.pronounceMessage(alert, data, () => {
log.debug("clearing alert");
this.clear();
this.resumePlayer();
ackFunction();
Expand Down
98 changes: 67 additions & 31 deletions src/logic/voice/VoiceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ function base64ToArrayBuffer(base64) {
export class VoiceController {
audioCtx = new AudioContext();
playingSource: AudioBufferSourceNode | null = null;
onEndHandler: any|null = null;
onEndHandler: any | null = null;
recipientId: string;

constructor(recipientId: string) {
this.recipientId = recipientId;
}

pronounceTitle(alert: any, data: any, onEndHandler: any) {
log.debug("start to pronounce title");
const playIfMessageEmpty = this.findSetting(
alert.properties,
"enableVoiceWhenMessageIsEmpty",
Expand All @@ -40,7 +41,6 @@ export class VoiceController {
}
return;
}
log.debug("text to voice " + data.message);
const message = data?.message?.trim();
const headerForVoice = message
? this.findSetting(alert.properties, "voiceTextTemplate", null)
Expand All @@ -59,50 +59,86 @@ export class VoiceController {
.replace("<amount>", data.amount.amount)
.replace("<minoramount>", data.amount.amount * 100)
.replace("<streamer>", this.recipientId);
this.voiceByGoogle(resultText).then((audio) =>
this.pronounce(audio, onEndHandler),
);
try {
this.voiceByGoogle(resultText).then((audio) =>
this.pronounce(audio, onEndHandler),
);
} catch (error) {
console.log(error);
if (onEndHandler) {
onEndHandler();
}
}
}

pronounceMessage(alert: any, data: any, onEndHandler: any) {
if (!data || !data.message || data.message.length === 0) {
log.debug("start to pronounce message");
try {
if (!data || !data.message || data.message.length === 0) {
if (onEndHandler) {
onEndHandler();
}
return;
}
this.voiceByMCS(data.message).then((audio) =>
this.pronounce(audio, onEndHandler),
);
} catch (error) {
console.log(error);
if (onEndHandler) {
onEndHandler();
}
return;
}
this.voiceByMCS(data.message).then((audio) =>
this.pronounce(audio, onEndHandler),
);
}

private pronounce(buffer: ArrayBuffer, onEndHandler: any) {
this.audioCtx.decodeAudioData(
buffer,
(buf) => {
let source = this.audioCtx.createBufferSource();
this.audioCtx
.decodeAudioData(
buffer,
(buf) => {
let source = this.audioCtx.createBufferSource();
if (onEndHandler) {
this.onEndHandler = onEndHandler;
source.addEventListener("ended", onEndHandler);
}
this.playingSource = source;
source.connect(this.audioCtx.destination);
source.buffer = buf;
source.loop = false;
source.start(0);
},
(err) => {
console.log(err);
},
)
.catch((error) => {
console.log(error);
if (onEndHandler) {
this.onEndHandler = onEndHandler;
source.addEventListener("ended", onEndHandler);
console.log('calling onEndHandler');
onEndHandler();
}
this.playingSource = source;
source.connect(this.audioCtx.destination);
source.buffer = buf;
source.loop = false;
source.start(0);
},
(err) => {
console.log(err);
},
);
});
}

playAudio(alert: any, onEndHandler: any) {
fetch(`${process.env.REACT_APP_FILE_API_ENDPOINT}/files/${alert.audio}`, {
method: "GET",
})
.then((response) => response.arrayBuffer())
.then((buffer) => this.pronounce(buffer, onEndHandler));
try {
fetch(`${process.env.REACT_APP_FILE_API_ENDPOINT}/files/${alert.audio}`, {
method: "GET",
})
.then((response) => response.arrayBuffer())
.then((buffer) => this.pronounce(buffer, onEndHandler))
.catch((error) => {
console.log(error);
if (onEndHandler) {
onEndHandler();
}
});
} catch (error) {
console.log(error);
if (onEndHandler) {
onEndHandler();
}
}
}

interrupt() {
Expand Down

0 comments on commit 2d90016

Please sign in to comment.