-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #303 from HazyResearch/feature/audio
Add audio demo
- Loading branch information
Showing
19 changed files
with
896 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from meerkat import env | ||
from meerkat.tools.lazy_loader import LazyLoader | ||
from meerkat.tools.utils import requires | ||
|
||
torch = LazyLoader("torch") | ||
torchaudio = LazyLoader("torchaudio") | ||
|
||
|
||
class Audio: | ||
def __init__( | ||
self, | ||
data, | ||
sampling_rate: int, | ||
bits: int = None, | ||
) -> None: | ||
if not env.is_package_installed("torch"): | ||
raise ValueError( | ||
f"{type(self)} requires torch. Follow these instructions " | ||
"to install torch: https://pytorch.org/get-started/locally/." | ||
) | ||
self.data = torch.as_tensor(data) | ||
self.sampling_rate = sampling_rate | ||
self.bits = bits | ||
|
||
def duration(self) -> float: | ||
"""Return the duration of the audio in seconds.""" | ||
return len(self.data) / self.sampling_rate | ||
|
||
@requires("torchaudio") | ||
def resample(self, sampling_rate: int) -> "Audio": | ||
"""Resample the audio with a new sampling rate. | ||
Args: | ||
sampling_rate: The new sampling rate. | ||
Returns: | ||
The resampled audio. | ||
""" | ||
if not env.is_package_installed("torchaudio"): | ||
raise ValueError( | ||
"resample requires torchaudio. Install with `pip install torchaudio`." | ||
) | ||
|
||
return Audio( | ||
torchaudio.functional.resample( | ||
self.data, self.sampling_rate, sampling_rate | ||
), | ||
sampling_rate, | ||
) | ||
|
||
def normalize( | ||
self, lower: float = 0.0, upper: float = 1.0, eps: float = 1e-6 | ||
) -> "Audio": | ||
"""Normalize the audio to a given range. | ||
Args: | ||
lower: The lower bound of the range. | ||
upper: The upper bound of the range. | ||
eps: The epsilon to used to avoid division by zero. | ||
Returns: | ||
The normalized audio. | ||
""" | ||
_min = torch.amin(self.data) | ||
_max = torch.amax(self.data) | ||
data = lower + (upper - lower) * (self.data - _min) / (_max - _min + eps) | ||
return Audio(data=data, sampling_rate=self.sampling_rate) | ||
|
||
def quantize(self, bits: int, epsilon: float = 1e-2) -> "Audio": | ||
"""Linearly quantize a signal to a given number of bits. | ||
The signal must be in the range [0, 1]. | ||
Args: | ||
bits: The number of bits to quantize to. | ||
epsilon: The epsilon to use for clipping the signal. | ||
Returns: | ||
The quantized audio. | ||
""" | ||
if self.bits is not None: | ||
raise ValueError( | ||
"Audio is already quantized. Use `.dequantize` to dequantize " | ||
"the signal and then requantize." | ||
) | ||
|
||
if torch.any(self.data < 0) or torch.any(self.data > 1): | ||
raise ValueError("Audio must be in the range [0, 1] to quantize.") | ||
|
||
q_levels = 1 << bits | ||
samples = (q_levels - epsilon) * self.data | ||
samples += epsilon / 2 | ||
return Audio(samples.long(), sampling_rate=self.sampling_rate, bits=self.bits) | ||
|
||
def dequantize(self) -> "Audio": | ||
"""Dequantize a signal. | ||
Returns: | ||
The dequantized audio. | ||
""" | ||
if self.bits is None: | ||
raise ValueError("Audio is not quantized.") | ||
|
||
q_levels = 1 << self.bits | ||
return Audio( | ||
self.data.float() / (q_levels / 2) - 1, sampling_rate=self.sampling_rate | ||
) | ||
|
||
def __repr__(self) -> str: | ||
return f"Audio({self.duration()} seconds @ {self.sampling_rate}Hz)" | ||
|
||
def __eq__(self, other: "Audio") -> bool: | ||
return ( | ||
self.data.shape == other.data.shape | ||
and self.sampling_rate == other.sampling_rate | ||
and torch.allclose(self.data, other.data) | ||
) | ||
|
||
def __getitem__(self, key: int) -> "Audio": | ||
return Audio(self.data[key], self.sampling_rate) | ||
|
||
def __len__(self) -> int: | ||
return len(self.data) | ||
|
||
def _repr_html_(self) -> str: | ||
import IPython.display as ipd | ||
|
||
return ipd.Audio(self.data, rate=self.sampling_rate)._repr_html_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
meerkat/interactive/app/src/lib/component/core/audio/Audio.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<script lang="ts"> | ||
import { PlayFill, Pause } from 'svelte-bootstrap-icons'; | ||
import { setCurrentMedia, currentMedia } from '$lib/shared/media/current'; | ||
export let data: string; | ||
export let classes: string = ''; | ||
const startPlay = () => { | ||
setCurrentMedia(data); | ||
if ($currentMedia) { | ||
$currentMedia.paused = false; | ||
$currentMedia.currentTime = 0; | ||
} | ||
}; | ||
// FIXME: This should be based of the primary key of the cell. | ||
$: playing = $currentMedia && $currentMedia.data === data; | ||
</script> | ||
|
||
<div | ||
class={'h-full w-10 bg-slate-100 rounded-sm flex items-center justify-center self-center border' + | ||
classes} | ||
class:border-violet-600={playing} | ||
> | ||
{#if playing} | ||
{#if $currentMedia.paused} | ||
<button on:click={() => ($currentMedia.paused = false)}> | ||
<PlayFill class="text-slate-600" /> | ||
</button> | ||
{:else if $currentMedia.ended} | ||
<button on:click={startPlay}> <PlayFill class="text-slate-600" /> </button> | ||
{:else} | ||
<button on:click={() => ($currentMedia.paused = true)}> | ||
<Pause class="text-slate-600" /> | ||
</button> | ||
{/if} | ||
{:else} | ||
<button on:click={startPlay}> <PlayFill class="text-slate-600" /> </button> | ||
{/if} | ||
</div> |
6 changes: 6 additions & 0 deletions
6
meerkat/interactive/app/src/lib/component/core/audio/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from meerkat.interactive.app.src.lib.component.abstract import Component | ||
|
||
|
||
class Audio(Component): | ||
data: str | ||
classes: str = "" |
Oops, something went wrong.