<commit_before>﻿#include "UnitWaveformMaker.h"

using namespace std;

struct UnitWaveformMaker::Parameters UnitWaveformMaker::params;

bool UnitWaveformMaker::makeUnitWaveform(const vector<double>& voice, bool is_vcv)
{
  return makeUnitWaveform(voice, -1, is_vcv);
}

bool UnitWaveformMaker::makeUnitWaveform(const vector<double>& voice, short pitch, bool is_vcv)
{
  this->voice = voice;
  if (voice.size()==0 || pitchmarks.empty() || params.num_lobes==0) {
    cerr << "[UnitWaveformMaker] voice, pitch mark or lobe is null" << endl;
    return false;
  }
  if (sub_ovrl > sub_fade_start) {
    cerr << "[UnitWaveformMaker] invalid ovrl and rep_start" << endl;
    return false;
  }
  double fade_start_rms = makeUnitWaveform(sub_fade_start, pitch).data.getRMS();

  // make unit waveforms
  {
    double fade_scale=1.0, ovrl_scale=1.0;
    vector<double> base_wav;
    if (params.normalize) {
      fade_scale = params.target_rms/fade_start_rms;
      ovrl_scale = is_vcv?((sub_ovrl>0)?params.target_rms/makeUnitWaveform(0, pitch).data.getRMS():1):fade_scale;
    }
    for (size_t i=0; i<pitchmarks.size()&&i<sub_fade_end; i++) {
      double scale = 1.0;
      // adjust uwc volume
      if (params.normalize) {
        if (is_vcv) {
          if (i > sub_ovrl && i < sub_fade_start) {
            double tmp = (i-sub_ovrl) / (double)(sub_fade_start-sub_ovrl);
            scale = (ovrl_scale*(1.0-tmp)) + (fade_scale*tmp);
          } else {
            scale = params.target_rms/makeUnitWaveform(i, pitch).data.getRMS();
          }
        } else {
          if (sub_ovrl==0 || i>=sub_fade_start) {
            scale = fade_scale;
          } else if (i <= sub_ovrl) {
            scale = ovrl_scale;
          } else {
            double tmp = (i-sub_ovrl) / (double)(sub_fade_start-sub_ovrl);
            scale = (ovrl_scale*(1.0-tmp)) + (fade_scale*tmp);
          }
        }
      }
      // make unit waveform
      uwc->unit_waveforms.push_back(makeUnitWaveform(i, pitch, scale));
      if (i > sub_fade_start+params.min_repeat_length) {
        if (base_wav.size() == 0) {
          base_wav = uwc->unit_waveforms[sub_fade_start].data.getData();
        }
        vector<double> target_wav = uwc->unit_waveforms.back().data.getData();
      }
    }
  }<commit_msg>scale bugfix at unit waveform maker<commit_after>﻿#include "UnitWaveformMaker.h"

using namespace std;

struct UnitWaveformMaker::Parameters UnitWaveformMaker::params;

bool UnitWaveformMaker::makeUnitWaveform(const vector<double>& voice, bool is_vcv)
{
  return makeUnitWaveform(voice, -1, is_vcv);
}

bool UnitWaveformMaker::makeUnitWaveform(const vector<double>& voice, short pitch, bool is_vcv)
{
  this->voice = voice;
  if (voice.size()==0 || pitchmarks.empty() || params.num_lobes==0) {
    cerr << "[UnitWaveformMaker] voice, pitch mark or lobe is null" << endl;
    return false;
  }
  if (sub_ovrl > sub_fade_start) {
    cerr << "[UnitWaveformMaker] invalid ovrl and rep_start" << endl;
    return false;
  }
  double fade_start_rms = makeUnitWaveform(sub_fade_start, pitch).data.getRMS();

  // make unit waveforms
  {
    double fade_scale=1.0, ovrl_scale=1.0;
    vector<double> base_wav;
    if (params.normalize) {
      fade_scale = params.target_rms/fade_start_rms;
      ovrl_scale = is_vcv?((sub_ovrl>0)?params.target_rms/makeUnitWaveform(0, pitch).data.getRMS():1):fade_scale;
    }
    for (size_t i=0; i<pitchmarks.size()&&i<sub_fade_end; i++) {
      double scale = 1.0;
      // adjust uwc volume
      if (params.normalize) {
        if (is_vcv) {
          if (i > sub_ovrl && i < sub_fade_start) {
            double tmp = (i-sub_ovrl) / (double)(sub_fade_start-sub_ovrl);
            scale = (ovrl_scale*(1.0-tmp)) + (fade_scale*tmp);
          } else if (i <= sub_ovrl) {
            scale = ovrl_scale;
          } else {
            scale = fade_scale;
          }
        } else {
          if (sub_ovrl==0 || i>=sub_fade_start) {
            scale = fade_scale;
          } else if (i <= sub_ovrl) {
            scale = ovrl_scale;
          } else {
            double tmp = (i-sub_ovrl) / (double)(sub_fade_start-sub_ovrl);
            scale = (ovrl_scale*(1.0-tmp)) + (fade_scale*tmp);
          }
        }
      }
      // make unit waveform
      uwc->unit_waveforms.push_back(makeUnitWaveform(i, pitch, scale));
      if (i > sub_fade_start+params.min_repeat_length) {
        if (base_wav.size() == 0) {
          base_wav = uwc->unit_waveforms[sub_fade_start].data.getData();
        }
        vector<double> target_wav = uwc->unit_waveforms.back().data.getData();
      }
    }
  }
{'fraction_non_alphanumeric': 0.13612679114198872, 'fraction_numerical': 0.008250108554059922, 'mean_word_length': 2.7485760781122863, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '632966', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1642, 'n_words': 296}