Created
May 26, 2020 01:01
-
-
Save risingBirdSong/6568ba42f13b4eeda6deb1f09e8b38f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { config } from "./config"; | |
import fs from "fs"; | |
import * as fileHelpers from "./helper"; | |
import request from "request"; | |
convertTextToSpeech("In mindfulness practice, we are learning how to return to the moment"); | |
function convertTextToSpeech(inputText: string) { | |
const authRequestOptions: request.CoreOptions = { | |
headers: { | |
"Ocp-Apim-Subscription-Key": config.texttospeechkey | |
} | |
}; | |
let base_url = 'https://westus.tts.speech.microsoft.com/' | |
let path = 'cognitiveservices/v1'; | |
let full_url = base_url + path; | |
request.post( | |
config.texttospeechendpoint, | |
authRequestOptions, | |
(err, response, body) => { | |
if (err) { | |
console.log('err', err); | |
} | |
const accessToken = response.body; | |
// console.log('response body', accessToken); | |
const payLoad = `<speak version='1.0' xml:lang='en-US'> | |
<voice xml:lang='en-US' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)'> | |
${inputText} | |
</voice> | |
</speak>`; | |
const requestOptions: request.CoreOptions = { | |
headers: { | |
"X-Microsoft-OutputFormat": "audio-24khz-96kbitrate-mono-mp3", | |
"Content-Type": "application/ssml+xml", | |
"Content-Length": payLoad.length, | |
"Authorization": "Bearer " + accessToken, | |
"User-Agent": "NodeJS" | |
}, | |
body: payLoad | |
}; | |
request.post( | |
full_url, | |
requestOptions, | |
(err, response, body) => { | |
if (err) { | |
console.log('err or!', err) | |
} | |
var convertedAudio = Buffer.from(response.body); | |
fs.writeFileSync(__dirname + "/output.mp3", response.body, | |
{ encoding: 'binary' }); | |
} | |
) | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment