Skip to content

Commit

Permalink
static castやnullopt比較をやめ、value関数などを使うようにする (#118)
Browse files Browse the repository at this point in the history
* use value and has value

* remove has_value
  • Loading branch information
y-chan authored May 28, 2022
1 parent 6253a8d commit d62a44b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions core/src/engine/full_context_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ bool Phoneme::is_pause() const { return contexts.at("f1") == "xx"; }
void Mora::set_context(const std::string &key, const std::string &value) {
vowel.contexts[key] = value;

if (!consonant.has_value()) {
if (!consonant) {
consonant.value().contexts[key] = value;
}
}

std::vector<Phoneme> Mora::phonemes() const {
std::vector<Phoneme> phonemes;
if (consonant.has_value()) {
if (consonant) {
phonemes = {consonant.value(), vowel};
} else {
phonemes = {vowel};
Expand Down
12 changes: 6 additions & 6 deletions core/src/engine/kana_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ AccentPhraseModel text_to_accent_phrase(const std::string& phrase) {
matched_text = stack;
}
}
if (matched_text == std::nullopt) {
throw std::runtime_error("unknown text in accent phrase: " + stack);
} else {
moras.push_back(text2mora.at(*matched_text));
if (matched_text) {
moras.push_back(text2mora.at(matched_text.value()));
base_index += matched_text->size();
stack = "";
matched_text = std::nullopt;
} else {
throw std::runtime_error("unknown text in accent phrase: " + stack);
}
if (outer_loop > LOOP_LIMIT) throw std::runtime_error("detect infinity loop!");
}
if (accent_index == std::nullopt) throw std::runtime_error("accent not found in accent phrase: " + phrase);

AccentPhraseModel accent_phrase = {
moras,
static_cast<unsigned int>(*accent_index),
accent_index.value(),
};
return accent_phrase;
}
Expand Down Expand Up @@ -178,7 +178,7 @@ std::string create_kana(std::vector<AccentPhraseModel> accent_phrases) {
}

if (i < accent_phrases.size()) {
if (phrase.pause_mora != std::nullopt)
if (phrase.pause_mora)
text += PAUSE_DELIMITER;
else
text += NOPAUSE_DELIMITER;
Expand Down
22 changes: 11 additions & 11 deletions core/src/engine/synthesis_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ std::vector<MoraModel> to_flatten_moras(std::vector<AccentPhraseModel> accent_ph
for (MoraModel mora : moras) {
flatten_moras.push_back(mora);
}
if (accent_phrase.pause_mora != std::nullopt) {
MoraModel pause_mora = static_cast<MoraModel>(*accent_phrase.pause_mora);
if (accent_phrase.pause_mora) {
MoraModel pause_mora = accent_phrase.pause_mora.value();
flatten_moras.push_back(pause_mora);
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ std::vector<AccentPhraseModel> SynthesisEngine::create_accent_phrases(std::strin
if (moras_text == "n") moras_text = "N";
std::optional<std::string> consonant = std::nullopt;
std::optional<float> consonant_length = std::nullopt;
if (mora.consonant.has_value()) {
if (mora.consonant) {
consonant = mora.consonant.value().phoneme();
consonant_length = 0.0f;
}
Expand Down Expand Up @@ -204,15 +204,15 @@ std::vector<AccentPhraseModel> SynthesisEngine::replace_phoneme_length(std::vect
std::vector<MoraModel> moras = accent_phrase.moras;
for (size_t j = 0; j < moras.size(); j++) {
MoraModel mora = moras[j];
if (mora.consonant != std::nullopt) {
if (mora.consonant) {
mora.consonant_length = phoneme_length[vowel_indexes_data[index + 1] - 1];
}
mora.vowel_length = phoneme_length[vowel_indexes_data[index + 1]];
index++;
moras[j] = mora;
}
accent_phrase.moras = moras;
if (accent_phrase.pause_mora != std::nullopt) {
if (accent_phrase.pause_mora) {
std::optional<MoraModel> pause_mora = accent_phrase.pause_mora;
pause_mora->vowel_length = phoneme_length[vowel_indexes_data[index + 1]];
index++;
Expand Down Expand Up @@ -310,7 +310,7 @@ std::vector<AccentPhraseModel> SynthesisEngine::replace_mora_pitch(std::vector<A
moras[j] = mora;
}
accent_phrase.moras = moras;
if (accent_phrase.pause_mora != std::nullopt) {
if (accent_phrase.pause_mora) {
std::optional<MoraModel> pause_mora = accent_phrase.pause_mora;
pause_mora->pitch = f0_list[index + 1];
index++;
Expand Down Expand Up @@ -427,8 +427,8 @@ std::vector<float> SynthesisEngine::synthesis(AudioQueryModel query, int64_t *sp
int count = 0;

for (MoraModel mora : flatten_moras) {
if (mora.consonant != std::nullopt) {
phoneme_length_list.push_back(static_cast<float>(*mora.consonant_length));
if (mora.consonant) {
phoneme_length_list.push_back(mora.consonant_length.value());
}
phoneme_length_list.push_back(mora.vowel_length);
float f0_single = mora.pitch * std::pow(2.0f, pitch_scale);
Expand Down Expand Up @@ -509,7 +509,7 @@ void SynthesisEngine::initial_process(std::vector<AccentPhraseModel> &accent_phr
phoneme_str_list.push_back("pau");
for (MoraModel mora : flatten_moras) {
std::optional<std::string> consonant = mora.consonant;
if (consonant != std::nullopt) phoneme_str_list.push_back(static_cast<std::string>(*consonant));
if (consonant) phoneme_str_list.push_back(consonant.value());
phoneme_str_list.push_back(mora.vowel);
}
phoneme_str_list.push_back("pau");
Expand All @@ -530,11 +530,11 @@ void SynthesisEngine::create_one_accent_list(std::vector<int64_t> &accent_list,
else
value = 0;
one_accent_list.push_back(value);
if (mora.consonant != std::nullopt) {
if (mora.consonant) {
one_accent_list.push_back(value);
}
}
if (accent_phrase.pause_mora != std::nullopt) one_accent_list.push_back(0);
if (accent_phrase.pause_mora) one_accent_list.push_back(0);
std::copy(one_accent_list.begin(), one_accent_list.end(), std::back_inserter(accent_list));
}
} // namespace voicevox::core::engine

0 comments on commit d62a44b

Please sign in to comment.