torch-mdct 0.1.0__tar.gz
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.
- torch_mdct-0.1.0/.gitignore +160 -0
- torch_mdct-0.1.0/LICENSE +21 -0
- torch_mdct-0.1.0/PKG-INFO +56 -0
- torch_mdct-0.1.0/README.md +37 -0
- torch_mdct-0.1.0/notebooks/audio_samples/sample.ogg +0 -0
- torch_mdct-0.1.0/notebooks/example_usage.ipynb +263 -0
- torch_mdct-0.1.0/pyproject.toml +26 -0
- torch_mdct-0.1.0/src/torch_mdct/__init__.py +3 -0
- torch_mdct-0.1.0/src/torch_mdct/functional.py +145 -0
- torch_mdct-0.1.0/src/torch_mdct/torch_mdct.py +128 -0
- torch_mdct-0.1.0/src/torch_mdct/windows.py +68 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
110
|
+
.pdm.toml
|
|
111
|
+
|
|
112
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
113
|
+
__pypackages__/
|
|
114
|
+
|
|
115
|
+
# Celery stuff
|
|
116
|
+
celerybeat-schedule
|
|
117
|
+
celerybeat.pid
|
|
118
|
+
|
|
119
|
+
# SageMath parsed files
|
|
120
|
+
*.sage.py
|
|
121
|
+
|
|
122
|
+
# Environments
|
|
123
|
+
.env
|
|
124
|
+
.venv
|
|
125
|
+
env/
|
|
126
|
+
venv/
|
|
127
|
+
ENV/
|
|
128
|
+
env.bak/
|
|
129
|
+
venv.bak/
|
|
130
|
+
|
|
131
|
+
# Spyder project settings
|
|
132
|
+
.spyderproject
|
|
133
|
+
.spyproject
|
|
134
|
+
|
|
135
|
+
# Rope project settings
|
|
136
|
+
.ropeproject
|
|
137
|
+
|
|
138
|
+
# mkdocs documentation
|
|
139
|
+
/site
|
|
140
|
+
|
|
141
|
+
# mypy
|
|
142
|
+
.mypy_cache/
|
|
143
|
+
.dmypy.json
|
|
144
|
+
dmypy.json
|
|
145
|
+
|
|
146
|
+
# Pyre type checker
|
|
147
|
+
.pyre/
|
|
148
|
+
|
|
149
|
+
# pytype static type analyzer
|
|
150
|
+
.pytype/
|
|
151
|
+
|
|
152
|
+
# Cython debug symbols
|
|
153
|
+
cython_debug/
|
|
154
|
+
|
|
155
|
+
# PyCharm
|
|
156
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
157
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
158
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
159
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
160
|
+
#.idea/
|
torch_mdct-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kinyugo Maina
|
|
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.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: torch_mdct
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A PyTorch implementation of the Modified Discrete Cosine Transform (MDCT) and its inverse for audio processing.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Kinyugo/torch_mdct
|
|
6
|
+
Project-URL: Issues, https://github.com/Kinyugo/torch_mdct/issues
|
|
7
|
+
Author-email: Kinyugo Maina <kinyugomaina@gmail.com>
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: audio,imdct,mdct,pytorch,signal processing
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Requires-Dist: torch
|
|
15
|
+
Provides-Extra: notebook
|
|
16
|
+
Requires-Dist: ipython; extra == 'notebook'
|
|
17
|
+
Requires-Dist: matplotlib; extra == 'notebook'
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# torch_mdct
|
|
21
|
+
|
|
22
|
+
PyTorch implementation of Modified Discrete Cosine Transform and Inverse Modified Discrete Cosine Transform.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install torch_mdct
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import torchaudio
|
|
34
|
+
from torch_mdct import MDCT, IMDCT
|
|
35
|
+
|
|
36
|
+
# Load a sample waveform
|
|
37
|
+
waveform, sample_rate = torchaudio.load("/path/to/audio.file")
|
|
38
|
+
|
|
39
|
+
# Initialize the mdct and imdct transforms
|
|
40
|
+
mdct = MDCT(win_length=2048)
|
|
41
|
+
imdct = IMDCT(win_length=2048)
|
|
42
|
+
|
|
43
|
+
# Transform waveform into mdct spectrogram
|
|
44
|
+
spectrogram = mdct(waveform)
|
|
45
|
+
|
|
46
|
+
# Transform spectrogram back to audio
|
|
47
|
+
reconst_waveform = imdct(spectrogram)
|
|
48
|
+
|
|
49
|
+
# Compute the differences
|
|
50
|
+
print(f"L1: {(waveform - reconst_waveform).abs().mean()}")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## References
|
|
54
|
+
[[1]](https://github.com/zafarrafii/Zaf-Python) Zaf-Python: Zafar's Audio Functions in **Python** for audio signal analysis.
|
|
55
|
+
|
|
56
|
+
[[2]](https://github.com/nils-werner/mdct) MDCT: A fast MDCT implementation using SciPy and FFTs.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# torch_mdct
|
|
2
|
+
|
|
3
|
+
PyTorch implementation of Modified Discrete Cosine Transform and Inverse Modified Discrete Cosine Transform.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install torch_mdct
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import torchaudio
|
|
15
|
+
from torch_mdct import MDCT, IMDCT
|
|
16
|
+
|
|
17
|
+
# Load a sample waveform
|
|
18
|
+
waveform, sample_rate = torchaudio.load("/path/to/audio.file")
|
|
19
|
+
|
|
20
|
+
# Initialize the mdct and imdct transforms
|
|
21
|
+
mdct = MDCT(win_length=2048)
|
|
22
|
+
imdct = IMDCT(win_length=2048)
|
|
23
|
+
|
|
24
|
+
# Transform waveform into mdct spectrogram
|
|
25
|
+
spectrogram = mdct(waveform)
|
|
26
|
+
|
|
27
|
+
# Transform spectrogram back to audio
|
|
28
|
+
reconst_waveform = imdct(spectrogram)
|
|
29
|
+
|
|
30
|
+
# Compute the differences
|
|
31
|
+
print(f"L1: {(waveform - reconst_waveform).abs().mean()}")
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## References
|
|
35
|
+
[[1]](https://github.com/zafarrafii/Zaf-Python) Zaf-Python: Zafar's Audio Functions in **Python** for audio signal analysis.
|
|
36
|
+
|
|
37
|
+
[[2]](https://github.com/nils-werner/mdct) MDCT: A fast MDCT implementation using SciPy and FFTs.
|
|
Binary file
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cells": [
|
|
3
|
+
{
|
|
4
|
+
"cell_type": "markdown",
|
|
5
|
+
"metadata": {},
|
|
6
|
+
"source": [
|
|
7
|
+
"# Example Usage"
|
|
8
|
+
]
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"cell_type": "markdown",
|
|
12
|
+
"metadata": {},
|
|
13
|
+
"source": [
|
|
14
|
+
"## Setup"
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"cell_type": "code",
|
|
19
|
+
"execution_count": 1,
|
|
20
|
+
"metadata": {},
|
|
21
|
+
"outputs": [],
|
|
22
|
+
"source": [
|
|
23
|
+
"# Uncomment to install the torch_mdct package\n",
|
|
24
|
+
"# %pip install torch_mdct"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"cell_type": "code",
|
|
29
|
+
"execution_count": 2,
|
|
30
|
+
"metadata": {},
|
|
31
|
+
"outputs": [],
|
|
32
|
+
"source": [
|
|
33
|
+
"# Comment out the following line if you have installed the torch_mdct package\n",
|
|
34
|
+
"import sys\n",
|
|
35
|
+
"\n",
|
|
36
|
+
"sys.path.append(\"../src\")"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"cell_type": "markdown",
|
|
41
|
+
"metadata": {},
|
|
42
|
+
"source": [
|
|
43
|
+
"## Imports"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"cell_type": "code",
|
|
48
|
+
"execution_count": 3,
|
|
49
|
+
"metadata": {},
|
|
50
|
+
"outputs": [],
|
|
51
|
+
"source": [
|
|
52
|
+
"import torch\n",
|
|
53
|
+
"import torchaudio\n",
|
|
54
|
+
"from IPython.display import Audio\n",
|
|
55
|
+
"from matplotlib import pyplot as plt\n",
|
|
56
|
+
"\n",
|
|
57
|
+
"from torch_mdct import IMDCT, MDCT"
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"cell_type": "markdown",
|
|
62
|
+
"metadata": {},
|
|
63
|
+
"source": [
|
|
64
|
+
"## Utils"
|
|
65
|
+
]
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"cell_type": "code",
|
|
69
|
+
"execution_count": 4,
|
|
70
|
+
"metadata": {},
|
|
71
|
+
"outputs": [],
|
|
72
|
+
"source": [
|
|
73
|
+
"def plot_waveform(waveform: torch.Tensor, sample_rate: int, title: str) -> None:\n",
|
|
74
|
+
" channels, n_frames = waveform.shape\n",
|
|
75
|
+
"\n",
|
|
76
|
+
" skip = int(n_frames / (0.01 * n_frames))\n",
|
|
77
|
+
" waveform = waveform[..., 0:-1:skip]\n",
|
|
78
|
+
"\n",
|
|
79
|
+
" n_frames = waveform.shape[-1]\n",
|
|
80
|
+
" time_axis = torch.linspace(0, n_frames / (sample_rate / skip), steps=n_frames)\n",
|
|
81
|
+
"\n",
|
|
82
|
+
" fig, axes = plt.subplots(2, max(channels // 2, 1), constrained_layout=True)\n",
|
|
83
|
+
" axes = axes.flatten()\n",
|
|
84
|
+
"\n",
|
|
85
|
+
" for c in range(channels):\n",
|
|
86
|
+
" axes[c].plot(time_axis, waveform[c], linewidth=1)\n",
|
|
87
|
+
" axes[c].grid(True)\n",
|
|
88
|
+
"\n",
|
|
89
|
+
" if channels > 1:\n",
|
|
90
|
+
" axes[c].set_ylabel(f\"Channel {c}\")\n",
|
|
91
|
+
"\n",
|
|
92
|
+
" fig.suptitle(title)\n",
|
|
93
|
+
" plt.xlabel(\"Time (s)\")\n",
|
|
94
|
+
" plt.show(block=False)\n",
|
|
95
|
+
"\n",
|
|
96
|
+
"\n",
|
|
97
|
+
"def plot_spectrogram(spectrogram: torch.Tensor, title: str) -> None:\n",
|
|
98
|
+
" channels = spectrogram.shape[0]\n",
|
|
99
|
+
"\n",
|
|
100
|
+
" fig, axes = plt.subplots(2, max(channels // 2, 1), constrained_layout=True)\n",
|
|
101
|
+
" axes = axes.flatten()\n",
|
|
102
|
+
"\n",
|
|
103
|
+
" for c in range(channels):\n",
|
|
104
|
+
" im = axes[c].imshow(\n",
|
|
105
|
+
" torch.log(spectrogram[c].abs() + 1e-5), origin=\"lower\", aspect=\"auto\"\n",
|
|
106
|
+
" )\n",
|
|
107
|
+
" fig.colorbar(im, ax=axes[c])\n",
|
|
108
|
+
"\n",
|
|
109
|
+
" if channels > 1:\n",
|
|
110
|
+
" axes[c].set_ylabel(f\"Channels {c}\")\n",
|
|
111
|
+
"\n",
|
|
112
|
+
" fig.suptitle(title)\n",
|
|
113
|
+
" plt.xlabel(\"Time\")\n",
|
|
114
|
+
" plt.show(block=False)\n",
|
|
115
|
+
"\n",
|
|
116
|
+
"\n",
|
|
117
|
+
"def stats(x: torch.Tensor) -> str:\n",
|
|
118
|
+
" return f\"Shape: {tuple(x.shape)} Min: {x.min():.2f} Max: {x.max():.2f} Mean: {x.mean():.2f} Std: {x.std():.2f}\""
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"cell_type": "markdown",
|
|
123
|
+
"metadata": {},
|
|
124
|
+
"source": [
|
|
125
|
+
"## DataLoading"
|
|
126
|
+
]
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
"cell_type": "code",
|
|
130
|
+
"execution_count": null,
|
|
131
|
+
"metadata": {},
|
|
132
|
+
"outputs": [],
|
|
133
|
+
"source": [
|
|
134
|
+
"waveform, sample_rate = torchaudio.load(\"audio_samples/sample.ogg\")\n",
|
|
135
|
+
"Audio(waveform, rate=sample_rate)"
|
|
136
|
+
]
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
"cell_type": "code",
|
|
140
|
+
"execution_count": null,
|
|
141
|
+
"metadata": {},
|
|
142
|
+
"outputs": [],
|
|
143
|
+
"source": [
|
|
144
|
+
"plot_waveform(waveform, sample_rate, f\"Waveform: \\n({stats(waveform)})\")"
|
|
145
|
+
]
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
"cell_type": "markdown",
|
|
149
|
+
"metadata": {},
|
|
150
|
+
"source": [
|
|
151
|
+
"## Transforms"
|
|
152
|
+
]
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
"cell_type": "code",
|
|
156
|
+
"execution_count": 7,
|
|
157
|
+
"metadata": {},
|
|
158
|
+
"outputs": [],
|
|
159
|
+
"source": [
|
|
160
|
+
"mdct = MDCT(win_length=2048)\n",
|
|
161
|
+
"imdct = IMDCT(win_length=2048)"
|
|
162
|
+
]
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
"cell_type": "markdown",
|
|
166
|
+
"metadata": {},
|
|
167
|
+
"source": [
|
|
168
|
+
"## MDCT Experiments"
|
|
169
|
+
]
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"cell_type": "code",
|
|
173
|
+
"execution_count": null,
|
|
174
|
+
"metadata": {},
|
|
175
|
+
"outputs": [],
|
|
176
|
+
"source": [
|
|
177
|
+
"spectrogram = mdct(waveform)\n",
|
|
178
|
+
"plot_spectrogram(spectrogram, f\"Log Absolute Spectrogram: \\n({stats(spectrogram)})\")"
|
|
179
|
+
]
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
"cell_type": "markdown",
|
|
183
|
+
"metadata": {},
|
|
184
|
+
"source": [
|
|
185
|
+
"## IMDCT Experiments"
|
|
186
|
+
]
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
"cell_type": "code",
|
|
190
|
+
"execution_count": null,
|
|
191
|
+
"metadata": {},
|
|
192
|
+
"outputs": [],
|
|
193
|
+
"source": [
|
|
194
|
+
"reconst_waveform = imdct(spectrogram, length=waveform.shape[-1])\n",
|
|
195
|
+
"Audio(reconst_waveform, rate=sample_rate)"
|
|
196
|
+
]
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
"cell_type": "code",
|
|
200
|
+
"execution_count": null,
|
|
201
|
+
"metadata": {},
|
|
202
|
+
"outputs": [],
|
|
203
|
+
"source": [
|
|
204
|
+
"plot_waveform(\n",
|
|
205
|
+
" reconst_waveform,\n",
|
|
206
|
+
" sample_rate,\n",
|
|
207
|
+
" f\"Reconstructed Waveform: \\n({stats(reconst_waveform)})\",\n",
|
|
208
|
+
")"
|
|
209
|
+
]
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
"cell_type": "markdown",
|
|
213
|
+
"metadata": {},
|
|
214
|
+
"source": [
|
|
215
|
+
"## Waveform Difference"
|
|
216
|
+
]
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
"cell_type": "code",
|
|
220
|
+
"execution_count": null,
|
|
221
|
+
"metadata": {},
|
|
222
|
+
"outputs": [],
|
|
223
|
+
"source": [
|
|
224
|
+
"waveform_diff = waveform - reconst_waveform\n",
|
|
225
|
+
"Audio(waveform_diff, rate=sample_rate)"
|
|
226
|
+
]
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
"cell_type": "code",
|
|
230
|
+
"execution_count": null,
|
|
231
|
+
"metadata": {},
|
|
232
|
+
"outputs": [],
|
|
233
|
+
"source": [
|
|
234
|
+
"plot_waveform(\n",
|
|
235
|
+
" waveform_diff,\n",
|
|
236
|
+
" sample_rate,\n",
|
|
237
|
+
" f\"Waveform Difference: \\n({stats(waveform_diff)})\",\n",
|
|
238
|
+
")"
|
|
239
|
+
]
|
|
240
|
+
}
|
|
241
|
+
],
|
|
242
|
+
"metadata": {
|
|
243
|
+
"kernelspec": {
|
|
244
|
+
"display_name": "torch",
|
|
245
|
+
"language": "python",
|
|
246
|
+
"name": "python3"
|
|
247
|
+
},
|
|
248
|
+
"language_info": {
|
|
249
|
+
"codemirror_mode": {
|
|
250
|
+
"name": "ipython",
|
|
251
|
+
"version": 3
|
|
252
|
+
},
|
|
253
|
+
"file_extension": ".py",
|
|
254
|
+
"mimetype": "text/x-python",
|
|
255
|
+
"name": "python",
|
|
256
|
+
"nbconvert_exporter": "python",
|
|
257
|
+
"pygments_lexer": "ipython3",
|
|
258
|
+
"version": "3.11.8"
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
"nbformat": 4,
|
|
262
|
+
"nbformat_minor": 2
|
|
263
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "torch_mdct"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
authors = [{name = "Kinyugo Maina", email = "kinyugomaina@gmail.com"}]
|
|
9
|
+
description = "A PyTorch implementation of the Modified Discrete Cosine Transform (MDCT) and its inverse for audio processing."
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
licence = {file = "LICENSE"}
|
|
12
|
+
requires-python = ">=3.8"
|
|
13
|
+
keywords = ["pytorch", "mdct", "imdct", "audio", "signal processing"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
dependencies = ["torch"]
|
|
20
|
+
|
|
21
|
+
[project.optional-dependencies]
|
|
22
|
+
notebook = ["matplotlib", "ipython"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/Kinyugo/torch_mdct"
|
|
26
|
+
Issues = "https://github.com/Kinyugo/torch_mdct/issues"
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch.nn import functional as F
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def mdct(
|
|
8
|
+
waveform: torch.Tensor, window: torch.Tensor, center: bool = True
|
|
9
|
+
) -> torch.Tensor:
|
|
10
|
+
"""
|
|
11
|
+
Compute the Modified Discrete Cosine Transform (MDCT) of a waveform.
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
waveform : torch.Tensor
|
|
16
|
+
Input waveform tensor of shape (..., n_samples).
|
|
17
|
+
window : torch.Tensor
|
|
18
|
+
Window tensor of shape (win_length,).
|
|
19
|
+
center : bool, optional
|
|
20
|
+
If True, pad the waveform on both sides with half the window length, by default True.
|
|
21
|
+
|
|
22
|
+
Returns
|
|
23
|
+
-------
|
|
24
|
+
torch.Tensor
|
|
25
|
+
MDCT spectrogram of the input waveform of shape (..., win_length // 2, n_frames).
|
|
26
|
+
"""
|
|
27
|
+
n_samples = waveform.shape[-1]
|
|
28
|
+
win_length = window.shape[-1]
|
|
29
|
+
hop_length = win_length // 2
|
|
30
|
+
|
|
31
|
+
# Flatten the input tensor
|
|
32
|
+
shape = waveform.shape
|
|
33
|
+
waveform = waveform.flatten(end_dim=-2)
|
|
34
|
+
|
|
35
|
+
# Center the waveform by padding on both sides
|
|
36
|
+
n_frames = int(math.ceil(n_samples / hop_length)) + 1
|
|
37
|
+
if center:
|
|
38
|
+
waveform = F.pad(
|
|
39
|
+
waveform,
|
|
40
|
+
(hop_length, (n_frames + 1) * hop_length - n_samples),
|
|
41
|
+
mode="constant",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
# Prepare pre&post processing arrays
|
|
45
|
+
pre_twiddle = torch.exp(
|
|
46
|
+
-1j
|
|
47
|
+
* torch.pi
|
|
48
|
+
/ win_length
|
|
49
|
+
* torch.arange(0, win_length, device=waveform.device)
|
|
50
|
+
)
|
|
51
|
+
post_twiddle = torch.exp(
|
|
52
|
+
-1j
|
|
53
|
+
* torch.pi
|
|
54
|
+
/ win_length
|
|
55
|
+
* (win_length / 2 + 1)
|
|
56
|
+
* torch.arange(0.5, win_length / 2 + 0.5, device=waveform.device)
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Convolve the waveform with the window and fft matrices
|
|
60
|
+
spectrogram = waveform.unfold(dimension=-1, size=win_length, step=hop_length)
|
|
61
|
+
spectrogram = torch.einsum("...kj,j->...kj", spectrogram, window)
|
|
62
|
+
spectrogram = torch.einsum("...kj,j->...kj", spectrogram, pre_twiddle)
|
|
63
|
+
spectrogram = torch.fft.fft(spectrogram, dim=-1)
|
|
64
|
+
spectrogram = torch.einsum(
|
|
65
|
+
"...jk,k->...kj", spectrogram[..., : win_length // 2], post_twiddle
|
|
66
|
+
)
|
|
67
|
+
spectrogram = torch.real(spectrogram)
|
|
68
|
+
|
|
69
|
+
# Unflatten the output
|
|
70
|
+
spectrogram = spectrogram.reshape(shape[:-1] + spectrogram.shape[-2:])
|
|
71
|
+
|
|
72
|
+
return spectrogram
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def imdct(
|
|
76
|
+
spectrogram: torch.Tensor, window: torch.Tensor, center: bool = True
|
|
77
|
+
) -> torch.Tensor:
|
|
78
|
+
"""
|
|
79
|
+
Compute the inverse Modified Discrete Cosine Transform (iMDCT) of a spectrogram.
|
|
80
|
+
|
|
81
|
+
Parameters
|
|
82
|
+
----------
|
|
83
|
+
spectrogram : torch.Tensor
|
|
84
|
+
Input MDCT spectrogram tensor of shape (..., win_length // 2, n_frames).
|
|
85
|
+
window : torch.Tensor
|
|
86
|
+
Window tensor of shape (win_length,).
|
|
87
|
+
center : bool, optional
|
|
88
|
+
If True, remove the padding added during MDCT, by default True.
|
|
89
|
+
|
|
90
|
+
Returns
|
|
91
|
+
-------
|
|
92
|
+
torch.Tensor
|
|
93
|
+
Reconstructed waveform tensor of shape (..., n_samples).
|
|
94
|
+
"""
|
|
95
|
+
win_length = window.shape[-1]
|
|
96
|
+
hop_length = win_length // 2
|
|
97
|
+
n_freqs, n_frames = spectrogram.shape[-2:]
|
|
98
|
+
n_samples = hop_length * (n_frames + 1)
|
|
99
|
+
|
|
100
|
+
# Flatten the input tensor
|
|
101
|
+
shape = spectrogram.shape
|
|
102
|
+
spectrogram = spectrogram.flatten(end_dim=-3)
|
|
103
|
+
|
|
104
|
+
# Prepare pre&post processing arrays
|
|
105
|
+
pre_twiddle = torch.exp(
|
|
106
|
+
-1j
|
|
107
|
+
* torch.pi
|
|
108
|
+
/ (2 * n_freqs)
|
|
109
|
+
* (n_freqs + 1)
|
|
110
|
+
* torch.arange(n_freqs, device=spectrogram.device)
|
|
111
|
+
)
|
|
112
|
+
post_twiddle = (
|
|
113
|
+
torch.exp(
|
|
114
|
+
-1j
|
|
115
|
+
* torch.pi
|
|
116
|
+
/ (2 * n_freqs)
|
|
117
|
+
* torch.arange(
|
|
118
|
+
0.5 + n_freqs / 2,
|
|
119
|
+
2 * n_freqs + n_freqs / 2 + 0.5,
|
|
120
|
+
device=spectrogram.device,
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
/ n_freqs
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
# Apply fft and the window
|
|
127
|
+
spectrogram = torch.einsum("...jk,j->...jk", spectrogram, pre_twiddle)
|
|
128
|
+
spectrogram = torch.fft.fft(spectrogram, n=2 * n_freqs, dim=1)
|
|
129
|
+
spectrogram = torch.einsum("...jk,j->...jk", spectrogram, post_twiddle)
|
|
130
|
+
spectrogram = torch.real(spectrogram)
|
|
131
|
+
spectrogram = 2 * torch.einsum("...jk,j->...jk", spectrogram, window)
|
|
132
|
+
|
|
133
|
+
# Recover the waveform with the time-domain aliasing cancelling principle
|
|
134
|
+
waveform = F.fold(
|
|
135
|
+
spectrogram, (1, n_samples), kernel_size=(1, win_length), stride=(1, hop_length)
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
# Remove padding
|
|
139
|
+
if center:
|
|
140
|
+
waveform = waveform[..., hop_length:-hop_length]
|
|
141
|
+
|
|
142
|
+
# Unflatten the output
|
|
143
|
+
waveform = waveform.reshape((*shape[:-2], -1))
|
|
144
|
+
|
|
145
|
+
return waveform
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
from functools import partial
|
|
2
|
+
from typing import Any, Dict, Optional, Union
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
from torch import nn
|
|
6
|
+
|
|
7
|
+
from .functional import imdct, mdct
|
|
8
|
+
from .windows import kaiser_bessel_derived, vorbis
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MDCT(nn.Module):
|
|
12
|
+
"""
|
|
13
|
+
Module to compute the Modified Discrete Cosine Transform (MDCT) of a waveform.
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
win_length : int
|
|
18
|
+
The length of the window.
|
|
19
|
+
window_fn : callable, optional
|
|
20
|
+
A function to generate the window, by default vorbis.
|
|
21
|
+
window_kwargs : dict, optional
|
|
22
|
+
Additional keyword arguments to pass to the window function, by default {}.
|
|
23
|
+
center : bool, optional
|
|
24
|
+
If True, pad the waveform on both sides with half the window length, by default True.
|
|
25
|
+
|
|
26
|
+
Attributes
|
|
27
|
+
----------
|
|
28
|
+
window : torch.Tensor
|
|
29
|
+
The window tensor.
|
|
30
|
+
mdct : callable
|
|
31
|
+
The MDCT function with the specified center parameter.
|
|
32
|
+
|
|
33
|
+
Methods
|
|
34
|
+
-------
|
|
35
|
+
forward(waveform: torch.Tensor) -> torch.Tensor
|
|
36
|
+
Compute the MDCT of the input waveform.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(
|
|
40
|
+
self,
|
|
41
|
+
win_length: int,
|
|
42
|
+
window_fn: Union[kaiser_bessel_derived, vorbis] = vorbis,
|
|
43
|
+
window_kwargs: Dict[str, Any] = {},
|
|
44
|
+
center: bool = True,
|
|
45
|
+
) -> None:
|
|
46
|
+
super().__init__()
|
|
47
|
+
|
|
48
|
+
self.register_buffer("window", window_fn(win_length, **window_kwargs))
|
|
49
|
+
self.mdct = partial(mdct, center=center)
|
|
50
|
+
|
|
51
|
+
def forward(self, waveform: torch.Tensor) -> torch.Tensor:
|
|
52
|
+
"""
|
|
53
|
+
Compute the MDCT of the input waveform.
|
|
54
|
+
|
|
55
|
+
Parameters
|
|
56
|
+
----------
|
|
57
|
+
waveform : torch.Tensor
|
|
58
|
+
Input waveform tensor of shape (..., n_samples).
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
torch.Tensor
|
|
63
|
+
MDCT spectrogram of the input waveform of shape (..., win_length // 2, n_frames).
|
|
64
|
+
"""
|
|
65
|
+
return self.mdct(waveform, self.window)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class IMDCT(nn.Module):
|
|
69
|
+
"""
|
|
70
|
+
Module to compute the inverse Modified Discrete Cosine Transform (iMDCT) of a spectrogram.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
win_length : int
|
|
75
|
+
The length of the window.
|
|
76
|
+
window_fn : callable, optional
|
|
77
|
+
A function to generate the window, by default vorbis.
|
|
78
|
+
window_kwargs : dict, optional
|
|
79
|
+
Additional keyword arguments to pass to the window function, by default {}.
|
|
80
|
+
center : bool, optional
|
|
81
|
+
If True, remove the padding added during MDCT, by default True.
|
|
82
|
+
|
|
83
|
+
Attributes
|
|
84
|
+
----------
|
|
85
|
+
window : torch.Tensor
|
|
86
|
+
The window tensor.
|
|
87
|
+
imdct : callable
|
|
88
|
+
The iMDCT function with the specified center parameter.
|
|
89
|
+
|
|
90
|
+
Methods
|
|
91
|
+
-------
|
|
92
|
+
forward(spectrogram: torch.Tensor) -> torch.Tensor
|
|
93
|
+
Compute the iMDCT of the input spectrogram.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
def __init__(
|
|
97
|
+
self,
|
|
98
|
+
win_length: int,
|
|
99
|
+
window_fn: Union[kaiser_bessel_derived, vorbis] = vorbis,
|
|
100
|
+
window_kwargs: Dict[str, Any] = {},
|
|
101
|
+
center: bool = True,
|
|
102
|
+
) -> None:
|
|
103
|
+
super().__init__()
|
|
104
|
+
|
|
105
|
+
self.register_buffer("window", window_fn(win_length, **window_kwargs))
|
|
106
|
+
self.imdct = partial(imdct, center=center)
|
|
107
|
+
|
|
108
|
+
def forward(
|
|
109
|
+
self, spectrogram: torch.Tensor, length: Optional[int] = None
|
|
110
|
+
) -> torch.Tensor:
|
|
111
|
+
"""
|
|
112
|
+
Compute the iMDCT of the input spectrogram.
|
|
113
|
+
|
|
114
|
+
Parameters
|
|
115
|
+
----------
|
|
116
|
+
spectrogram : torch.Tensor
|
|
117
|
+
Input MDCT spectrogram tensor of shape (..., win_length // 2, n_frames).
|
|
118
|
+
|
|
119
|
+
length : int, optional
|
|
120
|
+
The length of the output waveform, by default None.
|
|
121
|
+
|
|
122
|
+
Returns
|
|
123
|
+
-------
|
|
124
|
+
torch.Tensor
|
|
125
|
+
Reconstructed waveform tensor of shape (..., n_samples).
|
|
126
|
+
"""
|
|
127
|
+
length = length or -1 # By default, return the full waveform
|
|
128
|
+
return self.imdct(spectrogram, self.window)[..., :length]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def kaiser_bessel_derived(
|
|
5
|
+
win_length: int,
|
|
6
|
+
beta: float = 12.0,
|
|
7
|
+
*,
|
|
8
|
+
dtype: torch.dtype = None,
|
|
9
|
+
device: torch.device = None
|
|
10
|
+
) -> torch.Tensor:
|
|
11
|
+
"""
|
|
12
|
+
Generate a Kaiser-Bessel derived window.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
win_length : int
|
|
17
|
+
The length of the window.
|
|
18
|
+
beta : float, optional
|
|
19
|
+
The beta parameter for the Kaiser window, by default 12.0.
|
|
20
|
+
dtype : torch.dtype, optional
|
|
21
|
+
The desired data type of returned tensor, by default None.
|
|
22
|
+
device : torch.device, optional
|
|
23
|
+
The desired device of returned tensor, by default None.
|
|
24
|
+
|
|
25
|
+
Returns
|
|
26
|
+
-------
|
|
27
|
+
torch.Tensor
|
|
28
|
+
The generated Kaiser-Bessel derived window of shape (win_length,).
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
half_w_length = win_length // 2
|
|
32
|
+
kaiser_w = torch.kaiser_window(
|
|
33
|
+
half_w_length + 1, True, beta, dtype=dtype, device=device
|
|
34
|
+
)
|
|
35
|
+
kaiser_w_csum = torch.cumsum(kaiser_w, dim=-1)
|
|
36
|
+
half_w = torch.sqrt(kaiser_w_csum[:-1] / kaiser_w_csum[-1])
|
|
37
|
+
w = torch.cat((half_w, torch.flip(half_w, dims=(0,))), axis=0)
|
|
38
|
+
|
|
39
|
+
return w
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def vorbis(
|
|
43
|
+
win_length: int, *, dtype: torch.dtype = None, device: torch.device = None
|
|
44
|
+
) -> torch.Tensor:
|
|
45
|
+
"""
|
|
46
|
+
Generate a Vorbis window.
|
|
47
|
+
|
|
48
|
+
Parameters
|
|
49
|
+
----------
|
|
50
|
+
win_length : int
|
|
51
|
+
The length of the window.
|
|
52
|
+
dtype : torch.dtype, optional
|
|
53
|
+
The desired data type of returned tensor, by default None.
|
|
54
|
+
device : torch.device, optional
|
|
55
|
+
The desired device of returned tensor, by default None.
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
-------
|
|
59
|
+
torch.Tensor
|
|
60
|
+
The generated Vorbis window of shape (win_length,).
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
arg = torch.arange(win_length, dtype=dtype, device=device) + 0.5
|
|
64
|
+
w = torch.sin(
|
|
65
|
+
torch.pi / 2.0 * torch.pow(torch.sin(torch.pi / win_length * arg), 2.0)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
return w
|