-
Notifications
You must be signed in to change notification settings - Fork 8
/
watson_tts_request.cpp
157 lines (134 loc) · 3.93 KB
/
watson_tts_request.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
155
156
157
#include "watson_tts_request.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrl>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStandardPaths>
WatsonTTSRequest::WatsonTTSRequest(QObject *parent)
: QObject(parent)
, m_networkAccessManager(new QNetworkAccessManager(this))
, m_accessToken("")
, m_voice("")
, m_text("")
, m_apiURL("")
, m_filePath("")
, m_errorString("")
, m_status(RequestStatus::Idle)
{
m_filePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/watson_tts.ogg";
}
WatsonTTSRequest::~WatsonTTSRequest()
{
}
void WatsonTTSRequest::sendRequest()
{
// Set up the API endpoint URL
QUrl endpointUrl = m_apiURL.resolved(QUrl("v1/synthesize"));
QUrlQuery params;
params.addQueryItem("voice", m_voice);
endpointUrl.setQuery(params);
// Set up the request headers
QNetworkRequest request(endpointUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
//request.setRawHeader("Authorization", "Bearer " + m_accessToken.toUtf8());
request.setRawHeader("Authorization", "Basic " + QString("apikey:" + m_accessToken).toLocal8Bit().toBase64());
//request.setRawHeader("Accept", "audio/wav");
request.setRawHeader("Accept", "audio/ogg;codecs=vorbis");
// Set up the request body
QJsonObject requestBody;
requestBody.insert("text", m_text);
// Send the request
QNetworkReply *reply = m_networkAccessManager->post(request, QJsonDocument(requestBody).toJson(QJsonDocument::Compact));
m_status = RequestStatus::InProgress;
emit statusChanged();
// Connect signals and slots to handle the response
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) {
QFile saveFile(m_filePath);
if (saveFile.open(QIODevice::WriteOnly)) {
saveFile.write(reply->readAll());
saveFile.close();
m_status = RequestStatus::Success;
emit statusChanged();
emit requestFinished();
} else {
m_errorString = "Could not open file for writing";
emit errorStringChanged();
m_status = RequestStatus::Error;
emit statusChanged();
emit requestError(m_errorString);
}
} else {
m_errorString = reply->errorString();
qDebug() << reply->error() << reply->errorString() << reply->readAll();
emit errorStringChanged();
m_status = RequestStatus::Error;
emit statusChanged();
emit requestError(m_errorString);
}
reply->deleteLater();
});
}
void WatsonTTSRequest::execute()
{
sendRequest();
}
QString WatsonTTSRequest::accessToken() const
{
return m_accessToken;
}
void WatsonTTSRequest::setAccessToken(const QString& accessToken)
{
if (m_accessToken != accessToken) {
m_accessToken = accessToken;
emit accessTokenChanged();
}
}
QString WatsonTTSRequest::voice() const
{
return m_voice;
}
void WatsonTTSRequest::setVoice(const QString& voice)
{
if (m_voice != voice) {
m_voice = voice;
emit voiceChanged();
}
}
QString WatsonTTSRequest::text() const
{
return m_text;
}
void WatsonTTSRequest::setText(const QString& text)
{
if (m_text != text) {
m_text = text;
emit textChanged();
}
}
QUrl WatsonTTSRequest::apiURL() const
{
return m_apiURL;
}
void WatsonTTSRequest::setApiURL(const QUrl& apiURL)
{
if (m_apiURL != apiURL) {
m_apiURL = apiURL;
emit apiURLChanged();
}
}
QString WatsonTTSRequest::filePath() const
{
return m_filePath;
}
QString WatsonTTSRequest::errorString() const
{
return m_errorString;
}
WatsonTTSRequest::RequestStatus WatsonTTSRequest::status() const
{
return m_status;
}