Skip to content

Commit c56eaaa

Browse files
committed
Add support for s32 and s32p
1 parent f8bcf75 commit c56eaaa

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/render/audio.nim

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,10 @@ proc get(getter: Getter, start: int, endSample: int): seq[int16] =
129129
if frame.format == AV_SAMPLE_FMT_S16.cint:
130130
# Interleaved 16-bit
131131
let audioData = cast[ptr UncheckedArray[int16]](frame.data[0])
132-
for i in 0..<samplesToProcess:
133-
let frameIndex = samplesSkippedInFrame + i
134-
let resultIndex = (totalSamples + i) * 2 # Interleaved index
135-
for ch in 0 ..< channels:
136-
result[resultIndex + ch] = audioData[frameIndex * channels + ch]
132+
let sourceOffset = samplesSkippedInFrame * channels
133+
let destOffset = totalSamples * channels
134+
let bytesToCopy = samplesToProcess * channels * sizeof(int16)
135+
copyMem(addr result[destOffset], addr audioData[sourceOffset], bytesToCopy)
137136

138137
elif frame.format == AV_SAMPLE_FMT_S16P.cint:
139138
# Planar 16-bit
@@ -169,6 +168,27 @@ proc get(getter: Getter, start: int, endSample: int): seq[int16] =
169168
let floatSample = channelData[frameIndex]
170169
let clampedSample = max(-1.0, min(1.0, floatSample))
171170
result[resultIndex + ch] = int16(clampedSample * 32767.0)
171+
172+
elif frame.format == AV_SAMPLE_FMT_S32.cint:
173+
# Interleaved 32-bit
174+
let audioData = cast[ptr UncheckedArray[int32]](frame.data[0])
175+
for i in 0..<samplesToProcess:
176+
let frameIndex = samplesSkippedInFrame + i
177+
let resultIndex = (totalSamples + i) * 2 # Interleaved index
178+
for ch in 0 ..< channels:
179+
# Convert 32-bit to 16-bit by shifting right 16 bits
180+
result[resultIndex + ch] = int16(audioData[frameIndex * channels + ch] shr 16)
181+
182+
elif frame.format == AV_SAMPLE_FMT_S32P.cint:
183+
# Planar 32-bit
184+
for i in 0..<samplesToProcess:
185+
let frameIndex = samplesSkippedInFrame + i
186+
let resultIndex = (totalSamples + i) * 2 # Interleaved index
187+
for ch in 0 ..< channels:
188+
if frame.data[ch] != nil:
189+
let channelData = cast[ptr UncheckedArray[int32]](frame.data[ch])
190+
# Convert 32-bit to 16-bit by shifting right 16 bits
191+
result[resultIndex + ch] = int16(channelData[frameIndex] shr 16)
172192
else:
173193
error &"Unsupported audio format: {av_get_sample_fmt_name(frame.format)}"
174194

0 commit comments

Comments
 (0)