Mercurial > hg > dml-open-backendtools
comparison pyspark/transforms/semitoneHistogram.py @ 0:e34cf1b6fe09 tip
commit
author | Daniel Wolff |
---|---|
date | Sat, 20 Feb 2016 18:14:24 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e34cf1b6fe09 |
---|---|
1 # Part of DML (Digital Music Laboratory) | |
2 # | |
3 # This program is free software; you can redistribute it and/or | |
4 # modify it under the terms of the GNU General Public License | |
5 # as published by the Free Software Foundation; either version 2 | |
6 # of the License, or (at your option) any later version. | |
7 # | |
8 # This program is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
13 # You should have received a copy of the GNU General Public | |
14 # License along with this library; if not, write to the Free Software | |
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | |
17 # -*- coding: utf-8 -*- | |
18 __author__="hargreavess" | |
19 | |
20 from rdflib import Namespace, BNode, RDF, Literal | |
21 from n3Parser import get_rdf_graph_from_n3 | |
22 from csvParser import get_dict_from_csv | |
23 | |
24 dml_ns = Namespace("http://dml.org/dml/cla#") | |
25 semitone_labels = ("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B") | |
26 semitone_label_codes = dict() | |
27 for octave in range(0, 11): | |
28 | |
29 semitone_idx = 1 | |
30 | |
31 for semitone_label in semitone_labels: | |
32 | |
33 semitone_label_with_octave = semitone_label + str(octave) | |
34 semitone_label_codes[semitone_label_with_octave] = semitone_idx | |
35 semitone_idx += 1 | |
36 | |
37 | |
38 # normalisation per clip ? | |
39 perfilenorm = 1 | |
40 | |
41 # Add triples representing a 'pitch histogram' result to | |
42 # an RDF graph | |
43 def add_semitone_histogram_to_graph(semitone_histogram, output_rdf_graph, transform, sample_count, input_f_files): | |
44 | |
45 output_bnode = BNode() | |
46 output_rdf_graph.add((transform, dml_ns.output, output_bnode)) | |
47 for input_f_file in input_f_files: | |
48 output_rdf_graph.add((transform, dml_ns.input, input_f_file)) | |
49 output_rdf_graph.add((output_bnode, RDF.type, dml_ns.SemitoneHistogram)) | |
50 output_rdf_graph.add((output_bnode, dml_ns.sample_count, Literal(sample_count))) | |
51 | |
52 for semitone in semitone_histogram: | |
53 | |
54 bin_bnode = BNode() | |
55 output_rdf_graph.add((output_bnode, dml_ns.bin, bin_bnode)) | |
56 output_rdf_graph.add((bin_bnode, dml_ns.bin_number, Literal(semitone))) | |
57 output_rdf_graph.add((bin_bnode, dml_ns.bin_value, Literal(semitone_histogram.get(semitone)))) | |
58 output_rdf_graph.add((bin_bnode, dml_ns.bin_name, Literal(semitone_labels[semitone - 1]))) | |
59 | |
60 return output_rdf_graph | |
61 | |
62 # Parse an input_f_file n3 file, and generate | |
63 # a semitone histogram | |
64 def find_semitone_histogram(input_f_file, perfilenorm): | |
65 | |
66 piece_semitone_hist = dict() | |
67 | |
68 for x in range(1, 13): | |
69 | |
70 piece_semitone_hist[x] = 0 | |
71 | |
72 piece_duration = 0 | |
73 | |
74 if input_f_file.endswith('.csv'): | |
75 | |
76 csv_dict = get_dict_from_csv(input_f_file, columtype = ['time','duration','pitch','velocity','label']) | |
77 | |
78 for row in csv_dict: | |
79 | |
80 duration = float(row['duration']) | |
81 piece_semitone_hist[semitone_label_codes[row['label']]] += duration | |
82 piece_duration += duration | |
83 | |
84 else: | |
85 | |
86 f_file_graph = get_rdf_graph_from_n3(input_f_file) | |
87 | |
88 qres = f_file_graph.query( | |
89 """prefix dml: <http://dml.org/dml/cla#> | |
90 prefix tl: <http://purl.org/NET/c4dm/timeline.owl#> | |
91 prefix af: <http://purl.org/ontology/af/> | |
92 SELECT ?event ?pitch ?duration | |
93 WHERE { | |
94 ?event a af:Note . | |
95 ?event event:time ?event_time . | |
96 ?event_time tl:duration ?duration . | |
97 ?event rdfs:label ?pitch . | |
98 }""") | |
99 | |
100 for row in qres: | |
101 | |
102 # parse xsd:duration type | |
103 tl_duration_str_len = len(row.duration) | |
104 tl_duration = float(row.duration[2:tl_duration_str_len-1]) | |
105 | |
106 piece_semitone_hist[semitone_label_codes[row.pitch.__str__()]] += tl_duration | |
107 piece_duration += tl_duration | |
108 | |
109 # normalise if necessary | |
110 if perfilenorm: | |
111 | |
112 for x in range(1, 13): | |
113 | |
114 piece_semitone_hist[x] /= piece_duration | |
115 | |
116 return piece_semitone_hist | |
117 | |
118 # Parse the input_f_files n3 files, and generate | |
119 # a collection-level semitone histogram | |
120 def find_cla_semitone_histogram(input_f_files): | |
121 | |
122 num_f_files = len(input_f_files) | |
123 semitone_hist = dict() | |
124 | |
125 for x in range(1, 13): | |
126 | |
127 semitone_hist[x] = 0 | |
128 | |
129 for input_f_file in input_f_files: | |
130 | |
131 piece_semitone_hist = find_semitone_histogram(input_f_file, perfilenorm) | |
132 | |
133 for x in range(1, 13): | |
134 | |
135 semitone_hist[x] += piece_semitone_hist[x] | |
136 | |
137 # normalise the collection histogram by duration | |
138 hist_total = 0 | |
139 | |
140 for semitone_bin in semitone_hist: | |
141 | |
142 hist_total += semitone_hist[semitone_bin] | |
143 | |
144 for semitone_bin in semitone_hist: | |
145 | |
146 semitone_hist[semitone_bin] /= hist_total | |
147 | |
148 return (semitone_hist, num_f_files) |