{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "555ff0cb", "metadata": {}, "outputs": [], "source": [ "import os\n", "# If running inside \"tests\" folder, move up one level\n", "pwd = os.getcwd()\n", "if pwd.endswith(\"tests\"):\n", " os.chdir(os.path.dirname(pwd))\n", "\n", "import torch\n", "import torchaudio\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from IPython.display import Audio\n", "from marble.utils.utils import id2chord_str\n", "\n", "def display_sample_audio_and_labels(dataset: torch.utils.data.Dataset, sample_idx: int):\n", " \"\"\"\n", " Visualizes an audio sample and its corresponding chord labels from the dataset.\n", " Also supports audio playback in Jupyter Notebooks.\n", "\n", " Args:\n", " dataset (torch.utils.data.Dataset): The dataset to extract the sample from.\n", " sample_idx (int): The index of the sample to visualize.\n", " \"\"\"\n", " waveform, targets, audio_path = dataset[sample_idx]\n", " \n", " # Plot the waveform with reduced opacity for clearer visibility of labels\n", " plt.figure(figsize=(12, 6))\n", " plt.plot(np.linspace(0, len(waveform[0]) / dataset.sample_rate, len(waveform[0])), waveform[0].numpy(), color='gray', alpha=0.5)\n", " plt.title(f\"Waveform of {audio_path}\")\n", " plt.xlabel(\"Time (s)\")\n", " plt.ylabel(\"Amplitude\")\n", " \n", " # Plot the chord labels over the waveform\n", " label_seq = targets.numpy()\n", " time_axis = np.linspace(0, len(label_seq) / dataset.label_freq, len(label_seq))\n", "\n", " # Merge consecutive segments with the same chord label\n", " merged_segments = []\n", " current_label = label_seq[0]\n", " segment_start_time = time_axis[0]\n", "\n", " for i in range(1, len(label_seq)):\n", " if label_seq[i] != current_label:\n", " segment_end_time = time_axis[i]\n", " merged_segments.append((segment_start_time, segment_end_time, current_label))\n", " current_label = label_seq[i]\n", " segment_start_time = time_axis[i]\n", " \n", " # Add the last segment\n", " merged_segments.append((segment_start_time, time_axis[-1], current_label))\n", "\n", " # Display chord labels in the center of each segment\n", " for start_time, end_time, chord_label in merged_segments:\n", " # Assign color based on chord label (e.g., H chord gets a distinct color)\n", " chord_color = \"blue\" # Changed to blue for a neutral tone\n", " # Display the chord label in the center of the segment\n", " plt.text((start_time + end_time) / 2, 0.5, id2chord_str(chord_label), color=chord_color, fontsize=12, ha='center', va='center')\n", "\n", " # Draw a vertical line at the segment boundary\n", " plt.axvline(x=start_time, color='black', linestyle='-', linewidth=1)\n", " plt.axvline(x=end_time, color='black', linestyle='-', linewidth=1)\n", "\n", " # Tight layout to avoid label clipping\n", " plt.tight_layout()\n", " plt.show()\n", "\n", " # Playback the audio in the notebook\n", " audio_data = waveform.squeeze().numpy() # Remove channel dimension if mono\n", " return Audio(audio_data, rate=dataset.sample_rate)\n", "\n", "from marble.tasks.Chords1217.datamodule import Chords1217AudioTrain\n", "\n", "# Example usage:\n", "dataset = Chords1217AudioTrain(sample_rate=44100, channels=1, clip_seconds=5.0, jsonl=\"data/Chords1217/Chords1217.train.jsonl\", label_freq=10)\n", "sample_idx = 100 # Any index you'd like to inspect\n", "display_sample_audio_and_labels(dataset, sample_idx)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "81585aec", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "marble3", "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.10.16" } }, "nbformat": 4, "nbformat_minor": 5 }