How to get PCM stream from Azure's SpeechSynthesizer

David A 11 Reputation points
2022-02-09T00:49:39.257+00:00

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;
}
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,713 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,736 Reputation points
    2022-02-10T04:18:05.51+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.