Example #1
0
        private async Task Speak(string text, VoiceInfo voice)
        {
            Authentication authentication = new Authentication(SettingsHelper.Instance.SpeechApiKey, this.apiRegion);
            string         tokenString    = await authentication.RetrieveNewTokenAsync();

            string voiceUrl = string.Format(EndpointTemplate, this.apiRegion);

            WebRequest webRequest = WebRequest.Create(voiceUrl);

            webRequest.ContentType = SSMLContentType;
            webRequest.Headers.Add(AudioOutputFormatName, AudioOutputFormatValue);
            webRequest.Headers.Add("User-Agent", SSMLUserAgent);
            webRequest.Headers["Authorization"] = "Bearer " + tokenString;
            webRequest.Timeout = MaxTimeout;
            webRequest.Method  = "POST";

            string ssml = string.Format(SSMLTemplate, voice.Locale, voice.Name, text);

            byte[] btBodys = Encoding.UTF8.GetBytes(ssml);

            webRequest.ContentLength = btBodys.Length;
            webRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

            string      uniqueName = Guid.NewGuid().ToString();
            StorageFile audioFile  = await this.cacheDataFolder.CreateFileAsync(uniqueName + ".wav");

            WebResponse httpWebResponse = await webRequest.GetResponseAsync();

            using (Stream stream = httpWebResponse.GetResponseStream())
            {
                using (var outputStream = await audioFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await stream.CopyToAsync(outputStream.AsStreamForWrite());

                    await outputStream.FlushAsync();
                }
            }

            StorageFile metadataFile = await this.cacheDataFolder.CreateFileAsync(uniqueName + ".json");

            var cachedResult = new CachedResult {
                Text = text, VoiceId = voice.Id
            };

            using (var outoutStream = await metadataFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                string       json   = JsonConvert.SerializeObject(cachedResult, Formatting.Indented);
                StreamWriter writer = new StreamWriter(outoutStream.AsStreamForWrite());
                await writer.WriteAsync(json);

                writer.Close();
            }

            // Add result to cache list and play it
            cachedResult.AudioFilePath    = audioFile.Path;
            cachedResult.MetadataFilePath = metadataFile.Path;
            CachedResults.Insert(0, cachedResult);

            PlayAudioFile(audioFile.Path);
        }
Example #2
0
        private async Task DeleteCachedResult(CachedResult dataContext)
        {
            try
            {
                var audioFile = await StorageFile.GetFileFromPathAsync(dataContext.AudioFilePath);

                await audioFile.DeleteAsync();
            }
            catch { }

            try
            {
                var metadataFile = await StorageFile.GetFileFromPathAsync(dataContext.MetadataFilePath);

                await metadataFile.DeleteAsync();
            }
            catch { }

            CachedResults.Remove(dataContext);
        }