| 29 | } // namespace |
| 30 | |
| 31 | int main(int argc, char ** argv) { |
| 32 | if (argc != 7) { |
| 33 | std::cerr << "Usage: stft_bench <wav> <kind:stft> <iters> <n_fft> <hop_length> <win_length>\n"; |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | const std::filesystem::path wav_path(argv[1]); |
| 38 | const std::string kind(argv[2]); |
| 39 | const int iters = std::stoi(argv[3]); |
| 40 | const int64_t n_fft = std::stoll(argv[4]); |
| 41 | const int64_t hop_length = std::stoll(argv[5]); |
| 42 | const int64_t win_length = std::stoll(argv[6]); |
| 43 | |
| 44 | auto wav = engine::audio::read_wav_f32(wav_path); |
| 45 | if (wav.channels != 1) { |
| 46 | throw std::runtime_error("stft_bench requires mono WAV"); |
| 47 | } |
| 48 | if (iters <= 0) { |
| 49 | throw std::runtime_error("iters must be > 0"); |
| 50 | } |
| 51 | |
| 52 | const engine::audio::STFTConfig config{ |
| 53 | n_fft, |
| 54 | hop_length, |
| 55 | win_length, |
| 56 | true, |
| 57 | engine::audio::STFTPadMode::Constant, |
| 58 | }; |
| 59 | const auto window = make_hann_window(win_length); |
| 60 | |
| 61 | double total_ms = 0.0; |
| 62 | size_t output_size = 0; |
| 63 | for (int i = 0; i < iters; ++i) { |
| 64 | const auto started = std::chrono::steady_clock::now(); |
| 65 | engine::audio::AudioTensor tensor; |
| 66 | if (kind == "stft") { |
| 67 | tensor = engine::audio::STFT().compute_complex( |
| 68 | wav.samples, window, 1, static_cast<int64_t>(wav.samples.size()), config); |
| 69 | } else { |
| 70 | throw std::runtime_error("kind must be 'stft'"); |
| 71 | } |
| 72 | const auto ended = std::chrono::steady_clock::now(); |
| 73 | total_ms += std::chrono::duration<double, std::milli>(ended - started).count(); |
| 74 | output_size = tensor.values.size(); |
| 75 | } |
| 76 | |
| 77 | std::cout << "kind=" << kind << "\n"; |
| 78 | std::cout << "iters=" << iters << "\n"; |
| 79 | std::cout << "n_fft=" << n_fft << "\n"; |
| 80 | std::cout << "hop_length=" << hop_length << "\n"; |
| 81 | std::cout << "win_length=" << win_length << "\n"; |
| 82 | std::cout << "output_size=" << output_size << "\n"; |
| 83 | std::cout << "total_ms=" << total_ms << "\n"; |
| 84 | std::cout << "avg_ms=" << (total_ms / static_cast<double>(iters)) << "\n"; |
| 85 | return 0; |
| 86 | } |
nothing calls this directly
no test coverage detected