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

Add pitch_spec #34

Merged
merged 11 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add stonemask
  • Loading branch information
takenori-y committed May 9, 2023
commit a5acf1620ba68ee9947fe508e481d9fa236d14d5
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ set(CC_SOURCES
${THIRD_PARTY_DIR}/WORLD/dio.cc
${THIRD_PARTY_DIR}/WORLD/fft_world.cc
${THIRD_PARTY_DIR}/WORLD/matlabfunctions.cc
${THIRD_PARTY_DIR}/WORLD/stonemask.cc
${SOURCE_DIR}/analysis/adaptive_generalized_cepstral_analysis.cc
${SOURCE_DIR}/analysis/adaptive_mel_cepstral_analysis.cc
${SOURCE_DIR}/analysis/adaptive_mel_generalized_cepstral_analysis.cc
Expand Down
5 changes: 1 addition & 4 deletions include/SPTK/analysis/spectrum_extraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ class SpectrumExtraction {
* @return True on success, false on failure.
*/
bool Run(const std::vector<double>& waveform, const std::vector<double>& f0,
std::vector<std::vector<double> >* spectrum) const {
return (NULL != spectrum_extraction_ &&
spectrum_extraction_->Run(waveform, f0, spectrum));
}
std::vector<std::vector<double> >* spectrum) const;

private:
SpectrumExtractionInterface* spectrum_extraction_;
Expand Down
2 changes: 1 addition & 1 deletion include/SPTK/analysis/spectrum_extraction_by_world.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SpectrumExtractionByWorld : public SpectrumExtractionInterface {
/**
* @return Frame shift.
*/
int GetFrameShift() const {
virtual int GetFrameShift() const {
return frame_shift_;
}

Expand Down
5 changes: 5 additions & 0 deletions include/SPTK/analysis/spectrum_extraction_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class SpectrumExtractionInterface {
virtual ~SpectrumExtractionInterface() {
}

/**
* @return Frame shift.
*/
virtual int GetFrameShift() const = 0;

/**
* @return True if this object is valid.
*/
Expand Down
29 changes: 29 additions & 0 deletions src/analysis/spectrum_extraction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include "SPTK/analysis/spectrum_extraction.h"

#include <algorithm> // std::copy, std::fill, std::min
#include <cmath> // std::ceil

#include "SPTK/analysis/spectrum_extraction_by_world.h"

namespace sptk {
Expand All @@ -36,4 +39,30 @@ SpectrumExtraction::SpectrumExtraction(
}
}

bool SpectrumExtraction::Run(
const std::vector<double>& waveform, const std::vector<double>& f0,
std::vector<std::vector<double> >* spectrum) const {
if (!IsValid()) {
return false;
}

const int target_f0_length(
static_cast<int>(std::ceil(static_cast<double>(waveform.size()) /
spectrum_extraction_->GetFrameShift())));
const int given_f0_length(static_cast<int>(f0.size()));
if (target_f0_length == given_f0_length) {
return spectrum_extraction_->Run(waveform, f0, spectrum);
}

std::vector<double> length_fixed_f0(target_f0_length);
std::copy(f0.begin(),
f0.begin() + std::min(target_f0_length, given_f0_length),
length_fixed_f0.begin());
if (1 <= given_f0_length && given_f0_length < target_f0_length) {
std::fill(length_fixed_f0.begin() + given_f0_length, length_fixed_f0.end(),
f0.back());
}
return spectrum_extraction_->Run(waveform, length_fixed_f0, spectrum);
}

} // namespace sptk
14 changes: 12 additions & 2 deletions src/analysis/spectrum_extraction_by_world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

#include "SPTK/analysis/spectrum_extraction_by_world.h"

#include <cstddef> // std::size_t
#include <algorithm> // std::min_element
#include <cstddef> // std::size_t

#include "WORLD/world/cheaptrick.h"
#include "WORLD/world/stonemask.h"

namespace sptk {

Expand Down Expand Up @@ -47,6 +49,7 @@ bool SpectrumExtractionByWorld::Run(
world::CheapTrickOption option;
world::InitializeCheapTrickOption(sampling_rate_, &option);
option.fft_size = fft_length_;
option.f0_floor = *std::min_element(f0.begin(), f0.end());

const int f0_length(f0.size());
const double frame_shift_in_sec(frame_shift_ / sampling_rate_);
Expand All @@ -66,15 +69,22 @@ bool SpectrumExtractionByWorld::Run(
}
}

// Modify F0 for pitch-adaptive spectrum estimation.
std::vector<double> modified_f0(f0_length);
world::StoneMask(waveform.data(), static_cast<int>(waveform.size()),
static_cast<int>(sampling_rate_), time_axis.data(),
f0.data(), f0_length, modified_f0.data());

std::vector<double*> pointers;
pointers.reserve(f0_length);
for (std::vector<double>& vector : *spectrum) {
pointers.push_back(vector.data());
}

// Estimate spectrum.
world::CheapTrick(waveform.data(), static_cast<int>(waveform.size()),
static_cast<int>(sampling_rate_), time_axis.data(),
f0.data(), f0_length, &option, pointers.data());
modified_f0.data(), f0_length, &option, pointers.data());

return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/sp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void PrintUsage(std::ostream* stream) {
*stream << " pitch (double)" << std::endl;
*stream << " stdout:" << std::endl;
*stream << " spectrum (double)" << std::endl;
*stream << " notice:" << std::endl;
*stream << " magic number representing unvoiced symbol is 0 (q = 0, 1) or -1e+10 (q = 2)" << std::endl; // NOLINT
*stream << std::endl;
*stream << " SPTK: version " << sptk::kVersion << std::endl;
*stream << std::endl;
Expand Down Expand Up @@ -158,7 +160,7 @@ int main(int argc, char* argv[]) {
}
case 'p': {
if (!sptk::ConvertStringToInteger(optarg, &frame_shift) ||
frame_shift <= 0.0) {
frame_shift <= 0) {
std::ostringstream error_message;
error_message
<< "The argument for the -p option must be a positive integer";
Expand Down
36 changes: 36 additions & 0 deletions test/test_sp.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bats
# ------------------------------------------------------------------------ #
# 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. #
# ------------------------------------------------------------------------ #

sptk3=tools/sptk/bin
sptk4=bin
tmp=test_sp
data=asset/data.short

setup() {
mkdir -p $tmp
}

teardown() {
rm -rf $tmp
}

@test "sp: valgrind" {
$sptk3/x2x +sd $data > $tmp/0
$sptk4/pitch $tmp/0 > $tmp/1
run valgrind $sptk4/sp "$a" -l 12 $tmp/1 $tmp/0
[ "$(echo "${lines[-1]}" | sed -r 's/.*SUMMARY: ([0-9]*) .*/\1/')" -eq 0 ]
}
Loading