Thinking Different




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
//*************************************//
//    SAPI Class Library by copynull
//                      2010. 06. 15
//                 copynull@nate.com
//*************************************//
 
#pragma once
#include <sapi.h>
 
class SAPI
{
    int Volume;
    int Rate;
    ISpVoice *pVoice;
public:
    SAPI() { 
        pVoice = NULL;    // sapi resource init
        Volume = 100;    // volume init        (0 ~ 100)
        Rate = 0;        // rate speed init    (-10 ~ 10)
    }
    BOOL Initialize() 
    {
        if (FAILED(::CoInitialize(NULL)))        // COM Init
            return FALSE;
 
        HRESULT hr = CoCreateInstance(        // pVoice Device Instance Set
                     CLSID_SpVoice, 
                     NULL, 
                     CLSCTX_ALL, 
                     IID_ISpVoice, 
                     (void **)&pVoice);
 
        if(FAILED(hr))    return FALSE;
 
        pVoice->SetVolume(Volume);            // default Volume set
        pVoice->SetRate(Rate);                // default Rate set
 
        return TRUE;
    }
    VOID SetVolume(int vol) {            // Volume Setting Function
        if(vol <= 0) {
            vol = 0;
        } else if(vol >= 100)  {
            vol = 100;
        }
        Volume = vol;
        pVoice->SetVolume(Volume);
    }
    VOID SetSpeed(int rate) {            // Speed Setting Function
        if(rate <= -10) {
            rate = -10;
        } else if(rate >= 10) {
            rate = 10;
        }
        Rate = rate;
        pVoice->SetRate(Rate);
    }
    int GetVolume() { return Volume; }    // Now Get Volume Function
    int GetSpeed() { return Rate; }        // Now Get Speed Function
 
    BOOL Speaking(const wchar_t *str)    // Speaking Function
    {
        if(!pVoice)
            return FALSE;
 
        HRESULT hr = pVoice->Speak(str, 0, NULL);
        if(FAILED(hr))
            return FALSE;
 
        return TRUE;
    }
    virtual ~SAPI() {            // All Resource Release
        if(pVoice) {            // pVoice Resource Release
            pVoice->Release();
            pVoice = NULL;
        }
        ::CoUninitialize();        // COM Clear
    }
};
 
#include "CSapi.h"
int _tmain(int argc, _TCHAR* argv[])
{
    SAPI ap;
    if(ap.Initialize())
    {
        ap.SetSpeed(0);
        ap.SetVolume(100);
        ap.Speaking(L"Korea Team Fighting");
    }
 
    return 0;
}