diff --git a/core/transcriber.cpp b/core/transcriber.cpp index 77c2733..e572335 100644 --- a/core/transcriber.cpp +++ b/core/transcriber.cpp @@ -426,10 +426,27 @@ void Transcriber::transcribe_stream(int32_t stream_id, uint32_t flags, std::lock_guard lock(stream->vad_mutex); stream->vad->process_audio(audio_data, (int32_t)audio_length, INTERNAL_SAMPLE_RATE); - segments = *(stream->vad->get_segments()); + const std::vector *vad_segments = + stream->vad->get_segments(); + segments.reserve(vad_segments->size()); + for (const VoiceActivitySegment &segment : *vad_segments) { + VoiceActivitySegment segment_copy; + segment_copy.start_time = segment.start_time; + segment_copy.end_time = segment.end_time; + segment_copy.is_complete = segment.is_complete; + segment_copy.just_updated = segment.just_updated; + if (segment.just_updated) { + segment_copy.audio_data = segment.audio_data; + } + segments.push_back(std::move(segment_copy)); + } } stream->clear_new_audio_buffer(); this->update_transcript_from_segments(segments, stream, out_transcript); + if (!this->options.return_audio_data) { + std::lock_guard lock(stream->vad_mutex); + stream->vad->clear_completed_segment_audio_data(); + } } std::string Transcriber::transcript_to_string( diff --git a/core/voice-activity-detector.cpp b/core/voice-activity-detector.cpp index f49fbcc..58fa083 100644 --- a/core/voice-activity-detector.cpp +++ b/core/voice-activity-detector.cpp @@ -96,6 +96,14 @@ void VoiceActivityDetector::process_audio(const float *audio_data, processing_remainder_audio_buffer = processing_buffer; } +void VoiceActivityDetector::clear_completed_segment_audio_data() { + for (VoiceActivitySegment &segment : segments) { + if (segment.is_complete && !segment.audio_data.empty()) { + std::vector().swap(segment.audio_data); + } + } +} + void VoiceActivityDetector::process_audio_chunk(const float *audio_data, size_t audio_data_size) { assert(audio_data_size == (size_t)(hop_size)); diff --git a/core/voice-activity-detector.h b/core/voice-activity-detector.h index 3279bc8..bfee186 100644 --- a/core/voice-activity-detector.h +++ b/core/voice-activity-detector.h @@ -56,6 +56,7 @@ class VoiceActivityDetector { const std::vector *get_segments() const { return &segments; } + void clear_completed_segment_audio_data(); std::string to_string() const; private: @@ -66,4 +67,4 @@ class VoiceActivityDetector { void process_audio_chunk(const float *audio_data, size_t audio_data_size); }; -#endif \ No newline at end of file +#endif