#define _USE_MATH_DEFINES // for M_PI #include "common-whisper.h" #include "common.h" #include "hf-cache.h" #include "whisper.h" // third-party utilities // use your favorite implementations #define STB_VORBIS_HEADER_ONLY #include "stb_vorbis.c" /* Enables Vorbis decoding. */ #ifdef _WIN32 #ifndef NOMINMAX #define NOMINMAX #endif #endif #define MA_NO_DEVICE_IO #define MA_NO_THREADING #define MA_NO_ENCODING #define MA_NO_GENERATION #define MA_NO_RESOURCE_MANAGER #define MA_NO_NODE_GRAPH #define MINIAUDIO_IMPLEMENTATION #include "miniaudio.h" #ifdef _WIN32 #include #include #endif #include #include #include #include #include #ifdef WHISPER_COMMON_FFMPEG // as implemented in ffmpeg-trancode.cpp only embedded in common lib if whisper built with ffmpeg support extern bool ffmpeg_decode_audio(const std::string & ifname, std::vector & wav_data, int out_sample_rate = WHISPER_SAMPLE_RATE); #endif // extract f32 PCM frames from an initialized decoder, downmix to mono and keep the stereo split static bool read_audio_from_decoder(ma_decoder & decoder, std::vector & pcmf32, std::vector> & pcmf32s, bool stereo) { ma_result result; ma_uint64 frame_count; ma_uint64 frames_read; if ((result = ma_decoder_get_length_in_pcm_frames(&decoder, &frame_count)) != MA_SUCCESS) { fprintf(stderr, "error: failed to retrieve the length of the audio data (%s)\n", ma_result_description(result)); return false; } pcmf32.resize(stereo ? frame_count*2 : frame_count); if ((result = ma_decoder_read_pcm_frames(&decoder, pcmf32.data(), frame_count, &frames_read)) != MA_SUCCESS) { fprintf(stderr, "error: failed to read the frames of the audio data (%s)\n", ma_result_description(result)); return false; } if (stereo) { std::vector stereo_data = pcmf32; pcmf32.resize(frame_count); for (uint64_t i = 0; i < frame_count; i++) { pcmf32[i] = (stereo_data[2*i] + stereo_data[2*i + 1]); } pcmf32s.resize(2); pcmf32s[0].resize(frame_count); pcmf32s[1].resize(frame_count); for (uint64_t i = 0; i < frame_count; i++) { pcmf32s[0][i] = stereo_data[2*i]; pcmf32s[1][i] = stereo_data[2*i + 1]; } } return true; } bool read_audio_data(const std::string & fname, std::vector & pcmf32, std::vector> & pcmf32s, bool stereo) { std::vector audio_data; // used for pipe input from stdin or ffmpeg decoding output ma_result result; ma_decoder_config decoder_config; struct decoder_guard { ma_decoder decoder; bool initialized = false; ma_decoder * operator&() { return &decoder; } ~decoder_guard() { if (initialized) { ma_decoder_uninit(&decoder); } } }; decoder_guard decoder{}; decoder_config = ma_decoder_config_init(ma_format_f32, stereo ? 2 : 1, WHISPER_SAMPLE_RATE); if (fname == "-") { #ifdef _WIN32 _setmode(_fileno(stdin), _O_BINARY); #endif uint8_t buf[1024]; while (true) { const size_t n = fread(buf, 1, sizeof(buf), stdin); if (n == 0) { break; } audio_data.insert(audio_data.end(), buf, buf + n); } result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder); if (result != MA_SUCCESS) { fprintf(stderr, "%s: failed to open audio data from stdin (%s)\n", __func__, ma_result_description(result)); return false; } decoder.initialized = true; fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, audio_data.size()); } else { fprintf(stderr, "%s: reading audio data from '%s' ...\n", __func__, fname.c_str()); // first try miniaudio. if it fails (or skipped) - try ffmpeg { const char * skip = getenv("WHISPER_COMMON_MINIAUDIO_SKIP"); if (!skip || strlen(skip) == 0 || strcmp(skip, "0") == 0) { fprintf(stderr, "%s: trying to decode with miniaudio\n", __func__); result = ma_decoder_init_file(fname.c_str(), &decoder_config, &decoder); if (result == MA_SUCCESS) { decoder.initialized = true; } } else { fprintf(stderr, "%s: skipping miniaudio\n", __func__); } } #if defined(WHISPER_COMMON_FFMPEG) if (!decoder.initialized) { fprintf(stderr, "%s: trying to decode with ffmpeg\n", __func__); if (ffmpeg_decode_audio(fname, audio_data) != 0) { fprintf(stderr, "%s: failed to ffmpeg decode\n", __func__); return false; } result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder); if (result != MA_SUCCESS) { fprintf(stderr, "%s: failed to read audio data as wav (%s)\n", __func__, ma_result_description(result)); return false; } decoder.initialized = true; } #endif if (!decoder.initialized) { fprintf(stderr, "%s: failed to read audio data\n", __func__); return false; } } return read_audio_from_decoder(decoder.decoder, pcmf32, pcmf32s, stereo); } // decode audio bytes already held in memory bool read_audio_data(const char * buffer, size_t buffer_size, std::vector & pcmf32, std::vector> & pcmf32s, bool stereo) { ma_decoder_config decoder_config = ma_decoder_config_init(ma_format_f32, stereo ? 2 : 1, WHISPER_SAMPLE_RATE); ma_decoder decoder; if (ma_decoder_init_memory(buffer, buffer_size, &decoder_config, &decoder) != MA_SUCCESS) { fprintf(stderr, "error: failed to decode audio data from memory buffer\n"); return false; } bool ok = read_audio_from_decoder(decoder, pcmf32, pcmf32s, stereo); ma_decoder_uninit(&decoder); return ok; } // 500 -> 00:05.000 // 6000 -> 01:00.000 std::string to_timestamp(int64_t t, bool comma) { int64_t msec = t * 10; int64_t hr = msec / (1000 * 60 * 60); msec = msec - hr * (1000 * 60 * 60); int64_t min = msec / (1000 * 60); msec = msec - min * (1000 * 60); int64_t sec = msec / 1000; msec = msec - sec * 1000; char buf[32]; snprintf(buf, sizeof(buf), "%02d:%02d:%02d%s%03d", (int) hr, (int) min, (int) sec, comma ? "," : ".", (int) msec); return std::string(buf); } int timestamp_to_sample(int64_t t, int n_samples, int whisper_sample_rate) { return std::max(0, std::min((int) n_samples - 1, (int) ((t*whisper_sample_rate)/100))); } int utf8_trailing_bytes_needed(const std::string & s) { const int n = (int) s.size(); int i = n - 1; while (i >= 0 && ((unsigned char) s[i] & 0xC0) == 0x80) { --i; } if (i < 0) { return 0; } const unsigned char c = (unsigned char) s[i]; int expected; if ((c & 0x80) == 0x00) { expected = 1; } else if ((c & 0xE0) == 0xC0) { expected = 2; } else if ((c & 0xF0) == 0xE0) { expected = 3; } else if ((c & 0xF8) == 0xF0) { expected = 4; } else { return 0; } const int have = n - i; return have >= expected ? 0 : (expected - have); } bool speak_with_file(const std::string & command, const std::string & text, const std::string & path, int voice_id) { std::ofstream speak_file(path.c_str()); if (speak_file.fail()) { fprintf(stderr, "%s: failed to open speak_file\n", __func__); return false; } else { speak_file.write(text.c_str(), text.size()); speak_file.close(); int ret = system((command + " " + std::to_string(voice_id) + " " + path).c_str()); if (ret != 0) { fprintf(stderr, "%s: failed to speak\n", __func__); return false; } } return true; } // filename test for whisper GGML models: ggml-*.bin static bool whisper_hf_is_ggml_bin(const std::string & name) { return name.rfind("ggml-", 0) == 0 && name.size() >= 4 && name.compare(name.size() - 4, 4, ".bin") == 0; } // pick the primary file from a listing: exact hf_file match, else the first ggml-*.bin static const hf_cache::hf_file * whisper_hf_pick_primary(const hf_cache::hf_files & files, const std::string & hf_file) { for (const auto & file : files) { if (!hf_file.empty()) { if (file.path == hf_file) { return &file; } } else { const std::string name = std::filesystem::path(file.path).filename().string(); if (whisper_hf_is_ggml_bin(name)) { return &file; } } } return nullptr; } // collect the entries whose filename matches ggml-*.bin static hf_cache::hf_files whisper_hf_ggml_candidates(const hf_cache::hf_files & files) { hf_cache::hf_files out; for (const auto & file : files) { const std::string name = std::filesystem::path(file.path).filename().string(); if (whisper_hf_is_ggml_bin(name)) { out.push_back(file); } } return out; } // print an error message followed by the sorted list of candidate filenames static void whisper_hf_print_candidates(const std::string & msg, const hf_cache::hf_files & candidates) { fprintf(stderr, "%s\n", msg.c_str()); std::vector names; for (const auto & file : candidates) { names.push_back(std::filesystem::path(file.path).filename().string()); } std::sort(names.begin(), names.end()); for (const auto & name : names) { fprintf(stderr, " - %s\n", name.c_str()); } } std::string whisper_hf_resolve_model(const std::string & hf_repo, const std::string & hf_file) { const char * token_env = std::getenv("HF_TOKEN"); const std::string token = token_env ? token_env : ""; // honor an HF offline mode (huggingface_hub convention): skip the network path entirely const char * offline_env = std::getenv("HF_HUB_OFFLINE"); const bool offline = offline_env && *offline_env && std::string(offline_env) != "0"; // -hf alone (no --hf-file): cache-first, and refuse ambiguity rather than guess. if (hf_file.empty()) { const hf_cache::hf_files cached = whisper_hf_ggml_candidates(hf_cache::get_cached_files(hf_repo)); if (cached.size() == 1) { return hf_cache::finalize_file(cached.front()); } if (cached.size() > 1) { whisper_hf_print_candidates( "error: multiple models cached for " + hf_repo + "; specify one with -hff/--hf-file:", cached); return ""; } // none cached if (offline) { fprintf(stderr, "error: %s not found in HF cache\n", hf_repo.c_str()); return ""; } const hf_cache::hf_files remote = whisper_hf_ggml_candidates(hf_cache::get_repo_files(hf_repo, token)); if (remote.empty()) { fprintf(stderr, "error: no models found in %s\n", hf_repo.c_str()); return ""; } // don't auto-pick/download a multi-model repo; list what's available instead whisper_hf_print_candidates( "error: multiple models available in " + hf_repo + "; specify one with -hff/--hf-file:", remote); return ""; } // explicit --hf-file: download-first with cache fall-back (Phase 2, unchanged). // 1. try download first: list the repo over the network and fetch the primary file. // get_repo_files swallows network errors into an empty result (graceful degradation). if (!offline) { const hf_cache::hf_files remote = hf_cache::get_repo_files(hf_repo, token); if (const hf_cache::hf_file * primary = whisper_hf_pick_primary(remote, hf_file)) { if (hf_cache::download_file(*primary, token)) { return hf_cache::finalize_file(*primary); } } } // 2. fall back to the on-disk HF hub cache scan (Phase 1 behavior). const hf_cache::hf_files cached = hf_cache::get_cached_files(hf_repo); if (const hf_cache::hf_file * primary = whisper_hf_pick_primary(cached, hf_file)) { return hf_cache::finalize_file(*primary); } fprintf(stderr, "error: file '%s' not found in %s (cache or network)\n", hf_file.c_str(), hf_repo.c_str()); return ""; } #undef STB_VORBIS_HEADER_ONLY #include "stb_vorbis.c"