2015-11-18 19:59:03 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2016-09-21 08:32:26 +00:00
|
|
|
#include "phoneRecognition.h"
|
2016-11-15 20:56:39 +00:00
|
|
|
#include "SampleRateConverter.h"
|
2015-12-29 15:26:01 +00:00
|
|
|
#include "platformTools.h"
|
2015-11-19 20:17:35 +00:00
|
|
|
#include "tools.h"
|
2016-01-19 21:05:06 +00:00
|
|
|
#include <format.h>
|
2016-02-09 21:08:11 +00:00
|
|
|
#include <s3types.h>
|
|
|
|
#include <regex>
|
2016-02-29 19:58:58 +00:00
|
|
|
#include <gsl_util.h>
|
2016-02-29 20:47:36 +00:00
|
|
|
#include <logging.h>
|
2016-11-16 10:49:25 +00:00
|
|
|
#include "DcOffset.h"
|
2016-11-15 20:56:39 +00:00
|
|
|
#include "Timeline.h"
|
|
|
|
#include "voiceActivityDetection.h"
|
|
|
|
#include "AudioSegment.h"
|
2016-06-03 19:07:49 +00:00
|
|
|
#include "languageModels.h"
|
|
|
|
#include "tokenization.h"
|
|
|
|
#include "g2p.h"
|
2016-06-21 17:20:27 +00:00
|
|
|
#include "ContinuousTimeline.h"
|
2016-11-15 20:56:39 +00:00
|
|
|
#include "processing.h"
|
2016-07-27 19:15:00 +00:00
|
|
|
#include "parallel.h"
|
2016-09-08 20:23:19 +00:00
|
|
|
#include <boost/version.hpp>
|
2016-09-21 08:28:31 +00:00
|
|
|
#include "ObjectPool.h"
|
2016-11-15 20:56:39 +00:00
|
|
|
#include "timedLogging.h"
|
2015-11-19 20:17:35 +00:00
|
|
|
|
2015-12-21 12:09:09 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <pocketsphinx.h>
|
|
|
|
#include <sphinxbase/err.h>
|
2016-01-19 21:05:06 +00:00
|
|
|
#include <ps_alignment.h>
|
|
|
|
#include <state_align_search.h>
|
|
|
|
#include <pocketsphinx_internal.h>
|
2016-06-30 18:06:38 +00:00
|
|
|
#include <ngram_search.h>
|
2015-12-21 12:09:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:59:03 +00:00
|
|
|
using std::runtime_error;
|
2016-02-09 21:08:11 +00:00
|
|
|
using std::invalid_argument;
|
2015-11-18 19:59:03 +00:00
|
|
|
using std::unique_ptr;
|
|
|
|
using std::shared_ptr;
|
|
|
|
using std::string;
|
2016-01-19 21:05:06 +00:00
|
|
|
using std::vector;
|
2015-11-18 19:59:03 +00:00
|
|
|
using std::map;
|
|
|
|
using boost::filesystem::path;
|
2016-01-08 09:53:35 +00:00
|
|
|
using std::function;
|
2016-02-09 21:08:11 +00:00
|
|
|
using std::regex;
|
|
|
|
using std::regex_replace;
|
2016-03-01 20:57:05 +00:00
|
|
|
using std::chrono::duration;
|
2016-06-03 19:07:49 +00:00
|
|
|
using boost::optional;
|
|
|
|
using std::u32string;
|
2016-06-29 19:44:17 +00:00
|
|
|
using std::chrono::duration_cast;
|
2016-10-21 19:41:50 +00:00
|
|
|
using std::array;
|
2015-11-18 19:59:03 +00:00
|
|
|
|
2016-03-15 18:56:02 +00:00
|
|
|
constexpr int sphinxSampleRate = 16000;
|
2015-11-18 19:59:03 +00:00
|
|
|
|
2016-06-03 19:07:49 +00:00
|
|
|
const path& getSphinxModelDirectory() {
|
|
|
|
static path sphinxModelDirectory(getBinDirectory() / "res/sphinx");
|
|
|
|
return sphinxModelDirectory;
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:37:39 +00:00
|
|
|
logging::Level ConvertSphinxErrorLevel(err_lvl_t errorLevel) {
|
2016-02-29 20:47:36 +00:00
|
|
|
switch (errorLevel) {
|
|
|
|
case ERR_DEBUG:
|
|
|
|
case ERR_INFO:
|
|
|
|
case ERR_INFOCONT:
|
2016-04-13 20:37:39 +00:00
|
|
|
return logging::Level::Trace;
|
2016-02-29 20:47:36 +00:00
|
|
|
case ERR_WARN:
|
2016-04-13 20:37:39 +00:00
|
|
|
return logging::Level::Warn;
|
2016-02-29 20:47:36 +00:00
|
|
|
case ERR_ERROR:
|
2016-04-13 20:37:39 +00:00
|
|
|
return logging::Level::Error;
|
2016-02-29 20:47:36 +00:00
|
|
|
case ERR_FATAL:
|
2016-04-13 20:37:39 +00:00
|
|
|
return logging::Level::Fatal;
|
2016-02-29 20:47:36 +00:00
|
|
|
default:
|
|
|
|
throw invalid_argument("Unknown log level.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sphinxLogCallback(void* user_data, err_lvl_t errorLevel, const char* format, ...) {
|
|
|
|
UNUSED(user_data);
|
2015-11-19 21:48:17 +00:00
|
|
|
|
|
|
|
// Create varArgs list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2016-02-29 19:58:58 +00:00
|
|
|
auto _ = gsl::finally([&args]() { va_end(args); });
|
2015-11-19 21:48:17 +00:00
|
|
|
|
|
|
|
// Format message
|
|
|
|
const int initialSize = 256;
|
2016-01-19 21:05:06 +00:00
|
|
|
vector<char> chars(initialSize);
|
2015-11-19 21:48:17 +00:00
|
|
|
bool success = false;
|
|
|
|
while (!success) {
|
|
|
|
int charsWritten = vsnprintf(chars.data(), chars.size(), format, args);
|
|
|
|
if (charsWritten < 0) throw runtime_error("Error formatting Pocketsphinx log message.");
|
|
|
|
|
|
|
|
success = charsWritten < static_cast<int>(chars.size());
|
|
|
|
if (!success) chars.resize(chars.size() * 2);
|
|
|
|
}
|
2016-05-17 15:56:11 +00:00
|
|
|
regex waste("^(DEBUG|INFO|INFOCONT|WARN|ERROR|FATAL): ");
|
|
|
|
string message = regex_replace(chars.data(), waste, "", std::regex_constants::format_first_only);
|
2015-11-19 21:48:17 +00:00
|
|
|
boost::algorithm::trim(message);
|
|
|
|
|
2016-04-13 20:37:39 +00:00
|
|
|
logging::Level logLevel = ConvertSphinxErrorLevel(errorLevel);
|
|
|
|
logging::log(logLevel, message);
|
2015-11-19 21:48:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 11:11:01 +00:00
|
|
|
BoundedTimeline<string> recognizeWords(const vector<int16_t>& audioBuffer, ps_decoder_t& decoder) {
|
2016-05-17 12:16:16 +00:00
|
|
|
// Restart timing at 0
|
2016-06-03 19:07:49 +00:00
|
|
|
ps_start_stream(&decoder);
|
2016-05-17 12:16:16 +00:00
|
|
|
|
2016-01-19 21:05:06 +00:00
|
|
|
// Start recognition
|
2016-06-03 19:07:49 +00:00
|
|
|
int error = ps_start_utt(&decoder);
|
2016-01-19 21:05:06 +00:00
|
|
|
if (error) throw runtime_error("Error starting utterance processing for word recognition.");
|
|
|
|
|
2016-09-18 20:02:02 +00:00
|
|
|
// Process entire audio clip
|
|
|
|
const bool noRecognition = false;
|
|
|
|
const bool fullUtterance = true;
|
2016-09-26 11:11:01 +00:00
|
|
|
int searchedFrameCount = ps_process_raw(&decoder, audioBuffer.data(), audioBuffer.size(), noRecognition, fullUtterance);
|
2016-09-18 20:02:02 +00:00
|
|
|
if (searchedFrameCount < 0) throw runtime_error("Error analyzing raw audio data for word recognition.");
|
2016-01-19 21:05:06 +00:00
|
|
|
|
|
|
|
// End recognition
|
2016-06-03 19:07:49 +00:00
|
|
|
error = ps_end_utt(&decoder);
|
2016-01-19 21:05:06 +00:00
|
|
|
if (error) throw runtime_error("Error ending utterance processing for word recognition.");
|
|
|
|
|
2016-09-26 11:11:01 +00:00
|
|
|
BoundedTimeline<string> result(TimeRange(0_cs, centiseconds(100 * audioBuffer.size() / sphinxSampleRate)));
|
2016-06-30 18:06:38 +00:00
|
|
|
bool noWordsRecognized = reinterpret_cast<ngram_search_t*>(decoder.search)->bpidx == 0;
|
|
|
|
if (noWordsRecognized) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect words
|
2016-06-19 19:18:40 +00:00
|
|
|
for (ps_seg_t* it = ps_seg_iter(&decoder); it; it = ps_seg_next(it)) {
|
2016-01-19 21:05:06 +00:00
|
|
|
const char* word = ps_seg_word(it);
|
2016-03-01 20:57:05 +00:00
|
|
|
int firstFrame, lastFrame;
|
|
|
|
ps_seg_frames(it, &firstFrame, &lastFrame);
|
2016-05-17 12:16:16 +00:00
|
|
|
result.set(centiseconds(firstFrame), centiseconds(lastFrame + 1), word);
|
2016-02-09 21:08:11 +00:00
|
|
|
}
|
2016-01-19 21:05:06 +00:00
|
|
|
|
2016-02-09 21:08:11 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-17 12:16:16 +00:00
|
|
|
s3wid_t getWordId(const string& word, dict_t& dictionary) {
|
|
|
|
s3wid_t wordId = dict_wordid(&dictionary, word.c_str());
|
|
|
|
if (wordId == BAD_S3WID) throw invalid_argument(fmt::format("Unknown word '{}'.", word));
|
|
|
|
return wordId;
|
2016-01-19 21:05:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 19:44:17 +00:00
|
|
|
optional<Timeline<Phone>> getPhoneAlignment(
|
2016-05-02 18:31:59 +00:00
|
|
|
const vector<s3wid_t>& wordIds,
|
2016-09-26 11:11:01 +00:00
|
|
|
const vector<int16_t>& audioBuffer,
|
2016-09-18 20:02:02 +00:00
|
|
|
ps_decoder_t& decoder)
|
2016-05-02 18:31:59 +00:00
|
|
|
{
|
2016-01-19 21:05:06 +00:00
|
|
|
// Create alignment list
|
|
|
|
lambda_unique_ptr<ps_alignment_t> alignment(
|
2016-06-03 19:07:49 +00:00
|
|
|
ps_alignment_init(decoder.d2p),
|
2016-01-19 21:05:06 +00:00
|
|
|
[](ps_alignment_t* alignment) { ps_alignment_free(alignment); });
|
|
|
|
if (!alignment) throw runtime_error("Error creating alignment.");
|
|
|
|
for (s3wid_t wordId : wordIds) {
|
|
|
|
// Add word. Initial value for duration is ignored.
|
|
|
|
ps_alignment_add_word(alignment.get(), wordId, 0);
|
|
|
|
}
|
|
|
|
int error = ps_alignment_populate(alignment.get());
|
|
|
|
if (error) throw runtime_error("Error populating alignment struct.");
|
|
|
|
|
|
|
|
// Create search structure
|
2016-06-03 19:07:49 +00:00
|
|
|
acmod_t* acousticModel = decoder.acmod;
|
2016-01-19 21:05:06 +00:00
|
|
|
lambda_unique_ptr<ps_search_t> search(
|
2016-06-03 19:07:49 +00:00
|
|
|
state_align_search_init("state_align", decoder.config, acousticModel, alignment.get()),
|
2016-01-19 21:05:06 +00:00
|
|
|
[](ps_search_t* search) { ps_search_free(search); });
|
|
|
|
if (!search) throw runtime_error("Error creating search.");
|
|
|
|
|
|
|
|
// Start recognition
|
|
|
|
error = acmod_start_utt(acousticModel);
|
|
|
|
if (error) throw runtime_error("Error starting utterance processing for alignment.");
|
|
|
|
|
2016-06-21 17:20:27 +00:00
|
|
|
{
|
|
|
|
// Eventually end recognition
|
|
|
|
auto endRecognition = gsl::finally([&]() { acmod_end_utt(acousticModel); });
|
|
|
|
|
|
|
|
// Start search
|
|
|
|
ps_search_start(search.get());
|
|
|
|
|
2016-09-18 20:02:02 +00:00
|
|
|
// Process entire audio clip
|
2016-09-26 11:11:01 +00:00
|
|
|
const int16* nextSample = audioBuffer.data();
|
|
|
|
size_t remainingSamples = audioBuffer.size();
|
2016-09-18 20:02:02 +00:00
|
|
|
bool fullUtterance = true;
|
|
|
|
while (acmod_process_raw(acousticModel, &nextSample, &remainingSamples, fullUtterance) > 0) {
|
|
|
|
while (acousticModel->n_feat_frame > 0) {
|
|
|
|
ps_search_step(search.get(), acousticModel->output_frame);
|
|
|
|
acmod_advance(acousticModel);
|
2016-01-19 21:05:06 +00:00
|
|
|
}
|
2016-09-18 20:02:02 +00:00
|
|
|
}
|
2016-01-19 21:05:06 +00:00
|
|
|
|
2016-06-21 17:20:27 +00:00
|
|
|
// End search
|
|
|
|
error = ps_search_finish(search.get());
|
|
|
|
if (error) return boost::none;
|
|
|
|
}
|
2016-01-19 21:05:06 +00:00
|
|
|
|
|
|
|
// Extract phones with timestamps
|
2016-06-03 19:07:49 +00:00
|
|
|
char** phoneNames = decoder.dict->mdef->ciname;
|
2016-06-29 19:44:17 +00:00
|
|
|
Timeline<Phone> result;
|
2016-01-19 21:05:06 +00:00
|
|
|
for (ps_alignment_iter_t* it = ps_alignment_phones(alignment.get()); it; it = ps_alignment_iter_next(it)) {
|
|
|
|
// Get phone
|
|
|
|
ps_alignment_entry_t* phoneEntry = ps_alignment_iter_get(it);
|
|
|
|
s3cipid_t phoneId = phoneEntry->id.pid.cipid;
|
2016-05-02 18:31:59 +00:00
|
|
|
string phoneName = phoneNames[phoneId];
|
|
|
|
|
|
|
|
if (phoneName == "SIL") continue;
|
2016-01-19 21:05:06 +00:00
|
|
|
|
2016-04-09 20:07:25 +00:00
|
|
|
// Add entry
|
|
|
|
centiseconds start(phoneEntry->start);
|
|
|
|
centiseconds duration(phoneEntry->duration);
|
2016-09-30 15:12:10 +00:00
|
|
|
Phone phone = PhoneConverter::get().parse(phoneName);
|
|
|
|
if (phone == Phone::AH && duration < 6_cs) {
|
|
|
|
// Heuristic: < 6_cs is schwa. Pocketsphinx doesn't differentiate.
|
|
|
|
phone = Phone::Schwa;
|
|
|
|
}
|
|
|
|
Timed<Phone> timedPhone(start, start + duration, phone);
|
2016-04-09 20:07:25 +00:00
|
|
|
result.set(timedPhone);
|
2016-01-19 21:05:06 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-25 19:52:04 +00:00
|
|
|
bool dictionaryContains(dict_t& dictionary, const string& word) {
|
|
|
|
return dict_wordid(&dictionary, word.c_str()) != BAD_S3WID;
|
|
|
|
}
|
|
|
|
|
2016-06-03 19:07:49 +00:00
|
|
|
void addMissingDictionaryWords(const vector<string>& words, ps_decoder_t& decoder) {
|
|
|
|
map<string, string> missingPronunciations;
|
|
|
|
for (const string& word : words) {
|
2016-06-25 19:52:04 +00:00
|
|
|
if (!dictionaryContains(*decoder.dict, word)) {
|
2016-06-03 19:07:49 +00:00
|
|
|
string pronunciation;
|
|
|
|
for (Phone phone : wordToPhones(word)) {
|
|
|
|
if (pronunciation.length() > 0) pronunciation += " ";
|
|
|
|
pronunciation += PhoneConverter::get().toString(phone);
|
|
|
|
}
|
|
|
|
missingPronunciations[word] = pronunciation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto it = missingPronunciations.begin(); it != missingPronunciations.end(); ++it) {
|
|
|
|
bool isLast = it == --missingPronunciations.end();
|
|
|
|
logging::infoFormat("Unknown word '{}'. Guessing pronunciation '{}'.", it->first, it->second);
|
|
|
|
ps_add_word(&decoder, it->first.c_str(), it->second.c_str(), isLast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 19:41:50 +00:00
|
|
|
lambda_unique_ptr<ngram_model_t> createDefaultLanguageModel(ps_decoder_t& decoder) {
|
|
|
|
path modelPath = getSphinxModelDirectory() / "en-us.lm.bin";
|
|
|
|
lambda_unique_ptr<ngram_model_t> result(
|
|
|
|
ngram_model_read(decoder.config, modelPath.string().c_str(), NGRAM_AUTO, decoder.lmath),
|
|
|
|
[](ngram_model_t* lm) { ngram_model_free(lm); });
|
|
|
|
if (!result) {
|
|
|
|
throw runtime_error(fmt::format("Error reading language model from {}.", modelPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
lambda_unique_ptr<ngram_model_t> createDialogLanguageModel(ps_decoder_t& decoder, const u32string& dialog) {
|
|
|
|
// Split dialog into normalized words
|
|
|
|
vector<string> words = tokenizeText(dialog, [&](const string& word) { return dictionaryContains(*decoder.dict, word); });
|
|
|
|
|
|
|
|
// Add dialog-specific words to the dictionary
|
|
|
|
addMissingDictionaryWords(words, decoder);
|
|
|
|
|
|
|
|
// Create dialog-specific language model
|
|
|
|
words.insert(words.begin(), "<s>");
|
|
|
|
words.push_back("</s>");
|
|
|
|
return createLanguageModel(words, decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
lambda_unique_ptr<ngram_model_t> createBiasedLanguageModel(ps_decoder_t& decoder, const u32string& dialog) {
|
|
|
|
auto defaultLanguageModel = createDefaultLanguageModel(decoder);
|
|
|
|
auto dialogLanguageModel = createDialogLanguageModel(decoder, dialog);
|
|
|
|
constexpr int modelCount = 2;
|
|
|
|
array<ngram_model_t*, modelCount> languageModels{ defaultLanguageModel.get(), dialogLanguageModel.get() };
|
|
|
|
array<char*, modelCount> modelNames{ "defaultLM", "dialogLM" };
|
|
|
|
array<float, modelCount> modelWeights{ 0.1f, 0.9f };
|
|
|
|
lambda_unique_ptr<ngram_model_t> result(
|
|
|
|
ngram_model_set_init(nullptr, languageModels.data(), modelNames.data(), modelWeights.data(), modelCount),
|
|
|
|
[](ngram_model_t* lm) { ngram_model_free(lm); });
|
|
|
|
if (!result) {
|
|
|
|
throw runtime_error("Error creating biased language model.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:05:09 +00:00
|
|
|
lambda_unique_ptr<ps_decoder_t> createDecoder(optional<u32string> dialog) {
|
|
|
|
lambda_unique_ptr<cmd_ln_t> config(
|
|
|
|
cmd_ln_init(
|
|
|
|
nullptr, ps_args(), true,
|
|
|
|
// Set acoustic model
|
|
|
|
"-hmm", (getSphinxModelDirectory() / "acoustic-model").string().c_str(),
|
|
|
|
// Set pronunciation dictionary
|
|
|
|
"-dict", (getSphinxModelDirectory() / "cmudict-en-us.dict").string().c_str(),
|
|
|
|
// Add noise against zero silence (see http://cmusphinx.sourceforge.net/wiki/faq#qwhy_my_accuracy_is_poor)
|
|
|
|
"-dither", "yes",
|
2016-08-10 18:46:32 +00:00
|
|
|
// Disable VAD -- we're doing that ourselves
|
|
|
|
"-remove_silence", "no",
|
2016-09-18 20:02:02 +00:00
|
|
|
// Perform per-utterance cepstral mean normalization
|
|
|
|
"-cmn", "batch",
|
2016-06-26 19:05:09 +00:00
|
|
|
nullptr),
|
|
|
|
[](cmd_ln_t* config) { cmd_ln_free_r(config); });
|
|
|
|
if (!config) throw runtime_error("Error creating configuration.");
|
|
|
|
|
|
|
|
lambda_unique_ptr<ps_decoder_t> decoder(
|
|
|
|
ps_init(config.get()),
|
|
|
|
[](ps_decoder_t* recognizer) { ps_free(recognizer); });
|
|
|
|
if (!decoder) throw runtime_error("Error creating speech decoder.");
|
|
|
|
|
|
|
|
// Set language model
|
2016-10-21 19:41:50 +00:00
|
|
|
lambda_unique_ptr<ngram_model_t> languageModel(dialog
|
|
|
|
? createBiasedLanguageModel(*decoder, *dialog)
|
|
|
|
: createDefaultLanguageModel(*decoder));
|
2016-06-26 19:05:09 +00:00
|
|
|
ps_set_lm(decoder.get(), "lm", languageModel.get());
|
|
|
|
ps_set_search(decoder.get(), "lm");
|
|
|
|
|
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:44:34 +00:00
|
|
|
Timeline<void> getNoiseSounds(TimeRange utteranceTimeRange, const Timeline<Phone>& phones) {
|
|
|
|
Timeline<void> noiseSounds;
|
|
|
|
|
|
|
|
// Find utterance parts without recogniced phones
|
|
|
|
noiseSounds.set(utteranceTimeRange);
|
|
|
|
for (const auto& timedPhone : phones) {
|
|
|
|
noiseSounds.clear(timedPhone.getTimeRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove undesired elements
|
2016-09-29 09:55:36 +00:00
|
|
|
const centiseconds minSoundDuration = 12_cs;
|
2016-09-29 08:44:34 +00:00
|
|
|
for (const auto& unknownSound : Timeline<void>(noiseSounds)) {
|
|
|
|
bool startsAtZero = unknownSound.getStart() == 0_cs;
|
2016-09-29 10:02:00 +00:00
|
|
|
bool tooShort = unknownSound.getDuration() < minSoundDuration;
|
2016-09-29 08:44:34 +00:00
|
|
|
if (startsAtZero || tooShort) {
|
|
|
|
noiseSounds.clear(unknownSound.getTimeRange());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return noiseSounds;
|
|
|
|
}
|
|
|
|
|
2016-06-29 19:44:17 +00:00
|
|
|
Timeline<Phone> utteranceToPhones(
|
2016-07-20 18:42:27 +00:00
|
|
|
const AudioClip& audioClip,
|
2016-09-29 08:44:34 +00:00
|
|
|
TimeRange utteranceTimeRange,
|
2016-06-29 19:44:17 +00:00
|
|
|
ps_decoder_t& decoder,
|
|
|
|
ProgressSink& utteranceProgressSink)
|
|
|
|
{
|
|
|
|
ProgressMerger utteranceProgressMerger(utteranceProgressSink);
|
|
|
|
ProgressSink& wordRecognitionProgressSink = utteranceProgressMerger.addSink(1.0);
|
|
|
|
ProgressSink& alignmentProgressSink = utteranceProgressMerger.addSink(0.5);
|
|
|
|
|
2016-09-29 08:44:34 +00:00
|
|
|
// Pad time range to give Pocketsphinx some breathing room
|
|
|
|
TimeRange paddedTimeRange = utteranceTimeRange;
|
|
|
|
const centiseconds padding(3);
|
|
|
|
paddedTimeRange.grow(padding);
|
|
|
|
paddedTimeRange.trim(audioClip.getTruncatedRange());
|
|
|
|
|
|
|
|
const unique_ptr<AudioClip> clipSegment = audioClip.clone() | segment(paddedTimeRange) | resample(sphinxSampleRate);
|
2016-09-26 11:11:01 +00:00
|
|
|
const auto audioBuffer = copyTo16bitBuffer(*clipSegment);
|
2016-06-29 19:44:17 +00:00
|
|
|
|
|
|
|
// Get words
|
2016-09-26 11:11:01 +00:00
|
|
|
BoundedTimeline<string> words = recognizeWords(audioBuffer, decoder);
|
2016-09-18 20:02:02 +00:00
|
|
|
wordRecognitionProgressSink.reportProgress(1.0);
|
2016-09-29 08:44:34 +00:00
|
|
|
|
|
|
|
// Log utterance text
|
|
|
|
string text;
|
|
|
|
for (auto& timedWord : words) {
|
|
|
|
string word = timedWord.getValue();
|
|
|
|
// Skip details
|
|
|
|
if (word == "<s>" || word == "</s>" || word == "<sil>") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
word = regex_replace(word, regex("\\(\\d\\)"), "");
|
|
|
|
if (text.size() > 0) {
|
|
|
|
text += " ";
|
|
|
|
}
|
|
|
|
text += word;
|
|
|
|
}
|
2016-11-15 20:56:39 +00:00
|
|
|
logTimedEvent("utterance", utteranceTimeRange, text);
|
2016-09-29 08:44:34 +00:00
|
|
|
|
|
|
|
// Log words
|
2016-06-29 19:44:17 +00:00
|
|
|
for (Timed<string> timedWord : words) {
|
2016-09-29 08:44:34 +00:00
|
|
|
timedWord.getTimeRange().shift(paddedTimeRange.getStart());
|
2016-11-15 20:56:39 +00:00
|
|
|
logTimedEvent("word", timedWord);
|
2016-06-29 19:44:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:44:34 +00:00
|
|
|
// Convert word strings to word IDs using dictionary
|
2016-06-29 19:44:17 +00:00
|
|
|
vector<s3wid_t> wordIds;
|
|
|
|
for (const auto& timedWord : words) {
|
|
|
|
wordIds.push_back(getWordId(timedWord.getValue(), *decoder.dict));
|
|
|
|
}
|
|
|
|
if (wordIds.empty()) return Timeline<Phone>();
|
|
|
|
|
|
|
|
// Align the words' phones with speech
|
2016-09-08 20:23:19 +00:00
|
|
|
#if BOOST_VERSION < 105600 // Support legacy syntax
|
|
|
|
#define value_or get_value_or
|
|
|
|
#endif
|
2016-09-29 08:44:34 +00:00
|
|
|
Timeline<Phone> utterancePhones = getPhoneAlignment(wordIds, audioBuffer, decoder)
|
2016-08-11 08:18:03 +00:00
|
|
|
.value_or(ContinuousTimeline<Phone>(clipSegment->getTruncatedRange(), Phone::Noise));
|
2016-09-18 20:02:02 +00:00
|
|
|
alignmentProgressSink.reportProgress(1.0);
|
2016-09-29 08:44:34 +00:00
|
|
|
utterancePhones.shift(paddedTimeRange.getStart());
|
|
|
|
|
|
|
|
// Log raw phones
|
|
|
|
for (const auto& timedPhone : utterancePhones) {
|
2016-11-15 20:56:39 +00:00
|
|
|
logTimedEvent("rawPhone", timedPhone);
|
2016-06-29 19:44:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:44:34 +00:00
|
|
|
// Guess positions of noise sounds
|
|
|
|
Timeline<void> noiseSounds = getNoiseSounds(utteranceTimeRange, utterancePhones);
|
|
|
|
for (const auto& noiseSound : noiseSounds) {
|
|
|
|
utterancePhones.set(noiseSound.getTimeRange(), Phone::Noise);
|
2016-08-11 08:18:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:44:34 +00:00
|
|
|
// Log phones
|
|
|
|
for (const auto& timedPhone : utterancePhones) {
|
2016-11-15 20:56:39 +00:00
|
|
|
logTimedEvent("phone", timedPhone);
|
2016-08-11 08:18:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:44:34 +00:00
|
|
|
return utterancePhones;
|
2016-08-11 08:18:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 08:32:26 +00:00
|
|
|
BoundedTimeline<Phone> recognizePhones(
|
2016-07-20 18:42:27 +00:00
|
|
|
const AudioClip& inputAudioClip,
|
2016-06-03 19:07:49 +00:00
|
|
|
optional<u32string> dialog,
|
2016-08-11 08:29:01 +00:00
|
|
|
int maxThreadCount,
|
2016-02-09 21:08:11 +00:00
|
|
|
ProgressSink& progressSink)
|
|
|
|
{
|
2016-06-29 19:44:17 +00:00
|
|
|
ProgressMerger totalProgressMerger(progressSink);
|
|
|
|
ProgressSink& voiceActivationProgressSink = totalProgressMerger.addSink(1.0);
|
|
|
|
ProgressSink& dialogProgressSink = totalProgressMerger.addSink(15);
|
|
|
|
|
|
|
|
// Make sure audio stream has no DC offset
|
2016-11-16 10:49:25 +00:00
|
|
|
const unique_ptr<AudioClip> audioClip = inputAudioClip.clone() | removeDcOffset();
|
2016-06-29 19:44:17 +00:00
|
|
|
|
|
|
|
// Split audio into utterances
|
|
|
|
BoundedTimeline<void> utterances;
|
|
|
|
try {
|
2016-08-11 08:29:01 +00:00
|
|
|
utterances = detectVoiceActivity(*audioClip, maxThreadCount, voiceActivationProgressSink);
|
2016-06-29 19:44:17 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
std::throw_with_nested(runtime_error("Error detecting segments of speech."));
|
|
|
|
}
|
|
|
|
|
2015-11-19 21:48:17 +00:00
|
|
|
// Discard Pocketsphinx output
|
|
|
|
err_set_logfp(nullptr);
|
|
|
|
|
2016-02-29 20:47:36 +00:00
|
|
|
// Redirect Pocketsphinx output to log
|
|
|
|
err_set_callback(sphinxLogCallback, nullptr);
|
2015-11-19 21:48:17 +00:00
|
|
|
|
2016-06-29 19:44:17 +00:00
|
|
|
// Prepare pool of decoders
|
2016-09-21 08:28:31 +00:00
|
|
|
ObjectPool<ps_decoder_t, lambda_unique_ptr<ps_decoder_t>> decoderPool(
|
|
|
|
[&dialog] { return createDecoder(dialog); });
|
2016-06-14 18:12:12 +00:00
|
|
|
|
2016-08-11 08:18:03 +00:00
|
|
|
BoundedTimeline<Phone> phones(audioClip->getTruncatedRange());
|
2016-06-30 18:06:38 +00:00
|
|
|
std::mutex resultMutex;
|
2016-06-29 19:44:17 +00:00
|
|
|
auto processUtterance = [&](Timed<void> timedUtterance, ProgressSink& utteranceProgressSink) {
|
|
|
|
// Detect phones for utterance
|
2016-09-21 08:28:31 +00:00
|
|
|
auto decoder = decoderPool.acquire();
|
2016-08-11 08:18:03 +00:00
|
|
|
Timeline<Phone> utterancePhones =
|
2016-09-29 08:44:34 +00:00
|
|
|
utteranceToPhones(*audioClip, timedUtterance.getTimeRange(), *decoder, utteranceProgressSink);
|
2016-06-29 19:44:17 +00:00
|
|
|
|
|
|
|
// Copy phones to result timeline
|
|
|
|
std::lock_guard<std::mutex> lock(resultMutex);
|
2016-08-11 08:18:03 +00:00
|
|
|
for (const auto& timedPhone : utterancePhones) {
|
|
|
|
phones.set(timedPhone);
|
2016-06-29 19:44:17 +00:00
|
|
|
}
|
|
|
|
};
|
2016-05-17 12:16:16 +00:00
|
|
|
|
2016-06-29 19:44:17 +00:00
|
|
|
auto getUtteranceProgressWeight = [](const Timed<void> timedUtterance) {
|
2016-09-29 10:02:00 +00:00
|
|
|
return timedUtterance.getDuration().count();
|
2016-06-29 19:44:17 +00:00
|
|
|
};
|
2016-05-17 12:16:16 +00:00
|
|
|
|
2016-06-29 19:44:17 +00:00
|
|
|
// Perform speech recognition
|
|
|
|
try {
|
|
|
|
// Determine how many parallel threads to use
|
|
|
|
int threadCount = std::min({
|
2016-08-11 08:29:01 +00:00
|
|
|
maxThreadCount,
|
2016-06-29 19:44:17 +00:00
|
|
|
// Don't use more threads than there are utterances to be processed
|
|
|
|
static_cast<int>(utterances.size()),
|
|
|
|
// Don't waste time creating additional threads (and decoders!) if the recording is short
|
2016-09-29 09:55:36 +00:00
|
|
|
static_cast<int>(duration_cast<std::chrono::seconds>(audioClip->getTruncatedRange().getDuration()).count() / 5)
|
2016-06-29 19:44:17 +00:00
|
|
|
});
|
2016-08-11 13:45:16 +00:00
|
|
|
if (threadCount < 1) {
|
|
|
|
threadCount = 1;
|
|
|
|
}
|
|
|
|
logging::debugFormat("Speech recognition using {} threads -- start", threadCount);
|
2016-07-27 19:15:00 +00:00
|
|
|
runParallel(processUtterance, utterances, threadCount, dialogProgressSink, getUtteranceProgressWeight);
|
2016-06-14 15:38:11 +00:00
|
|
|
logging::debug("Speech recognition -- end");
|
2015-12-21 12:09:09 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
2016-02-29 20:47:36 +00:00
|
|
|
std::throw_with_nested(runtime_error("Error performing speech recognition via Pocketsphinx."));
|
2015-11-19 17:32:14 +00:00
|
|
|
}
|
2016-08-11 08:18:03 +00:00
|
|
|
|
|
|
|
return phones;
|
2015-11-18 19:59:03 +00:00
|
|
|
}
|