@David A Thanks, Here are the Speech synthesis samples to pull audio output stream.
How to get PCM stream from Azure's SpeechSynthesizer
David A
11
Reputation points
I am trying to get a PCM stream so I can use it in apis like Discord.Net (https://discordnet.dev/guides/voice/sending-voice.html) but it doesn't seem to work at all
Can anyone help me with how I can get these to work?
public static async Task<MemoryStream> GetTTSStream(string text)
{
var config = SpeechConfig.FromSubscription("", "");
MemoryStream outputs = new();
using var stream = AudioOutputStream.CreatePullStream();
using (var streamConfig = AudioConfig.FromStreamOutput(stream))
using (var synthesizer = new SpeechSynthesizer(config, streamConfig))
{
using var result = await synthesizer.SpeakTextAsync(text);
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Logger.Warning($"Speech synthesized for text [{text}], and the audio was written to output stream.");
outputs.Write(result.AudioData);
Logger.Warning($"Size: {outputs.Length}");
return outputs;
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Logger.Warning($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Logger.Warning($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Logger.Warning($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Logger.Warning($"CANCELED: Did you update the subscription info?");
}
return null;
}
}
return null;
}