如何将 SpeechToText 结果上传到 Azure 容器中?
How to upload the SpeechToText result into an Azure Container?
我正在使用 Azure 认知服务语音转文本来获取 Azure Blob 的转录。
得到结果后,我尝试将其上传回另一个 Azure 容器。
服务方式:
public async Task<MemoryStream> TextToSpeech(string subscriptionKey, string region, string text)
{
var speechTranslateConfig = SpeechTranslationConfig.FromSubscription(subscriptionKey, region);
using var synthesizer = new SpeechSynthesizer(speechTranslateConfig, null);
var speechSynthesisResult = await synthesizer.SpeakTextAsync(text);
using var audioDataStream = AudioDataStream.FromResult(speechSynthesisResult);
audioDataStream.SetPosition(0);
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0) ;
var stream = new MemoryStream(buffer);
return stream;
}
在 Controller 中,在我得到结果后,我尝试将结果上传到另一个容器中:
var translatedStream = await _speechService.TextToSpeech(_cognitiveServiceConfig.SubscriptionKey, _cognitiveServiceConfig.Region, text);
var translatedStorageFile = new StorageFile() { Stream = translatedStream, Name = $"{fileName}-TRANSLATED", Extension = audioExtension };
var translatedBlobUrl = _azureBlobStorageService.UploadFileAsync(translatedStorageFile, "translated").Result;
上传方式:
public async Task<string> UploadFileAsync(StorageFile storageFile, string container)
{
var containerClient = new BlobContainerClient(_cloudStorageAccountConfig.ConnectionString, container);
var blobClient = containerClient.GetBlobClient($"{storageFile.Name}.{storageFile.Extension}");
if (!blobClient.Exists())
{
await blobClient.UploadAsync(storageFile.Stream);
}
return blobClient.Uri.AbsoluteUri;
}
我认为我的这部分代码没有按预期工作(即使我在他们的文档中找到了这一点)并且流结果不正确:
audioDataStream.SetPosition(0);
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0) ;
var stream = new MemoryStream(buffer);
我这么说是因为如果我下载结果文件,它只有16kb,无法播放。
我相信你 运行 陷入这个问题是因为你总是在从 AudioDataStream
读取时创建 MemoryStream
的新实例,这就是你的流大小为 16KB 的原因(字节缓冲区的大小)。
你能试试像下面这样的东西吗(虽然未经测试的代码):
public async Task<MemoryStream> TextToSpeech(string subscriptionKey, string region, string text)
{
var speechTranslateConfig = SpeechTranslationConfig.FromSubscription(subscriptionKey, region);
using var synthesizer = new SpeechSynthesizer(speechTranslateConfig, null);
var speechSynthesisResult = await synthesizer.SpeakTextAsync(text);
using var audioDataStream = AudioDataStream.FromResult(speechSynthesisResult);
audioDataStream.SetPosition(0);
MemoryStream stream = new MemoryStream();//Create new memory stream
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0)
{
stream.Write(buffer);
}
return stream;
}
我设法通过使用 speechSynthesisResult
中的 AudioData
解决了这个问题,并从中创建了一个流:
var buffer = speechSynthesisResult.AudioData;
MemoryStream stream = new MemoryStream(buffer);
我正在使用 Azure 认知服务语音转文本来获取 Azure Blob 的转录。
得到结果后,我尝试将其上传回另一个 Azure 容器。
服务方式:
public async Task<MemoryStream> TextToSpeech(string subscriptionKey, string region, string text)
{
var speechTranslateConfig = SpeechTranslationConfig.FromSubscription(subscriptionKey, region);
using var synthesizer = new SpeechSynthesizer(speechTranslateConfig, null);
var speechSynthesisResult = await synthesizer.SpeakTextAsync(text);
using var audioDataStream = AudioDataStream.FromResult(speechSynthesisResult);
audioDataStream.SetPosition(0);
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0) ;
var stream = new MemoryStream(buffer);
return stream;
}
在 Controller 中,在我得到结果后,我尝试将结果上传到另一个容器中:
var translatedStream = await _speechService.TextToSpeech(_cognitiveServiceConfig.SubscriptionKey, _cognitiveServiceConfig.Region, text);
var translatedStorageFile = new StorageFile() { Stream = translatedStream, Name = $"{fileName}-TRANSLATED", Extension = audioExtension };
var translatedBlobUrl = _azureBlobStorageService.UploadFileAsync(translatedStorageFile, "translated").Result;
上传方式:
public async Task<string> UploadFileAsync(StorageFile storageFile, string container)
{
var containerClient = new BlobContainerClient(_cloudStorageAccountConfig.ConnectionString, container);
var blobClient = containerClient.GetBlobClient($"{storageFile.Name}.{storageFile.Extension}");
if (!blobClient.Exists())
{
await blobClient.UploadAsync(storageFile.Stream);
}
return blobClient.Uri.AbsoluteUri;
}
我认为我的这部分代码没有按预期工作(即使我在他们的文档中找到了这一点)并且流结果不正确:
audioDataStream.SetPosition(0);
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0) ;
var stream = new MemoryStream(buffer);
我这么说是因为如果我下载结果文件,它只有16kb,无法播放。
我相信你 运行 陷入这个问题是因为你总是在从 AudioDataStream
读取时创建 MemoryStream
的新实例,这就是你的流大小为 16KB 的原因(字节缓冲区的大小)。
你能试试像下面这样的东西吗(虽然未经测试的代码):
public async Task<MemoryStream> TextToSpeech(string subscriptionKey, string region, string text)
{
var speechTranslateConfig = SpeechTranslationConfig.FromSubscription(subscriptionKey, region);
using var synthesizer = new SpeechSynthesizer(speechTranslateConfig, null);
var speechSynthesisResult = await synthesizer.SpeakTextAsync(text);
using var audioDataStream = AudioDataStream.FromResult(speechSynthesisResult);
audioDataStream.SetPosition(0);
MemoryStream stream = new MemoryStream();//Create new memory stream
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0)
{
stream.Write(buffer);
}
return stream;
}
我设法通过使用 speechSynthesisResult
中的 AudioData
解决了这个问题,并从中创建了一个流:
var buffer = speechSynthesisResult.AudioData;
MemoryStream stream = new MemoryStream(buffer);