{ "cells": [ { "cell_type": "code", "execution_count": 6, "id": "2fd534c4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'output.jams'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#!/usr/bin/env python\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "from glob import glob\n", "import librosa\n", "import jams\n", "infile = 'experiment.wav'\n", "#infile = '05_SS1-68-E_solo_mic.wav'\n", "def beat_track(infile, outfile):\n", "\n", " # Load the audio file\n", " y, sr = librosa.load(infile)\n", "\n", " # Compute the track duration\n", " track_duration = librosa.get_duration(y=y, sr=sr)\n", "\n", " # Extract tempo and beat estimates\n", " tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)\n", "\n", " # Convert beat frames to time\n", " beat_times = librosa.frames_to_time(beat_frames, sr=sr)\n", "\n", " # Construct a new JAMS object and annotation records\n", " jam = jams.JAMS()\n", "\n", " # Store the track duration\n", " jam.file_metadata.duration = track_duration\n", "\n", " beat_a = jams.Annotation(namespace='beat')\n", " beat_a.annotation_metadata = jams.AnnotationMetadata(data_source='librosa beat tracker')\n", "\n", " # Add beat timings to the annotation record.\n", " # The beat namespace does not require value or confidence fields,\n", " # so we can leave those blank.\n", " for t in beat_times:\n", " beat_a.append(time=t, duration=0.0)\n", "\n", " # Store the new annotation in the jam\n", " jam.annotations.append(beat_a)\n", "\n", " # Add tempo estimation to the annotation.\n", " tempo_a = jams.Annotation(namespace='tempo', time=0, duration=track_duration)\n", " tempo_a.annotation_metadata = jams.AnnotationMetadata(data_source='librosa tempo estimator')\n", "\n", " # The tempo estimate is global, so it should start at time=0 and cover the full\n", " # track duration.\n", " # If we had a likelihood score on the estimation, it could be stored in\n", " # `confidence`. Since we have no competing estimates, we'll set it to 1.0.\n", " tempo_a.append(time=0.0,\n", " duration=track_duration,\n", " value=tempo,\n", " confidence=1.0)\n", "\n", " # Store the new annotation in the jam\n", " jam.annotations.append(tempo_a)\n", "\n", " # Save to disk\n", " jam.save(outfile)\n", " return outfile\n", "\n", "beat_track(infile, 'output1.jams')\n", "'output.jams'\n", "#if __name__ == '__main__':\n", "\n", "# infile = librosa.util.example_audio_file()\n", "# beat_track(infile, 'output.jams')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "644bef5d", "metadata": {}, "outputs": [], "source": [] } ], "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.8.12" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }