{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"# New Section"
],
"metadata": {
"id": "jbe_aWYkjWRH"
},
"id": "jbe_aWYkjWRH"
},
{
"cell_type": "markdown",
"source": [
"### Instructions for running:\n",
"\n",
"* Make sure to use a GPU runtime, click: __Runtime >> Change Runtime Type >> GPU__\n",
"* Press ▶️ on the left of each of the cells\n",
"* View the code: Double-click any of the cells\n",
"* Hide the code: Double click the right side of the cell\n"
],
"metadata": {
"id": "8UON6ncSApA9"
},
"id": "8UON6ncSApA9"
},
{
"cell_type": "code",
"source": [
"#@title #Install and Import\n",
"\n",
"#@markdown Installing the required data and dependencies. This step might take some minutes\n",
"\n",
"#download the files\n",
"! git clone https://github.com/eloimoliner/denoising-historical-recordings.git\n",
"! wget https://github.com/eloimoliner/denoising-historical-recordings/releases/download/v0.0/checkpoint.zip\n",
"! unzip checkpoint.zip -d denoising-historical-recordings/experiments/trained_model/\n",
"\n",
"%cd denoising-historical-recordings\n",
"\n",
"#install dependencies\n",
"! pip install hydra-core==0.11.3\n",
"\n",
"#All the code goes here\n",
"import unet\n",
"import tensorflow as tf\n",
"import soundfile as sf\n",
"import numpy as np\n",
"from tqdm import tqdm\n",
"import scipy.signal\n",
"import hydra\n",
"import os\n",
"#workaround to load hydra conf file\n",
"import yaml\n",
"from pathlib import Path\n",
"args = yaml.safe_load(Path('conf/conf.yaml').read_text())\n",
"class dotdict(dict):\n",
" \"\"\"dot.notation access to dictionary attributes\"\"\"\n",
" __getattr__ = dict.get\n",
" __setattr__ = dict.__setitem__\n",
" __delattr__ = dict.__delitem__\n",
"args=dotdict(args)\n",
"unet_args=dotdict(args.unet)\n",
"\n",
"path_experiment=str(args.path_experiment)\n",
"\n",
"unet_model = unet.build_model_denoise(unet_args=unet_args)\n",
"\n",
"ckpt=os.path.join(\"/content/denoising-historical-recordings\",path_experiment, 'checkpoint')\n",
"unet_model.load_weights(ckpt)\n",
"\n",
"def do_stft(noisy):\n",
" \n",
" window_fn = tf.signal.hamming_window\n",
"\n",
" win_size=args.stft[\"win_size\"]\n",
" hop_size=args.stft[\"hop_size\"]\n",
"\n",
" \n",
" stft_signal_noisy=tf.signal.stft(noisy,frame_length=win_size, window_fn=window_fn, frame_step=hop_size, pad_end=True)\n",
" stft_noisy_stacked=tf.stack( values=[tf.math.real(stft_signal_noisy), tf.math.imag(stft_signal_noisy)], axis=-1)\n",
"\n",
" return stft_noisy_stacked\n",
"\n",
"def do_istft(data):\n",
" \n",
" window_fn = tf.signal.hamming_window\n",
"\n",
" win_size=args.stft[\"win_size\"]\n",
" hop_size=args.stft[\"hop_size\"]\n",
"\n",
" inv_window_fn=tf.signal.inverse_stft_window_fn(hop_size, forward_window_fn=window_fn)\n",
"\n",
" pred_cpx=data[...,0] + 1j * data[...,1]\n",
" pred_time=tf.signal.inverse_stft(pred_cpx, win_size, hop_size, window_fn=inv_window_fn)\n",
" return pred_time\n",
"\n",
"def denoise_audio(audio):\n",
"\n",
" data, samplerate = sf.read(audio)\n",
" print(data.dtype)\n",
" #Stereo to mono\n",
" if len(data.shape)>1:\n",
" data=np.mean(data,axis=1)\n",
" \n",
" if samplerate!=44100: \n",
" print(\"Resampling\")\n",
" \n",
" data=scipy.signal.resample(data, int((44100 / samplerate )*len(data))+1) \n",
" \n",
" \n",
" \n",
" segment_size=44100*5 #20s segments\n",
"\n",
" length_data=len(data)\n",
" overlapsize=2048 #samples (46 ms)\n",
" window=np.hanning(2*overlapsize)\n",
" window_right=window[overlapsize::]\n",
" window_left=window[0:overlapsize]\n",
" audio_finished=False\n",
" pointer=0\n",
" denoised_data=np.zeros(shape=(len(data),))\n",
" residual_noise=np.zeros(shape=(len(data),))\n",
" numchunks=int(np.ceil(length_data/segment_size))\n",
" \n",
" for i in tqdm(range(numchunks)):\n",
" if pointer+segment_size\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Saving Carmen-Habanera_(Love_is_Like_a_Woo_-_Marguerite_D'Alvarez_noisy_input.wav to Carmen-Habanera_(Love_is_Like_a_Woo_-_Marguerite_D'Alvarez_noisy_input.wav\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#@title #Denoise\n",
"\n",
"#@markdown Execute this cell to denoise the uploaded file\n",
"for fn in uploaded.keys():\n",
" print('Denoising uploaded file \"{name}\"'.format(\n",
" name=fn))\n",
" denoise_data=denoise_audio(fn)\n",
" basename=os.path.splitext(fn)[0]\n",
" wav_output_name=basename+\"_denoised\"+\".wav\"\n",
" sf.write(wav_output_name, denoise_data, 44100)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"cellView": "form",
"id": "0po6zpvrylc2",
"outputId": "173f5355-2939-41fe-c702-591aa752fc7e"
},
"id": "0po6zpvrylc2",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Denoising uploaded file \"Carmen-Habanera_(Love_is_Like_a_Woo_-_Marguerite_D'Alvarez_noisy_input.wav\"\n",
"float64\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"100%|██████████| 41/41 [00:30<00:00, 1.34it/s]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#@title #Download\n",
"\n",
"#@markdown Execute this cell to download the denoised recording\n",
"files.download(wav_output_name)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 17
},
"cellView": "form",
"id": "3tEshWBezYvf",
"outputId": "54588c26-0b3c-42bf-aca2-8316ab54603f"
},
"id": "3tEshWBezYvf",
"execution_count": 7,
"outputs": [
{
"output_type": "display_data",
"data": {
"application/javascript": [
"\n",
" async function download(id, filename, size) {\n",
" if (!google.colab.kernel.accessAllowed) {\n",
" return;\n",
" }\n",
" const div = document.createElement('div');\n",
" const label = document.createElement('label');\n",
" label.textContent = `Downloading \"${filename}\": `;\n",
" div.appendChild(label);\n",
" const progress = document.createElement('progress');\n",
" progress.max = size;\n",
" div.appendChild(progress);\n",
" document.body.appendChild(div);\n",
"\n",
" const buffers = [];\n",
" let downloaded = 0;\n",
"\n",
" const channel = await google.colab.kernel.comms.open(id);\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
"\n",
" for await (const message of channel.messages) {\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
" if (message.buffers) {\n",
" for (const buffer of message.buffers) {\n",
" buffers.push(buffer);\n",
" downloaded += buffer.byteLength;\n",
" progress.value = downloaded;\n",
" }\n",
" }\n",
" }\n",
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
" const a = document.createElement('a');\n",
" a.href = window.URL.createObjectURL(blob);\n",
" a.download = filename;\n",
" div.appendChild(a);\n",
" a.click();\n",
" div.remove();\n",
" }\n",
" "
],
"text/plain": [
""
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"application/javascript": [
"download(\"download_3e3ca242-937f-408e-a16c-4aa5bb2b9e50\", \"Carmen-Habanera_(Love_is_Like_a_Woo_-_Marguerite_D'Alvarez_noisy_input_denoised.wav\", 17961334)"
],
"text/plain": [
""
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "v_FuSJ4J-WO-"
},
"id": "v_FuSJ4J-WO-",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
},
"colab": {
"name": "demo.ipynb",
"provenance": [],
"include_colab_link": true
},
"accelerator": "GPU"
},
"nbformat": 4,
"nbformat_minor": 5
}