| 72 | } |
| 73 | |
| 74 | std::vector<float> trim_mono_librosa_effects( |
| 75 | const std::vector<float> & mono_samples, |
| 76 | const LibrosaEffectsTrimOptions & options) { |
| 77 | if (options.frame_length <= 0 || options.hop_length <= 0) { |
| 78 | throw std::runtime_error("librosa effects trim frame and hop lengths must be positive"); |
| 79 | } |
| 80 | if (!std::isfinite(options.top_db) || options.top_db < 0.0F) { |
| 81 | throw std::runtime_error("librosa effects trim top_db must be finite and non-negative"); |
| 82 | } |
| 83 | if (!std::isfinite(options.amin) || options.amin <= 0.0F) { |
| 84 | throw std::runtime_error("librosa effects trim amin must be finite and positive"); |
| 85 | } |
| 86 | if (mono_samples.empty()) { |
| 87 | return mono_samples; |
| 88 | } |
| 89 | |
| 90 | const int64_t sample_count = static_cast<int64_t>(mono_samples.size()); |
| 91 | const int64_t frames = 1 + sample_count / options.hop_length; |
| 92 | std::vector<float> rms(static_cast<size_t>(frames), 0.0F); |
| 93 | float ref = options.amin; |
| 94 | for (int64_t frame = 0; frame < frames; ++frame) { |
| 95 | const int64_t start = frame * options.hop_length - options.frame_length / 2; |
| 96 | double power = 0.0; |
| 97 | for (int64_t i = 0; i < options.frame_length; ++i) { |
| 98 | const int64_t sample_index = start + i; |
| 99 | const float sample = |
| 100 | sample_index >= 0 && sample_index < sample_count |
| 101 | ? mono_samples[static_cast<size_t>(sample_index)] |
| 102 | : 0.0F; |
| 103 | power += static_cast<double>(sample) * static_cast<double>(sample); |
| 104 | } |
| 105 | const float value = std::sqrt(static_cast<float>(power / static_cast<double>(options.frame_length))); |
no test coverage detected