ref: https://github.com/openai/whisper/blob/main/whisper/audio.py#L110-L157
| 360 | |
| 361 | // ref: https://github.com/openai/whisper/blob/main/whisper/audio.py#L110-L157 |
| 362 | static bool log_mel_spectrogram( |
| 363 | const float * samples, |
| 364 | const int n_samples_in, |
| 365 | const int n_threads, |
| 366 | const filter_params & params, |
| 367 | const mtmd_audio_cache & cache, |
| 368 | mtmd_audio_mel & out) { |
| 369 | //const int64_t t_start_us = ggml_time_us(); |
| 370 | |
| 371 | out.n_len_org = n_samples_in; |
| 372 | int n_samples = n_samples_in; |
| 373 | |
| 374 | // Hann window |
| 375 | const float * hann = cache.hann_window.data(); |
| 376 | const int frame_size = (params.n_fft_bins - 1) * 2; |
| 377 | const int frame_step = params.hop_length; |
| 378 | |
| 379 | // Padding |
| 380 | std::vector<float> samples_padded; |
| 381 | if (params.no_padding) { |
| 382 | // no padding, use samples as-is |
| 383 | samples_padded = std::vector<float>(samples, samples + n_samples); |
| 384 | samples = samples_padded.data(); |
| 385 | n_samples = samples_padded.size(); |
| 386 | } else if (params.center_padding) { |
| 387 | const auto pad_amount = frame_size / 2; |
| 388 | samples_padded = std::vector<float>(n_samples + 2 * pad_amount, 0); |
| 389 | std::copy(samples, samples + n_samples, samples_padded.data() + pad_amount); |
| 390 | samples = samples_padded.data(); |
| 391 | n_samples = samples_padded.size(); |
| 392 | } else { |
| 393 | // existing padding logic |
| 394 | int64_t stage_1_pad = params.sample_rate * 30; |
| 395 | int64_t stage_2_pad = frame_size / 2; |
| 396 | samples_padded.resize(n_samples + stage_1_pad + stage_2_pad * 2); |
| 397 | std::copy(samples, samples + n_samples, samples_padded.begin() + stage_2_pad); |
| 398 | // pad 30 seconds of zeros at the end of audio (480,000 samples) + reflective pad 200 samples at the end of audio |
| 399 | std::fill(samples_padded.begin() + n_samples + stage_2_pad, samples_padded.begin() + n_samples + stage_1_pad + 2 * stage_2_pad, 0); |
| 400 | // reflective pad 200 samples at the beginning of audio |
| 401 | if (n_samples < stage_2_pad + 1) { |
| 402 | // TODO: Handle short audio differently or return error |
| 403 | return false; |
| 404 | } |
| 405 | std::reverse_copy(samples + 1, samples + 1 + stage_2_pad, samples_padded.begin()); |
| 406 | } |
| 407 | |
| 408 | // preemphasis |
| 409 | if (params.preemph) { |
| 410 | const int pad_amount = frame_size / 2; |
| 411 | const float preemph = 0.97f; |
| 412 | float prev = samples_padded[pad_amount]; |
| 413 | for (int i = pad_amount + 1; i + pad_amount < n_samples; ++i) { |
| 414 | float cur = samples_padded[i]; |
| 415 | samples_padded[i] = cur - preemph * prev; |
| 416 | prev = cur; |
| 417 | } |
| 418 | } |
| 419 |
no test coverage detected