使用C ++ Rest SDK发送音频缓冲区

时间:2017-07-11 17:55:55

标签: c++ casablanca cpprest-sdk

我最近开始使用C ++ Rest SDK,我尝试向Watson Speech to Text服务发送音频缓冲区,文档没有告诉我如何使用或上传缓冲区lib,我开始使用Microsfot示例上传文件(https://msdn.microsoft.com/en-us/library/jj950081.aspx),然后我尝试修改代码以发送缓冲区作为示例(https://msdn.microsoft.com/en-us/library/jj950083.aspx),但我对于某些内容如何&#39工作。 这是我目前的代码:

我目前的代码:

#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();
}
});
}

0 个答案:

没有答案