| 425 | } |
| 426 | |
| 427 | std::vector<float> make_sine_source( |
| 428 | const HiftVocoderWeights & weights, |
| 429 | const std::vector<float> & f0_upsampled, |
| 430 | uint64_t seed, |
| 431 | uint64_t prior_noise_values, |
| 432 | const std::vector<float> * source_random_values) { |
| 433 | constexpr float kPi = 3.14159265358979323846F; |
| 434 | constexpr float kTwoPi = 2.0F * kPi; |
| 435 | const int64_t frames = static_cast<int64_t>(f0_upsampled.size()); |
| 436 | const int64_t harmonics = weights.config.nb_harmonics + 1; |
| 437 | const size_t phase_count = static_cast<size_t>(harmonics); |
| 438 | const size_t gaussian_count = static_cast<size_t>(harmonics * frames); |
| 439 | if (source_random_values != nullptr && |
| 440 | source_random_values->size() != phase_count + gaussian_count) { |
| 441 | throw std::runtime_error("HiFT source random value count mismatch"); |
| 442 | } |
| 443 | std::vector<float> phase_uniform = source_random_values == nullptr |
| 444 | ? sampling::generate_torch_cuda_uniform(phase_count, seed, prior_noise_values) |
| 445 | : std::vector<float>(source_random_values->begin(), source_random_values->begin() + static_cast<std::ptrdiff_t>(phase_count)); |
| 446 | std::vector<float> phase(static_cast<size_t>(harmonics), 0.0F); |
| 447 | for (int64_t h = 0; h < harmonics; ++h) { |
| 448 | phase[static_cast<size_t>(h)] = phase_uniform[static_cast<size_t>(h)] * kTwoPi - kPi; |
| 449 | } |
| 450 | phase[0] = 0.0F; |
| 451 | const uint64_t gaussian_offset = prior_noise_values + static_cast<uint64_t>(phase_uniform.size()); |
| 452 | std::vector<float> gaussian = source_random_values == nullptr |
| 453 | ? sampling::generate_torch_cuda_randn( |
| 454 | gaussian_count, |
| 455 | seed, |
| 456 | sampling::TorchRandnPrecision::Float32, |
| 457 | gaussian_offset) |
| 458 | : std::vector<float>(source_random_values->begin() + static_cast<std::ptrdiff_t>(phase_count), source_random_values->end()); |
| 459 | |
| 460 | std::vector<float> source(static_cast<size_t>(frames), 0.0F); |
| 461 | std::vector<float> frac(static_cast<size_t>(harmonics), 0.0F); |
| 462 | for (int64_t t = 0; t < frames; ++t) { |
| 463 | const float base_f0 = f0_upsampled[static_cast<size_t>(t)]; |
| 464 | const float uv = base_f0 > weights.config.nsf_voiced_threshold ? 1.0F : 0.0F; |
| 465 | float sum = weights.source_linear.bias[0]; |
| 466 | for (int64_t h = 0; h < harmonics; ++h) { |
| 467 | frac[static_cast<size_t>(h)] += |
| 468 | base_f0 * static_cast<float>(h + 1) / static_cast<float>(weights.config.sampling_rate); |
| 469 | frac[static_cast<size_t>(h)] -= std::floor(frac[static_cast<size_t>(h)]); |
| 470 | const float theta = kTwoPi * frac[static_cast<size_t>(h)]; |
| 471 | float wave = |
| 472 | weights.config.nsf_alpha * std::sin(theta + phase[static_cast<size_t>(h)]); |
| 473 | const float noise_amp = |
| 474 | uv * weights.config.nsf_sigma + (1.0F - uv) * weights.config.nsf_alpha / 3.0F; |
| 475 | wave = wave * uv + noise_amp * gaussian[static_cast<size_t>(h * frames + t)]; |
| 476 | sum += wave * weights.source_linear.weight[static_cast<size_t>(h)]; |
| 477 | } |
| 478 | source[static_cast<size_t>(t)] = std::tanh(sum); |
| 479 | } |
| 480 | return source; |
| 481 | } |
| 482 | |
| 483 | std::vector<float> source_stft_bct( |
| 484 | const HiftVocoderWeights & weights, |
no test coverage detected