我最近开始使用 C++ Rest SDK 并尝试将音频缓冲区发送到 Watson Speech to Text 服务,但文档并没有向我说明如何使用此库使用或上传缓冲区,我开始使用 Microsfot上传文件的示例(https://msdn.microsoft.com/en-us/library/jj950081.aspx)然后我尝试修改代码以发送缓冲区作为示例(https://msdn.microsoft。 com/en-us/library/jj950083.aspx),但我对某些人来说是行不通的。这是我当前的代码:
我当前的代码:
#include <codecvt>
#include <cpprest/containerstream.h>
#include <cpprest/http_client.h>
#include <iostream>
#include <cpprest/producerconsumerstream.h>
#include <cpprest/rawptrstream.h>
#include <cpprest/json.h>
#include "ReqHTTP.h"
#include <sstream>
#include <fstream>
#include <Windows.h>
using namespace std;
using namespace web;
using namespace concurrency;
using namespace concurrency::streams;
using namespace web::http;
using namespace web::http::client;
using namespace utility::conversions;
#pragma comment(lib, "winmm.lib")
pplx::task<void> RequestHTTP::UploadFileToHttpServerAsync(streams::ostream outStream, LPSTR buffer)
{
container_buffer<LPSTR> outAudioBuffer(move(buffer));
return outStream.write(outAudioBuffer, outAudioBuffer.size()).then([](size_t
bytesWritten)
{
try {
http_client_config config;
credentials cred(L"c674021d-5a0f-4ec1-84a4-da6fbd50541c", L"WvYMlztHWCbC");
config.set_credentials(cred);
http_request req(methods::POST);
req.headers().add(L"Transfer-Encoding", L"chunked");
req.headers().add(L"Content-Type", L"audio/wav");
req.set_request_uri(L"speech-to-text/api/v1/recognize?continuous=true&model=pt-BR_BroadbandModel");
req.set_body(bytesWritten);
// Make HTTP request with the file stream as the body.
http_client client(L"https://stream.watsonplatform.net/", config);
//concurrency::streams::producer_consumer_buffer<uint8_t> buf;
//buf.putn_nocopy(&body[0], body.size());
return client.request(req).then([bytesWritten](pplx::task<http_response> previousTask)
{
//buffer.close;
std::wostringstream ss;
try
{
auto response = previousTask.get();
response.extract_json().then([=](json::value js) {
int i = 0;
auto campo = js.at(to_string_t("results"));
while (i < campo.size()) {
auto transc = campo.operator[](i).operator[](to_string_t("alternatives")).operator[](0);
wcout << transc.at(to_string_t("transcript")) << endl;
i++;
}
});
}
catch (const http_exception& e)
{
ss << e.what() << std::endl;
}
std::wcout << ss.str();
});
}
catch (const std::system_error& e)
{
std::wostringstream ss;
ss << e.what() << std::endl;
std::wcout << ss.str();
// Return an empty task.
return pplx::task_from_result();
}
});
}