| 66 | double variance = 0.0; |
| 67 | for (int64_t frame = 0; frame < frames; ++frame) { |
| 68 | double mono = 0.0; |
| 69 | for (int ch = 0; ch < audio.channels; ++ch) { |
| 70 | mono += static_cast<double>(audio.samples[static_cast<size_t>(frame * audio.channels + ch)]); |
| 71 | } |
| 72 | mono /= static_cast<double>(audio.channels); |
| 73 | const double delta = mono - mean; |
| 74 | variance += delta * delta; |
| 75 | } |
| 76 | const double denom = frames > 1 ? static_cast<double>(frames - 1) : 1.0; |
| 77 | const float std = static_cast<float>(std::sqrt(variance / denom) + 1.0e-8); |
| 78 | const float mean_f32 = static_cast<float>(mean); |
| 79 | #ifdef _OPENMP |
| 80 | #pragma omp parallel for if(audio.samples.size() >= 1 << 16) |
| 81 | #endif |
| 82 | for (int64_t i = 0; i < static_cast<int64_t>(audio.samples.size()); ++i) { |
| 83 | audio.samples[static_cast<size_t>(i)] = (audio.samples[static_cast<size_t>(i)] - mean_f32) / std; |
| 84 | } |
| 85 | return {mean_f32, std}; |
| 86 | } |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | HTDemucsSession::HTDemucsSession( |
| 91 | runtime::TaskSpec task, |
| 92 | runtime::SessionOptions options, |
| 93 | std::shared_ptr<const HTDemucsAssets> assets, |
| 94 | std::shared_ptr<const engine::model_spec::ModelContract> contract) |
| 95 | : RuntimeSessionBase(runtime::apply_option_v1_compatibility( |
| 96 | std::move(options), |
| 97 | {{"weight_type", "htdemucs.weight_type"}}, |
| 98 | "HTDemucs")), |
| 99 | task_(std::move(task)), |
| 100 | assets_(require_assets(std::move(assets))), |
| 101 | contract_(require_contract(std::move(contract))) { |
| 102 | runtime::validate_spec_backed_session_options(RuntimeSessionBase::options(), *contract_, kFamily, "HTDemucs"); |
| 103 | if (task_.task != runtime::VoiceTaskKind::SourceSeparation) { |
| 104 | throw std::runtime_error("HTDemucs models only support --task sep"); |
| 105 | } |
| 106 | if (task_.mode != runtime::RunMode::Offline) { |
| 107 | throw std::runtime_error("HTDemucs models only support offline mode"); |