Exemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            var text = "The quick brown fox jumps over the lazy dog.";

            if (args?.Length > 1)
            {
                text = args[1];
            }

            // credentials
            var userProfile     = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            var keyFile         = Path.Combine(userProfile, "Projects", "DevKeys", "azure-speech.txt");
            var lines           = File.ReadAllLines(keyFile);
            var subscriptionKey = lines[0];
            var region          = lines[1];
            var resourceName    = lines[2];

            // caching
            var cacheDirName = Path.Combine(userProfile, "Projects");
            var cacheDir     = new DirectoryInfo(cacheDirName);
            var cache        = new CachingStrategy
            {
                new FileCacheLayer(cacheDir)
            };

            var voiceListDecoder = new JsonFactory <Voice[]>();

            var audioDecoder = new NAudioAudioDataDecoder();

            var ttsClient = new TextToSpeechClient(
                region,
                subscriptionKey,
                resourceName,
                voiceListDecoder,
                audioDecoder,
                cache);

            var voices = await ttsClient
                         .GetVoicesAsync()
                         .ConfigureAwait(false);

            var voice = Array.Find(voices, v => v.Locale == "en-US" && v.Gender == "Female");

            try
            {
                //await DecodeAudio(text, audioDecoder, ttsClient, voice);
                await PlayAudioAsync(text, audioDecoder, ttsClient, voice).ConfigureAwait(false);

                Console.WriteLine("Success!");
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
Exemplo n.º 2
0
        private static async Task PlayAudioAsync(string text, NAudioAudioDataDecoder audioDecoder, TextToSpeechClient ttsClient, Voice voice)
        {
            using var audioStream = await ttsClient
                                    .GetAudioDataStreamAsync(text, voice.ShortName)
                                    .ConfigureAwait(false);

            using var waveStream = audioDecoder.MakeDecodingStream(audioStream);
            var sr  = waveStream.WaveFormat.SampleRate;
            var bps = waveStream.WaveFormat.BitsPerSample;

            System.Console.Write($"{bps} * {sr} = {bps * sr}");
            //await Task.Yield();
            await PlayAsync(waveStream).ConfigureAwait(false);
        }
Exemplo n.º 3
0
        public async Task DecodeAudioAsync()
        {
            var audioRequest = await MakeSpeechRequestAsync().ConfigureAwait(false);

            var audioDecoder = new NAudioAudioDataDecoder
            {
                Format = audioRequest.OutputFormat
            };

            using var audio = await cache
                              .LoadAsync(audioDecoder, audioRequest)
                              .ConfigureAwait(false);

            Assert.AreEqual(MediaType.Audio.PCMA, audio.Format.ContentType);
            Assert.AreEqual(audioRequest.OutputFormat.SampleRate, audio.Format.SampleRate);
        }