Beispiel #1
0
        public static string Download(string content, string language)
        {
            var errorCount = 0;

Retry:
            try
            {
                var fileName = Path.Combine(Vars.CacheDir, Conf.GetRandomFileName() + "GOOG.mp3");
                Bridge.ALog("(E2) 正在下载 TTS, 文件名: " + fileName);
                var instance = new GoogleTTS(content, language);
                instance.WriteFile(fileName);
                // validate if file is playable
                using (var reader = new AudioFileReader(fileName)) { }
                return(fileName);
            }
            catch (Exception ex)
            {
                Bridge.ALog("(E2) TTS 下载失败: " + ex.Message);
                errorCount += 1;
                Vars.TotalFails++;
                if (errorCount <= Vars.CurrentConf.DownloadFailRetryCount)
                {
                    goto Retry;
                }
                return(null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Play something, other options are defined by current configurations
        /// </summary>
        /// <param name="content">Final TTS Content</param>
        /// <param name="ignoreRandomDitch">Specify true to ignore random ditching</param>
        public static async Task UnifiedPlay(string content, bool ignoreRandomDitch = false, bool overrideReadInQueue = false)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                Bridge.ALog("放弃: 内容为空");
                return;
            }
            Bridge.ALog("尝试朗读: " + content);
            if (!Conf.GetRandomBool(Vars.CurrentConf.ReadPossibility) && !ignoreRandomDitch)
            {
                Bridge.ALog("放弃: 已随机丢弃");
                return;
            }
            string fileName;

            if (Vars.CurrentConf.EnableUrlEncode)
            {
                content = HttpUtility.UrlEncode(content);
                Bridge.ALog("URL 编码完成: " + content);
            }
            switch (Vars.CurrentConf.Engine)
            {
            default:
                fileName = await BaiduTTS.Download(content);

                break;

            case 1:
                if (!Vars.SystemSpeechAvailable)
                {
                    Bridge.Log(".NET 框架引擎在此系统上不可用,无法朗读");
                    return;
                }
                fileName = FrameworkTTS.Download(content);
                break;

            case 2:
                fileName = GoogleTTS.Download(content, "zh-CN");
                break;

            case 3:
                fileName = await BaiduTTS.Download(content, true);

                break;

            case 4:
                fileName = await YoudaoTTS.Download(content);

                break;

            case 5:
                fileName = await CustomTTS.Download(content);

                break;

            case 6:
                fileName = await BaiduTTS.AiApi.Download(content, BaiduTTS.AiApi.ParseToSpeechPerson(Vars.CurrentConf.SpeechPerson));

                break;
            }
            if (fileName == null)
            {
                Bridge.ALog("下载失败,丢弃");
                return;
            }
            if (Vars.CurrentConf.ReadInQueue && !overrideReadInQueue)
            {
                Bridge.ALog($"正在添加下列文件到播放列表: {fileName}");
                fileList.Add(new TTSEntry(fileName));
            }
            else
            {
                Bridge.ALog($"正在直接播放: {fileName}");
                Play(fileName, false);
            }
        }