Skip to content

Instantly share code, notes, and snippets.

@Dobby233Liu
Last active October 23, 2022 08:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Dobby233Liu/1daafa6ea07f780725250cdf1082bc2e to your computer and use it in GitHub Desktop.
Save Dobby233Liu/1daafa6ea07f780725250cdf1082bc2e 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();
});
});
@alekssamos
Copy link

Hello. Please tell me how to do this via Python 3.8 + aiohttp? I can get the data, but I don't know how to play it. ffmpeg does not understand raw data from the this websocket.

@Dobby233Liu
Copy link
Author

Dobby233Liu commented Sep 22, 2021

Hello. Please tell me how to do this via Python 3.8 + aiohttp? I can get the data, but I don't know how to play it. ffmpeg does not understand raw data from the this websocket.

Don't use this script as a reference, I don't really know tho

@alekssamos
Copy link

I withdraw the question. I did it all right. Everything is working well and successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment