@ -0,0 +1,64 @@ |
|||||
|
// Copyright (c) 2021 Ximalaya Speech Team (Xiang Lyu)
|
||||
|
//
|
||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
// you may not use this file except in compliance with the License.
|
||||
|
// You may obtain a copy of the License at
|
||||
|
//
|
||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
//
|
||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
// See the License for the specific language governing permissions and
|
||||
|
// limitations under the License.
|
||||
|
|
||||
|
#include "frontend/wav.h"
|
||||
|
#include "grpc/grpc_client.h"
|
||||
|
#include "utils/flags.h"
|
||||
|
#include "utils/timer.h"
|
||||
|
|
||||
|
DEFINE_string(hostname, "127.0.0.1", "hostname of websocket server"); |
||||
|
DEFINE_int32(port, 10086, "port of websocket server"); |
||||
|
DEFINE_int32(nbest, 1, "n-best of decode result"); |
||||
|
DEFINE_string(wav_path, "", "test wav file path"); |
||||
|
DEFINE_bool(continuous_decoding, false, "continuous decoding mode"); |
||||
|
|
||||
|
int main(int argc, char* argv[]) { |
||||
|
gflags::ParseCommandLineFlags(&argc, &argv, false); |
||||
|
google::InitGoogleLogging(argv[0]); |
||||
|
wenet::GrpcClient client(FLAGS_hostname, FLAGS_port, FLAGS_nbest, |
||||
|
FLAGS_continuous_decoding); |
||||
|
|
||||
|
wenet::WavReader wav_reader(FLAGS_wav_path); |
||||
|
const int sample_rate = 16000; |
||||
|
// Only support 16K
|
||||
|
CHECK_EQ(wav_reader.sample_rate(), sample_rate); |
||||
|
const int num_samples = wav_reader.num_samples(); |
||||
|
std::vector<float> pcm_data(wav_reader.data(), |
||||
|
wav_reader.data() + num_samples); |
||||
|
// Send data every 0.5 second
|
||||
|
const float interval = 0.5; |
||||
|
const int sample_interval = interval * sample_rate; |
||||
|
for (int start = 0; start < num_samples; start += sample_interval) { |
||||
|
if (client.done()) { |
||||
|
break; |
||||
|
} |
||||
|
int end = std::min(start + sample_interval, num_samples); |
||||
|
// Convert to short
|
||||
|
std::vector<int16_t> data; |
||||
|
data.reserve(end - start); |
||||
|
for (int j = start; j < end; j++) { |
||||
|
data.push_back(static_cast<int16_t>(pcm_data[j])); |
||||
|
} |
||||
|
// Send PCM data
|
||||
|
client.SendBinaryData(data.data(), data.size() * sizeof(int16_t)); |
||||
|
VLOG(2) << "Send " << data.size() << " samples"; |
||||
|
std::this_thread::sleep_for( |
||||
|
std::chrono::milliseconds(static_cast<int>(interval * 1000))); |
||||
|
} |
||||
|
wenet::Timer timer; |
||||
|
|
||||
|
client.Join(); |
||||
|
VLOG(2) << "Total latency: " << timer.Elapsed() << "ms."; |
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,50 @@ |
|||||
|
// Copyright (c) 2021 Ximalaya Speech Team (Xiang Lyu)
|
||||
|
//
|
||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
// you may not use this file except in compliance with the License.
|
||||
|
// You may obtain a copy of the License at
|
||||
|
//
|
||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
//
|
||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
// See the License for the specific language governing permissions and
|
||||
|
// limitations under the License.
|
||||
|
|
||||
|
#include <grpcpp/ext/proto_server_reflection_plugin.h>
|
||||
|
#include <grpcpp/grpcpp.h>
|
||||
|
#include <grpcpp/health_check_service_interface.h>
|
||||
|
|
||||
|
#include "decoder/params.h"
|
||||
|
#include "grpc/grpc_server.h"
|
||||
|
|
||||
|
DEFINE_int32(port, 10086, "grpc listening port"); |
||||
|
DEFINE_int32(workers, 4, "grpc num workers"); |
||||
|
|
||||
|
using grpc::Server; |
||||
|
using grpc::ServerBuilder; |
||||
|
|
||||
|
int main(int argc, char* argv[]) { |
||||
|
gflags::ParseCommandLineFlags(&argc, &argv, false); |
||||
|
google::InitGoogleLogging(argv[0]); |
||||
|
|
||||
|
auto decode_config = wenet::InitDecodeOptionsFromFlags(); |
||||
|
auto feature_config = wenet::InitFeaturePipelineConfigFromFlags(); |
||||
|
auto decode_resource = wenet::InitDecodeResourceFromFlags(); |
||||
|
|
||||
|
wenet::GrpcServer service(feature_config, decode_config, decode_resource); |
||||
|
grpc::EnableDefaultHealthCheckService(true); |
||||
|
grpc::reflection::InitProtoReflectionServerBuilderPlugin(); |
||||
|
ServerBuilder builder; |
||||
|
std::string address("0.0.0.0:" + std::to_string(FLAGS_port)); |
||||
|
builder.AddListeningPort(address, grpc::InsecureServerCredentials()); |
||||
|
builder.RegisterService(&service); |
||||
|
builder.SetSyncServerOption(ServerBuilder::SyncServerOption::NUM_CQS, |
||||
|
FLAGS_workers); |
||||
|
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||
|
LOG(INFO) << "Listening at port " << FLAGS_port; |
||||
|
server->Wait(); |
||||
|
google::ShutdownGoogleLogging(); |
||||
|
return 0; |
||||
|
} |