{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from pathlib import Path\n", "import numpy as np\n", "import util as u\n", "import json\n", "import pickle\n", "\n", "\n", "BASE_PATH = \"../\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "all_df = pd.read_csv(Path(BASE_PATH,\"metadata.csv\"))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def is_manually_checked(row):\n", " #check based on the audio click files\n", " if Path(BASE_PATH,row[\"midi_performance\"][:-4]+ \"_click.wav\").exists():\n", " return False\n", " else:\n", " return True\n", " \n", "def key_number_from_number_of_sharps(nos):\n", " if nos >= 0:\n", " return int((nos * 7)%12)\n", " else:\n", " return int((np.abs(nos)*5)%12)\n", " \n", "type(key_number_from_number_of_sharps(-3))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def get_beats_from_txt(ann_path):\n", " ann_df = pd.read_csv(Path(BASE_PATH,ann_path),header=None, names=[\"time\",\"time2\",\"type\"],sep='\\t')\n", " return ann_df[\"time\"].tolist()\n", "\n", "def get_downbeats_from_txt(ann_path):\n", " ann_df = pd.read_csv(Path(BASE_PATH,ann_path),header=None, names=[\"time\",\"time2\",\"type\"],sep='\\t')\n", " downbeats = [a[\"time\"] for i,a in ann_df.iterrows() if a[\"type\"].split(\",\")[0] == \"db\"]\n", " return downbeats\n", "\n", "def get_beats_db_dict_from_txt(ann_path):\n", " ann_df = pd.read_csv(Path(BASE_PATH,ann_path),header=None, names=[\"time\",\"time2\",\"type\"],sep='\\t')\n", " out_dict = {str(a[\"time\"]): a[\"type\"].split(\",\")[0] for i,a in ann_df.iterrows()}\n", " return out_dict\n", "\n", "def get_key_from_txt(ann_path):\n", " ann_df = pd.read_csv(Path(BASE_PATH,ann_path),header=None, names=[\"time\",\"time2\",\"type\"],sep='\\t')\n", " keys = {}\n", " for i, r in ann_df.iterrows():\n", " if len(r[\"type\"].split(\",\"))==3:\n", " number_of_sharps = int(r[\"type\"].split(\",\")[2])\n", " key_number = key_number_from_number_of_sharps(number_of_sharps)\n", " keys[str(r[\"time\"])] = [key_number, number_of_sharps]\n", " return keys\n", "\n", "def get_ts_from_txt(ann_path):\n", " ann_df = pd.read_csv(Path(BASE_PATH,ann_path),header=None, names=[\"time\",\"time2\",\"type\"],sep='\\t')\n", " tss = {}\n", " for i, r in ann_df.iterrows():\n", " if len(r[\"type\"].split(\",\"))>1 and r[\"type\"].split(\",\")[1]!=\"\":\n", " ts = r[\"type\"].split(\",\")[1]\n", " beat_n = int(u.ts2n_of_beats(ts))\n", " tss[str(r[\"time\"])] = [ts, beat_n]\n", " return tss\n", "\n", "def midi_and_score_aligned(row):\n", " return len(get_beats_from_txt(row[\"performance_annotations\"])) == len(get_beats_from_txt(row[\"midi_score_annotations\"]))\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Creating json\n", "###########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################" ] } ], "source": [ "asap_ann = {}\n", "print(\"Creating json\")\n", "\n", "counter = 0\n", "for i,row in all_df.iterrows():\n", " asap_ann[row[\"midi_performance\"]] = {\n", " \"performance_beats\" : get_beats_from_txt(row[\"performance_annotations\"]),\n", " \"performance_downbeats\" : get_downbeats_from_txt(row[\"performance_annotations\"]),\n", " \"performance_beats_type\" : get_beats_db_dict_from_txt(row[\"performance_annotations\"]),\n", " \"perf_time_signatures\" : get_ts_from_txt(row[\"performance_annotations\"]),\n", " \"perf_key_signatures\" : get_key_from_txt(row[\"performance_annotations\"]),\n", " \"midi_score_beats\" : get_beats_from_txt(row[\"midi_score_annotations\"]),\n", " \"midi_score_downbeats\" : get_downbeats_from_txt(row[\"midi_score_annotations\"]),\n", " \"midi_score_beats_type\" : get_beats_db_dict_from_txt(row[\"midi_score_annotations\"]),\n", " \"midi_score_time_signatures\" : get_ts_from_txt(row[\"midi_score_annotations\"]),\n", " \"midi_score_key_signatures\" : get_key_from_txt(row[\"midi_score_annotations\"]),\n", " \"downbeats_score_map\" : u.same_number_of_measures_with_repetitions(row[\"xml_score\"], str(Path(BASE_PATH,row[\"midi_score_annotations\"])), base_path=BASE_PATH),\n", " \"score_and_performance_aligned\" : midi_and_score_aligned(row) ,\n", "# \"manually_checked\" : is_manually_checked(row)\n", " }\n", " counter +=1\n", " print(\"#\",end=\"\")\n", " " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [], "source": [ "with open(Path('../asap_annotations.json'), 'w') as outfile:\n", " json.dump(asap_ann, outfile)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 4 }