Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Performance: Quick wins for long transcriptions (#157)

Murtaza Nasir8def6179c630
diff --git a/static/js/modules/composables/transcription.js b/static/js/modules/composables/transcription.js
index 59ebbff6..80648315 100644
--- a/static/js/modules/composables/transcription.js
+++ b/static/js/modules/composables/transcription.js
@@ -7,7 +7,7 @@ export function useTranscription(state, utils) {
const {
showTextEditorModal, showAsrEditorModal, selectedRecording,
editingTranscriptionContent, editingSegments, availableSpeakers,
- recordings, dropdownPositions
+ recordings, dropdownPositions, openAsrDropdownIndex
} = state;
const { showToast, setGlobalError, nextTick } = utils;
@@ -126,29 +126,31 @@ export function useTranscription(state, utils) {
}
};
+ // O(1) dropdown management using single ref instead of O(n) forEach
const openSpeakerSuggestions = (index) => {
if (editingSegments.value[index]) {
- // Close other dropdowns
- editingSegments.value.forEach((seg, i) => {
- if (i !== index) seg.showSuggestions = false;
- });
-
- editingSegments.value[index].showSuggestions = true;
+ // Simply set the open index - O(1) instead of O(n) forEach
+ openAsrDropdownIndex.value = index;
filterSpeakerSuggestions(index);
updateDropdownPosition(index);
}
};
const closeSpeakerSuggestions = (index) => {
- if (editingSegments.value[index]) {
- editingSegments.value[index].showSuggestions = false;
+ // Only close if this index is currently open
+ if (openAsrDropdownIndex.value === index) {
+ openAsrDropdownIndex.value = null;
}
};
const closeAllSpeakerSuggestions = () => {
- editingSegments.value.forEach(seg => {
- seg.showSuggestions = false;
- });
+ // O(1) instead of O(n) - just set to null
+ openAsrDropdownIndex.value = null;
+ };
+
+ // Helper to check if a dropdown is open (for template v-if)
+ const isDropdownOpen = (index) => {
+ return openAsrDropdownIndex.value === index;
};
const getDropdownPosition = (index) => {
@@ -423,6 +425,7 @@ export function useTranscription(state, utils) {
openSpeakerSuggestions,
closeSpeakerSuggestions,
closeAllSpeakerSuggestions,
+ isDropdownOpen,
getDropdownPosition,
updateDropdownPosition,
selectSpeaker,
diff --git a/static/js/modules/composables/ui.js b/static/js/modules/composables/ui.js
index 4feb8895..d1c80c94 100644
--- a/static/js/modules/composables/ui.js
+++ b/static/js/modules/composables/ui.js
@@ -985,6 +985,38 @@ export function useUI(state, utils, processedTranscription) {
};
// --- Active Segment Tracking ---
+
+ // Binary search to find the segment containing the current time
+ // Returns the index of the last segment where startTime <= currentTime
+ // O(log n) instead of O(n) - critical for long transcriptions (4500+ segments)
+ const binarySearchSegment = (segments, currentTime) => {
+ if (segments.length === 0) return null;
+
+ let low = 0;
+ let high = segments.length - 1;
+ let result = null;
+
+ while (low <= high) {
+ const mid = Math.floor((low + high) / 2);
+ const startTime = segments[mid].startTime || segments[mid].start_time;
+
+ if (startTime === undefined) {
+ // Skip segments without timing info
+ high = mid - 1;
+ continue;
+ }
+
+ if (startTime <= currentTime) {
+ result = mid; // This segment is a candidate
+ low = mid + 1; // Look for later segments that might also match
+ } else {
+ high = mid - 1; // Current time is before this segment
+ }
+ }
+
+ return result;
+ };
+
const handleAudioTimeUpdate = (event) => {
const transcription = processedTranscription.value;
@@ -996,24 +1028,14 @@ export function useUI(state, utils, processedTranscription) {
const currentTime = audioElement.currentTime;
// Find the segment that contains the current time
- let segments = transcription.simpleSegments || [];
+ const segments = transcription.simpleSegments || [];
if (segments.length === 0) {
return;
}
- // Find the active segment index using a more robust algorithm
- let activeIndex = null;
-
- for (let i = segments.length - 1; i >= 0; i--) {
- const segment = segments[i];
- const startTime = segment.startTime || segment.start_time;
-
- if (startTime !== undefined && currentTime >= startTime) {
- activeIndex = i;
- break;
- }
- }
+ // Find the active segment index using binary search - O(log n)
+ const activeIndex = binarySearchSegment(segments, currentTime);
// Only update if changed
if (activeIndex !== currentPlayingSegmentIndex.value) {
diff --git a/static/js/modules/state/modals.js b/static/js/modules/state/modals.js
index cf97ef95..84e8b19f 100644
--- a/static/js/modules/state/modals.js
+++ b/static/js/modules/state/modals.js
@@ -95,6 +95,11 @@ export function createModalState(ref, reactive) {
const dropdownPositions = ref({});
const editSpeakerDropdownPositions = ref({});
+ // --- Single-ref dropdown tracking (performance optimization) ---
+ // Instead of each segment having showSuggestions property (O(n) to close all),
+ // track which dropdown is open with a single ref (O(1) operations)
+ const openAsrDropdownIndex = ref(null);
+
return {
// Modal visibility
showEditModal,
@@ -180,6 +185,9 @@ export function createModalState(ref, reactive) {
// Dropdown positions
dropdownPositions,
- editSpeakerDropdownPositions
+ editSpeakerDropdownPositions,
+
+ // Single-ref dropdown tracking
+ openAsrDropdownIndex
};
}
diff --git a/templates/components/detail/desktop-transcription-panel.html b/templates/components/detail/desktop-transcription-panel.html
index 5b40b659..9e4b5047 100644
--- a/templates/components/detail/desktop-transcription-panel.html
+++ b/templates/components/detail/desktop-transcription-panel.html
@@ -129,7 +129,7 @@
class="transcription-simple-view">
<div v-if="processedTranscription.hasDialogue">
<div v-for="(segment, index) in processedTranscription.simpleSegments"
- :key="segment.startTime || Math.random()"
+ :key="`seg-${index}-${segment.startTime}`"
:class="['speaker-segment', { 'active-playing-segment': currentPlayingSegmentIndex === index }]"
@click="seekAudioFromEvent"
:data-start-time="segment.startTime"
@@ -146,7 +146,7 @@
<div v-else>
<div v-if="processedTranscription.simpleSegments && processedTranscription.simpleSegments.length > 0">
<div v-for="(segment, index) in processedTranscription.simpleSegments"
- :key="segment.startTime || Math.random()"
+ :key="`seg-${index}-${segment.startTime}`"
:class="['transcript-segment cursor-pointer hover:bg-[var(--bg-accent-hover)] p-2 rounded transition-colors', { 'active-playing-segment': currentPlayingSegmentIndex === index }]"
@click="seekAudioFromEvent"
:data-start-time="segment.startTime"
@@ -166,7 +166,7 @@
<template v-for="(row, rowIndex) in processedTranscription.bubbleRows" :key="`${selectedRecording.id}-bubble-row-${rowIndex}`">
<div :class="['bubble-row', row.isMe ? 'speaker-me' : '']">
<div v-for="(bubble, bubbleIndex) in row.bubbles"
- :key="bubble.startTime || Math.random()"
+ :key="`bubble-${rowIndex}-${bubbleIndex}-${bubble.startTime}`"
:class="['speaker-bubble', bubble.color, row.isMe ? 'speaker-me' : '', { 'active-playing-segment': currentPlayingSegmentIndex === getBubbleGlobalIndex(rowIndex, bubbleIndex) }]"
@click="seekAudioFromEvent"
:data-start-time="bubble.startTime"
diff --git a/templates/components/detail/mobile-transcript-panel.html b/templates/components/detail/mobile-transcript-panel.html
index 5b8fe694..b1b49b03 100644
--- a/templates/components/detail/mobile-transcript-panel.html
+++ b/templates/components/detail/mobile-transcript-panel.html
@@ -54,14 +54,14 @@
<div v-if="selectedRecording.transcription">
<div v-if="!processedTranscription.hasDialogue || transcriptionViewMode === 'simple'" class="transcription-simple-view">
<div v-if="processedTranscription.hasDialogue">
- <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="segment.startTime || Math.random()" :class="['speaker-segment', { 'active-playing-segment': currentPlayingSegmentIndex === index }]" @click="seekAudioFromEvent" :data-start-time="segment.startTime" :data-segment-index="index">
+ <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="`seg-${index}-${segment.startTime}`" :class="['speaker-segment', { 'active-playing-segment': currentPlayingSegmentIndex === index }]" @click="seekAudioFromEvent" :data-start-time="segment.startTime" :data-segment-index="index">
<div v-if="segment.showSpeaker" :class="['speaker-tablet', segment.color]">${segment.speaker}</div>
<div class="speaker-text">${segment.sentence}</div>
</div>
</div>
<div v-else>
<div v-if="processedTranscription.simpleSegments && processedTranscription.simpleSegments.length > 0">
- <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="segment.startTime || Math.random()" :class="['transcript-segment cursor-pointer hover:bg-[var(--bg-accent-hover)] p-2 rounded transition-colors', { 'active-playing-segment': currentPlayingSegmentIndex === index }]" @click="seekAudioFromEvent" :data-start-time="segment.startTime" :data-segment-index="index">${segment.sentence}</div>
+ <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="`seg-${index}-${segment.startTime}`" :class="['transcript-segment cursor-pointer hover:bg-[var(--bg-accent-hover)] p-2 rounded transition-colors', { 'active-playing-segment': currentPlayingSegmentIndex === index }]" @click="seekAudioFromEvent" :data-start-time="segment.startTime" :data-segment-index="index">${segment.sentence}</div>
</div>
<div v-else class="whitespace-pre-wrap">${processedTranscription.content}</div>
</div>
@@ -69,7 +69,7 @@
<div v-else-if="transcriptionViewMode === 'bubble'" class="transcription-with-speakers">
<template v-for="(row, rowIndex) in processedTranscription.bubbleRows" :key="`${selectedRecording.id}-bubble-row-${rowIndex}`">
<div :class="['bubble-row', row.isMe ? 'speaker-me' : '']">
- <div v-for="(bubble, bubbleIndex) in row.bubbles" :key="bubble.startTime || Math.random()" :class="['speaker-bubble', bubble.color, row.isMe ? 'speaker-me' : '', { 'active-playing-segment': currentPlayingSegmentIndex === getBubbleGlobalIndex(rowIndex, bubbleIndex) }]" @click="seekAudioFromEvent" :data-start-time="bubble.startTime" :data-segment-index="getBubbleGlobalIndex(rowIndex, bubbleIndex)">
+ <div v-for="(bubble, bubbleIndex) in row.bubbles" :key="`bubble-${rowIndex}-${bubbleIndex}-${bubble.startTime}`" :class="['speaker-bubble', bubble.color, row.isMe ? 'speaker-me' : '', { 'active-playing-segment': currentPlayingSegmentIndex === getBubbleGlobalIndex(rowIndex, bubbleIndex) }]" @click="seekAudioFromEvent" :data-start-time="bubble.startTime" :data-segment-index="getBubbleGlobalIndex(rowIndex, bubbleIndex)">
<div class="speaker-bubble-content">${bubble.sentence}</div>
</div>
</div>
diff --git a/templates/modals/asr-editor-modal.html b/templates/modals/asr-editor-modal.html
index bcd12642..605de2a6 100644
--- a/templates/modals/asr-editor-modal.html
+++ b/templates/modals/asr-editor-modal.html
@@ -72,7 +72,7 @@
</tr>
</thead>
<tbody>
- <tr v-for="(segment, index) in editingSegments" :key="segment.id || index" class="hover:bg-[var(--bg-tertiary)] transition-colors border-b border-[var(--border-primary)]">
+ <tr v-for="(segment, index) in editingSegments" :key="`asr-${index}-${segment.start_time}`" class="hover:bg-[var(--bg-tertiary)] transition-colors border-b border-[var(--border-primary)]">
<td class="border-r border-[var(--border-primary)] align-middle">
<div class="relative h-full" :ref="'speakerDropdown' + index">
<div class="flex items-center h-full">
@@ -88,7 +88,7 @@
</div>
<!-- Fixed position dropdown that can escape modal overflow -->
<teleport to="body">
- <div v-if="segment.showSuggestions"
+ <div v-if="isDropdownOpen(index)"
class="fixed z-[100] w-40 bg-[var(--bg-secondary)] border border-[var(--border-primary)] rounded-md shadow-lg max-h-48 overflow-y-auto"
:style="getDropdownPosition(index)">
<div v-for="speaker in segment.filteredSpeakers" :key="speaker" @mousedown.prevent="selectSpeaker(index, speaker)" class="px-3 py-2 cursor-pointer hover:bg-[var(--bg-tertiary)] text-sm">${speaker}</div>
diff --git a/templates/modals/speaker-modal.html b/templates/modals/speaker-modal.html
index 53f205dd..1e3c7789 100644
--- a/templates/modals/speaker-modal.html
+++ b/templates/modals/speaker-modal.html
@@ -178,7 +178,7 @@
<!-- Transcript Section -->
<div class="flex-grow overflow-y-auto custom-scrollbar speaker-modal-transcript min-h-0" @click="seekAudioFromEvent">
<div v-if="processedTranscription.isJson">
- <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="index" class="speaker-segment mb-1.5 sm:mb-2 group relative" :data-start-time="segment.startTime">
+ <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="`seg-${index}-${segment.startTime}`" class="speaker-segment mb-1.5 sm:mb-2 group relative" :data-start-time="segment.startTime">
<div class="flex items-start gap-1">
<!-- Speaker tag on its own line on mobile -->
<span :class="[segment.color, 'speaker-tag', { 'speaker-highlight': highlightedSpeaker === segment.speakerId }]" :data-speaker-id="segment.speakerId" class="inline-block flex-shrink-0 truncate text-xs sm:text-sm font-medium w-16 sm:w-24" :title="segment.speaker">${ segment.speaker }</span>