Skip to content

Commit 62b0598

Browse files
committed
clear
1 parent fc70880 commit 62b0598

File tree

2 files changed

+122
-66
lines changed

2 files changed

+122
-66
lines changed

getstream/video/rtc/track_util.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -850,18 +850,19 @@ def copy(self) -> "PcmData":
850850
channels=self.channels,
851851
)
852852

853-
def truncate(self) -> "PcmData":
854-
"""Wipe all samples in this PcmData object in-place.
853+
def clear(self) -> None:
854+
"""Clear all samples in this PcmData object in-place.
855855
856-
Replaces the samples with an empty array of the appropriate dtype,
857-
preserving all other metadata (sample_rate, format, channels, etc.).
856+
Similar to list.clear() or dict.clear(), this method removes all samples
857+
from the PcmData object while preserving all other metadata (sample_rate,
858+
format, channels, timestamps, etc.).
858859
859-
Returns self for chaining.
860+
Returns None to match the behavior of standard Python collection methods.
860861
861862
Example:
862863
>>> import numpy as np
863864
>>> a = PcmData(sample_rate=16000, format="s16", samples=np.array([1, 2, 3], np.int16), channels=1)
864-
>>> a.truncate()
865+
>>> a.clear()
865866
>>> len(a.samples)
866867
0
867868
>>> a.sample_rate
@@ -875,7 +876,6 @@ def truncate(self) -> "PcmData":
875876
dtype = np.int16
876877

877878
self.samples = np.array([], dtype=dtype)
878-
return self
879879

