Skip to content

Commit

Permalink
Merge pull request #46 from sp-nitech/pitch2sin
Browse files Browse the repository at this point in the history
Add pitch2sin
  • Loading branch information
takenori-y committed Sep 28, 2023
2 parents 3b32c62 + a63fa55 commit e245e05
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ set(CC_SOURCES
${SOURCE_DIR}/generation/nonrecursive_maximum_likelihood_parameter_generation.cc
${SOURCE_DIR}/generation/normal_distributed_random_value_generation.cc
${SOURCE_DIR}/generation/recursive_maximum_likelihood_parameter_generation.cc
${SOURCE_DIR}/generation/sinusoidal_generation.cc
${SOURCE_DIR}/input/input_source_delay.cc
${SOURCE_DIR}/input/input_source_filling_magic_number.cc
${SOURCE_DIR}/input/input_source_from_array.cc
Expand Down Expand Up @@ -352,6 +353,7 @@ set(MAIN_SOURCES
${SOURCE_DIR}/main/pcas.cc
${SOURCE_DIR}/main/phase.cc
${SOURCE_DIR}/main/pitch.cc
${SOURCE_DIR}/main/pitch2sin.cc
${SOURCE_DIR}/main/pitch_mark.cc
${SOURCE_DIR}/main/pitch_spec.cc
${SOURCE_DIR}/main/poledf.cc
Expand Down
11 changes: 11 additions & 0 deletions doc/main/pitch2sin.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. _pitch2sin:

pitch2sin
=========

.. doxygenfile:: pitch2sin.cc

.. seealso:: :ref:`pitch` :ref:`excite`

.. doxygenclass:: sptk::SinusoidalGeneration
:members:
73 changes: 73 additions & 0 deletions include/SPTK/generation/sinusoidal_generation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// ------------------------------------------------------------------------ //
// Copyright 2021 SPTK Working Group //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ------------------------------------------------------------------------ //

#ifndef SPTK_GENERATION_SINUSOIDAL_GENERATION_H_
#define SPTK_GENERATION_SINUSOIDAL_GENERATION_H_

#include "SPTK/input/input_source_interpolation_with_magic_number.h"
#include "SPTK/utils/sptk_utils.h"

namespace sptk {

/**
* Generate sinusoidal sequence.
*
* The input is a sequence of pitch value which can be either a continuous value
* or a magic number. The output is the sinusoidal signal given the input
* sequence.
*/
class SinusoidalGeneration {
public:
/**
* @param[in] input_source Input source.
*/
explicit SinusoidalGeneration(
InputSourceInterpolationWithMagicNumber* input_source);

virtual ~SinusoidalGeneration() {
}

/**
* @return True if this object is valid.
*/
bool IsValid() const {
return is_valid_;
}

/**
* Get sinusoidal signal.
*
* @param[out] sin Sine waveform (optional).
* @param[out] cos Cosine waveform (optional).
* @param[out] pitch Pitch (optional).
* @return True on success, false on failure.
*/
bool Get(double* sin, double* cos, double* pitch);

private:
InputSourceInterpolationWithMagicNumber* input_source_;

bool is_valid_;

// Phase value ranging from 0.0 to 2 x pi
double phase_;

DISALLOW_COPY_AND_ASSIGN(SinusoidalGeneration);
};

} // namespace sptk

#endif // SPTK_GENERATION_SINUSOIDAL_GENERATION_H_
80 changes: 80 additions & 0 deletions src/generation/sinusoidal_generation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ------------------------------------------------------------------------ //
// Copyright 2021 SPTK Working Group //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ------------------------------------------------------------------------ //

#include "SPTK/generation/sinusoidal_generation.h"

#include <cmath> // std::cos, std::sin
#include <vector> // std::vector

namespace sptk {

SinusoidalGeneration::SinusoidalGeneration(
InputSourceInterpolationWithMagicNumber* input_source)
: input_source_(input_source), is_valid_(true), phase_(0.0) {
if (NULL == input_source_ || !input_source_->IsValid()) {
is_valid_ = false;
return;
}
}

bool SinusoidalGeneration::Get(double* sin, double* cos, double* pitch) {
if (!is_valid_) {
return false;
}

// Get pitch.
double pitch_in_current_point;
{
std::vector<double> tmp;
if (!input_source_->Get(&tmp) || tmp[0] < 0.0) {
return false;
}
pitch_in_current_point = tmp[0];
}

if (pitch) {
*pitch = pitch_in_current_point;
}

// If unvoiced point, return zero.
if (input_source_->GetMagicNumber() == pitch_in_current_point) {
phase_ = 0.0;
if (sin) {
*sin = 0.0;
}
if (cos) {
*cos = 0.0;
}
return true;
}

if (sin) {
*sin = std::sin(phase_);
}
if (cos) {
*cos = std::cos(phase_);
}

// Proceed phase.
phase_ += sptk::kTwoPi / pitch_in_current_point;
if (sptk::kTwoPi < phase_) {
phase_ -= sptk::kTwoPi;
}

return true;
}

} // namespace sptk
2 changes: 1 addition & 1 deletion src/main/gmm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License. //
// ------------------------------------------------------------------------ //

#include <fstream> // std::ifstream
#include <fstream> // std::ifstream, std::ofstream
#include <iomanip> // std::setw
#include <iostream> // std::cerr, std::cin, std::cout, std::endl, etc.
#include <sstream> // std::ostringstream
Expand Down
Loading

0 comments on commit e245e05

Please sign in to comment.