Example #1
0
        private static void Tts(Container container, IApiAiAppServiceFactory apiAiAppServiceFactory)
        {
            ///Create full contact app service
            var ttsAppService = apiAiAppServiceFactory.CreateTtsAppService("https://api.api.ai/v1", "YOUR_ACCESS_TOKEN");

            ///Create query request
            var ttsRequest = new TtsRequest
            {
                Text = "Hello, I want a pizza"
            };

            /// First - Create a path
            string path = @"D:\api-ai-csharp\tts";

            /// Call api.ai query by get
            var ttsResponse = ttsAppService.GetTtsAsync(ttsRequest).Result;

            if (ttsResponse == null)
            {
                throw new Exception("tts error - Get tts async returned null");
            }

            var fileName = ttsResponse.WriteToFile(path).Result;

            System.Console.Write($"File created: {path}\\{fileName}");
        }
Example #2
0
    private IEnumerator AnswersToAudio(string content)
    {
        String url       = "http://nlsapi.aliyun.com/speak?";
        String ak_id     = "LTAIr8I8A5HNZvdt";
        String ak_secret = "rBSsm6hewfAcSH6oeA6VXF6PCAACGD";

        //设置TTS的参数,详细参数说明详见文档部分2.1 参数配置
        Program    ttsDemo    = new Program();
        TtsRequest ttsRequest = new TtsRequest();

        ttsRequest.setEncodeType("wav");
        ttsRequest.setVoiceName("xiaoyun");
        ttsRequest.setVolume(50);
        ttsRequest.setSampleRate(16000);
        ttsRequest.setSpeechRate(0);
        ttsRequest.setPitchRate(0);
        ttsRequest.setTtsNus(1);
        ttsRequest.setBackgroundMusicId(0);
        ttsRequest.setBackgroundMusicOffset(0);
        ttsRequest.setBackgroundMusicVolume(0);

        String ttsUrlSpeak = url + "encode_type=" + ttsRequest.getEncodeType()
                             + "&voice_name=" + ttsRequest.getVoiceName()
                             + "&volume=" + ttsRequest.getVolume()
                             + "&sample_rate=" + ttsRequest.getSampleRate()
                             + "&speech_rate=" + ttsRequest.getSpeechRate()
                             + "&pitch_rate=" + ttsRequest.getPitchRate()
                             + "&tts_nus=" + ttsRequest.getTtsNus()
                             + "&background_music_id=" + ttsRequest.getBackgroundMusicId()
                             + "&background_music_offset=" + ttsRequest.getBackgroundMusicOffset()
                             + "&background_music_volume=" + ttsRequest.getBackgroundMusicVolume();


        string       fileName = System.Guid.NewGuid().ToString("N");
        HttpResponse response = HttpUtil.sendTtsPost(content, ttsRequest.getEncodeType(), fileName, ttsUrlSpeak, ak_id, ak_secret);

        AudioClip audioClip;
        string    fileUrl = "file://" + response.getResult();

        Debug.Log(fileUrl);
        WWW www = new WWW(fileUrl);

        yield return(www);

        if (www.isDone && www.error == null)
        {
            audioClip   = www.GetAudioClip();
            source.clip = audioClip;
            source.Play();
            if (false == source.isPlaying)
            {
                File.Delete(response.getResult());
                Debug.Log("删除成功!");
            }
        }
        flag = false;
        yield return(0);
    }
Example #3
0
        public static string ToQueryString(this TtsRequest ttsRequest)
        {
            string result = $"/tts?v={ttsRequest.V}";

            if (string.IsNullOrEmpty(ttsRequest.Text))
            {
                throw new ArgumentNullException("Query string 'query' is null or empty.");
            }

            return(result += $"&text={ttsRequest.Text}");
        }
Example #4
0
        public async Task <TtsResponse> GetTtsAsync(TtsRequest request)
        {
            using (var httpClient = HttpClientFactory.Create(AccessToken))
            {
                httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US");

                var httpResponseMessage = await httpClient.GetAsync(new Uri($"{BaseUrl}/{request.ToQueryString()}"));

                var content = await httpResponseMessage.ToStreamContentAsync();

                return(new TtsResponse(content));
            }
        }
