1 /* Copyright (C) 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 Lesser General Public License as published by */
5 /* the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details. */
12 
13 /* You should have received a copy of the GNU Lesser General Public License */
14 /* along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 #ifndef RHVOICE_EVENTS_HPP
17 #define RHVOICE_EVENTS_HPP
18 
19 #include <list>
20 
21 #include "client.hpp"
22 #include "hts_label.hpp"
23 #include "utterance.hpp"
24 #include "relation.hpp"
25 #include "item.hpp"
26 
27 namespace RHVoice
28 {
29   class event
30   {
31   public:
32     typedef std::shared_ptr<event> pointer;
33     typedef label_sequence::const_iterator position;
34 
~event()35     virtual ~event()
36     {
37     }
38 
get_position() const39     position get_position() const
40     {
41       return pos;
42     }
43 
set_position(position pos_)44     void set_position(position pos_)
45     {
46       pos=pos_;
47     }
48 
get_time() const49     int get_time() const
50     {
51       if(pos==position())
52         return 0;
53       else
54         return (pos->get_time()+pos->get_duration());
55     }
56 
57     virtual bool notify(client& c) const=0;
58 
59   private:
60     position pos;
61   };
62 
63   typedef std::list<event::pointer> event_sequence;
64 
65   class text_unit_event: public event
66   {
67   protected:
68     std::size_t text_start,text_length;
69 
text_unit_event()70     text_unit_event():
71       text_start(0),
72       text_length(0)
73     {
74     }
75   };
76 
77   class sentence_event: public text_unit_event
78   {
79   protected:
sentence_event(const utterance & utt)80     sentence_event(const utterance& utt)
81     {
82       const relation& token_rel=utt.get_relation("Token");
83       text_start=token_rel.first().get("position").as<std::size_t>();
84       text_length=token_rel.last().get("position").as<std::size_t>()+token_rel.last().get("length").as<std::size_t>()-text_start;
85     }
86   };
87 
88   class sentence_starts_event: public sentence_event
89   {
90   public:
sentence_starts_event(const utterance & utt)91     explicit sentence_starts_event(const utterance& utt):
92     sentence_event(utt)
93     {
94     }
95 
notify(client & c) const96     bool notify(client& c) const
97     {
98       return c.sentence_starts(text_start,text_length);
99     }
100   };
101 
102   class sentence_ends_event: public sentence_event
103   {
104   public:
sentence_ends_event(const utterance & utt)105     explicit sentence_ends_event(const utterance& utt):
106       sentence_event(utt)
107     {
108     }
109 
notify(client & c) const110     bool notify(client& c) const
111     {
112       return c.sentence_ends(text_start,text_length);
113     }
114   };
115 
116   class word_event: public text_unit_event
117   {
118   protected:
word_event(const item & token)119     word_event(const item& token)
120     {
121       text_start=token.get("position").as<std::size_t>();
122       text_length=token.get("length").as<std::size_t>();
123     }
124   };
125 
126   class word_starts_event: public word_event
127   {
128   public:
word_starts_event(const item & token)129     explicit word_starts_event(const item& token):
130       word_event(token)
131     {
132     }
133 
notify(client & c) const134     bool notify(client& c) const
135     {
136       return c.word_starts(text_start,text_length);
137     }
138   };
139 
140   class word_ends_event: public word_event
141   {
142   public:
word_ends_event(const item & token)143     explicit word_ends_event(const item& token):
144       word_event(token)
145     {
146     }
147 
notify(client & c) const148     bool notify(client& c) const
149     {
150       return c.word_ends(text_start,text_length);
151     }
152   };
153 
154   class mark_event: public event
155   {
156   public:
mark_event(const std::string & mark)157     explicit mark_event(const std::string& mark):
158       name(mark)
159     {
160     }
161 
notify(client & c) const162     bool notify(client& c) const
163     {
164       return c.process_mark(name);
165     }
166 
167   private:
168     std::string name;
169   };
170 
171   class audio_event: public event
172   {
173   public:
audio_event(const std::string & audio_src)174     explicit audio_event(const std::string& audio_src):
175       src(audio_src)
176     {
177     }
178 
notify(client & c) const179     bool notify(client& c) const
180     {
181       return c.play_audio(src);
182     }
183 
184   private:
185     std::string src;
186   };
187 }
188 #endif
189