@@ -129,11 +129,10 @@ proc get(getter: Getter, start: int, endSample: int): seq[int16] =
129
129
if frame.format == AV_SAMPLE_FMT_S16.cint :
130
130
# Interleaved 16-bit
131
131
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)
137
136
138
137
elif frame.format == AV_SAMPLE_FMT_S16P.cint :
139
138
# Planar 16-bit
@@ -169,6 +168,27 @@ proc get(getter: Getter, start: int, endSample: int): seq[int16] =
169
168
let floatSample = channelData[frameIndex]
170
169
let clampedSample = max(- 1.0 , min(1.0 , floatSample))
171
170
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 )
172
192
else :
173
193
error & " Unsupported audio format: { av_get_sample_fmt_name(frame.format)} "
174
194
0 commit comments