Example #5
0
    void Start()
    {
        String url       = "http://nlsapi.aliyun.com/speak?";
        String ak_id     = "LTAIr8I8A5HNZvdt";
        String ak_secret = "rBSsm6hewfAcSH6oeA6VXF6PCAACGD";
        String tts_text  = "薄雾浓云愁永昼。瑞脑消金兽。佳节又重阳,玉枕纱厨,半夜凉初透。东篱把酒黄昏后。有暗香盈袖。莫道不消魂,帘卷西风,人比黄花瘦。";

        //设置TTS的参数,详细参数说明详见文档部分2.1 参数配置
        TtsRequest ttsRequest = new TtsRequest();

        ttsRequest.setEncodeType("wav");
        ttsRequest.setVoiceName("xiaoyun");
        ttsRequest.setVolume(50);
        ttsRequest.setSampleRate(16000);
        ttsRequest.setSpeechRate(0);
        ttsRequest.setPitchRate(0);
        ttsRequest.setTtsNus(1);
        ttsRequest.setBackgroundMusicId(0);
        ttsRequest.setBackgroundMusicOffset(0);
        ttsRequest.setBackgroundMusicVolume(0);

        String ttsUrlSpeak = url + "encode_type=" + ttsRequest.getEncodeType()
                             + "&voice_name=" + ttsRequest.getVoiceName()
                             + "&volume=" + ttsRequest.getVolume()
                             + "&sample_rate=" + ttsRequest.getSampleRate()
                             + "&speech_rate=" + ttsRequest.getSpeechRate()
                             + "&pitch_rate=" + ttsRequest.getPitchRate()
                             + "&tts_nus=" + ttsRequest.getTtsNus()
                             + "&background_music_id=" + ttsRequest.getBackgroundMusicId()
                             + "&background_music_offset=" + ttsRequest.getBackgroundMusicOffset()
                             + "&background_music_volume=" + ttsRequest.getBackgroundMusicVolume();


        string fileName = System.Guid.NewGuid().ToString("N");

        StartCoroutine(IEPost(tts_text, ttsRequest.getEncodeType(), fileName, ttsUrlSpeak, ak_id, ak_secret));
    }
Example #6
0
 public static async Task SendTtsRequest(IHubContext <TtsHub, ITtsHub> context, string roomId, TtsRequest request)
 {
     await context.Clients.Group(roomId).TtsPlayRequest(request);
 }
