Skip to content

Commit 3617b4a

Browse files
committed
Resample to float format
1 parent ee89754 commit 3617b4a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

auto_editor/subcommands/levels.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,22 @@ def iter_audio(src, tb: Fraction, stream: int = 0) -> Iterator[float]:
9999
fifo = AudioFifo()
100100
try:
101101
container = av.open(src.path, "r")
102-
sample_rate = container.streams.audio[stream].rate
102+
audio_stream = container.streams.audio[stream]
103+
sample_rate = audio_stream.rate
103104

104105
exact_size = (1 / tb) * sample_rate
105106
accumulated_error = 0
106107

108+
# Resample so that audio data is between [-1, 1]
109+
resampler = av.AudioResampler(
110+
av.AudioFormat("flt"), audio_stream.layout, sample_rate
111+
)
112+
107113
for frame in container.decode(audio=stream):
108-
frame.pts = None # Skip check
109-
fifo.write(frame)
114+
frame.pts = None # Skip time checks
115+
116+
for reframe in resampler.resample(frame):
117+
fifo.write(reframe)
110118

111119
while fifo.samples >= math.ceil(exact_size):
112120
size_with_error = exact_size + accumulated_error

0 commit comments

Comments
 (0)