summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/WinterSpeak/speak/config/dialog_config_engine.cpp
blob: d95e610916428a16042feb666eadebebf2c1507e (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//==============================================================================
// 	Miranda Speak Plugin, © 2002 Ryan Winter
//==============================================================================

#pragma warning(disable:4786)

#include "dialog_config_engine.h"

#include "speak.h"
#include "resource.h"
#include "config/speech_interface.h"

#include <general/debug/debug.h>
#include <general/text_to_speech/text_to_speech/text_to_speech.h>

#include <windows.h>
#include <commctrl.h>

//------------------------------------------------------------------------------
DialogConfigEngine *DialogConfigEngine::m_instance = 0;

//------------------------------------------------------------------------------
// public:
//------------------------------------------------------------------------------
DialogConfigEngine::DialogConfigEngine(ConfigDatabase &db)
	:
	m_db(db),
    m_test_tts(0)
{
	CLASSCERR("DialogConfigEngine::DialogConfigEngine");

    m_instance = this;
}

//------------------------------------------------------------------------------
DialogConfigEngine::~DialogConfigEngine()
{
	CLASSCERR("DialogConfigEngine::~DialogConfigEngine");

    m_instance = 0;
}

//------------------------------------------------------------------------------
int CALLBACK 
DialogConfigEngine::process(HWND window, UINT message, WPARAM wparam, 
    LPARAM lparam)
{
    if (!m_instance)
    {
        return 1;
    }

	switch (message)
	{
      case WM_INITDIALOG:
        m_instance->load(window);
        break;

	  case WM_NOTIFY:
    	if (PSN_APPLY == reinterpret_cast<LPNMHDR>(lparam)->code)
    	{	
		    m_instance->save(window);
            m_instance->m_db.save();
	    }
        break;

	  case WM_HSCROLL:
		m_instance->changed(window);
		break;

	  case WM_COMMAND:
		m_instance->command(window, wparam);
        break;
	}

    return 0;
}

//------------------------------------------------------------------------------
// private:
//------------------------------------------------------------------------------
void
DialogConfigEngine::command(HWND window, int control)
{
	switch (LOWORD(control))
	{
	  case IDC_WELCOME_MSG:
        if (EN_CHANGE == HIWORD(control))
        {
            changed(window);
        }
        break;

      case IDC_SELECT_VOICE:
        if (CBN_SELCHANGE == HIWORD(control))
        {
            changed(window);
        }
        break;

      case IDC_SELECT_ENGINE:
        if (CBN_SELCHANGE == HIWORD(control))
        {
            updateVoices(window);
            changed(window);
        }
		break;

      case IDC_CONFIG_LEXICON:
        if (createTts(window))
        {
            if (!m_test_tts->lexiconDialog(window))
            {
   				MessageBox(window, "Lexicon for this engine is not supported",
                    "Speak", MB_OK | MB_ICONEXCLAMATION);
            }
        }
        break;

	  case IDC_BUTTON_TEST:
        if (createTts(window))
        {
            m_test_tts->say(Translate("testing testing 1 2 3"));
        }
        break;
	}
}

//------------------------------------------------------------------------------
void
DialogConfigEngine::load(HWND window)
{
	CLASSCERR("DialogConfigEngine::load()");

	TranslateDialogDefault(window);

    // add the available engines to the combo box
    SpeechInterface si;
    std::vector<std::string> engines = si.getAvailableEngines();
    for (unsigned int i = 0; i < engines.size(); ++i)
    {
    	CLASSCERR("DialogConfigEngine::load adding " << engines[i].c_str());
        
    	SendDlgItemMessage(window, IDC_SELECT_ENGINE, CB_ADDSTRING, 0, 
	    	(long)engines[i].c_str());
    }

    VoiceDesc desc = m_db.getVoiceDesc();

	// initialise the sliders
	SendDlgItemMessage(window, IDC_SLIDER_VOLUME, TBM_SETPOS, TRUE, 
		desc.volume);
	SendDlgItemMessage(window, IDC_SLIDER_PITCH, TBM_SETPOS, TRUE, 
	    desc.pitch);
	SendDlgItemMessage(window, IDC_SLIDER_RATE, TBM_SETPOS, TRUE, 
		desc.rate);

    // select the speech engine
	SendDlgItemMessage(window, IDC_SELECT_ENGINE, CB_SELECTSTRING, 0, 
		reinterpret_cast<long>(desc.engine.c_str()));

	// initialise the welcome message box
	SetDlgItemText(window, IDC_WELCOME_MSG, m_db.getWelcomeMessage().c_str());

    updateVoices(window);
}

//------------------------------------------------------------------------------
void
DialogConfigEngine::save(HWND window)
{
	CLASSCERR("DialogConfigEngine::save()");

    VoiceDesc desc;
    getVoiceDesc(window, desc);
    m_db.setVoiceDesc(desc);

	// store the welcome message
	char text[512];
	GetDlgItemText(window, IDC_WELCOME_MSG, text, sizeof(text));
	m_db.setWelcomeMessage(std::string(text));
}

//------------------------------------------------------------------------------
void 
DialogConfigEngine::updateVoices(HWND window)
{
	CLASSCERR("DialogConfigEngine::updateVoices()");

    SpeechInterface si;
    m_test_tts = std::auto_ptr<TextToSpeech>(si.createTts(getEngine(window)));

    if (!m_test_tts.get())
    {
        // we couldnt open the text to speech engine
    	CLASSCERR("DialogConfigEngine::updateVoices invalid tts");
        return;
    }

    // add the voices onto the list
    std::vector<std::string> voices = m_test_tts->getVoices();

	SendDlgItemMessage(window, IDC_SELECT_VOICE, CB_RESETCONTENT, 0, 0);
	for (unsigned int i = 0; i < voices.size(); ++i)
	{
		SendDlgItemMessage(window, IDC_SELECT_VOICE, CB_ADDSTRING, 0, 
			(long)voices[i].c_str());
	}

    // get the voice saved in the database
    std::string voice = m_db.getVoiceDesc().voice;
    
    if (FAILED(SendDlgItemMessage(window, IDC_SELECT_VOICE, 
        CB_FINDSTRINGEXACT, 0, (long)voice.c_str())))
    {
        // select the first one
	    SendDlgItemMessage(window, IDC_SELECT_VOICE, CB_SETCURSEL , 0, 0);
    }
    else
    {
        // select the saved voice
    	SendDlgItemMessage(window, IDC_SELECT_VOICE, CB_SELECTSTRING, 0, 
	    	(long)voice.c_str());
    }
}

//------------------------------------------------------------------------------
void
DialogConfigEngine::getVoiceDesc(HWND window, VoiceDesc &desc)
{
	CLASSCERR("DialogConfigEngine::getVoiceDesc(,)");

	// get the engine
	char text[100];
	GetDlgItemText(window, IDC_SELECT_ENGINE, text, sizeof(text));
	desc.engine = std::string(text);

	// get the voice
	std::auto_ptr<char> voice(new char[50]);
	SendDlgItemMessage(window, IDC_SELECT_VOICE, CB_GETLBTEXT, 
		SendDlgItemMessage(window, IDC_SELECT_VOICE, CB_GETCURSEL, 0, 0), 
		reinterpret_cast<long>(voice.get()));
    desc.voice = voice.get();

    // get the voice setting sliders
	desc.volume 
        = SendDlgItemMessage(window, IDC_SLIDER_VOLUME, TBM_GETPOS, 0, 0);
	desc.pitch 
        = SendDlgItemMessage(window, IDC_SLIDER_PITCH, TBM_GETPOS, 0, 0);
	desc.rate 
        = SendDlgItemMessage(window, IDC_SLIDER_RATE, TBM_GETPOS, 0, 0);
}

//------------------------------------------------------------------------------
std::string
DialogConfigEngine::getEngine(HWND window)
{
	CLASSCERR("DialogConfigEngine::getEngine()");

	// store the engine
	char text[100];
	GetDlgItemText(window, IDC_SELECT_ENGINE, text, sizeof(text));

	CLASSCERR("DialogConfigEngine::getEngine() return " << text);
    return text;
}

//------------------------------------------------------------------------------
bool
DialogConfigEngine::createTts(HWND window)
{
    CLASSCERR("DialogConfigEngine::createTts()");

    VoiceDesc desc;
    getVoiceDesc(window, desc);

    SpeechInterface si;
    m_test_tts = std::auto_ptr<TextToSpeech>(si.createTts(desc.engine));

    if (!m_test_tts.get())
    {
        return false;
    }
    
    si.configureTts(m_test_tts.get(), desc);
    return true;
}

//==============================================================================