Example #7
0
        public MainPage()
        {
            this.InitializeComponent();
            //LogTest.LogWrite("Start ====================================");
            //string urlSpeak = "http://httpbin.org/post?";
            String urlSpeak = "http://nlsapi.aliyun.com/speak?";
            String tts_text = "薄雾浓云愁永昼。瑞脑消金兽。佳节又重阳,玉枕纱厨,半夜凉初透。东篱把酒黄昏后。有暗香盈袖。莫道不消魂,帘卷西风,人比黄花瘦。";

            //private static String tts_text = "欢迎使用智能语音服务";

            //请使用https://ak-console.aliyun.com/ 页面获取的Access 信息
            //请提前开通智能语音服务(https://data.aliyun.com/product/nls)
            //String ak_id = "id";
            //String ak_secret = "secret";
            String ak_id     = "LTAILNHYi9oS9tmi";
            String ak_secret = "hZH1gI8FYIeOPc7rFxtQl8geg7aaGW";

            //设置TTS的参数,详细参数说明详见文档部分2.1 参数配置
            TtsRequest ttsRequest = new TtsRequest();

            ttsRequest.setEncodeType("wav");
            ttsRequest.setVoiceName("xiaoyun");
            ttsRequest.setVolume(50);
            ttsRequest.setSampleRate(16000);
            ttsRequest.setSpeechRate(0);
            ttsRequest.setPitchRate(0);
            ttsRequest.setTtsNus(1);
            ttsRequest.setBackgroundMusicId(0);
            ttsRequest.setBackgroundMusicOffset(0);
            ttsRequest.setBackgroundMusicVolume(0);

            urlSpeak = urlSpeak + "encode_type=" + ttsRequest.getEncodeType()
                       + "&voice_name=" + ttsRequest.getVoiceName()
                       + "&volume=" + ttsRequest.getVolume()
                       + "&sample_rate=" + ttsRequest.getSampleRate()
                       + "&speech_rate=" + ttsRequest.getSpeechRate()
                       + "&pitch_rate=" + ttsRequest.getPitchRate()
                       + "&tts_nus=" + ttsRequest.getTtsNus()
                       + "&background_music_id=" + ttsRequest.getBackgroundMusicId()
                       + "&background_music_offset=" + ttsRequest.getBackgroundMusicOffset()
                       + "&background_music_volume=" + ttsRequest.getBackgroundMusicVolume();

            string fileName = System.Guid.NewGuid().ToString("N");


            //tts 文字—》语音
            //HttpResponse response = HttpUtil.sendTtsPost(tts_text, ttsRequest.getEncodeType(), fileName, urlSpeak, ak_id, ak_secret);
            HttpUtil.sendTtsPost(tts_text, ttsRequest.getEncodeType(), fileName, urlSpeak, ak_id, ak_secret);

            //tts 语音--》文字
            String urls = "http://nlsapi.aliyun.com/recognize?";
            //使用对应的ASR模型 详情见文档部分2
            String model = "chat";

            urls = urls + "model=" + model;
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //注册GBK编码
            Encoding encodingUnicode = Encoding.Unicode;

            //string s;
            //using (Stream file = KnownFolders.VideosLibrary.OpenStreamForReadAsync("1.wav").Result)
            //{
            //    using (StreamReader read = new StreamReader(file, encodingUnicode))
            //    {
            //        s = read.ReadToEnd();
            //    }
            //}

            //byte[] audioBytes = encodingUnicode.GetBytes(s);
            //HttpUtil.sendAsrPost(audioBytes, "pcm", 16000, urls, ak_id, ak_secret);



            //智能对话
            String urlStr = "https://nlsapi.aliyun.com/manage/qas?action=single:prepub:qa";
            //String urlStr = "https://nlsapi.aliyun.com/manage/qas?action=projects:list";
            //String urlStr = "https://nlsapi.aliyun.com/manage/qas";

            String bdStr = "{\"projectId" + "\"" + ":" + 4649 + "," + "\"question" + "\"" + ":" + "\"" + "请问勒布朗詹姆斯最近的技术统计怎么样?" + "\"}";

            //String bdStr = "{\"offset" + "\"" + ":" + 0+ "," + "\"pageSize" + "\"" + ":" + 1 +"}";

            //String bdStr = "{\"version"+"\""+":"+"\""+"2.0"+"\""+"," + "\"app_key"+"\""+":"+"\""+"nui-d9lLBVpmR0dl"+"\""+"," + "\"question"+"\""+":"+"\""+"123545"+"\"}";
            //HttpProxy.sendRequest(urlStr, bdStr, ak_id, ak_secret);



            //语音采集
            //SoundRecord recorder = new SoundRecord();

            ////开始录音
            //string wavfile = null;
            //wavfile = "test.wav";
            //recorder.SetFileName(wavfile);
            //recorder.RecStart();
            ////结束
            //recorder.RecStop();
            //recorder = null;
        }
