File size: 7,519 Bytes
151b875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{
 "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
}