{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Create Melspectrogram from audio\n\nMelspectrogram is one of the method of feature extraction from audio data. \n\nThis notebook introduce show how to make melspectrogram from audio and make melspectrogram dataset. \n\n### Please Upvote if you Find this Useful :)","metadata":{}},{"cell_type":"markdown","source":"## Import package","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\n\nimport torch\nimport torchaudio\nimport torchaudio.transforms as T\nimport librosa","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T09:46:47.869521Z","iopub.execute_input":"2024-04-04T09:46:47.870065Z","iopub.status.idle":"2024-04-04T09:46:47.878410Z","shell.execute_reply.started":"2024-04-04T09:46:47.869999Z","shell.execute_reply":"2024-04-04T09:46:47.876855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define some function","metadata":{}},{"cell_type":"code","source":"def plot_melspectrogram(spec, title=None, ylabel=\"freq_bin\", aspect=\"auto\", xmax=None):\n    fig, axs = plt.subplots(1, 1, figsize=(10, 20))\n    axs.set_title(title or \"Spectrogram (db)\")\n    axs.set_ylabel(ylabel)\n    axs.set_xlabel(\"frame\")\n    im = axs.imshow(spec, origin=\"lower\", aspect=aspect)\n    if xmax:\n        axs.set_xlim((0, xmax))\n    plt.show(block=False)\n    \n# crop audio data with 5 sec (https://www.kaggle.com/code/nischaydnk/split-creating-melspecs-stage-1)    \ndef crop_or_pad(y, length, is_train=True, start=None):\n    if len(y) < length:\n        y = np.concatenate([y, np.zeros(length - len(y))])\n        \n        n_repeats = length // len(y)\n        epsilon = length % len(y)\n        \n        y = np.concatenate([y]*n_repeats + [y[:epsilon]])\n        \n    elif len(y) > length:\n        if not is_train:\n            start = start or 0\n        else:\n            start = start or np.random.randint(len(y) - length)\n\n        y = y[start:start + length]\n    return y\n\ndef create_melspectrogram(audio_path, sample_rate=32000, n_mels=1287, n_fft=2048, hop_length=512):\n    waveform, sample_rate = librosa.load(audio_path, sr=sample_rate)\n\n    sample_rate=sample_rate\n    n_mels = n_mels\n    n_fft = n_fft\n    hop_length = hop_length\n\n    eps = 1e-6\n    waveform = crop_or_pad(waveform, length=5*sample_rate)\n    spec = librosa.feature.melspectrogram(y=waveform, sr=sample_rate, hop_length=hop_length, \n            n_fft=n_fft, n_mels=128, fmin=0, fmax=None)\n    spec = librosa.power_to_db(spec, ref=np.max).astype(np.float32)\n    spec = (spec - spec.mean()) / (spec.std() + eps)\n    \n    return spec","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:05:47.364790Z","iopub.execute_input":"2024-04-04T10:05:47.365327Z","iopub.status.idle":"2024-04-04T10:05:47.382325Z","shell.execute_reply.started":"2024-04-04T10:05:47.365286Z","shell.execute_reply":"2024-04-04T10:05:47.380811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Display exmaple of melspectrogram","metadata":{}},{"cell_type":"code","source":"spec = create_melspectrogram(\"/kaggle/input/birdclef-2024/train_audio/bkskit1/XC350252.ogg\")\nplot_melspectrogram(spec, title=f'XC350252.ogg shape:{spec.shape}', aspect=2)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T10:05:47.907873Z","iopub.execute_input":"2024-04-04T10:05:47.908766Z","iopub.status.idle":"2024-04-04T10:05:48.674953Z","shell.execute_reply.started":"2024-04-04T10:05:47.908723Z","shell.execute_reply":"2024-04-04T10:05:48.671633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load train_metadata.csv","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/birdclef-2024/train_metadata.csv\")\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:08:07.474534Z","iopub.execute_input":"2024-04-04T10:08:07.475090Z","iopub.status.idle":"2024-04-04T10:08:07.634654Z","shell.execute_reply.started":"2024-04-04T10:08:07.475052Z","shell.execute_reply":"2024-04-04T10:08:07.633233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make melspectrogram dataset","metadata":{}},{"cell_type":"code","source":"try:\n    os.mkdir(\"/kaggle/working/bc24_melspectrogram\")\nexcept:\n    pass\n\naudio_dirs = os.listdir(\"/kaggle/input/birdclef-2024/train_audio\")\n\nfor dir_name in tqdm(audio_dirs):\n    output_dir = os.path.join(\"/kaggle/working/bc24_melspectrogram\", dir_name)\n    try:\n        os.mkdir(output_dir)\n    except:\n        pass\n    audio_files = glob.glob(\"/kaggle/input/birdclef-2024/train_audio/\" + dir_name + \"/*.ogg\")\n    for audio_file in audio_files:\n        spec = create_melspectrogram(audio_file)\n        output_file_name = os.path.join(output_dir, audio_file.split(\"/\")[-1].split(\".\")[0])\n        np.save(output_file_name, spec)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:11:02.733743Z","iopub.execute_input":"2024-04-04T10:11:02.735415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Display example of created melspectrogram","metadata":{}},{"cell_type":"code","source":"spec = np.load(\"/kaggle/working/bc24_melspectrogram/redspu1/XC325427.npy\")\nplot_melspectrogram(spec, title=f'/kaggle/working/bc24_melspectrogram/redspu1/XC325427.npy shape:{spec.shape}', aspect=2)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T10:10:51.528536Z","iopub.execute_input":"2024-04-04T10:10:51.529094Z","iopub.status.idle":"2024-04-04T10:10:52.176166Z","shell.execute_reply.started":"2024-04-04T10:10:51.529054Z","shell.execute_reply":"2024-04-04T10:10:52.175182Z"},"trusted":true},"execution_count":null,"outputs":[]}]}