Repository: ggerganov/whisper.cpp
File: src/whisper.cpp (C++)

Code before:

int whisper_full_lang_id_from_state(struct whisper_state * state) {
    return state->lang_id;
}

int whisper_full_lang_id(struct whisper_context * ctx) {
    return ctx->state->lang_id;
}

static int64_t map_processed_to_original_time(int64_t processed_time, const std::vector<vad_time_mapping>& mapping_table) {
    if (mapping_table.empty()) {
        return processed_time;
    }

    if (processed_time <= mapping_table.front().processed_time) {
        return mapping_table.front().original_time; // Before first mapping point
    }

    if (processed_time >= mapping_table.back().processed_time) {
        return mapping_table.back().original_time; // After last mapping point
    }

    // Binary search over the time map that finds the first entry that has a
    // processed time greater than or equal to the current processed time.
    auto upper = std::lower_bound(mapping_table.begin(), mapping_table.end(), processed_time,
        [](const vad_time_mapping& entry, int64_t time) {
            return entry.processed_time < time;
        }
    );

    // If exact match found
    if (upper->processed_time == processed_time) {
        return upper->original_time;
    }

    // Need to interpolate between two points
    auto lower = upper - 1;

    int64_t processed_diff = upper->processed_time - lower->processed_time;
    int64_t original_diff = upper->original_time - lower->original_time;
    int64_t offset = processed_time - lower->processed_time;

    if (processed_diff == 0) {
        return lower->original_time;
    }

    // Perform linear interpolation
    return lower->original_time + (offset * original_diff) / processed_diff;
}

// Function to get the starting timestamp of a segment

Review comment (suggestion):
```suggestion
        [](const vad_time_mapping & entry, int64_t time) {
```

Code after:
    return state->lang_id;
}

int whisper_full_lang_id(struct whisper_context * ctx) {
    return ctx->state->lang_id;
}

static int64_t map_processed_to_original_time(int64_t processed_time, const std::vector<vad_time_mapping> & mapping_table) {
    if (mapping_table.empty()) {
        return processed_time;
    }

    if (processed_time <= mapping_table.front().processed_time) {
        return mapping_table.front().original_time; // Before first mapping point
    }

    if (processed_time >= mapping_table.back().processed_time) {
        return mapping_table.back().original_time; // After last mapping point
    }

    // Binary search over the time map that finds the first entry that has a
    // processed time greater than or equal to the current processed time.
    auto upper = std::lower_bound(mapping_table.begin(), mapping_table.end(), processed_time,
        [](const vad_time_mapping & entry, int64_t time) {
            return entry.processed_time < time;
        }
    );

    // If exact match found
    if (upper->processed_time == processed_time) {
        return upper->original_time;
    }

    // Need to interpolate between two points
    auto lower = upper - 1;

    int64_t processed_diff = upper->processed_time - lower->processed_time;
    int64_t original_diff = upper->original_time - lower->original_time;
    int64_t offset = processed_time - lower->processed_time;

    if (processed_diff == 0) {
        return lower->original_time;
    }

    // Perform linear interpolation
    return lower->original_time + (offset * original_diff) / processed_diff;
}

// Function to get the starting timestamp of a segment
int64_t whisper_full_get_segment_t0_from_state(struct whisper_state * state, int i_segment) {
    // If VAD wasn't used, return the original timestamp