From d4e0b35803434e932368399a2a005e4111a332c0 Mon Sep 17 00:00:00 2001 From: takenori-y Date: Thu, 12 Jan 2023 20:05:37 +0900 Subject: [PATCH 1/2] update -n in excite --- src/generation/excitation_generation.cc | 7 ++- src/main/excite.cc | 67 ++++++++++++++++++------- test/test_excite.bats | 2 +- tools/Makefile | 2 +- 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/generation/excitation_generation.cc b/src/generation/excitation_generation.cc index d2b4ee9c..4c219316 100644 --- a/src/generation/excitation_generation.cc +++ b/src/generation/excitation_generation.cc @@ -28,8 +28,7 @@ ExcitationGeneration::ExcitationGeneration( random_generation_(random_generation), is_valid_(true), phase_(1.0) { - if (NULL == input_source_ || NULL == random_generation_ || - !input_source_->IsValid()) { + if (NULL == input_source_ || !input_source_->IsValid()) { is_valid_ = false; return; } @@ -52,8 +51,8 @@ bool ExcitationGeneration::Get(double* excitation, double* pulse, double* noise, } // Get noise. - double noise_in_current_point; - if (!random_generation_->Get(&noise_in_current_point)) { + double noise_in_current_point(0.0); + if (random_generation_ && !random_generation_->Get(&noise_in_current_point)) { return false; } diff --git a/src/main/excite.cc b/src/main/excite.cc index dfd94d51..7aa7940c 100644 --- a/src/main/excite.cc +++ b/src/main/excite.cc @@ -30,9 +30,11 @@ namespace { +enum UnvoicedType { kZero = 0, kGaussian, kMSequence, kNumUnvoicedTypes }; + const int kDefaultFramePeriod(100); const int kDefaultInterpolationPeriod(1); -const bool kDefaultFlagToUseNormalDistributedRandomValue(false); +const UnvoicedType kDefaultUnvoicedType(UnvoicedType::kMSequence); const int kDefaultSeed(1); const double kMagicNumberForUnvoicedFrame(0.0); @@ -44,18 +46,21 @@ void PrintUsage(std::ostream* stream) { *stream << " usage:" << std::endl; *stream << " excite [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; - *stream << " -p p : frame period ( int)[" << std::setw(5) << std::right << kDefaultFramePeriod << "][ 1 <= p <= ]" << std::endl; // NOLINT - *stream << " -i i : interpolation period ( int)[" << std::setw(5) << std::right << kDefaultInterpolationPeriod << "][ 0 <= i <= p/2 ]" << std::endl; // NOLINT - *stream << " -n : use gauss noise for unvoiced frame ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(kDefaultFlagToUseNormalDistributedRandomValue) << "]" << std::endl; // NOLINT - *stream << " default is M-sequence" << std::endl; - *stream << " -s s : seed for random generation ( int)[" << std::setw(5) << std::right << kDefaultSeed << "][ <= s <= ]" << std::endl; // NOLINT + *stream << " -p p : frame period ( int)[" << std::setw(5) << std::right << kDefaultFramePeriod << "][ 1 <= p <= ]" << std::endl; // NOLINT + *stream << " -i i : interpolation period ( int)[" << std::setw(5) << std::right << kDefaultInterpolationPeriod << "][ 0 <= i <= p/2 ]" << std::endl; // NOLINT + *stream << " -n n : unvoiced type ( int)[" << std::setw(5) << std::right << kDefaultUnvoicedType << "][ 0 <= n <= 2 ]" << std::endl; // NOLINT + *stream << " 0 (zero)" << std::endl; + *stream << " 1 (Gaussian noise)" << std::endl; + *stream << " 2 (M-sequence)" << std::endl; + *stream << " -s s : seed for random generation ( int)[" << std::setw(5) << std::right << kDefaultSeed << "][ <= s <= ]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; - *stream << " pitch period (double)[stdin]" << std::endl; // NOLINT + *stream << " pitch period (double)[stdin]" << std::endl; // NOLINT *stream << " stdout:" << std::endl; - *stream << " excitation (double)" << std::endl; // NOLINT + *stream << " excitation (double)" << std::endl; *stream << " notice:" << std::endl; *stream << " if i = 0, don't interpolate pitch" << std::endl; + *stream << " s is valid only if n = 1" << std::endl; *stream << " magic number for unvoiced frame is " << kMagicNumberForUnvoicedFrame << std::endl; // NOLINT *stream << std::endl; *stream << " SPTK: version " << sptk::kVersion << std::endl; @@ -72,8 +77,11 @@ void PrintUsage(std::ostream* stream) { * - frame_period @f$(1 \le P)@f$ * - @b -i @e int * - interpolation period @f$(0 \le I \le P/2)@f$ - * - @b -n - * - use gaussian noise instead of M-sequence for unvoiced frame + * - @b -n @e int + * - unvoiced type + * \arg @c 0 zero + * \arg @c 1 Gaussian noise + * \arg @c 2 M-sequence * - @b -s @e int * - seed for random number generation * - @b infile @e str @@ -102,12 +110,11 @@ void PrintUsage(std::ostream* stream) { int main(int argc, char* argv[]) { int frame_period(kDefaultFramePeriod); int interpolation_period(kDefaultInterpolationPeriod); - bool use_normal_distributed_random_value( - kDefaultFlagToUseNormalDistributedRandomValue); + UnvoicedType unvoiced_type(kDefaultUnvoicedType); int seed(kDefaultSeed); for (;;) { - const int option_char(getopt_long(argc, argv, "p:i:ns:h", NULL, NULL)); + const int option_char(getopt_long(argc, argv, "p:i:n:s:h", NULL, NULL)); if (-1 == option_char) break; switch (option_char) { @@ -134,7 +141,18 @@ int main(int argc, char* argv[]) { break; } case 'n': { - use_normal_distributed_random_value = true; + const int min(0); + const int max(static_cast(kNumUnvoicedTypes) - 1); + int tmp; + if (!sptk::ConvertStringToInteger(optarg, &tmp) || + !sptk::IsInRange(tmp, min, max)) { + std::ostringstream error_message; + error_message << "The argument for the -n option must be an integer " + << "in the range of " << min << " to " << max; + sptk::PrintErrorMessage("excite", error_message); + return 1; + } + unvoiced_type = static_cast(tmp); break; } case 's': { @@ -202,11 +220,22 @@ int main(int argc, char* argv[]) { // Run excitation generation. sptk::RandomGenerationInterface* random_generation(NULL); try { - if (use_normal_distributed_random_value) { - random_generation = - new sptk::NormalDistributedRandomValueGeneration(seed); - } else { - random_generation = new sptk::MSequenceGeneration(); + switch (unvoiced_type) { + case kZero: { + break; + } + case kGaussian: { + random_generation = + new sptk::NormalDistributedRandomValueGeneration(seed); + break; + } + case kMSequence: { + random_generation = new sptk::MSequenceGeneration(); + break; + } + default: { + return 1; + } } sptk::ExcitationGeneration excitation_generation( &input_source_interpolation_with_magic_number, random_generation); diff --git a/test/test_excite.bats b/test/test_excite.bats index 3148261c..dc4570b5 100755 --- a/test/test_excite.bats +++ b/test/test_excite.bats @@ -37,7 +37,7 @@ teardown() { [ "$status" -eq 0 ] $sptk3/excite -n -p 4 $tmp/1 > $tmp/2 - $sptk4/excite -n -p 4 $tmp/1 > $tmp/3 + $sptk4/excite -n 1 -p 4 $tmp/1 > $tmp/3 run $sptk4/aeq -L $tmp/2 $tmp/3 [ "$status" -eq 0 ] } diff --git a/tools/Makefile b/tools/Makefile index 7161b503..99e812df 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -15,7 +15,7 @@ # ------------------------------------------------------------------------ # PYTHON_VERSION := 3.8 -SHELLCHECK_VERSION := 0.8.0 +SHELLCHECK_VERSION := 0.9.0 JOBS := 4 From f040af6b6a9faa46a4bac6b8a89857a365ece8b0 Mon Sep 17 00:00:00 2001 From: takenori-y Date: Thu, 12 Jan 2023 20:11:42 +0900 Subject: [PATCH 2/2] rename variable [skip ci] --- src/main/excite.cc | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/excite.cc b/src/main/excite.cc index 7aa7940c..15239fd6 100644 --- a/src/main/excite.cc +++ b/src/main/excite.cc @@ -30,11 +30,11 @@ namespace { -enum UnvoicedType { kZero = 0, kGaussian, kMSequence, kNumUnvoicedTypes }; +enum NoiseType { kZero = 0, kGaussian, kMSequence, kNumNoiseTypes }; const int kDefaultFramePeriod(100); const int kDefaultInterpolationPeriod(1); -const UnvoicedType kDefaultUnvoicedType(UnvoicedType::kMSequence); +const NoiseType kDefaultNoiseType(NoiseType::kMSequence); const int kDefaultSeed(1); const double kMagicNumberForUnvoicedFrame(0.0); @@ -48,9 +48,9 @@ void PrintUsage(std::ostream* stream) { *stream << " options:" << std::endl; *stream << " -p p : frame period ( int)[" << std::setw(5) << std::right << kDefaultFramePeriod << "][ 1 <= p <= ]" << std::endl; // NOLINT *stream << " -i i : interpolation period ( int)[" << std::setw(5) << std::right << kDefaultInterpolationPeriod << "][ 0 <= i <= p/2 ]" << std::endl; // NOLINT - *stream << " -n n : unvoiced type ( int)[" << std::setw(5) << std::right << kDefaultUnvoicedType << "][ 0 <= n <= 2 ]" << std::endl; // NOLINT - *stream << " 0 (zero)" << std::endl; - *stream << " 1 (Gaussian noise)" << std::endl; + *stream << " -n n : noise type ( int)[" << std::setw(5) << std::right << kDefaultNoiseType << "][ 0 <= n <= 2 ]" << std::endl; // NOLINT + *stream << " 0 (none)" << std::endl; + *stream << " 1 (Gaussian)" << std::endl; *stream << " 2 (M-sequence)" << std::endl; *stream << " -s s : seed for random generation ( int)[" << std::setw(5) << std::right << kDefaultSeed << "][ <= s <= ]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; @@ -78,9 +78,9 @@ void PrintUsage(std::ostream* stream) { * - @b -i @e int * - interpolation period @f$(0 \le I \le P/2)@f$ * - @b -n @e int - * - unvoiced type - * \arg @c 0 zero - * \arg @c 1 Gaussian noise + * - noise type + * \arg @c 0 none + * \arg @c 1 Gaussian * \arg @c 2 M-sequence * - @b -s @e int * - seed for random number generation @@ -110,7 +110,7 @@ void PrintUsage(std::ostream* stream) { int main(int argc, char* argv[]) { int frame_period(kDefaultFramePeriod); int interpolation_period(kDefaultInterpolationPeriod); - UnvoicedType unvoiced_type(kDefaultUnvoicedType); + NoiseType noise_type(kDefaultNoiseType); int seed(kDefaultSeed); for (;;) { @@ -142,7 +142,7 @@ int main(int argc, char* argv[]) { } case 'n': { const int min(0); - const int max(static_cast(kNumUnvoicedTypes) - 1); + const int max(static_cast(kNumNoiseTypes) - 1); int tmp; if (!sptk::ConvertStringToInteger(optarg, &tmp) || !sptk::IsInRange(tmp, min, max)) { @@ -152,7 +152,7 @@ int main(int argc, char* argv[]) { sptk::PrintErrorMessage("excite", error_message); return 1; } - unvoiced_type = static_cast(tmp); + noise_type = static_cast(tmp); break; } case 's': { @@ -220,7 +220,7 @@ int main(int argc, char* argv[]) { // Run excitation generation. sptk::RandomGenerationInterface* random_generation(NULL); try { - switch (unvoiced_type) { + switch (noise_type) { case kZero: { break; }