1 /* Copyright (C) 2012, 2013  Olga Yakovleva <yakovleva.o.v@gmail.com> */
2 
3 /* This program is free software: you can redistribute it and/or modify */
4 /* it under the terms of the GNU General Public License as published by */
5 /* the Free Software Foundation, either version 2 of the License, or */
6 /* (at your option) any later version. */
7 
8 /* This program is distributed in the hope that it will be useful, */
9 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
10 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
11 /* GNU General Public License for more details. */
12 
13 /* You should have received a copy of the GNU General Public License */
14 /* along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 #include <exception>
17 #include <set>
18 #include "config_tts.hpp"
19 #include "io.hpp"
20 
21 namespace RHVoice
22 {
23   namespace sd
24   {
25     namespace cmd
26     {
config_tts()27       config_tts::config_tts()
28       {
29         register_settings(tts_settings);
30       }
31 
is_valid() const32       bool config_tts::is_valid() const
33       {
34         return !(tts_engine == nullptr);
35       }
36 
execute()37       action_t config_tts::execute()
38       {
39         reply r;
40         r("203 OK RECEIVING SETTINGS");
41         read_settings();
42         set_voice();
43         if(current_voice.empty())
44           r("303 ERROR NO VOICE HAS BEEN SET");
45         else
46           r("203 OK SETTINGS RECEIVED");
47         return action_continue;
48       }
49 
set_voice()50       void config_tts::set_voice()
51       {
52         const std::set<voice_profile>& profiles=tts_engine->get_voice_profiles();
53         voice_profile new_voice;
54         if(tts_settings.voice_name.is_set())
55           new_voice=tts_engine->create_voice_profile(tts_settings.voice_name.get());
56         else
57           {
58             if(tts_settings.language_code.is_set())
59               {
60                 voice_profile voice1,voice2,voice3;
61                 RHVoice_voice_gender gender=RHVoice_voice_gender_unknown;
62                 unsigned int index=0;
63                 voice_id_t id=tts_settings.voice_id;
64                 if(id>0)
65                   {
66                     gender=RHVoice_voice_gender_female;
67                     index=id;
68                   }
69                 else if(id<0)
70                   {
71                     gender=RHVoice_voice_gender_male;
72                     index=-id;
73                   }
74                 unsigned int count=0;
75                 for(std::set<voice_profile>::const_iterator it=profiles.begin();it!=profiles.end();++it)
76                   {
77                     if((it->voice_count()==1)&&(it->primary()->get_language()->get_alpha2_code()==tts_settings.language_code.get()))
78                       {
79                         if(voice3.empty())
80                           voice3=*it;
81                         if((gender!=RHVoice_voice_gender_unknown)&&(it->primary()->get_gender()==gender))
82                           {
83                             ++count;
84                             if(voice2.empty())
85                               voice2=*it;
86                             if(count==index)
87                               voice1=*it;
88                           }
89                       }
90                   }
91                 if(voice1.empty())
92                   {
93                     if(voice2.empty())
94                       {
95                         if(!voice3.empty())
96                           new_voice=voice3;
97                       }
98                     else
99                       new_voice=voice2;
100                   }
101                 else
102                   new_voice=voice1;
103               }
104           }
105         if(new_voice.empty()&&current_voice.empty())
106           new_voice=tts_engine->get_fallback_voice_profile();
107         if(!new_voice.empty())
108           {
109             current_voice=new_voice;
110             logger::log(4,"Will speak using voice ",current_voice.get_name());
111           }
112       }
113     }
114   }
115 }
116