File: speech_player.cpp

package info (click to toggle)
rhvoice 1.14.0-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 803,312 kB
  • sloc: cpp: 50,834; ansic: 6,748; python: 5,886; perl: 2,411; xml: 1,908; makefile: 304; sh: 265; lisp: 141; awk: 65
file content (162 lines) | stat: -rw-r--r-- 4,568 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* Copyright (C) 2012, 2018  Olga Yakovleva <yakovleva.o.v@gmail.com> */

/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 2 of the License, or */
/* (at your option) any later version. */

/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
/* GNU General Public License for more details. */

/* You should have received a copy of the GNU General Public License */
/* along with this program.  If not, see <https://www.gnu.org/licenses/>. */

#include"speech_player.hpp"
#include "io.hpp"

namespace RHVoice
{
  namespace sd
  {
    const synth_result::event synth_result::started_speaking(event_begin,"BEGIN");
    const synth_result::event synth_result::stopped_speaking(event_stop,"STOP");
    const synth_result::event synth_result::paused_speaking(event_pause,"PAUSE");
    const synth_result::event synth_result::finished_speaking(event_end,"END");

    void synth_result::report_event(const event& e)
    {
      if(e.id==event_index_mark)
        logger::log(5,"Reporting index mark ",e.content);
      else
        logger::log(5,"Reporting event ",e.name);
      message m;
      if(e.id==event_index_mark)
        m(e.id,"-",e.content);
      m(e.id," ",e.name);
    }

    void start_of_speech::output()
    {
      report_event(started_speaking);
    }

    synth_result::event end_of_speech::which_event()
    {
      state s;
      if(s.test(state::stopped))
        return stopped_speaking;
      else if(s.test(state::paused))
        return paused_speaking;
      else
        return finished_speaking;
    }

    void end_of_speech::clear_state()
    {
      state s;
      s.clear(state::pausing);
      s.clear(state::paused);
      s.clear(state::stopped);
      s.clear(state::speaking);
    }

    void end_of_speech::output()
    {
      event final_event(which_event());
      report_event(final_event);
      clear_state();
      if(playback_stream.is_open())
        {
          try
            {
              playback_stream.idle();
            }
          catch(const audio::error& e)
            {
              logger::log(2,e.what(),"\nClosing playback stream");
              playback_stream.close();
            }
        }
    }

    void speech_chunk::output()
    {
      if(cancelled())
        return;
      try
        {
          if(!playback_stream.is_open())
            {
              logger::log(2,"Reopening playback stream");
              playback_stream.open();
            }
          logger::log(5,"Writing ",data.size()," samples to the playback stream");
          playback_stream.write(&data[0],data.size());
        }
      catch(const audio::error& e)
        {
          state::Set(state::stopped);
          logger::log(2,e.what());
          if(playback_stream.is_open())
            {
              logger::log(2,"Closing playback stream");
              playback_stream.close();
            }
        }
    }

    void sample_rate_setting::output()
    {
      if(cancelled())
        return;
      logger::log(2,"Setting sample rate to ",sample_rate,", current sample rate is ",playback_stream.get_sample_rate());
      if(playback_stream.get_sample_rate()==sample_rate)
        return;
      try
        {
          if(playback_stream.is_open())
            {
              logger::log(2,"Playback stream is already open, draining");
              playback_stream.drain();
              logger::log(2,"Closing playback stream");
              playback_stream.close();
            }
          playback_stream.set_sample_rate(sample_rate);
        }
      catch(const audio::error& e)
        {
          state::Set(state::stopped);
          logger::log(2,e.what());
          if(playback_stream.is_open())
            {
              logger::log(2,"Closing playback stream");
              playback_stream.close();
            }
        }
    }

    void index_mark::output()
    {
      if(check_state())
        report_event(reached_index_mark(mark));
    }

    void speech_player::run()
    {
      try
        {
          synth_result_ptr ptr;
          while(speech_queue.pop(ptr))
            {
              ptr->output();
            }
        }
      catch(const std::exception& e)
        {
          logger::log(2,"Fatal error in the playback thread: ",e.what());
        }
    }
  }
}