-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.py
More file actions
31 lines (23 loc) · 711 Bytes
/
sound.py
File metadata and controls
31 lines (23 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import annotations
from typing import NamedTuple
import numpy as np
from numpy import ndarray
class Sound(NamedTuple):
data: ndarray
sr: int
@property
def duration(self) -> float:
return self.data.shape[-1] / self.sr
@staticmethod
def equals(a: "Sound", b: "Sound") -> bool:
return a.sr == b.sr and a.data is b.data
@staticmethod
def equals_content(a: "Sound", b: "Sound", approx: bool = False) -> bool:
if Sound.equals(a, b):
return True
if a.sr != b.sr:
return False
if approx:
return np.allclose(a.data, b.data)
else:
return np.array_equal(a.data, b.data)