Files
live-caption/src/ui/livetranscriber.cpp
T

60 lines
1.5 KiB
C++
Raw Normal View History

#include "livetranscriber.h"
#include "iasr.h"
namespace lc {
LiveTranscriber::LiveTranscriber(IAsr & asr, QObject * parent)
: QObject(parent), asr_(asr)
{
}
LiveTranscriber::~LiveTranscriber()
{
stop();
}
QString LiveTranscriber::deviceName() const
{
return pipeline_ ? QString::fromStdString(pipeline_->deviceName()) : QString();
}
bool LiveTranscriber::start(const StreamOptions & opts, QString * error)
{
stop();
LivePipelineOptions popt;
popt.stream = opts;
pipeline_ = std::make_unique<LivePipeline>(asr_, popt);
// 回调在工作线程上被调用;连到主线程的接收者时 Qt 会自动排队投递。
pipeline_->setCaptionCallback([this](const std::string & committed,
const std::string & tail,
const std::string & language) {
Q_EMIT caption(QString::fromStdString(committed),
QString::fromStdString(tail),
QString::fromStdString(language));
});
pipeline_->setErrorCallback([this](const std::string & message) {
Q_EMIT failed(QString::fromStdString(message));
});
std::string err;
if (!pipeline_->start(&err)) {
if (error) *error = QString::fromStdString(err);
pipeline_.reset();
return false;
}
return true;
}
void LiveTranscriber::stop()
{
if (pipeline_) {
pipeline_->stop();
pipeline_.reset();
}
}
} // namespace lc