Skip to content

Instantly share code, notes, and snippets.

@gotomypc
Forked from Dobby233Liu/a.js
Created October 23, 2022 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotomypc/86a98a7a60b99fc041321f84ce2a2b1c to your computer and use it in GitHub Desktop.
Save gotomypc/86a98a7a60b99fc041321f84ce2a2b1c to your computer and use it in GitHub Desktop.
my messing w/ msedge dev read aloud. ONLY RUN IN edge dev. i give up, so it wont work properly
var ARRAY_LENGTH = 16;
var MIN_HEX_LENGTH = 2;
class UUID {
static createUUID() {
const array = new Uint8Array(ARRAY_LENGTH);
window.crypto.getRandomValues(array);
let uuid = '';
for (let i = 0; i < ARRAY_LENGTH; i++) {
let hexString = array[i].toString(ARRAY_LENGTH);
hexString = this._formatHexString(hexString);
uuid += hexString;
}
return uuid;
}
static _formatHexString(hexString) {
if (hexString.length < MIN_HEX_LENGTH) {
hexString = '0' + hexString;
}
return hexString;
}
}
function startTTS(data, key, cb){ // { [outFmt] }
var socket = new WebSocket('wss://speech.platform.bing.com/consumer/speech/synthesize' +
'/readaloud/edge/v1?TrustedClientToken=' + key);
var fmt = {"context":{"synthesis":{"audio":{"metadataoptions":{"sentenceBoundaryEnabled":"false","wordBoundaryEnabled":"true"},"outputFormat":"audio-24khz-48kbitrate-mono-mp3"}}}};
if (data && data.outFmt) fmt.context.synthesis.audio.outputFormat = data.outFmt;
socket.addEventListener('message', function (event) {
console.log("msg", event);
var blob = new Blob([event.data], {type: 'audio/mpeg'});
var blobUrl = URL.createObjectURL(blob);
console.log(blob);
setTimeout((objectUrl) => {
new Audio(objectUrl).play();
}, 2000, blobUrl);
});
socket.addEventListener('open', function (event) {
socket.send('X-Timestamp:' + new Date().toString() +
'\r\nContent-Type:application/json; ' +
'charset=utf-8\r\nPath:speech.config\r\n\r\n' + JSON.stringify(fmt));
cb(socket);
});
return socket;
}
window.uuid = UUID.createUUID();
chrome.systemPrivate.getApiKey(function(a){window.key=a;
var tts = startTTS({},key,(tts)=>{
var ssml = 'X-RequestId:' + uuid +
'\r\nContent-Type:application\/ssml+xml\r\nX-Timestamp:'
+ new Date().toString() + 'Z\r\nPath:ssml\r\n\r\n' + `
<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'><voice name='Microsoft Server Speech Text to Speech Voice (zh-CN, XiaoxiaoNeural)'><prosody pitch='+0Hz' rate ='+0%' volume='+0%'> How to download files using axios</prosody></voice></speak>
`;
tts.send(ssml);
// tts.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment