Chris@1
|
1
|
Chris@9
|
2 program convertShiftedW;
|
Chris@1
|
3
|
Chris@1
|
4 /*
|
Chris@1
|
5
|
Chris@1
|
6 The shiftedW.txt file contains the data from shiftedW.mat in the
|
Chris@1
|
7 amt_mssiplca_fast repository, exported as text via MATLAB dlmwrite.
|
Chris@1
|
8
|
Chris@1
|
9 This is described as "3 sets of piano templates learned from MAPS
|
Chris@1
|
10 database".
|
Chris@1
|
11
|
Chris@1
|
12 The data consists of a 4D array of dimensions 5 x 88 x 545 x 3.
|
Chris@1
|
13
|
Chris@1
|
14 I believe these dimensions are:
|
Chris@1
|
15 * bins per semitone (5)
|
Chris@1
|
16 * semitones, or number of components (88)
|
Chris@1
|
17 * spectral profile for a single pitch template (545)
|
Chris@1
|
18 * distinct piano sources (3)
|
Chris@1
|
19
|
Chris@1
|
20 In the text file, it is formatted as 5 rows of 143880 values. Each
|
Chris@1
|
21 row contains 88 consecutive values from shiftedW(row,1,1,1) to
|
Chris@1
|
22 shiftedW(row,88,1,1), then 88 values from shiftedW(row,1,2,1) to
|
Chris@1
|
23 shiftedW(row,88,2,1), etc.
|
Chris@1
|
24
|
Chris@1
|
25 Here we load the file, convert it to a more structured JSON format,
|
Chris@9
|
26 and write it out again as templates.json; we also produce a set of
|
Chris@9
|
27 three files pianoN.csv which contain only the middle bin per
|
Chris@9
|
28 semitone for a single piano sample.
|
Chris@1
|
29
|
Chris@1
|
30 */
|
Chris@1
|
31
|
Chris@1
|
32 load yeti.experimental.json;
|
Chris@1
|
33
|
Chris@1
|
34 mat = load may.matrix;
|
Chris@1
|
35
|
Chris@1
|
36 bps = 5;
|
Chris@1
|
37 semitones = 88;
|
Chris@1
|
38 values = 545;
|
Chris@1
|
39 sources = 3;
|
Chris@1
|
40
|
Chris@1
|
41 f = openInFile "shiftedW.txt" "UTF-8";
|
Chris@1
|
42
|
Chris@1
|
43 raw = array (map do s: array (map number (strSplit "," s)) done (f.lines ()));
|
Chris@1
|
44
|
Chris@1
|
45 println "Read \(length raw) rows, length of first row is \(length (head raw))";
|
Chris@1
|
46
|
Chris@1
|
47 if length raw != 5 or length (head raw) != 88*545*3 then
|
Chris@1
|
48 failWith "Error: expected 5 rows of \(88*545*3) values each"
|
Chris@1
|
49 fi;
|
Chris@1
|
50
|
Chris@9
|
51 for [0..sources-1] do source:
|
Chris@9
|
52 o = openOutFile "piano\(source+1).csv" "UTF-8";
|
Chris@9
|
53 for [0..semitones-1] do semitone:
|
Chris@9
|
54 template = map do i:
|
Chris@9
|
55 raw[int(bps/2)][source * (semitones * values) +
|
Chris@9
|
56 i * semitones +
|
Chris@9
|
57 semitone]
|
Chris@9
|
58 done [0..values-1];
|
Chris@9
|
59 o.write ((strJoin "," (map string template)) ^ "\n");
|
Chris@9
|
60 done;
|
Chris@9
|
61 o.close ();
|
Chris@9
|
62 done;
|
Chris@9
|
63
|
Chris@1
|
64 reshaped = jsonList (concatMap
|
Chris@1
|
65 do source:
|
Chris@1
|
66 map do bin:
|
Chris@1
|
67 jsonObj [
|
Chris@1
|
68 "source": jsonNum source,
|
Chris@1
|
69 "f": jsonNum bin,
|
Chris@1
|
70 "templates": mat.json
|
Chris@1
|
71 (mat.generate do i semitone:
|
Chris@1
|
72 raw[bin][source * (semitones * values) +
|
Chris@1
|
73 i * semitones +
|
Chris@1
|
74 semitone]
|
Chris@1
|
75 done { rows = values, columns = semitones })
|
Chris@1
|
76 ]
|
Chris@1
|
77 done [0..bps-1]
|
Chris@1
|
78 done [0..sources-1]);
|
Chris@1
|
79
|
Chris@1
|
80 o = openOutFile "templates.json" "UTF-8";
|
Chris@1
|
81
|
Chris@1
|
82 o.write (jsonEncode reshaped);
|
Chris@1
|
83
|
Chris@1
|
84 o.close ();
|
Chris@1
|
85
|