Coverage for src/blob_dict/blob/audio.py: 0%

21 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-03-27 22:31 -0700

1from __future__ import annotations 

2 

3from io import BytesIO 

4from typing import NamedTuple, override 

5 

6import numpy 

7import soundfile 

8 

9from . import BytesBlob 

10 

11 

12class AudioData(NamedTuple): 

13 data: numpy.ndarray 

14 sample_rate: int 

15 

16 

17class AudioBlob(BytesBlob): 

18 def __init__(self, blob: bytes | AudioData) -> None: 

19 if isinstance(blob, AudioData): 

20 bio = BytesIO() 

21 soundfile.write(bio, AudioData.data, AudioData.sample_rate) 

22 blob = bio.getvalue() 

23 

24 super().__init__(blob) 

25 

26 def as_audio(self) -> AudioData: 

27 return AudioData(*soundfile.read(BytesIO(self._blob_bytes))) 

28 

29 @override 

30 def __repr__(self) -> str: 

31 return f"{self.__class__.__name__}({self.as_audio().__repr__()})"