chore: clean Smart Speed whitespace

This commit is contained in:
Jonathan Baldie 2026-05-07 20:44:45 +01:00
parent 577f111a17
commit 0f8be10275
7 changed files with 30 additions and 30 deletions

View file

@ -121,7 +121,7 @@ export default class LocalAudioPlayer extends EventEmitter {
// Map AudioContext time to Media time
const delayMs = this.audioContext.currentTime * 1000 - msg.time
this._silenceStartTime = this.player.currentTime * 1000 - delayMs
// Dynamically increase playback rate
if (this.enableSmartSpeed) {
this.player.playbackRate = this.defaultPlaybackRate * this.smartSpeedRatio
@ -164,7 +164,7 @@ export default class LocalAudioPlayer extends EventEmitter {
this.silenceMap.reset()
this.updateSmartSpeedRegions()
this._silenceStartTime = null
// Reset playback rate in case we were in the middle of a silence region
if (this.player) {
this.player.playbackRate = this.defaultPlaybackRate
@ -419,7 +419,7 @@ export default class LocalAudioPlayer extends EventEmitter {
setPlaybackRate(playbackRate) {
if (!this.player) return
this.defaultPlaybackRate = playbackRate
// If we're in the middle of a silence region, we should multiply the new rate
if (this.enableSmartSpeed && this._silenceStartTime !== null) {
this.player.playbackRate = playbackRate * this.smartSpeedRatio
@ -454,7 +454,7 @@ export default class LocalAudioPlayer extends EventEmitter {
this.silenceMap.reset()
this.updateSmartSpeedRegions()
this.playWhenReady = playWhenReady
// Reset playback rate in case we were in a silence region
if (this.enableSmartSpeed && this.player.playbackRate !== this.defaultPlaybackRate) {
this.player.playbackRate = this.defaultPlaybackRate

View file

@ -38,7 +38,7 @@ class SilenceMap {
}
this._regions = merged
// Cap the number of regions to prevent memory leaks for long audiobooks
// Assuming each region is ~1 second, 5000 regions is over an hour of silence
if (this._regions.length > 5000) {

View file

@ -3,16 +3,16 @@ class TimeMapper {
this.ratio = compressionRatio
// Only keep regions >= 200ms
this.regions = silenceRegions.filter(r => (r.end - r.start) >= 200)
// Calculate compressed durations and cumulative time saved
this.processedRegions = []
let accumulatedSaved = 0
for (const r of this.regions) {
const originalDuration = r.end - r.start
const compressedDuration = this.ratio === 0 ? 0 : originalDuration / this.ratio
const saved = originalDuration - compressedDuration
this.processedRegions.push({
...r,
originalDuration,
@ -20,10 +20,10 @@ class TimeMapper {
saved,
accumulatedSavedBefore: accumulatedSaved
})
accumulatedSaved += saved
}
this._totalTimeSaved = accumulatedSaved
}
@ -31,53 +31,53 @@ class TimeMapper {
if (this.ratio === 1.0 || this.regions.length === 0) return wallMs
let audioMs = wallMs
for (const r of this.processedRegions) {
// The start time of this region in wall-clock time
const regionWallStart = r.start - r.accumulatedSavedBefore
if (wallMs < regionWallStart) {
// Before this region, no more accumulated saved to add
break
}
const regionWallEnd = regionWallStart + r.compressedDuration
if (wallMs <= regionWallEnd) {
// Inside the compressed region
const timeSpentInRegionWall = wallMs - regionWallStart
const timeSpentInRegionAudio = timeSpentInRegionWall * this.ratio
return r.start + timeSpentInRegionAudio
}
// After this region, we add the total time saved by this region
audioMs = wallMs + (r.accumulatedSavedBefore + r.saved)
}
return audioMs
}
audioToWallClock(audioMs) {
if (this.ratio === 1.0 || this.regions.length === 0) return audioMs
let wallMs = audioMs
for (const r of this.processedRegions) {
if (audioMs < r.start) {
break
}
if (audioMs <= r.end) {
// Inside the region
const timeSpentInRegionAudio = audioMs - r.start
const timeSpentInRegionWall = timeSpentInRegionAudio / this.ratio
return r.start - r.accumulatedSavedBefore + timeSpentInRegionWall
}
// After the region
wallMs = audioMs - (r.accumulatedSavedBefore + r.saved)
}
return wallMs
}