Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static castやnullopt比較をやめ、value関数などを使うようにする #118

Merged
merged 2 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (accent_index == std::nullopt) throw std::runtime_error("accent not found in accent phrase: " + phrase);
if (!accent_index) throw std::runtime_error("accent not found in accent phrase: " + phrase);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あと83行目もありますが、そこに関しては「中身があるならthrow」というぱっと見非自明なif文なのでどちらが可読性が良いのかは不明です

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

83行目のことも考慮して現在のコードになっています。
統一したほうが可読性が高いと思うので、こちらもそのままにすべきと思いましたが、どうでしょうか?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、意図的なものであればOKです


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