880880
@classmethod
881881
def from_response(

tests/rtc/test_pcm_data.py

Lines changed: 115 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -453,60 +453,6 @@ def test_copy_append_does_not_modify_original():
453453
assert pcm_a.samples is not pcm_copy.samples
454454

455455

456-
def test_truncate_wipes_samples_preserves_metadata():
457-
"""Test that truncate() wipes samples but preserves all metadata."""
458-
sr = 16000
459-
samples = np.array([1, 2, 3, 4, 5], dtype=np.int16)
460-
pcm = PcmData(
461-
sample_rate=sr,
462-
format="s16",
463-
samples=samples,
464-
channels=1,
465-
pts=1000,
466-
dts=2000,
467-
time_base=0.001,
468-
)
469-
470-
# Store original metadata
471-
original_sr = pcm.sample_rate
472-
original_format = pcm.format
473-
original_channels = pcm.channels
474-
original_pts = pcm.pts
475-
original_dts = pcm.dts
476-
original_time_base = pcm.time_base
477-
478-
# Truncate and verify it returns self
479-
result = pcm.truncate()
480-
assert result is pcm
481-
482-
# Samples should be empty
483-
assert isinstance(pcm.samples, np.ndarray)
484-
assert len(pcm.samples) == 0
485-
assert pcm.samples.dtype == np.int16 # Correct dtype for s16
486-
487-
# Metadata should be preserved
488-
assert pcm.sample_rate == original_sr
489-
assert pcm.format == original_format
490-
assert pcm.channels == original_channels
491-
assert pcm.pts == original_pts
492-
assert pcm.dts == original_dts
493-
assert pcm.time_base == original_time_base
494-
495-
496-
def test_truncate_f32_uses_correct_dtype():
497-
"""Test that truncate() uses float32 dtype for f32 format."""
498-
sr = 16000
499-
samples = np.array([0.1, 0.2, 0.3], dtype=np.float32)
500-
pcm = PcmData(sample_rate=sr, format="f32", samples=samples, channels=1)
501-
502-
pcm.truncate()
503-
504-
# Samples should be empty with float32 dtype
505-
assert isinstance(pcm.samples, np.ndarray)
506-
assert len(pcm.samples) == 0
507-
assert pcm.samples.dtype == np.float32
508-
509-
510456
def test_copy_preserves_all_metadata():
511457
"""Test that copy() preserves all metadata including timestamps."""
512458
sr = 16000
@@ -554,22 +500,132 @@ def test_append_chaining_with_copy():
554500
assert np.array_equal(result.samples, np.array([1, 2, 3, 4, 5, 6], dtype=np.int16))
555501

556502

557-
def test_truncate_after_append():
558-
"""Test that truncate() can be used to clear accumulated samples."""
503+
# ===== Tests for clear() method (like list.clear()) =====
504+
505+
506+
def test_clear_wipes_samples_like_list_clear():
507+
"""Test that clear() works like list.clear() - removes all items, returns None."""
508+
sr = 16000
509+
samples = np.array([1, 2, 3, 4, 5], dtype=np.int16)
510+
pcm = PcmData(sample_rate=sr, format="s16", samples=samples, channels=1)
511+
512+
# clear() should return None, like list.clear()
513+
result = pcm.clear()
514+
assert result is None
515+
516+
# Samples should be empty
517+
assert isinstance(pcm.samples, np.ndarray)
518+
assert len(pcm.samples) == 0
519+
assert pcm.samples.dtype == np.int16
520+
521+
522+
def test_clear_preserves_metadata():
523+
"""Test that clear() preserves all metadata like list.clear() preserves list identity."""
524+
sr = 16000
525+
samples = np.array([1, 2, 3, 4, 5], dtype=np.int16)
526+
pcm = PcmData(
527+
sample_rate=sr,
528+
format="s16",
529+
samples=samples,
530+
channels=2,
531+
pts=1000,
532+
dts=2000,
533+
time_base=0.001,
534+
)
535+
536+
# Store original metadata and id
537+
original_id = id(pcm)
538+
original_sr = pcm.sample_rate
539+
original_format = pcm.format
540+
original_channels = pcm.channels
541+
original_pts = pcm.pts
542+
original_dts = pcm.dts
543+
original_time_base = pcm.time_base
544+
545+
# Clear
546+
pcm.clear()
547+
548+
# Object identity preserved (like list.clear())
549+
assert id(pcm) == original_id
550+
551+
# Samples should be empty
552+
assert len(pcm.samples) == 0
553+
554+
# Metadata should be preserved
555+
assert pcm.sample_rate == original_sr
556+
assert pcm.format == original_format
557+
assert pcm.channels == original_channels
558+
assert pcm.pts == original_pts
559+
assert pcm.dts == original_dts
560+
assert pcm.time_base == original_time_base
561+
562+
563+
def test_clear_f32_uses_correct_dtype():
564+
"""Test that clear() uses float32 dtype for f32 format."""
565+
sr = 16000
566+
samples = np.array([0.1, 0.2, 0.3], dtype=np.float32)
567+
pcm = PcmData(sample_rate=sr, format="f32", samples=samples, channels=1)
568+
569+
pcm.clear()
570+
571+
# Samples should be empty with float32 dtype
572+
assert isinstance(pcm.samples, np.ndarray)
573+
assert len(pcm.samples) == 0
574+
assert pcm.samples.dtype == np.float32
575+
576+
577+
def test_clear_after_append():
578+
"""Test that clear() can be used to clear accumulated samples."""
559579
sr = 16000
560580
a = np.array([1, 2, 3], dtype=np.int16)
561581
b = np.array([4, 5, 6], dtype=np.int16)
562582

563583
pcm_a = PcmData(sample_rate=sr, format="s16", samples=a, channels=1)
564584
pcm_b = PcmData(sample_rate=sr, format="s16", samples=b, channels=1)
565585

566-
# Append then truncate
586+
# Append then clear
567587
pcm_a.append(pcm_b)
568588
assert len(pcm_a.samples) == 6
569589

570-
pcm_a.truncate()
590+
pcm_a.clear()
571591
assert len(pcm_a.samples) == 0
572592

573-
# Can append again after truncate
593+
# Can append again after clear
574594
pcm_a.append(pcm_b)
575595
assert np.array_equal(pcm_a.samples, np.array([4, 5, 6], dtype=np.int16))
596+
597+
598+
def test_clear_returns_none():
599+
"""Test that clear() returns None like list.clear()."""
600+
sr = 16000
601+
samples = np.array([1, 2, 3], dtype=np.int16)
602+
603+
pcm = PcmData(sample_rate=sr, format="s16", samples=samples, channels=1)
604+
result = pcm.clear()
605+
606+
# Should return None like list.clear()
607+
assert result is None
608+
609+
# Samples should be empty
610+
assert len(pcm.samples) == 0
611+
612+
613+
def test_clear_multiple_times():
614+
"""Test that clear() can be called multiple times safely."""
615+
sr = 16000
616+
samples = np.array([1, 2, 3], dtype=np.int16)
617+
pcm = PcmData(sample_rate=sr, format="s16", samples=samples, channels=1)
618+
619+
# Clear multiple times
620+
pcm.clear()
621+
assert len(pcm.samples) == 0
622+
623+
pcm.clear() # Should work on empty PcmData
624+
assert len(pcm.samples) == 0
625+
626+
pcm.clear() # Should work again
627+
assert len(pcm.samples) == 0
628+
629+
# Metadata should still be intact
630+
assert pcm.sample_rate == sr
631+
assert pcm.format == "s16"

0 commit comments

Comments
 (0)