-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d34a0cf
commit a7240e6
Showing
2 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "Q32l5f06ohtz" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install dasp-pytorch" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"!wget https://csteinmetz1.github.io/sounds/assets/short_riff.wav" | ||
], | ||
"metadata": { | ||
"id": "MsHHumIypsAy" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"import torch\n", | ||
"import torchaudio\n", | ||
"import dasp_pytorch" | ||
], | ||
"metadata": { | ||
"id": "VGSU9x2VojWg" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Load audio\n", | ||
"x, sr = torchaudio.load(\"short_riff.wav.1\")\n", | ||
"\n", | ||
"# create batch dim\n", | ||
"# (batch_size, n_channels, n_samples)\n", | ||
"x = x.unsqueeze(0)\n", | ||
"\n", | ||
"# apply some distortion with 16 dB drive\n", | ||
"drive = torch.tensor([16.0])\n", | ||
"y = dasp_pytorch.functional.distortion(x, sr, drive)\n", | ||
"\n", | ||
"# create a parameter to optimizer\n", | ||
"drive_hat = torch.nn.Parameter(torch.tensor(0.0))\n", | ||
"optimizer = torch.optim.Adam([drive_hat], lr=0.01)\n", | ||
"\n", | ||
"# optimize the parameter\n", | ||
"n_iters = 2500\n", | ||
"for n in range(n_iters):\n", | ||
" # apply distortion with the estimated parameter\n", | ||
" y_hat = dasp_pytorch.functional.distortion(x, sr, drive_hat)\n", | ||
"\n", | ||
" # compute distance between estimate and target\n", | ||
" loss = torch.nn.functional.mse_loss(y_hat, y)\n", | ||
"\n", | ||
" # optimize\n", | ||
" optimizer.zero_grad()\n", | ||
" loss.backward()\n", | ||
" optimizer.step()\n", | ||
" print(\n", | ||
" f\"step: {n+1}/{n_iters}, loss: {loss.item():.3e}, drive: {drive_hat.item():.3f}\\r\"\n", | ||
" )\n", | ||
"" | ||
], | ||
"metadata": { | ||
"id": "MOhyTl3mopSi" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
] | ||
} |