Skip to content

Commit 843ff79

Browse files
committed
Add comment
1 parent 88b335a commit 843ff79

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/torchcodec/_core/ops.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,21 @@ def encode_audio_to_file_like(
176176
if samples.dtype != torch.float32:
177177
raise ValueError(f"samples must have dtype torch.float32, got {samples.dtype}")
178178

179-
samples = samples.contiguous()
179+
# We're having the same problem as with the decoder's create_from_file_like:
180+
# We should be able to pass a tensor directly, but this leads to a pybind
181+
# error. In order to work around this, we pass the pointer to the tensor's
182+
# data, and its shape, in order to re-construct it in C++. For this to work:
183+
# - the tensor must be float32
184+
# - the tensor must be contiguous, which is why we call contiguous().
185+
# In theory we could avoid this restriction by also passing the strides?
186+
# - IMPORTANT: the input samples tensor and its underlying data must be
187+
# alive during the call.
188+
#
189+
# A more elegant solution would be to cast the tensor into a py::object, but
190+
# casting the py::object backk to a tensor in C++ seems to lead to the same
191+
# pybing error.
180192

193+
samples = samples.contiguous()
181194
_pybind_ops.encode_audio_to_file_like(
182195
samples.data_ptr(),
183196
list(samples.shape),

0 commit comments

Comments
 (0)