Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/framework/audio/chunking.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "engine/framework/audio/chunking.h"

#include "engine/framework/debug/trace.h"
#include "engine/framework/runtime/options.h"

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <limits>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
Expand Down Expand Up @@ -599,7 +601,20 @@ void append_chunk_word_timestamps(
const int64_t local_start = std::max<int64_t>(word.span.start_sample, 0);
const int64_t local_end = std::min<int64_t>(word.span.end_sample, source_samples);
if (local_start >= local_end) {
throw std::runtime_error("Audio chunker word merge received a timestamp outside the chunk span");
std::ostringstream warning;
warning << "dropping word timestamp outside chunk span"
<< " word=\"" << word.word << "\""
<< " local_start=" << word.span.start_sample
<< " local_end=" << word.span.end_sample
<< " source_samples=" << source_samples
<< " source_start=" << source_span.start_sample
<< " source_end=" << source_span.end_sample
<< " keep_start=" << keep_span.start_sample
<< " keep_end=" << keep_span.end_sample
<< " source_sample_rate=" << source_sample_rate
<< " timestamp_sample_rate=" << timestamp_sample_rate;
debug::log_message(debug::LogLevel::Warning, "audio.chunking", warning.str());
continue;
}
const int64_t global_start = timestamp_source_span.start_sample + local_start;
if (global_start < timestamp_keep_span.start_sample || global_start >= timestamp_keep_span.end_sample) {
Expand Down
29 changes: 19 additions & 10 deletions tests/unittests/test_audio_chunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,16 +836,6 @@ void test_chunk_word_timestamp_merge_rejects_invalid_spans() {
},
"inverted chunk span");

require_throws(
[]() {
std::vector<engine::runtime::WordTimestamp> merged;
engine::audio::append_chunk_word_timestamps(
merged,
{word("outside", 120, 140)},
engine::runtime::TimeSpan{1000, 1100});
},
"word outside chunk");

require_throws(
[]() {
std::vector<engine::runtime::WordTimestamp> merged;
Expand All @@ -871,6 +861,24 @@ void test_chunk_word_timestamp_merge_rejects_invalid_spans() {
"invalid source sample rate");
}

void test_chunk_word_timestamp_merge_drops_outside_words() {
std::vector<engine::runtime::WordTimestamp> merged;
engine::audio::append_chunk_word_timestamps(
merged,
{
word("kept", 20, 40),
word("outside", 120, 140),
word("also_kept", 50, 80),
},
engine::runtime::TimeSpan{1000, 1100});

engine::test::require_eq(merged.size(), static_cast<size_t>(2), "outside word dropped");
engine::test::require_eq(merged[0].word, std::string("kept"), "first valid word kept");
require_span(merged[0].span, 1020, 1040, "first valid word span");
engine::test::require_eq(merged[1].word, std::string("also_kept"), "second valid word kept");
require_span(merged[1].span, 1050, 1080, "second valid word span");
}

} // namespace

int main() {
Expand Down Expand Up @@ -903,6 +911,7 @@ int main() {
test_chunk_word_timestamp_merge_rescales_chunk_domain();
test_chunk_speech_metadata_merge_rescales_chunk_domain();
test_chunk_word_timestamp_merge_rejects_invalid_spans();
test_chunk_word_timestamp_merge_drops_outside_words();
std::cout << "audio_chunking_test passed\n";
} catch (const std::exception & ex) {
std::cerr << "audio_chunking_test failed: " << ex.what() << "\n";
Expand Down
Loading