-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b32c62
commit 3ce257c
Showing
7 changed files
with
453 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.