{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# This is how I needed to install madmom on my system\n", "!pip install git+https://github.com/CPJKU/madmom\n", "!pip install demucs" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import subprocess\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from tqdm import tqdm\n", "import madmom\n", "from pathlib import Path\n", "\n", "from madmom.audio.signal import FramedSignalProcessor, Signal\n", "from madmom.audio.stft import ShortTimeFourierTransformProcessor\n", "from madmom.processors import SequentialProcessor\n", "from madmom.audio.spectrogram import FilteredSpectrogramProcessor, LogarithmicSpectrogramProcessor" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# input\n", "source_mp3_path = '../../datasets/HarmonixMP3_original/'\n", "metadata_file = '../dataset/metadata.csv'\n", "\n", "# output\n", "demucs_path = './tmp_demucs'\n", "specs_path = './out_peter'\n", "\n", "# device\n", "device = 'cpu' # 'cuda', 'mps' or 'cpu'\n", "\n", "SAMPLE_RATE = 44100\n", "FPS = 100\n", "FFT_SIZE = 2048\n", "NUM_BANDS = 12\n", "\n", "# ensure path is present\n", "os.makedirs(demucs_path, exist_ok=True)\n", "os.makedirs(specs_path, exist_ok=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def get_processor():\n", " frames = FramedSignalProcessor(\n", " frame_size=FFT_SIZE,\n", " fps=FPS\n", " )\n", " stft = ShortTimeFourierTransformProcessor()\n", " filt = FilteredSpectrogramProcessor(\n", " num_bands=NUM_BANDS\n", " )\n", " spec = LogarithmicSpectrogramProcessor()\n", " processor = SequentialProcessor([frames, stft, filt, spec])\n", " return processor" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "\n", "def get_demucs_audio(id, audio_path):\n", " bass_path = f'{demucs_path}/htdemucs/{id}/bass.wav'\n", " drums_path = f'{demucs_path}/htdemucs/{id}/drums.wav'\n", " other_path = f'{demucs_path}/htdemucs/{id}/other.wav'\n", " vocals_path = f'{demucs_path}/htdemucs/{id}/vocals.wav'\n", "\n", " if os.path.exists(bass_path) and os.path.exists(drums_path) and os.path.exists(other_path) and os.path.exists(vocals_path):\n", " return bass_path, drums_path, other_path, vocals_path\n", "\n", " subprocess.run([\n", " sys.executable, '-m', 'demucs.separate',\n", " '--out', demucs_path,\n", " '--name', 'htdemucs',\n", " '--device', str(device),\n", " Path(audio_path).as_posix(),\n", " ],\n", " check=True,\n", " )\n", " \n", " return bass_path, drums_path, other_path, vocals_path\n", "\n", "def generate_spectograms(id, audio_path):\n", " spectogram_path_stems = f'{specs_path}/{id}_stems.npy'\n", " spectogram_path_full = f'{specs_path}/{id}_full.npy'\n", " if os.path.exists(spectogram_path_stems) and os.path.exists(spectogram_path_full):\n", " return True\n", " \n", " bass_path, drums_path, other_path, vocals_path = get_demucs_audio(id, audio_path)\n", " \n", " audio = Signal(audio_path, num_channels=1, sample_rate=SAMPLE_RATE)\n", " audio_bass = Signal(bass_path, num_channels=1, sample_rate=SAMPLE_RATE)\n", " audio_drums = Signal(drums_path, num_channels=1, sample_rate=SAMPLE_RATE)\n", " audio_other = Signal(other_path, num_channels=1, sample_rate=SAMPLE_RATE)\n", " audio_vocals = Signal(vocals_path, num_channels=1, sample_rate=SAMPLE_RATE)\n", "\n", " pre_processor = get_processor()\n", " spec = pre_processor(audio)\n", " spec_bass = pre_processor(audio_bass)\n", " spec_drums = pre_processor(audio_drums)\n", " spec_other = pre_processor(audio_other)\n", " spec_vocals = pre_processor(audio_vocals)\n", "\n", " spec_stems = np.stack([spec_bass, spec_drums, spec_other, spec_vocals])\n", " np.save(spectogram_path_stems, spec_stems)\n", " np.save(spectogram_path_full, spec)\n", "\n", " return True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "metadata = pd.read_csv(metadata_file)\n", "total_items = len(metadata)\n", "for index, row in tqdm(metadata.iterrows(), total=total_items, desc=\"Processing\"):\n", " id = row['File']\n", " audio_path = f'{source_mp3_path}/{id}.mp3'\n", " generate_spectograms(id, audio_path)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.18" } }, "nbformat": 4, "nbformat_minor": 4 }