File size: 3,187 Bytes
fe01e0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "This is the data processing script for POP909:A Pop song Dataset for Music Arrangement Generation\n",
    "============\n",
    "It will allow you to quickly process the POP909 Files (Midi) into the Google Magenta's music representation \n",
    "    as like [Music Transformer](https://magenta.tensorflow.org/music-transformer) \n",
    "            [Performance RNN](https://magenta.tensorflow.org/performance-rnn).\n",
    "\n",
    "'''\n",
    "import pickle\n",
    "import os\n",
    "import sys\n",
    "import utils\n",
    "from processor import MidiEventProcessor\n",
    "import pretty_midi as pyd\n",
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total = 0\n",
    "def preprocess_midi(path):\n",
    "    global total\n",
    "    data = pyd.PrettyMIDI(path)\n",
    "    main_notes = []\n",
    "    acc_notes = []\n",
    "    for ins in data.instruments:\n",
    "        acc_notes.extend(ins.notes)\n",
    "    for i in range(len(main_notes)):\n",
    "        main_notes[i].start = round(main_notes[i].start,2)\n",
    "        main_notes[i].end = round(main_notes[i].end,2)\n",
    "    for i in range(len(acc_notes)):\n",
    "        acc_notes[i].start = round(acc_notes[i].start,2)\n",
    "        acc_notes[i].end = round(acc_notes[i].end,2)\n",
    "    main_notes.sort(key = lambda x:x.start)\n",
    "    acc_notes.sort(key = lambda x:x.start)\n",
    "    mpr = MidiEventProcessor()\n",
    "    repr_seq = mpr.encode([main_notes, acc_notes])\n",
    "    total += len(repr_seq)\n",
    "    return repr_seq\n",
    "\n",
    "def preprocess_pop909(midi_root, save_dir):\n",
    "    save_py = []\n",
    "    midi_paths = [d for d in os.listdir(midi_root)]\n",
    "    i = 0\n",
    "    out_fmt = '{}-{}.data'\n",
    "    for path in midi_paths:\n",
    "        print(' ', end='[{}]'.format(path), flush=True)\n",
    "        filename = midi_root + path\n",
    "        try:\n",
    "            data = preprocess_midi(filename)\n",
    "        except KeyboardInterrupt:\n",
    "            print(' Abort')\n",
    "            return\n",
    "        except EOFError:\n",
    "            print('EOF Error')\n",
    "            return\n",
    "        save_py.append(data)\n",
    "    save_py = np.array(save_py)\n",
    "    print(save_py.size)\n",
    "    np.save(\"pop909-event-token.npy\", save_py)\n",
    "            \n",
    "    \n",
    "# replace the folder with your POP909 data folder\n",
    "preprocess_pop909(\"../pop909\",\"midi_data/\")"
   ]
  }
 ],
 "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}