File: config_audio.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 (108 lines) | stat: -rw-r--r-- 3,603 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
/* Copyright (C) 2012  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 <exception>
#include "core/str.hpp"
#include "config_audio.hpp"
#include "io.hpp"

namespace RHVoice
{
  namespace sd
  {
    namespace cmd
    {
      config_audio::config_audio()
      {
        register_settings(audio_settings);
      }

      bool config_audio::is_valid() const
      {
        return !playback_stream.is_initialized();
      }

      action_t config_audio::execute()
      {
        reply r;
        r("207 OK RECEIVING AUDIO SETTINGS");
        reset_settings();
        read_settings();
        if(initialize_audio())
          {
            r("203-Opened a playback stream");
            r("203 OK AUDIO INITIALIZED");
          }
        else
          {
            r("400-Cannot open a playback stream");
            r("400 ERROR AUDIO NOT INITIALIZED");
          }
        return action_continue;
      }

      bool config_audio::try_output_method(const std::string& method)
      {
        try
          {
            logger::log(2,"Trying to initialize audio output using method ",method);
            playback_stream.reset_params();
            if(method=="pulse")
              {
                playback_stream.set_backend(audio::backend_pulse);
                if(audio_settings.pulse_server.is_set()&&audio_settings.pulse_server.get()!="default")
                  playback_stream.set_server(audio_settings.pulse_server);
              }
            else if(method=="alsa")
              {
                playback_stream.set_backend(audio::backend_alsa);
                if(audio_settings.alsa_device.is_set()&&(audio_settings.alsa_device.get()!="default"))
                  playback_stream.set_device(audio_settings.alsa_device);
              }
            else if(method=="oss")
              {
                playback_stream.set_backend(audio::backend_oss);
                if(audio_settings.oss_device.is_set()&&(audio_settings.oss_device.get()!="default"))
                  playback_stream.set_device(audio_settings.oss_device);
              }
            else if(method=="libao")
              playback_stream.set_lib(audio::lib_libao);
            else
              return false;
            playback_stream.set_buffer_size(20);
            playback_stream.open();
            logger::log(2,"Audio output initialized");
            return true;
          }
        catch(const audio::error& e)
          {
            logger::log(2,"Audio output initialization error: ",e.what());
            return false;
          }
      }

      bool config_audio::initialize_audio()
      {
        str::tokenizer<str::is_equal_to> tok(audio_settings.output_method,str::is_equal_to(','));
        for(str::tokenizer<str::is_equal_to>::iterator it=tok.begin();it!=tok.end();++it)
          {
            if(try_output_method(*it))
              return true;
          }
        return false;
      }
    }
  }
}