问题描述
我需要一些指导。我的Azure函数(用node.js编写)会将一些随机文本转换为语音,然后将语音输出上传到Blob。我想这样做而不使用中间本地文件。 BlockBLobClient.upload方法需要Blob,字符串,ArrayBuffer,ArrayBufferView或返回新Readable流以及内容长度的函数。我无法从调用TTS返回的RequestPromise对象中获取这些(到目前为止,我正在使用request-promise来调用TTS)。任何建议将不胜感激。
谢谢
添加可以作为“ node TTSSample.js”进行测试的代码示例 示例代码基于
-
在https://github.com/Azure-Samples/azure-sdk-for-js-storage-blob-stream-nodejs/blob/master/v12/routes/index.js上共享的
-
Azure Blob流相关代码
-
在https://github.com/Azure-Samples/Cognitive-Speech-TTS/tree/master/Samples-Http/NodeJS
处的文本到语音的天蓝色示例代码 -
在所附的settings.js中替换适当的键和参数
-
我正在使用node.js运行时v12.18.3
-
在此代码示例中,硬编码了输入文本和输出blob名称。
// TTSSample.js // TTSSample.js // Converts given text to Audio and uploads to Azure Blob storage // Ultimately to be used in an Azure function // As of now input text as well as the target BLobStorage object are hard coded. // To install dependencies,run: npm install const xmlbuilder = require('xmlbuilder'); // request-promise has a dependency on request const rp = require('request-promise'); const fs = require('fs'); const readline = require('readline-sync'); const settings = require('./settings.js'); const multer = require('multer'); const inMemoryStorage = multer.memoryStorage(); const uploadStrategy = multer({ storage: inMemoryStorage }).single('image'); const getStream = require('into-stream'); const ONE_MEGABYTE = 1024 * 1024; const uploadOptions = { bufferSize: 4 * ONE_MEGABYTE,maxBuffers: 20 }; const ONE_MINUTE = 60 * 1000; // Blob Storage const { BlobServiceClient } = require('@azure/storage-blob'); // Gets an access token. function getAccessToken(subscriptionKey) { let options = { method: 'POST',uri: settings.issueTokenUri,headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey } } return rp(options); } // Converts text to speech using the input from readline. function textToSpeech_rp(accessToken,text) { // Create the SSML request. let xml_body = xmlbuilder.create('speak') .att('version','1.0') .att('xml:lang','en-us') .ele('voice') .att('xml:lang','en-us') .att('name','en-US-Guy24kRUS') // Short name for 'Microsoft Server Speech Text to Speech Voice (en-US,Guy24KRUS)' .txt(text) .end(); // Convert the XML into a string to send in the TTS request. let body = xml_body.toString(); //console.log("xml string is done"); let options = { method: 'POST',baseUrl: settings.cognitiveUri,url: 'cognitiveservices/v1',headers: { 'Authorization': 'Bearer ' + accessToken,'cache-control': 'no-cache','User-Agent': settings.cognitiveResource,'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm','Content-Type': 'application/ssml+xml' },body: body } console.log(options); let request = rp(options) .on('response',async (response) => { if (response.statusCode === 200) { console.log("Inside response"); const stream = getStream(response); //request.pipe(fs.createWriteStream('TTSOutput.wav')); const AZURE_STORAGE_CONNECTION_STRING = settings.storageConnectionString; const storageAccount = settings.storageAccount; const storageKey = settings.storageKey; const containerName = settings.audioContainer; // Create the BlobServiceClient object which will be used to create a container client const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING); // Get a reference to a container const containerClient = blobServiceClient.getContainerClient(containerName); // Create a unique name for the blob //const blobName = id + '.mp3'; const blobName = 'audio1.mp3'; // Get a block blob client const blockBlobClient = containerClient.getBlockBlobClient(blobName); //blockBlobClient.upload(buffer,61000); //blockBlobClient.upload(request.body); try { await blockBlobClient.uploadStream(stream,uploadOptions.bufferSize,uploadOptions.maxBuffers,{ blobHTTPHeaders: { blobContentType: "audio/mpeg3" } }); //res.render('success',{ message: 'File uploaded to Azure Blob storage.' }); console.log('Success'); } catch (err) { //res.render('error',{ message: err.message }); console.log("Failure",err.stack); } console.log("I am done"); } }); return request; }; // Use async and await to get the token before attempting // to convert text to speech. async function main() { const subscriptionKey = settings.subscriptionKey; //process.env.SPEECH_SERVICE_KEY; if (!subscriptionKey) { throw new Error('Environment variable for your subscription key is not set.') }; // Prompts the user to input text. //const text = readline.question('What would you like to convert to speech? '); const text = "Hello there"; try { const accessToken = await getAccessToken(subscriptionKey); //console.log("Access Token is done"); await textToSpeech_rp(accessToken,text); } catch (err) { console.log(`Something went wrong: ${err}`); console.log(err.stack); } } // Run the application main() //============ Settings.js (function() { "use strict"; module.exports = { // Replace with your own subscription key,service region (e.g.,"westus"),// and recognition language. subscriptionKey: "CognitiveServiceSubsctiptionKey",serviceRegion: "eastus",// e.g.,"westus" language: "en-US",issueTokenUri: 'https://eastus.api.cognitive.microsoft.com/sts/v1.0/issuetoken',cognitiveUri: 'https://eastus.tts.speech.microsoft.com/',cognitiveResource: 'CognitiveResourceName',// Storage storageAccount: "StorageAccount",storageKey: "sN/StorageKey==",storageConnectionString: "StorageConnectionString",audioContainer: "AudioContainer",}; }());
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)