write_correction function added

pull/14/head
Ewald de Wit 2022-11-22 09:43:09 +01:00
rodzic ddbeb28d79
commit b487b17019
1 zmienionych plików z 12 dodań i 4 usunięć

Wyświetl plik

@ -6,12 +6,14 @@ import numpy.typing as npt
class Sound(NamedTuple): class Sound(NamedTuple):
"""Audio sample- and meta- data."""
data: np.ndarray data: np.ndarray
rate: int rate: int
width: int = 4 width: int = 4
Correction = List[Tuple[float, float]] Correction = List[Tuple[float, float]]
"""List of (frequency, db) tuples."""
def write_wav(path: str, data: npt.ArrayLike, rate: int, width: int = 4): def write_wav(path: str, data: npt.ArrayLike, rate: int, width: int = 4):
@ -51,9 +53,7 @@ def write_wav(path: str, data: npt.ArrayLike, rate: int, width: int = 4):
def read_wav(path: str) -> Sound: def read_wav(path: str) -> Sound:
""" """Read WAV file and return float32 arrays between -1 and 1."""
Read WAV file and return float32 arrays between -1 and 1.
"""
with wave.open(path, 'rb') as wav: with wave.open(path, 'rb') as wav:
ch, width, rate, n, _, _ = wav.getparams() ch, width, rate, n, _, _ = wav.getparams()
frames = wav.readframes(n) frames = wav.readframes(n)
@ -79,12 +79,20 @@ def read_wav(path: str) -> Sound:
return Sound(data, rate, width) return Sound(data, rate, width)
def write_correction(path: str, correction: Correction):
"""Write (frequency, db) tuples to a space-separated file."""
txt = '\n'.join(f'{freq} {db}' for freq, db in correction)
with open(path, 'w') as f:
f.write(txt)
def read_correction(path: str) -> Correction: def read_correction(path: str) -> Correction:
"""Read (frequency, db) tuples from comma- or space-separated file."""
corr = [] corr = []
with open(path, 'r') as f: with open(path, 'r') as f:
for line in f.readlines(): for line in f.readlines():
try: try:
freq, db = line.split() freq, db = line.split(',' if ',' in line else None)
corr.append((float(freq), float(db))) corr.append((float(freq), float(db)))
except ValueError: except ValueError:
pass pass