Example #8
0
        public MainPage()
        {
            this.InitializeComponent();
            //LogTest.LogWrite("Start ====================================");

            String urlSpeak = "http://nlsapi.aliyun.com/speak?";
            String tts_text = "薄雾浓云愁永昼。瑞脑消金兽。佳节又重阳,玉枕纱厨,半夜凉初透。东篱把酒黄昏后。有暗香盈袖。莫道不消魂,帘卷西风,人比黄花瘦。";

            //private static String tts_text = "欢迎使用智能语音服务";

            //请使用https://ak-console.aliyun.com/ 页面获取的Access 信息
            //请提前开通智能语音服务(https://data.aliyun.com/product/nls)
            //String ak_id = "id";
            //String ak_secret = "secret";
            String ak_id     = "LTAIr8I8A5HNZvdt";
            String ak_secret = "rBSsm6hewfAcSH6oeA6VXF6PCAACGD";

            //设置TTS的参数,详细参数说明详见文档部分2.1 参数配置
            TtsRequest ttsRequest = new TtsRequest();

            ttsRequest.setEncodeType("wav");
            ttsRequest.setVoiceName("xiaoyun");
            ttsRequest.setVolume(50);
            ttsRequest.setSampleRate(16000);
            ttsRequest.setSpeechRate(0);
            ttsRequest.setPitchRate(0);
            ttsRequest.setTtsNus(1);
            ttsRequest.setBackgroundMusicId(0);
            ttsRequest.setBackgroundMusicOffset(0);
            ttsRequest.setBackgroundMusicVolume(0);

            urlSpeak = urlSpeak + "encode_type=" + ttsRequest.getEncodeType()
                       + "&voice_name=" + ttsRequest.getVoiceName()
                       + "&volume=" + ttsRequest.getVolume()
                       + "&sample_rate=" + ttsRequest.getSampleRate()
                       + "&speech_rate=" + ttsRequest.getSpeechRate()
                       + "&pitch_rate=" + ttsRequest.getPitchRate()
                       + "&tts_nus=" + ttsRequest.getTtsNus()
                       + "&background_music_id=" + ttsRequest.getBackgroundMusicId()
                       + "&background_music_offset=" + ttsRequest.getBackgroundMusicOffset()
                       + "&background_music_volume=" + ttsRequest.getBackgroundMusicVolume();

            string fileName = System.Guid.NewGuid().ToString("N");


            //tts 文字—》语音
            //HttpResponse response = HttpUtil.sendTtsPost(tts_text, ttsRequest.getEncodeType(), fileName, urlSpeak, ak_id, ak_secret);
            HttpUtil.sendTtsPost(tts_text, ttsRequest.getEncodeType(), fileName, urlSpeak, ak_id, ak_secret);



            //tts 语音--》文字
            //String urls = "http://nlsapi.aliyun.com/recognize?";
            ////使用对应的ASR模型 详情见文档部分2
            //String model = "chat";
            //urls = urls + "model=" + model;
            ////string pa = KnownFolders.VideosLibrary.Path + "/1.wav";
            ////FileStream fs = new FileStream(pa, FileMode.Open, FileAccess.Read);
            ////byte[] audioBytes = new byte[fs.Length];
            ////fs.Read(audioBytes, 0, (int)fs.Length);
            //string s;
            //using (Stream file = KnownFolders.VideosLibrary.OpenStreamForReadAsync("2.txt").Result)
            //{
            //    using (StreamReader read = new StreamReader(file))
            //    {
            //        s = read.ReadToEnd();
            //    }
            //}
            //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            ////注册GBK编码
            //Encoding encodingGbk = Encoding.GetEncoding("GBK");
            ////byte[] audioBytes;
            ////audioBytes = encodingGbk.GetBytes(s);
            //byte[] audioBytes = new byte[144972];
            //string[] str = s.Split(',');
            //for (int i = 0; i < audioBytes.Length; i++)
            //{
            //    audioBytes[i] = Convert.ToByte(str[i]);
            //}
            //HttpUtil.sendAsrPost(audioBytes, "pcm", 16000, urls, ak_id, ak_secret);


            //智能对话
            String urlStr = "https://nlsapi.aliyun.com/manage/qas?action=single:prepub:qa";
            //String urlStr = "https://nlsapi.aliyun.com/manage/qas?action=projects:list";
            //String urlStr = "https://nlsapi.aliyun.com/manage/qas";

            String bdStr = "{\"projectId" + "\"" + ":" + 4649 + "," + "\"question" + "\"" + ":" + "\"" + "请问勒布朗詹姆斯最近的技术统计怎么样?" + "\"}";

            //String bdStr = "{\"offset" + "\"" + ":" + 0 + "," + "\"pageSize" + "\"" + ":" + 1 + "}";

            //String bdStr = "{\"version" + "\"" + ":" + "\"" + "2.0" + "\"" + "," + "\"app_key" + "\"" + ":" + "\"" + "nui-d9lLBVpmR0dl" + "\"" + "," + "\"question" + "\"" + ":" + "\"" + "123545" + "\"}";
            //HttpProxy.sendRequest(urlStr, bdStr, ak_id, ak_secret);



            //语音采集
            //SoundRecord recorder = new SoundRecord();

            ////开始录音
            //string wavfile = null;
            //wavfile = "test.wav";
            //recorder.SetFileName(wavfile);
            //recorder.RecStart();
            ////结束
            //recorder.RecStop();
            //recorder = null;
        }
Example #9
0
 public async Task <IHttpActionResult> TextToSpeech(TtsRequest ttsRequest)
 {
     return(Ok(await new TtsHandler().Run(ttsRequest.Text)));
 }