Skip to content

Commit 0ce125f

Browse files
committed
fix: skip aligned PCM padding bytes for ranged WAV responses
For non-frame-aligned WAV data ranges, seek to the aligned PCM offset but discard leading padding bytes so Content-Range matches samples.
1 parent b16601e commit 0ce125f

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

‎src/api/loadStream.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ async function handler(
830830
let rangeStart = 0
831831
let rangeEnd = totalFileSize > 0 ? totalFileSize - 1 : 0
832832
let rangeHeaderBytesToSend: Buffer | null = null
833+
let pcmSkipBytes = 0
833834

834835
const rangeHeader = req.headers.range
835836
if (
@@ -871,6 +872,9 @@ async function handler(
871872
}
872873

873874
const alignedPcmByteOffset = Math.floor(pcmByteOffset / 4) * 4
875+
// If the requested range starts at a non-frame-aligned PCM byte, seek to the aligned
876+
// position but skip the extra leading bytes so returned data matches Content-Range exactly.
877+
pcmSkipBytes = pcmByteOffset - alignedPcmByteOffset
874878
const seekPositionMs = Math.floor((alignedPcmByteOffset / 192000) * 1000)
875879
input.position = seekPositionMs
876880
}
@@ -1113,20 +1117,31 @@ async function handler(
11131117
return
11141118
}
11151119

1120+
let skipRemaining = pcmSkipBytes
11161121
const limiter = new Transform({
11171122
transform(chunk: Buffer, _enc, cb) {
1123+
let buf = chunk
1124+
if (skipRemaining > 0) {
1125+
if (buf.length <= skipRemaining) {
1126+
skipRemaining -= buf.length
1127+
cb()
1128+
return
1129+
}
1130+
buf = buf.subarray(skipRemaining)
1131+
skipRemaining = 0
1132+
}
11181133
if (bytesRemaining <= 0) {
11191134
cb()
11201135
return
11211136
}
1122-
if (chunk.length <= bytesRemaining) {
1123-
bytesRemaining -= chunk.length
1124-
cb(null, chunk)
1137+
if (buf.length <= bytesRemaining) {
1138+
bytesRemaining -= buf.length
1139+
cb(null, buf)
11251140
if (bytesRemaining <= 0) {
11261141
limiter.end()
11271142
}
11281143
} else {
1129-
const slice = chunk.subarray(0, bytesRemaining)
1144+
const slice = buf.subarray(0, bytesRemaining)
11301145
bytesRemaining = 0
11311146
cb(null, slice)
11321147
limiter.end()

0 commit comments

Comments
 (0)