-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy path02. personal_voice_assistant_'en'.cpp
154 lines (126 loc) · 4.12 KB
/
02. personal_voice_assistant_'en'.cpp
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
#include <iostream>
#include <Windows.h>
#include <sapi.h>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <ctime>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <wininet.h>
#include <json/json.h>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/uri.h>
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "sapi.lib")
using namespace std;
using namespace std::chrono;
using namespace web::http;
using namespace web::http::client;
ISpVoice* voice;
void speak(const string& text) {
voice->Speak(CString(text.c_str()), 0, NULL);
}
void initializeVoice() {
CoInitialize(NULL);
CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&voice);
voice->SetRate(185);
}
void takeCommand(string& user_input) {
ISpRecognizer* recognizer;
CoCreateInstance(CLSID_SpRecognizer, NULL, CLSCTX_ALL, IID_ISpRecognizer, (void**)&recognizer);
recognizer->SetInput(NULL, TRUE);
ISpAudio* audio;
SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &audio);
ISpStream* stream;
recognizer->CreateStream(NULL, &stream);
recognizer->SetInput(stream, FALSE);
recognizer->SetRecoState(SPRST_ACTIVE_ALWAYS);
stream->SetBaseStream(audio, SPDFID_WAVEFORM, FALSE, FALSE);
audio->SetState(SPAUDIOSTATE_ACTIVE);
cout << "Listening..." << endl;
SPRECOGNIZEINFO info = { 0 };
info.cbSize = sizeof(info);
info.pvEngineContext = NULL;
HRESULT hr;
while (true) {
hr = recognizer->Recognize(NULL, info);
if (hr == S_OK) {
ISpRecoResult* result;
recognizer->GetResult(&result);
SPPHRASE* phrase;
result->GetPhrase(&phrase);
wchar_t* text = phrase->pProperties->pszValue;
string user_input_text = CW2A(text);
user_input = user_input_text;
break;
}
else if (hr == S_FALSE) {
continue;
}
else {
cout << "Error occurred while recognizing speech." << endl;
break;
}
}
stream->Release();
audio->SetState(SPAUDIOSTATE_INACTIVE);
audio->Release();
recognizer->SetRecoState(SPRST_INACTIVE);
recognizer->Release();
}
string getCurrentDateTime() {
auto now = system_clock::now();
auto time = system_clock::to_time_t(now);
stringstream ss;
ss << put_time(localtime(&time), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
void getWeatherReport(const string& city) {
string url = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=99e68c086c34059f58d3349bd4fb694c&units=metric";
http_client client(U(url));
client.request(methods::GET).then([](http_response response) {
if (response.status_code() == 200) {
auto body = response.extract_json().get();
double temperature = body["main"]["temp"].as_double();
double humidity = body["main"]["humidity"].as_double();
double wind_speed = body["wind"]["speed"].as_double();
cout << "Temperature: " << temperature << "°C" << endl;
cout << "Humidity: " << humidity << "%" << endl;
cout << "Wind Speed: " << wind_speed << "m/s" << endl;
}
else {
cout << "Error occurred while fetching weather data." << endl;
}
}).wait();
}
int main() {
initializeVoice();
string user_input;
while (true) {
takeCommand(user_input);
transform(user_input.begin(), user_input.end(), user_input.begin(), ::tolower);
if (user_input == "exit") {
speak("Goodbye!");
break;
}
else if (user_input == "time") {
string current_time = getCurrentDateTime();
speak("The current time is " + current_time);
}
else if (user_input.substr(0, 5) == "weather") {
string city = user_input.substr(7);
getWeatherReport(city);
}
else {
speak("Sorry, I didn't understand that.");
}
}
voice->Release();
CoUninitialize();
return 0;
}