Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions av/audio/frame.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ cdef class AudioFrame(Frame):

cdef void _init(self, lib.AVSampleFormat format, lib.AVChannelLayout layout, unsigned int nb_samples, unsigned int align)
cdef void _init_user_attributes(self)
cdef int _nb_planes(self)

cdef AudioFrame alloc_audio_frame()
6 changes: 5 additions & 1 deletion av/audio/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,18 @@ def from_ndarray(array, format="s16", layout="stereo"):
plane.update(array[i, :])
return frame

@cython.cfunc
def _nb_planes(self) -> cython.int:
return self.layout.nb_channels if self.format.is_planar else 1

@property
def planes(self):
"""
A tuple of :class:`~av.audio.plane.AudioPlane`.

:type: tuple
"""
nb_planes: cython.int = self.layout.nb_channels if self.format.is_planar else 1
nb_planes: cython.int = self._nb_planes()
plane_count: cython.int = 0
while plane_count < nb_planes and self.ptr.extended_data[plane_count]:
plane_count += 1
Expand Down
4 changes: 1 addition & 3 deletions av/audio/plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
@cython.cclass
class AudioPlane(Plane):
def __cinit__(self, frame: AudioFrame, index: cython.int):
nb_planes: cython.int = (
frame.layout.nb_channels if frame.format.is_planar else 1
)
nb_planes: cython.int = frame._nb_planes()
if index < 0 or index >= nb_planes:
raise ValueError(f"plane index {index} out of range for {nb_planes} planes")

Expand Down
11 changes: 11 additions & 0 deletions tests/test_audioframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ def test_manual_fltp_7_1_constructor() -> None:
assertNdarraysEqual(frame.to_ndarray(), array)


def test_manual_fltp_22_2_constructor() -> None:
# More than 8 planes: extended_data is a separate heap array of exactly
# nb_channels pointers, so reading past it would be out of bounds.
frame = AudioFrame(format="fltp", layout="22.2", samples=160)
assert len(frame.planes) == 24

array = np.arange(24 * 160, dtype="f4").reshape(24, 160)
frame = AudioFrame.from_ndarray(array, format="fltp", layout="22.2")
assertNdarraysEqual(frame.to_ndarray(), array)


def test_manual_s16_mono_constructor() -> None:
frame = AudioFrame(format="s16", layout="mono", samples=160)
assert frame.format.name == "s16"
Expand Down
Loading