sonarch 0.1.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sonarch/__init__.py ADDED
@@ -0,0 +1,45 @@
1
+ """sonarch: the one import you need for audio work.
2
+
3
+ >>> import sonarch as sa
4
+ >>> audio = sa.load("clip.flac")
5
+ >>> audio.sample_rate, audio.channels, audio.duration
6
+ """
7
+
8
+ from importlib.metadata import PackageNotFoundError, version
9
+
10
+ from sonarch.core import (
11
+ Audio,
12
+ AudioValidationError,
13
+ ChannelMismatchError,
14
+ ClippingError,
15
+ DecodeError,
16
+ EncodeError,
17
+ LengthMismatchError,
18
+ SampleRateMismatchError,
19
+ SonarchError,
20
+ mix,
21
+ )
22
+ from sonarch.io import AudioInfo, info, load, save
23
+
24
+ try:
25
+ __version__ = version("sonarch")
26
+ except PackageNotFoundError: # running from a source tree without installing
27
+ __version__ = "0.0.0+unknown"
28
+
29
+ __all__ = [
30
+ "Audio",
31
+ "AudioInfo",
32
+ "AudioValidationError",
33
+ "ChannelMismatchError",
34
+ "ClippingError",
35
+ "DecodeError",
36
+ "EncodeError",
37
+ "LengthMismatchError",
38
+ "SampleRateMismatchError",
39
+ "SonarchError",
40
+ "__version__",
41
+ "info",
42
+ "load",
43
+ "mix",
44
+ "save",
45
+ ]
@@ -0,0 +1,26 @@
1
+ """Core types: the Audio object and the error hierarchy."""
2
+
3
+ from sonarch.core.audio import Audio, mix
4
+ from sonarch.core.errors import (
5
+ AudioValidationError,
6
+ ChannelMismatchError,
7
+ ClippingError,
8
+ DecodeError,
9
+ EncodeError,
10
+ LengthMismatchError,
11
+ SampleRateMismatchError,
12
+ SonarchError,
13
+ )
14
+
15
+ __all__ = [
16
+ "Audio",
17
+ "AudioValidationError",
18
+ "ChannelMismatchError",
19
+ "ClippingError",
20
+ "DecodeError",
21
+ "EncodeError",
22
+ "LengthMismatchError",
23
+ "SampleRateMismatchError",
24
+ "SonarchError",
25
+ "mix",
26
+ ]
sonarch/core/audio.py ADDED
@@ -0,0 +1,288 @@
1
+ """The :class:`Audio` object: a waveform that carries its own metadata."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ from typing import TYPE_CHECKING, Any, Literal
7
+
8
+ import numpy as np
9
+ import torch
10
+
11
+ from sonarch.core.errors import (
12
+ AudioValidationError,
13
+ ChannelMismatchError,
14
+ LengthMismatchError,
15
+ SampleRateMismatchError,
16
+ )
17
+
18
+ if TYPE_CHECKING:
19
+ from numpy.typing import NDArray
20
+
21
+ __all__ = ["Audio", "mix"]
22
+
23
+ _FLOAT_DTYPES = (torch.float16, torch.bfloat16, torch.float32, torch.float64)
24
+
25
+
26
+ def _as_tensor(data: torch.Tensor | NDArray[Any]) -> torch.Tensor:
27
+ """Convert input to a float tensor of shape (channels, samples), or raise."""
28
+ if isinstance(data, np.ndarray):
29
+ if not np.issubdtype(data.dtype, np.floating):
30
+ raise AudioValidationError(
31
+ f"audio data must be floating point in [-1, 1], got dtype {data.dtype}. "
32
+ "Convert integer PCM first, e.g. `data.astype(np.float32) / 32768` for int16."
33
+ )
34
+ tensor = torch.from_numpy(np.ascontiguousarray(data))
35
+ elif isinstance(data, torch.Tensor):
36
+ tensor = data
37
+ if tensor.dtype not in _FLOAT_DTYPES:
38
+ raise AudioValidationError(
39
+ f"audio data must be floating point in [-1, 1], got dtype {tensor.dtype}. "
40
+ "Convert integer PCM first, e.g. `x.float() / 32768` for int16."
41
+ )
42
+ else:
43
+ raise AudioValidationError(
44
+ f"audio data must be a torch.Tensor or numpy.ndarray, got {type(data).__name__}"
45
+ )
46
+
47
+ if tensor.ndim == 1:
48
+ tensor = tensor.unsqueeze(0)
49
+ elif tensor.ndim != 2:
50
+ raise AudioValidationError(
51
+ f"audio data must have shape (samples,) or (channels, samples), "
52
+ f"got {tuple(tensor.shape)}"
53
+ )
54
+
55
+ channels, samples = tensor.shape
56
+ if channels == 0:
57
+ raise AudioValidationError("audio must have at least one channel")
58
+ if samples > 0 and channels > samples:
59
+ raise AudioValidationError(
60
+ f"got shape {tuple(tensor.shape)}, which looks like (samples, channels). "
61
+ "sonarch uses (channels, samples): pass `data.T` instead."
62
+ )
63
+ return tensor
64
+
65
+
66
+ def _check_sample_rate(sample_rate: object) -> int:
67
+ if isinstance(sample_rate, bool) or not isinstance(sample_rate, (int, np.integer)):
68
+ raise AudioValidationError(
69
+ f"sample_rate must be an integer number of Hz, got {sample_rate!r}"
70
+ )
71
+ rate = int(sample_rate)
72
+ if rate <= 0:
73
+ raise AudioValidationError(f"sample_rate must be positive, got {rate}")
74
+ return rate
75
+
76
+
77
+ class Audio:
78
+ """A waveform plus the metadata needed to use it correctly.
79
+
80
+ The waveform is always a floating-point tensor of shape
81
+ ``(channels, samples)`` with values nominally in ``[-1, 1]``. Operations
82
+ that combine audio check sample rate, channels and length, and raise
83
+ instead of silently producing wrong results.
84
+
85
+ Args:
86
+ data: Waveform as a ``torch.Tensor`` or ``numpy.ndarray``, shape
87
+ ``(samples,)`` or ``(channels, samples)``. NumPy input is wrapped
88
+ without copying when possible.
89
+ sample_rate: Sample rate in Hz.
90
+
91
+ Example:
92
+ >>> audio = Audio(torch.zeros(2, 16_000), sample_rate=16_000)
93
+ >>> audio.duration
94
+ 1.0
95
+ """
96
+
97
+ __slots__ = ("_sample_rate", "_tensor")
98
+
99
+ def __init__(self, data: torch.Tensor | NDArray[Any], sample_rate: int) -> None:
100
+ self._tensor = _as_tensor(data)
101
+ self._sample_rate = _check_sample_rate(sample_rate)
102
+
103
+ # ------------------------------------------------------------------ metadata
104
+
105
+ @property
106
+ def tensor(self) -> torch.Tensor:
107
+ """The waveform, shape ``(channels, samples)``."""
108
+ return self._tensor
109
+
110
+ @property
111
+ def sample_rate(self) -> int:
112
+ """Sample rate in Hz."""
113
+ return self._sample_rate
114
+
115
+ @property
116
+ def channels(self) -> int:
117
+ """Number of channels."""
118
+ return int(self._tensor.shape[0])
119
+
120
+ @property
121
+ def num_samples(self) -> int:
122
+ """Number of samples per channel."""
123
+ return int(self._tensor.shape[1])
124
+
125
+ @property
126
+ def duration(self) -> float:
127
+ """Duration in seconds."""
128
+ return self.num_samples / self._sample_rate
129
+
130
+ @property
131
+ def shape(self) -> tuple[int, int]:
132
+ """Shape of the waveform, ``(channels, samples)``."""
133
+ return (self.channels, self.num_samples)
134
+
135
+ @property
136
+ def device(self) -> torch.device:
137
+ """Device the waveform lives on."""
138
+ return self._tensor.device
139
+
140
+ @property
141
+ def dtype(self) -> torch.dtype:
142
+ """Floating-point dtype of the waveform."""
143
+ return self._tensor.dtype
144
+
145
+ # --------------------------------------------------------------- conversion
146
+
147
+ def replace(
148
+ self,
149
+ *,
150
+ tensor: torch.Tensor | NDArray[Any] | None = None,
151
+ sample_rate: int | None = None,
152
+ ) -> Audio:
153
+ """Return a new ``Audio`` with some fields replaced.
154
+
155
+ Only use ``sample_rate=`` when relabelling audio whose rate was wrong;
156
+ to change the rate of real audio, resample it.
157
+ """
158
+ return Audio(
159
+ self._tensor if tensor is None else tensor,
160
+ self._sample_rate if sample_rate is None else sample_rate,
161
+ )
162
+
163
+ def to(
164
+ self,
165
+ device: str | torch.device | None = None,
166
+ dtype: torch.dtype | None = None,
167
+ ) -> Audio:
168
+ """Move to a device and/or cast to another floating-point dtype."""
169
+ if dtype is not None and dtype not in _FLOAT_DTYPES:
170
+ raise AudioValidationError(f"dtype must be a floating-point dtype, got {dtype}")
171
+ return self.replace(tensor=self._tensor.to(device=device, dtype=dtype))
172
+
173
+ def cpu(self) -> Audio:
174
+ """Shortcut for ``audio.to("cpu")``."""
175
+ return self.to("cpu")
176
+
177
+ def numpy(self) -> NDArray[Any]:
178
+ """Return the waveform as a NumPy array of shape ``(channels, samples)``.
179
+
180
+ bfloat16 audio is returned as float32, since NumPy has no bfloat16.
181
+ """
182
+ tensor = self._tensor.detach().cpu()
183
+ if tensor.dtype == torch.bfloat16:
184
+ tensor = tensor.float()
185
+ return tensor.numpy()
186
+
187
+ def to_mono(self) -> Audio:
188
+ """Average all channels into one. Mono audio is returned unchanged."""
189
+ if self.channels == 1:
190
+ return self
191
+ return self.replace(tensor=self._tensor.mean(dim=0, keepdim=True))
192
+
193
+ def to_stereo(self) -> Audio:
194
+ """Duplicate mono audio into two channels. Stereo audio is returned unchanged."""
195
+ if self.channels == 2:
196
+ return self
197
+ if self.channels != 1:
198
+ raise ChannelMismatchError(
199
+ 1, self.channels, hint="to_stereo() only converts mono audio; downmix first"
200
+ )
201
+ return self.replace(tensor=self._tensor.expand(2, -1).clone())
202
+
203
+ def save(self, path: str | os.PathLike[str], **kwargs: Any) -> None:
204
+ """Write to a file. See :func:`sonarch.save` for options."""
205
+ from sonarch.io import save
206
+
207
+ save(self, path, **kwargs)
208
+
209
+ # ------------------------------------------------------------------ dunders
210
+
211
+ def __len__(self) -> int:
212
+ return self.num_samples
213
+
214
+ def __repr__(self) -> str:
215
+ device = "" if self.device.type == "cpu" else f", device={self.device}"
216
+ return (
217
+ f"Audio(channels={self.channels}, sample_rate={self._sample_rate}, "
218
+ f"duration={self.duration:.3f}s, dtype={str(self.dtype).removeprefix('torch.')}"
219
+ f"{device})"
220
+ )
221
+
222
+ def __add__(self, other: object) -> Audio:
223
+ if not isinstance(other, Audio):
224
+ return NotImplemented
225
+ _check_compatible(self, other, check_length=True)
226
+ return self.replace(tensor=self._tensor + other._tensor)
227
+
228
+ __hash__ = None # type: ignore[assignment] # mutable tensor inside: not hashable
229
+
230
+
231
+ def _check_compatible(a: Audio, b: Audio, *, check_length: bool) -> None:
232
+ if a.sample_rate != b.sample_rate:
233
+ raise SampleRateMismatchError(
234
+ a.sample_rate, b.sample_rate, hint="Resample one clip to match the other"
235
+ )
236
+ if a.channels != b.channels:
237
+ raise ChannelMismatchError(
238
+ a.channels, b.channels, hint="Use to_mono() or to_stereo() to match channels"
239
+ )
240
+ if check_length and a.num_samples != b.num_samples:
241
+ raise LengthMismatchError(
242
+ a.num_samples,
243
+ b.num_samples,
244
+ hint="Use sa.mix(..., align='pad') or align='crop' to combine clips of "
245
+ "different lengths",
246
+ )
247
+
248
+
249
+ def mix(
250
+ *clips: Audio,
251
+ align: Literal["error", "pad", "crop"] = "error",
252
+ weights: list[float] | None = None,
253
+ ) -> Audio:
254
+ """Sum several clips into one.
255
+
256
+ All clips must share sample rate, channel count and device.
257
+
258
+ Args:
259
+ *clips: Two or more ``Audio`` objects.
260
+ align: What to do when lengths differ. ``"error"`` (default) raises,
261
+ ``"pad"`` zero-pads shorter clips to the longest, ``"crop"`` cuts
262
+ every clip to the shortest.
263
+ weights: Optional gain for each clip, e.g. ``[1.0, 0.3]``.
264
+
265
+ Note:
266
+ The result is a plain sum, so it can exceed ``[-1, 1]``. Normalize
267
+ afterwards if needed.
268
+ """
269
+ if len(clips) < 2:
270
+ raise ValueError(f"mix() needs at least two clips, got {len(clips)}")
271
+ if align not in ("error", "pad", "crop"):
272
+ raise ValueError(f"align must be 'error', 'pad' or 'crop', got {align!r}")
273
+ if weights is not None and len(weights) != len(clips):
274
+ raise ValueError(f"got {len(weights)} weights for {len(clips)} clips")
275
+
276
+ first = clips[0]
277
+ for clip in clips[1:]:
278
+ _check_compatible(first, clip, check_length=align == "error")
279
+
280
+ lengths = [clip.num_samples for clip in clips]
281
+ target = max(lengths) if align == "pad" else min(lengths)
282
+ gains = weights if weights is not None else [1.0] * len(clips)
283
+
284
+ out = torch.zeros((first.channels, target), dtype=first.dtype, device=first.device)
285
+ for clip, gain in zip(clips, gains, strict=True):
286
+ n = min(clip.num_samples, target)
287
+ out[:, :n] += gain * clip.tensor[:, :n]
288
+ return first.replace(tensor=out)
sonarch/core/errors.py ADDED
@@ -0,0 +1,76 @@
1
+ """Exception hierarchy for sonarch.
2
+
3
+ Every error sonarch raises on purpose derives from :class:`SonarchError`, so
4
+ callers can catch everything from this library with a single ``except``.
5
+ Validation errors also derive from :class:`ValueError` so generic handlers
6
+ keep working.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ __all__ = [
12
+ "AudioValidationError",
13
+ "ChannelMismatchError",
14
+ "ClippingError",
15
+ "DecodeError",
16
+ "EncodeError",
17
+ "LengthMismatchError",
18
+ "SampleRateMismatchError",
19
+ "SonarchError",
20
+ ]
21
+
22
+
23
+ class SonarchError(Exception):
24
+ """Base class for all errors raised by sonarch."""
25
+
26
+
27
+ class AudioValidationError(SonarchError, ValueError):
28
+ """Audio data or metadata is invalid (bad shape, dtype, sample rate...)."""
29
+
30
+
31
+ class SampleRateMismatchError(AudioValidationError):
32
+ """Two pieces of audio, or audio and a consumer, disagree on sample rate."""
33
+
34
+ def __init__(self, expected: int, got: int, hint: str | None = None) -> None:
35
+ self.expected = expected
36
+ self.got = got
37
+ message = f"sample rate mismatch: expected {expected} Hz, got {got} Hz"
38
+ if hint:
39
+ message = f"{message}. {hint}"
40
+ super().__init__(message)
41
+
42
+
43
+ class ChannelMismatchError(AudioValidationError):
44
+ """Two pieces of audio disagree on channel count."""
45
+
46
+ def __init__(self, expected: int, got: int, hint: str | None = None) -> None:
47
+ self.expected = expected
48
+ self.got = got
49
+ message = f"channel mismatch: expected {expected} channel(s), got {got}"
50
+ if hint:
51
+ message = f"{message}. {hint}"
52
+ super().__init__(message)
53
+
54
+
55
+ class LengthMismatchError(AudioValidationError):
56
+ """Two pieces of audio disagree on length (in samples)."""
57
+
58
+ def __init__(self, expected: int, got: int, hint: str | None = None) -> None:
59
+ self.expected = expected
60
+ self.got = got
61
+ message = f"length mismatch: expected {expected} samples, got {got}"
62
+ if hint:
63
+ message = f"{message}. {hint}"
64
+ super().__init__(message)
65
+
66
+
67
+ class ClippingError(AudioValidationError):
68
+ """Audio exceeds [-1, 1] and would be clipped by an integer encoding."""
69
+
70
+
71
+ class DecodeError(SonarchError):
72
+ """A file or stream could not be decoded as audio."""
73
+
74
+
75
+ class EncodeError(SonarchError):
76
+ """Audio could not be encoded or written."""
sonarch/io/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ """Loading, saving and probing audio files."""
2
+
3
+ from sonarch.io.info import AudioInfo, info
4
+ from sonarch.io.load import load
5
+ from sonarch.io.save import save
6
+
7
+ __all__ = ["AudioInfo", "info", "load", "save"]
sonarch/io/_source.py ADDED
@@ -0,0 +1,53 @@
1
+ """Opening audio sources with PyAV, with sonarch's error handling."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ from pathlib import Path
7
+ from typing import IO, Any, Union, cast
8
+
9
+ import av
10
+ import av.container
11
+
12
+ from sonarch.core.errors import DecodeError
13
+
14
+ Source = Union[str, "os.PathLike[str]", IO[bytes]]
15
+
16
+
17
+ def describe(source: Source) -> str:
18
+ if isinstance(source, (str, os.PathLike)):
19
+ return os.fspath(source)
20
+ return getattr(source, "name", None) or type(source).__name__
21
+
22
+
23
+ def _is_url(path: str) -> bool:
24
+ return "://" in path
25
+
26
+
27
+ def open_input(source: Source) -> av.container.InputContainer:
28
+ """Open a path, URL or binary file object for reading.
29
+
30
+ Raises:
31
+ FileNotFoundError: A local path does not exist.
32
+ DecodeError: FFmpeg cannot open or understand the input.
33
+ """
34
+ target: Any
35
+ if isinstance(source, (str, os.PathLike)):
36
+ target = os.fspath(source)
37
+ if not _is_url(target) and not Path(target).exists():
38
+ raise FileNotFoundError(f"no such file: {target!r}")
39
+ else:
40
+ target = source
41
+
42
+ try:
43
+ return cast(av.container.InputContainer, av.open(target, mode="r"))
44
+ except av.error.FFmpegError as exc:
45
+ raise DecodeError(f"could not open {describe(source)!r} as audio: {exc}") from exc
46
+
47
+
48
+ def first_audio_stream(
49
+ container: av.container.InputContainer, source: Source
50
+ ) -> av.audio.stream.AudioStream:
51
+ if not container.streams.audio:
52
+ raise DecodeError(f"{describe(source)!r} has no audio stream")
53
+ return container.streams.audio[0]
sonarch/io/info.py ADDED
@@ -0,0 +1,68 @@
1
+ """Fast metadata probing without decoding."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ import av
8
+
9
+ from sonarch.io._source import Source, first_audio_stream, open_input
10
+
11
+ __all__ = ["AudioInfo", "info"]
12
+
13
+
14
+ @dataclass(frozen=True)
15
+ class AudioInfo:
16
+ """Metadata about an audio file, read from its headers.
17
+
18
+ Attributes:
19
+ sample_rate: Sample rate in Hz.
20
+ channels: Number of channels.
21
+ duration: Duration in seconds, or ``None`` if the container does not
22
+ say. For compressed formats (MP3, AAC...) this is an estimate from
23
+ the headers and can differ slightly from the decoded length.
24
+ num_samples: ``duration * sample_rate`` rounded, or ``None``.
25
+ codec: Codec name, e.g. ``"pcm_s16le"``, ``"flac"``, ``"mp3"``.
26
+ format: Container format, e.g. ``"wav"``, ``"flac"``, ``"mp3"``.
27
+ bit_rate: Bit rate in bits/s, or ``None`` if unknown.
28
+ """
29
+
30
+ sample_rate: int
31
+ channels: int
32
+ duration: float | None
33
+ num_samples: int | None
34
+ codec: str
35
+ format: str
36
+ bit_rate: int | None
37
+
38
+
39
+ def info(source: Source) -> AudioInfo:
40
+ """Read metadata from an audio file without decoding it.
41
+
42
+ Args:
43
+ source: Local path, URL, or binary file object.
44
+
45
+ Example:
46
+ >>> sa.info("clip.flac")
47
+ AudioInfo(sample_rate=44100, channels=2, duration=12.4, ...)
48
+ """
49
+ with open_input(source) as container:
50
+ stream = first_audio_stream(container, source)
51
+ codec = stream.codec_context
52
+ sample_rate = int(codec.sample_rate)
53
+
54
+ duration: float | None = None
55
+ if stream.duration is not None and stream.time_base is not None:
56
+ duration = float(stream.duration * stream.time_base)
57
+ elif container.duration is not None:
58
+ duration = container.duration / av.time_base
59
+
60
+ return AudioInfo(
61
+ sample_rate=sample_rate,
62
+ channels=int(codec.channels),
63
+ duration=duration,
64
+ num_samples=None if duration is None else round(duration * sample_rate),
65
+ codec=codec.name,
66
+ format=container.format.name.split(",")[0],
67
+ bit_rate=(codec.bit_rate or container.bit_rate) or None,
68
+ )
sonarch/io/load.py ADDED
@@ -0,0 +1,161 @@
1
+ """Decoding audio files into :class:`~sonarch.Audio` objects."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import math
6
+ from typing import TYPE_CHECKING, Any
7
+
8
+ import av
9
+ import numpy as np
10
+ import torch
11
+
12
+ from sonarch.core.audio import Audio
13
+ from sonarch.core.errors import DecodeError
14
+ from sonarch.io._source import Source, describe, first_audio_stream, open_input
15
+
16
+ if TYPE_CHECKING:
17
+ from numpy.typing import NDArray
18
+
19
+ __all__ = ["load"]
20
+
21
+
22
+ def load(
23
+ source: Source,
24
+ *,
25
+ offset: float = 0.0,
26
+ duration: float | None = None,
27
+ ) -> Audio:
28
+ """Decode an audio file into an :class:`~sonarch.Audio` object.
29
+
30
+ Audio keeps its original sample rate and channels; nothing is converted
31
+ behind your back. Use ``.resample()`` / ``.to_mono()`` explicitly.
32
+
33
+ Args:
34
+ source: Local path, URL (``http(s)://``...), or binary file object.
35
+ Any format FFmpeg can decode works, including the audio track of
36
+ video files.
37
+ offset: Start reading this many seconds into the file.
38
+ duration: Read at most this many seconds. ``None`` reads to the end.
39
+
40
+ Returns:
41
+ Float32 ``Audio`` with shape ``(channels, samples)``.
42
+
43
+ Raises:
44
+ FileNotFoundError: A local path does not exist.
45
+ DecodeError: The input is not decodable audio.
46
+ ValueError: ``offset`` / ``duration`` are invalid, or ``offset`` is
47
+ past the end of the file.
48
+
49
+ Example:
50
+ >>> audio = sa.load("speech.mp3", offset=1.5, duration=3.0)
51
+ """
52
+ if not math.isfinite(offset) or offset < 0:
53
+ raise ValueError(f"offset must be a finite number >= 0, got {offset}")
54
+ if duration is not None and (not math.isfinite(duration) or duration <= 0):
55
+ raise ValueError(f"duration must be a finite number > 0 or None, got {duration}")
56
+
57
+ with open_input(source) as container:
58
+ stream = first_audio_stream(container, source)
59
+ sample_rate = int(stream.codec_context.sample_rate)
60
+ if sample_rate <= 0:
61
+ raise DecodeError(f"{describe(source)!r} reports an invalid sample rate")
62
+
63
+ start = round(offset * sample_rate)
64
+ stop = None if duration is None else start + round(duration * sample_rate)
65
+ # Lossy decoders (MP3, AAC, Opus...) produce wrong samples for a short
66
+ # while after a seek, so seek earlier and discard the warm-up audio.
67
+ preroll = 0 if _is_lossless(stream.codec_context.name) else sample_rate // 2
68
+ seek_to = max(start - preroll, 0)
69
+
70
+ try:
71
+ first, data = _decode(container, stream, sample_rate, seek_to, stop)
72
+ except av.error.FFmpegError as exc:
73
+ raise DecodeError(f"failed to decode {describe(source)!r}: {exc}") from exc
74
+
75
+ lo = max(start - first, 0)
76
+ hi = data.shape[1] if stop is None else max(stop - first, 0)
77
+ if start > 0 and lo >= data.shape[1]:
78
+ total = (first + data.shape[1]) / sample_rate
79
+ raise ValueError(
80
+ f"offset {offset}s is past the end of {describe(source)!r} ({total:.3f}s long)"
81
+ )
82
+ return Audio(torch.from_numpy(np.ascontiguousarray(data[:, lo:hi])), sample_rate)
83
+
84
+
85
+ _LOSSLESS_CODECS = frozenset({"flac", "alac", "wavpack", "tta", "ape", "mlp", "truehd"})
86
+
87
+
88
+ def _is_lossless(codec_name: str) -> bool:
89
+ return codec_name.startswith("pcm_") or codec_name in _LOSSLESS_CODECS
90
+
91
+
92
+ def _decode(
93
+ container: av.container.InputContainer,
94
+ stream: av.audio.stream.AudioStream,
95
+ sample_rate: int,
96
+ start: int,
97
+ stop: int | None,
98
+ ) -> tuple[int, NDArray[Any]]:
99
+ """Decode samples covering ``[start, stop)``.
100
+
101
+ Returns the index of the first decoded sample and a float32 array of shape
102
+ ``(channels, n)`` that begins at that index.
103
+ """
104
+ seeked = start > 0 and _seek(container, stream, start / sample_rate)
105
+ resampler = av.AudioResampler(format="fltp") # keep rate & layout, convert to float32
106
+ stream_start = stream.start_time or 0
107
+
108
+ chunks: list[NDArray[Any]] = []
109
+ first: int | None = None
110
+ position = 0
111
+ for frame in container.decode(stream):
112
+ if first is None:
113
+ if seeked and frame.pts is None:
114
+ # Can't tell where the seek landed; restart from the beginning.
115
+ return _decode_from_start(container, stream, sample_rate, start, stop)
116
+ first = (
117
+ round(float((frame.pts - stream_start) * frame.time_base) * sample_rate)
118
+ if seeked and frame.pts is not None
119
+ else 0
120
+ )
121
+ first = max(first, 0)
122
+ position = first
123
+ for out in resampler.resample(frame):
124
+ chunks.append(out.to_ndarray())
125
+ position += frame.samples
126
+ if stop is not None and position >= stop:
127
+ break
128
+ for out in resampler.resample(None):
129
+ chunks.append(out.to_ndarray())
130
+
131
+ channels = stream.codec_context.channels or 1
132
+ if not chunks:
133
+ return (first or 0), np.zeros((channels, 0), dtype=np.float32)
134
+ return (first or 0), np.concatenate(chunks, axis=1)
135
+
136
+
137
+ def _decode_from_start(
138
+ container: av.container.InputContainer,
139
+ stream: av.audio.stream.AudioStream,
140
+ sample_rate: int,
141
+ start: int,
142
+ stop: int | None,
143
+ ) -> tuple[int, NDArray[Any]]:
144
+ container.seek(0, stream=stream)
145
+ return _decode(container, stream, sample_rate, 0, stop)
146
+
147
+
148
+ def _seek(
149
+ container: av.container.InputContainer,
150
+ stream: av.audio.stream.AudioStream,
151
+ seconds: float,
152
+ ) -> bool:
153
+ """Seek to (at or before) ``seconds``. Returns False if the input can't seek."""
154
+ if stream.time_base is None:
155
+ return False
156
+ target = int(seconds / stream.time_base) + (stream.start_time or 0)
157
+ try:
158
+ container.seek(target, stream=stream, backward=True, any_frame=False)
159
+ except av.error.FFmpegError:
160
+ return False # nothing decoded yet, so we're still at the start
161
+ return True
sonarch/io/save.py ADDED
@@ -0,0 +1,168 @@
1
+ """Encoding :class:`~sonarch.Audio` objects to files."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import re
7
+ from pathlib import Path
8
+ from typing import Literal
9
+
10
+ import av
11
+ import numpy as np
12
+ import torch
13
+
14
+ from sonarch.core.audio import Audio
15
+ from sonarch.core.errors import AudioValidationError, ClippingError, EncodeError
16
+
17
+ __all__ = ["save"]
18
+
19
+ Encoding = Literal["pcm16", "pcm24", "pcm32", "float32"]
20
+
21
+ # container format, {encoding: (codec, sample_fmt)}
22
+ _WAV = {
23
+ "pcm16": ("pcm_s16le", "s16"),
24
+ "pcm24": ("pcm_s24le", "s32"),
25
+ "pcm32": ("pcm_s32le", "s32"),
26
+ "float32": ("pcm_f32le", "flt"),
27
+ }
28
+ _FLAC = {
29
+ "pcm16": ("flac", "s16"),
30
+ "pcm24": ("flac", "s32"),
31
+ }
32
+ _OPUS_RATES = (8_000, 12_000, 16_000, 24_000, 48_000)
33
+
34
+
35
+ def save(
36
+ audio: Audio,
37
+ path: str | os.PathLike[str],
38
+ *,
39
+ format: Literal["wav", "flac", "opus"] | None = None,
40
+ encoding: Encoding | None = None,
41
+ bitrate: str | int | None = None,
42
+ clip: bool = False,
43
+ ) -> None:
44
+ """Write audio to a file.
45
+
46
+ Args:
47
+ audio: The audio to write.
48
+ path: Destination path. The format is inferred from the extension
49
+ (``.wav``, ``.flac``, ``.opus``) unless ``format`` is given.
50
+ format: ``"wav"``, ``"flac"`` or ``"opus"``.
51
+ encoding: Sample encoding for WAV (``"pcm16"`` default, ``"pcm24"``,
52
+ ``"pcm32"``, ``"float32"``) or FLAC (``"pcm16"`` default,
53
+ ``"pcm24"``). Not used for Opus.
54
+ bitrate: Opus bitrate, e.g. ``"64kbps"`` or ``64000``. Default 64 kbps.
55
+ clip: Integer encodings and Opus can't store values outside
56
+ ``[-1, 1]``. By default sonarch raises :class:`ClippingError`
57
+ instead of silently clipping; pass ``clip=True`` to allow it.
58
+
59
+ Raises:
60
+ ClippingError: Audio exceeds ``[-1, 1]`` and ``clip`` is False.
61
+ SampleRateMismatchError: Opus was requested at an unsupported rate.
62
+ EncodeError: FFmpeg failed to write the file.
63
+ """
64
+ if not isinstance(audio, Audio):
65
+ raise TypeError(f"save() expects an Audio object, got {type(audio).__name__}")
66
+
67
+ path = Path(path)
68
+ fmt = format or _format_from_suffix(path)
69
+ codec_name, sample_fmt, options = _codec_for(fmt, encoding, bitrate, audio.sample_rate)
70
+
71
+ data = audio.tensor.detach().to(device="cpu", dtype=torch.float32)
72
+ if not torch.isfinite(data).all():
73
+ raise AudioValidationError("audio contains NaN or infinite values; refusing to write")
74
+ peak = float(data.abs().max()) if data.numel() else 0.0
75
+ if peak > 1.0 and not (fmt == "wav" and encoding == "float32"):
76
+ if not clip:
77
+ raise ClippingError(
78
+ f"audio peak is {peak:.3f}, outside [-1, 1]; {fmt} "
79
+ f"({encoding or 'default encoding'}) would clip it. Normalize first, "
80
+ "save with encoding='float32' (WAV), or pass clip=True to clip on purpose."
81
+ )
82
+ data = data.clamp(-1.0, 1.0)
83
+
84
+ layout = {1: "mono", 2: "stereo"}.get(audio.channels, f"{audio.channels}c")
85
+ array = np.ascontiguousarray(data.numpy())
86
+
87
+ try:
88
+ with av.open(str(path), mode="w", format=_CONTAINERS[fmt]) as container:
89
+ stream = container.add_stream(
90
+ codec_name, rate=audio.sample_rate, options=options, layout=layout
91
+ )
92
+ stream.format = sample_fmt
93
+ container.start_encoding() # write the header even if there are no samples
94
+
95
+ if array.shape[1] > 0:
96
+ frame = av.AudioFrame.from_ndarray(array, format="fltp", layout=layout)
97
+ frame.sample_rate = audio.sample_rate
98
+ frame.pts = 0
99
+ for packet in stream.encode(frame):
100
+ container.mux(packet)
101
+ for packet in stream.encode(None):
102
+ container.mux(packet)
103
+ except av.error.FFmpegError as exc:
104
+ raise EncodeError(f"failed to write {str(path)!r}: {exc}") from exc
105
+
106
+
107
+ _CONTAINERS = {"wav": "wav", "flac": "flac", "opus": "ogg"}
108
+
109
+
110
+ def _format_from_suffix(path: Path) -> Literal["wav", "flac", "opus"]:
111
+ suffix = path.suffix.lower()
112
+ if suffix in (".wav", ".wave"):
113
+ return "wav"
114
+ if suffix == ".flac":
115
+ return "flac"
116
+ if suffix in (".opus", ".ogg"):
117
+ return "opus"
118
+ raise ValueError(
119
+ f"can't infer the format from {path.name!r}; use a .wav, .flac or .opus "
120
+ "extension or pass format=..."
121
+ )
122
+
123
+
124
+ def _codec_for(
125
+ fmt: str,
126
+ encoding: Encoding | None,
127
+ bitrate: str | int | None,
128
+ sample_rate: int,
129
+ ) -> tuple[str, str, dict[str, str]]:
130
+ if fmt == "opus":
131
+ if encoding is not None:
132
+ raise ValueError("encoding= is not used for Opus; set bitrate= instead")
133
+ if sample_rate not in _OPUS_RATES:
134
+ from sonarch.core.errors import SampleRateMismatchError
135
+
136
+ raise SampleRateMismatchError(
137
+ 48_000,
138
+ sample_rate,
139
+ hint=f"Opus supports {', '.join(map(str, _OPUS_RATES))} Hz; resample first",
140
+ )
141
+ return "libopus", "flt", {"b": str(_parse_bitrate(bitrate))}
142
+
143
+ if bitrate is not None:
144
+ raise ValueError(f"bitrate= only applies to Opus, not {fmt}")
145
+ table = {"wav": _WAV, "flac": _FLAC}.get(fmt)
146
+ if table is None:
147
+ raise ValueError(f"unsupported format {fmt!r}; choose 'wav', 'flac' or 'opus'")
148
+ key = encoding or "pcm16"
149
+ if key not in table:
150
+ raise ValueError(f"{fmt} supports encodings {sorted(table)}, got {key!r}")
151
+ codec_name, sample_fmt = table[key]
152
+ return codec_name, sample_fmt, {}
153
+
154
+
155
+ def _parse_bitrate(bitrate: str | int | None) -> int:
156
+ if bitrate is None:
157
+ return 64_000
158
+ if isinstance(bitrate, int):
159
+ value = bitrate
160
+ else:
161
+ match = re.fullmatch(r"\s*(\d+(?:\.\d+)?)\s*(k|kbps|kb/s|bps)?\s*", bitrate.lower())
162
+ if not match:
163
+ raise ValueError(f"can't parse bitrate {bitrate!r}; use e.g. '64kbps' or 64000")
164
+ number, unit = float(match.group(1)), match.group(2)
165
+ value = int(number * 1000) if unit in ("k", "kbps", "kb/s") else int(number)
166
+ if not 6_000 <= value <= 510_000:
167
+ raise ValueError(f"Opus bitrate must be between 6 and 510 kbps, got {value} bps")
168
+ return value
sonarch/py.typed ADDED
File without changes
@@ -0,0 +1,368 @@
1
+ Metadata-Version: 2.5
2
+ Name: sonarch
3
+ Version: 0.1.0.dev0
4
+ Summary: The one import you need for audio work: loading, preprocessing, features, codecs and datasets, ready for ML.
5
+ Project-URL: Homepage, https://github.com/trivedi-aman/sonarch
6
+ Project-URL: Issues, https://github.com/trivedi-aman/sonarch/issues
7
+ Author: Aman Trivedi
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: audio,codec,machine-learning,preprocessing,pytorch,spectrogram
11
+ Classifier: Development Status :: 2 - Pre-Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Analysis
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.10
23
+ Requires-Dist: av>=12.0
24
+ Requires-Dist: numpy>=1.24
25
+ Requires-Dist: torch>=2.1
26
+ Provides-Extra: dev
27
+ Requires-Dist: librosa>=0.10; extra == 'dev'
28
+ Requires-Dist: mypy>=1.11; extra == 'dev'
29
+ Requires-Dist: pytest>=8.0; extra == 'dev'
30
+ Requires-Dist: ruff>=0.6; extra == 'dev'
31
+ Requires-Dist: soundfile>=0.12; extra == 'dev'
32
+ Description-Content-Type: text/markdown
33
+
34
+ # sonarch
35
+
36
+ **The one import you need for audio work: loading, preprocessing, features, codecs and datasets, ready for ML.**
37
+
38
+ ```python
39
+ import sonarch as sa
40
+
41
+ audio = sa.load("anything.mp3") # any format, any source; no system FFmpeg needed
42
+ audio = audio.resample(16_000).to_mono()
43
+ mel = sa.MelSpectrogram(n_mels=80)(audio) # CPU or GPU, matches librosa within tolerance
44
+ ```
45
+
46
+ > ⚠️ **Status: early development.** This README describes the design target. Every feature section is tagged:
47
+ > ✅ available · 🚧 in progress · 📋 planned. See the [Roadmap](#roadmap).
48
+
49
+ ---
50
+
51
+ ## Why sonarch?
52
+
53
+ Working with audio today means juggling several libraries:
54
+
55
+ - `librosa` for features (CPU-only, slow at scale)
56
+ - `torchaudio` for loading and transforms (loading is being deprecated in favour of `torchcodec`)
57
+ - `audiomentations` / `torch-audiomentations` for augmentation
58
+ - `ffmpeg` / `sox` for format conversion
59
+ - A separate repo for every neural codec (EnCodec, DAC, Mimi, SNAC…)
60
+
61
+ The glue code between them is where bugs live: **silent sample-rate mismatches**, **preprocessing that differs between training and inference**, and **data pipelines stuck on the CPU**.
62
+
63
+ sonarch gives you **one consistent API** for all of it. Where a great tool already exists, sonarch wraps it behind that API instead of reinventing it, so you get one `Audio` object, one style and one install.
64
+
65
+ | Problem | sonarch's answer |
66
+ |---|---|
67
+ | "Which library do I even use to load audio now?" | `sa.load()`: every common format, local or remote, with bundled decoders |
68
+ | Sample-rate / channel mismatch bugs | `Audio` objects carry their own metadata and check it at every step |
69
+ | Train/inference preprocessing drift | Pipelines save to YAML, so you ship the exact same steps with your model |
70
+ | Slow CPU preprocessing | Core ops run on CPU, CUDA or MPS, on single clips or batches |
71
+ | Fragmented neural codecs | One `Codec` interface over EnCodec, DAC, Mimi, SNAC |
72
+ | Huge raw datasets | Pack audio into compressed (FLAC / Opus) shards and stream them |
73
+ | "How much did compression hurt?" | Quality metrics: PESQ, STOI, SI-SDR, mel distance |
74
+
75
+ ---
76
+
77
+ ## Design principles
78
+
79
+ 1. **Easy by default, powerful on demand.** One-liners and presets for common cases; every setting is exposed for advanced users.
80
+ 2. **Metadata travels with data.** Sample rate, channels and loudness are never "assumed".
81
+ 3. **Reproducible.** A saved pipeline reloads with exactly the same configuration. Outputs match across CPU, CUDA and MPS within a tested numerical tolerance.
82
+ 4. **Device-agnostic.** The same code runs on CPU, CUDA or MPS. NumPy arrays and PyTorch tensors are both accepted.
83
+ 5. **Small core, optional extras.** The core needs only PyTorch, NumPy and a bundled decoder. Codecs, augmentation and metrics are opt-in extras.
84
+ 6. **Fail loudly.** Invalid shapes, sample rates or configs raise clear errors, never silent corruption.
85
+ 7. **Wrap first, replace later.** sonarch builds on proven libraries where they exist and only reimplements where it can be clearly faster or more correct.
86
+
87
+ ---
88
+
89
+ ## Installation
90
+
91
+ ```bash
92
+ pip install sonarch # core: load/save, resample, normalize, trim, features
93
+ pip install sonarch[augment] # augmentation ops
94
+ pip install sonarch[codecs] # neural codecs (EnCodec, DAC, Mimi, SNAC)
95
+ pip install sonarch[metrics] # PESQ, STOI, SI-SDR
96
+ pip install sonarch[cloud] # S3 / GCS / HTTP reading via fsspec
97
+ pip install sonarch[all] # everything
98
+ ```
99
+
100
+ Requires Python 3.10+ and PyTorch 2.x. Decoding uses bundled FFmpeg libraries (via PyAV), so **no system FFmpeg install is needed** on Linux, macOS or Windows.
101
+
102
+ ---
103
+
104
+ ## Quickstart
105
+
106
+ ### Load, inspect, convert 🚧
107
+
108
+ ```python
109
+ import sonarch as sa
110
+
111
+ audio = sa.load("clip.flac")
112
+ audio.sample_rate # 44100
113
+ audio.channels # 2
114
+ audio.duration # 12.4 (seconds)
115
+ audio.loudness # -18.2 (LUFS, computed on first access)
116
+
117
+ audio = audio.resample(16_000).to_mono().to("cuda")
118
+ audio.tensor # torch.Tensor, shape (channels, samples)
119
+ audio.numpy() # np.ndarray
120
+
121
+ sa.info("clip.flac") # fast metadata probe, no decoding
122
+ audio.save("clip_16k.wav")
123
+ ```
124
+
125
+ Mismatches fail loudly:
126
+
127
+ ```python
128
+ a, b = sa.load("a.wav"), sa.load("b_48k.wav")
129
+ a + b # ❌ SampleRateMismatchError: 16000 Hz vs 48000 Hz
130
+ sa.mix(a, b.resample(16_000), align="pad") # ✅ explicit, pads the shorter clip
131
+ ```
132
+
133
+ ### Build a pipeline 🚧
134
+
135
+ ```python
136
+ pipe = sa.Pipeline([
137
+ sa.Resample(16_000),
138
+ sa.ToMono(),
139
+ sa.LoudnessNorm(target_lufs=-23),
140
+ sa.TrimSilence(top_db=30),
141
+ sa.FixLength(seconds=10, mode="pad_or_crop"),
142
+ sa.MelSpectrogram(n_mels=80, n_fft=400, hop_length=160),
143
+ ])
144
+
145
+ mel = pipe("clip.wav") # path, Audio, tensor or array
146
+
147
+ pipe.save("preprocess.yaml") # ship this with your model
148
+ pipe = sa.Pipeline.load("preprocess.yaml")
149
+ ```
150
+
151
+ ### Batches on GPU 📋
152
+
153
+ ```python
154
+ pipe = pipe.to("cuda")
155
+ batch = pipe(["a.wav", "b.flac", "c.mp3"])
156
+ batch.tensor # (batch, n_mels, frames), padded to the longest item
157
+ batch.lengths # true length of each item, in frames
158
+ batch.mask # bool mask, True where values are real
159
+ ```
160
+
161
+ If the pipeline ends in `FixLength`, every item has the same length and no padding is added.
162
+
163
+ ### Presets 📋
164
+
165
+ ```python
166
+ pipe = sa.preset("whisper") # 16 kHz mono, 80-bin log-mel, 30 s (Whisper up to large-v2)
167
+ pipe = sa.preset("whisper-v3") # same, but 128 bins (Whisper large-v3)
168
+ features = pipe("speech.mp3")
169
+ ```
170
+
171
+ ---
172
+
173
+ ## Coming from another library?
174
+
175
+ | You used to write | In sonarch |
176
+ |---|---|
177
+ | `librosa.load(path, sr=16000, mono=True)` | `sa.load(path).resample(16_000).to_mono()` |
178
+ | `librosa.feature.melspectrogram(y=y, sr=sr, n_mels=80)` | `sa.MelSpectrogram(n_mels=80)(audio)` |
179
+ | `librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)` | `sa.MFCC(n_mfcc=13)(audio)` |
180
+ | `librosa.effects.trim(y, top_db=30)` | `audio.trim_silence(top_db=30)` |
181
+ | `torchaudio.load(path)` | `sa.load(path)` |
182
+ | `torchaudio.functional.resample(x, 44100, 16000)` | `audio.resample(16_000)` |
183
+ | `torchaudio.compliance.kaldi.fbank(x, ...)` | `sa.Fbank(...)(audio)` |
184
+ | `WhisperFeatureExtractor(...)(x)` | `sa.preset("whisper")(audio)` |
185
+
186
+ Numerical ops are tested against librosa / torchaudio so outputs match within tolerance. Full migration guides: 📋 `docs/migrate-librosa.md`, `docs/migrate-torchaudio.md`.
187
+
188
+ ---
189
+
190
+ ## Features
191
+
192
+ ### 1. Loading & I/O 🚧
193
+ - Decode WAV, FLAC, MP3, OGG/Opus, M4A/AAC, and audio tracks from video files
194
+ - Save to WAV, FLAC and Opus
195
+ - Fast metadata probing without decoding
196
+ - Streaming / chunked reads for long files 📋
197
+ - Read from URLs, S3 and GCS via `fsspec` (`sonarch[cloud]`) 📋
198
+
199
+ ### 2. Preprocessing ops
200
+ | Category | Ops | Status |
201
+ |---|---|---|
202
+ | Format | `Resample`, `ToMono`, `ToStereo`, `ChangeDtype` | 🚧 |
203
+ | Level | `PeakNorm`, `RMSNorm`, `LoudnessNorm` (EBU R128 / LUFS), `DCRemove` | 🚧 |
204
+ | Time | `TrimSilence`, `FixLength`, `Pad`, `Crop`, `Chunk` | 🚧 |
205
+ | Time | `VAD` | 📋 |
206
+ | Filters | `HighPass`, `LowPass`, `BandPass`, `PreEmphasis` | 📋 |
207
+ | Features | `STFT`, `MelSpectrogram`, `LogMel`, `MFCC` | 🚧 |
208
+ | Features | `Fbank` (Kaldi-compatible), `CQT`, `Chroma` | 📋 |
209
+
210
+ ### 3. Neural codecs and tokenization (`sonarch[codecs]`) 📋
211
+ One interface for discrete audio tokens, for audio language models and compression.
212
+
213
+ ```python
214
+ codec = sa.Codec("dac", bitrate="6kbps", device="cuda")
215
+ codec.sample_rate # 44100: the rate the model expects
216
+
217
+ tokens = codec.encode(audio) # (codebooks, frames) int tensor
218
+ recon = codec.decode(tokens) # Audio object
219
+
220
+ codec.encode(audio_16k) # ❌ SampleRateMismatchError: codec expects 44100 Hz
221
+ codec.encode(audio_16k, resample=True) # ✅ explicit
222
+
223
+ sa.Codec.available() # ["encodec", "dac", "mimi", "snac", ...]
224
+ ```
225
+
226
+ > **Model licenses:** sonarch is MIT, but pretrained codec weights come from their original authors and may carry different licenses. `sa.Codec.info("dac")` shows the license for each model; check it before commercial use.
227
+
228
+ ### 4. Datasets: compressed shards and streaming 📋
229
+ Turn large folders of WAV files into compact, streamable shards. Built on WebDataset-style tar shards, so they also work with existing tooling.
230
+
231
+ ```python
232
+ sa.dataset.pack(
233
+ source="raw_wavs/",
234
+ dest="shards/",
235
+ format="flac", # lossless; or "opus" with bitrate="64kbps"
236
+ shard_size="1GB",
237
+ preprocess=pipe, # optionally store preprocessed audio
238
+ )
239
+
240
+ ds = sa.dataset.stream("shards/", pipeline=pipe) # PyTorch IterableDataset
241
+ ```
242
+
243
+ Also reads Hugging Face `datasets` audio columns directly.
244
+
245
+ ### 5. Augmentation (`sonarch[augment]`) 📋
246
+ `AddNoise`, `AddReverb` (RIR), `GainJitter`, `PitchShift`, `TimeStretch`, `SpecAugment`, `Mixup`, `CodecAugment` (simulate MP3/Opus artifacts). All are probabilistic and seedable. Most run batched on GPU. `CodecAugment` and high-quality `PitchShift` / `TimeStretch` run on CPU.
247
+
248
+ ```python
249
+ train_pipe = pipe.with_augment([
250
+ sa.AddNoise(snr_db=(5, 20), p=0.5), # waveform op: placed before MelSpectrogram
251
+ sa.SpecAugment(time_masks=2, freq_masks=2), # spectrogram op: placed after it
252
+ ], seed=42)
253
+ ```
254
+
255
+ Each op declares whether it works on waveforms or spectrograms, and `with_augment` puts it in the right place automatically. `train_pipe.describe()` prints the final order.
256
+
257
+ ### 6. Quality evaluation (`sonarch[metrics]`) 📋
258
+ Know what a compression setting costs *before* you train on it.
259
+
260
+ ```python
261
+ report = sa.evaluate(original, recon, metrics=["pesq", "stoi", "si_sdr", "mel_distance"])
262
+ print(report)
263
+
264
+ sa.compare_codecs(
265
+ "eval_clips/",
266
+ codecs=[("opus", "24kbps"), ("dac", "6kbps"), ("encodec", "3kbps")],
267
+ ) # → table of bitrate vs. quality
268
+ ```
269
+
270
+ PESQ only supports 8 kHz and 16 kHz audio; sonarch resamples for it and says so in the report.
271
+
272
+ ### 7. Feature caching 📋
273
+ Compute features once and store them compactly.
274
+
275
+ ```python
276
+ sa.cache.build(dataset="shards/", pipeline=pipe, dest="mel_cache/", dtype="float16")
277
+ ```
278
+
279
+ ---
280
+
281
+ ## Writing a custom op
282
+
283
+ Every op subclasses `sa.Op` and declares what kind of data it takes and returns. sonarch uses this to validate pipelines and place augmentations.
284
+
285
+ ```python
286
+ class Invert(sa.Op):
287
+ input = "wave" # "wave" or "spec"
288
+ output = "wave"
289
+
290
+ def __call__(self, audio: sa.Audio) -> sa.Audio:
291
+ return audio.replace(tensor=-audio.tensor)
292
+ ```
293
+
294
+ Ops are automatically saved to YAML if their constructor arguments are plain types (numbers, strings, lists, dicts).
295
+
296
+ ---
297
+
298
+ ## Roadmap
299
+
300
+ | Milestone | Scope | Status |
301
+ |---|---|---|
302
+ | **v0.1: Core** | `sa.load` / `save` / `info`, `Audio` object, resample, mono/stereo, normalization, trim, pad/crop, STFT / mel / MFCC matched to librosa, migration guides | 🚧 In progress |
303
+ | **v0.2: ML-ready** | `Pipeline` + YAML, batched GPU execution with padding and masks, `whisper` / `wav2vec2` presets matched to Hugging Face feature extractors, benchmarks vs. librosa / torchaudio | 📋 Planned |
304
+ | **v0.3: Codecs** | Unified `Codec` API: EnCodec, DAC, Mimi, SNAC | 📋 Planned |
305
+ | **v0.4: Data** | `dataset.pack` / `stream`, FLAC/Opus shards, HF `datasets` support, cloud reading | 📋 Planned |
306
+ | **v0.5: Augment & metrics** | Augmentation suite, `evaluate`, `compare_codecs` | 📋 Planned |
307
+ | **v1.0** | Remaining features (filters, VAD, Fbank, CQT, caching), full docs, stable API | 📋 Planned |
308
+
309
+ ---
310
+
311
+ ## Project structure
312
+
313
+ ```
314
+ sonarch/
315
+ ├── src/sonarch/
316
+ │ ├── __init__.py
317
+ │ ├── core/
318
+ │ │ ├── audio.py # Audio object + metadata validation
319
+ │ │ ├── pipeline.py # Pipeline, YAML save/load
320
+ │ │ ├── op.py # Base class for all ops
321
+ │ │ └── errors.py
322
+ │ ├── io/ # load, save, info, decoding backends
323
+ │ ├── ops/
324
+ │ │ ├── format.py # resample, channels, dtype
325
+ │ │ ├── level.py # normalization, loudness
326
+ │ │ ├── time.py # trim, pad/crop, chunk
327
+ │ │ ├── filters.py
328
+ │ │ └── features.py # STFT, mel, MFCC
329
+ │ ├── codecs/ # Codec interface + backends
330
+ │ ├── dataset/ # pack, stream, shards
331
+ │ ├── augment/
332
+ │ ├── metrics/
333
+ │ ├── cache/
334
+ │ └── presets/ # YAML preset definitions
335
+ ├── tests/
336
+ ├── benchmarks/ # speed vs. librosa / torchaudio
337
+ ├── docs/
338
+ ├── examples/
339
+ ├── pyproject.toml
340
+ └── README.md
341
+ ```
342
+
343
+ ---
344
+
345
+ ## Development
346
+
347
+ ```bash
348
+ git clone https://github.com/trivedi-aman/sonarch.git
349
+ cd sonarch
350
+ pip install -e ".[dev,all]"
351
+ pytest
352
+ ```
353
+
354
+ Conventions:
355
+ - Type hints everywhere; `mypy --strict` on `sonarch/core`
356
+ - Formatting and linting with `ruff`
357
+ - Every op needs: a unit test, a CPU/GPU parity test, and a YAML save/load round-trip test
358
+ - Numerical ops are tested against a reference (librosa / torchaudio) within tolerance
359
+
360
+ ---
361
+
362
+ ## Contributing
363
+
364
+ Contributions are welcome. Open an issue to discuss new ops, codecs or presets before sending a large PR.
365
+
366
+ ## License
367
+
368
+ MIT for the sonarch code. Pretrained model weights used by `sonarch[codecs]` keep their original licenses.
@@ -0,0 +1,14 @@
1
+ sonarch/__init__.py,sha256=oiwrs9HNKjkRiBAIvoTpzSdfHPBJq3UKcZAh1JyOE7o,968
2
+ sonarch/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ sonarch/core/__init__.py,sha256=rOO1OCPauiQi0MNGJCJY0pZQ5Lji0pG8-Kbhi3e7tAM,548
4
+ sonarch/core/audio.py,sha256=4klwWIaDSLm81qEGXi5eXKzJ-clD32ipLmzM_xYd_uA,10249
5
+ sonarch/core/errors.py,sha256=LxHIB2Nf0KMQaojy0YHniGl2cKwaEktrS2HXdnpTDtY,2343
6
+ sonarch/io/__init__.py,sha256=yQBOLe_NZ_a48-c657fyWwq3dsPTbIb0diVgfDsFiLA,207
7
+ sonarch/io/_source.py,sha256=Xrq6uiHmIVd0xD_SZV-zlEwsWQd1wI8m80NvA5S5Hco,1560
8
+ sonarch/io/info.py,sha256=QMJnMBerZzvghjbV0xdnbDAr5Wzv6jT295XPkSAp5wQ,2233
9
+ sonarch/io/load.py,sha256=FF15PtpfT2QHzuRCUg9M5S-EJucrRmWlk6Fc5Z4Pbus,5848
10
+ sonarch/io/save.py,sha256=VJojEya6AKEaeBUa7z0oyVd6pevzqxQgth5j9W0NL68,6227
11
+ sonarch-0.1.0.dev0.dist-info/METADATA,sha256=HXKM4hskng2XTX26MKfJC8hE8JXC6ScgTpH2KmIo_w8,14663
12
+ sonarch-0.1.0.dev0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
13
+ sonarch-0.1.0.dev0.dist-info/licenses/LICENSE,sha256=d9CMrNZPd8-E7_fJnCy2BnjqPf0pjpy7OHItMmfb_LE,1069
14
+ sonarch-0.1.0.dev0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aman Trivedi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.