From 1e9aa0d0832b6b8e7a0b2e9b6e313f41a2ff0fad Mon Sep 17 00:00:00 2001 From: daniel k Date: Mon, 20 Nov 2023 22:38:05 +0900 Subject: [PATCH] test bing tts --- public/offscreen.js | 31 ++++++++++++++----------------- src/background.js | 36 ++++++++++++++++++++++++++++++++++++ src/contentScript.js | 6 +++--- src/translator/bing.js | 25 +++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 20 deletions(-) diff --git a/public/offscreen.js b/public/offscreen.js index 4f9cbf88..f6f45377 100644 --- a/public/offscreen.js +++ b/public/offscreen.js @@ -1,27 +1,24 @@ +var audio; + // Listen for messages from the extension chrome.runtime.onMessage.addListener((msg) => { if ("play" in msg) playAudio(msg.play); + if ("stop" in msg) stopAudio(); }); // Play sound with access to DOM APIs -function playAudio({ source, volume }) { - const audio = new Audio(source); +async function playAudio({ source, volume }) { + audio = new Audio(source); audio.volume = volume; + // audio.playbackRate=0.5; audio.play(); + // audio.onended = onended } -// async function playSound(source = 'default.wav', volume = 1) { -// await createOffscreen(); -// await chrome.runtime.sendMessage({ play: { source, volume } }); -// } - -// createOffscreen(); -// Create the offscreen document if it doesn't already exist -// async function createOffscreen() { -// if (await chrome.offscreen.hasDocument()) return; -// await chrome.offscreen.createDocument({ -// url: "offscreen.html", -// reasons: ["AUDIO_PLAYBACK"], -// justification: "testing", // details for using the API -// }); -// } +function stopAudio() { + if (!audio) { + return; + } + audio.pause(); + audio.currentTime = 0; +} diff --git a/src/background.js b/src/background.js index fc4d2b08..aac631cf 100644 --- a/src/background.js +++ b/src/background.js @@ -260,3 +260,39 @@ function addInstallUrl(url) { } }); } + +// bing tts test ============================================================================== +// manifest offscreen permission required +// playTts(); + +async function playTts() { + var ttsBlob = await translator["bing"].requestTts("hello world", ""); + var base64Url = await getUrl(ttsBlob); + playSound(base64Url); +} + +function getUrl(blob) { + return new Promise((resolve, reject) => { + var reader = new FileReader(); + reader.onload = function () { + var dataUrl = reader.result; + resolve(dataUrl); + }; + reader.readAsDataURL(blob); + }); +} + +async function playSound(source = "default.wav", volume = 1) { + await createOffscreen(); + await chrome.runtime.sendMessage({ play: { source, volume } }); +} + +// Create the offscreen document if it doesn't already exist +async function createOffscreen() { + if (await chrome.offscreen.hasDocument()) return; + await chrome.offscreen.createDocument({ + url: "offscreen.html", + reasons: ["AUDIO_PLAYBACK"], + justification: "play tts", // details for using the API + }); +} diff --git a/src/contentScript.js b/src/contentScript.js index 67c101dd..aa80398b 100644 --- a/src/contentScript.js +++ b/src/contentScript.js @@ -228,9 +228,9 @@ async function translateWithReverse(word) { } function wrapInlineHtml(translatedText, transliteration, targetLang) { - var text = `
${encode( - translatedText - )}
`; + var text = ` ${encode(translatedText)} `; if (transliteration && setting["useTransliteration"] == "true") { text = ` diff --git a/src/translator/bing.js b/src/translator/bing.js index 0ffe8c2d..d7d86bf6 100644 --- a/src/translator/bing.js +++ b/src/translator/bing.js @@ -79,6 +79,8 @@ var bingLangCode = { let bingAccessToken; let bingBaseUrl = "https://www.bing.com/ttranslatev3"; let bingTokenUrl = "https://www.bing.com/translator"; +let bingTtsUrl = "https://www.bing.com/tfettts"; + export default class bing extends BaseTranslator { static langCodeJson = bingLangCode; @@ -115,6 +117,29 @@ export default class bing extends BaseTranslator { return { translatedText, detectedLang, transliteration }; } } + + static async requestTts(text, lang) { + const { token, key, IG, IID } = await getBingAccessToken(); + var bingLang = this.encodeLangCode(lang); + var voice = "en-US-AriaNeural"; + // + // + + return await ky + .post(bingTtsUrl, { + searchParams: { + IG, + IID: IID && IID.length ? IID + "." + bingAccessToken.count++ : "", + isVertical: "1", + }, + body: new URLSearchParams({ + ssml: `${text}`, + token, + key, + }), + }) + .blob(); + } } async function getBingAccessToken() {