Daniel@0
|
1 # Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 #
|
Daniel@0
|
3 # This program is free software; you can redistribute it and/or
|
Daniel@0
|
4 # modify it under the terms of the GNU General Public License
|
Daniel@0
|
5 # as published by the Free Software Foundation; either version 2
|
Daniel@0
|
6 # of the License, or (at your option) any later version.
|
Daniel@0
|
7 #
|
Daniel@0
|
8 # This program is distributed in the hope that it will be useful,
|
Daniel@0
|
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
11 # GNU General Public License for more details.
|
Daniel@0
|
12 #
|
Daniel@0
|
13 # You should have received a copy of the GNU General Public
|
Daniel@0
|
14 # License along with this library; if not, write to the Free Software
|
Daniel@0
|
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
16
|
Daniel@0
|
17 # -*- coding: utf-8 -*-
|
Daniel@0
|
18 __author__="hargreavess"
|
Daniel@0
|
19
|
Daniel@0
|
20 import rdflib
|
Daniel@0
|
21 from rdflib import Namespace, BNode, RDF, Literal
|
Daniel@0
|
22 from n3Parser import get_rdf_graph_from_n3
|
Daniel@0
|
23 from semitoneHistogram import find_semitone_histogram, semitone_labels
|
Daniel@0
|
24 from tonicHistogram import find_last_key_in_piece, find_most_common_key_in_piece
|
Daniel@0
|
25
|
Daniel@0
|
26 dml_ns = Namespace("http://dml.org/dml/cla#")
|
Daniel@0
|
27 perfilenorm = 1
|
Daniel@0
|
28
|
Daniel@0
|
29 # normalisation per clip ?
|
Daniel@0
|
30 perfilenorm = 1
|
Daniel@0
|
31
|
Daniel@0
|
32 # Add triples representing a "pitch histogram" result to
|
Daniel@0
|
33 # an RDF graph
|
Daniel@0
|
34 def add_tonic_norm_semitone_histogram_to_graph(semitone_histogram, output_rdf_graph, transform, sample_count, input_f_files, input_rdf_graph):
|
Daniel@0
|
35
|
Daniel@0
|
36 query = rdflib.plugins.sparql.prepareQuery(
|
Daniel@0
|
37 """SELECT ?silvet_input ?tonic_input
|
Daniel@0
|
38 WHERE {
|
Daniel@0
|
39 ?tonicNormSemitoneInput dml:silvetInputSetItem ?silvet_input .
|
Daniel@0
|
40 ?tonicNormSemitoneInput dml:tonicInputSetItem ?tonic_input .
|
Daniel@0
|
41 }""", initNs = { "dml": dml_ns })
|
Daniel@0
|
42
|
Daniel@0
|
43 output_bnode = BNode()
|
Daniel@0
|
44 output_rdf_graph.add((transform, dml_ns.output, output_bnode))
|
Daniel@0
|
45
|
Daniel@0
|
46 for transform_input in input_f_files:
|
Daniel@0
|
47
|
Daniel@0
|
48 output_rdf_graph.add((transform, dml_ns.input, transform_input))
|
Daniel@0
|
49 qres = input_rdf_graph.query(query, initBindings={'tonicNormSemitoneInput': transform_input})
|
Daniel@0
|
50
|
Daniel@0
|
51 for row in qres:
|
Daniel@0
|
52
|
Daniel@0
|
53 output_rdf_graph.add((transform_input, dml_ns.silvetInputSetItem, row.silvet_input))
|
Daniel@0
|
54 output_rdf_graph.add((transform_input, dml_ns.tonicInputSetItem, row.tonic_input))
|
Daniel@0
|
55
|
Daniel@0
|
56 output_rdf_graph.add((output_bnode, RDF.type, dml_ns.SemitoneHistogram))
|
Daniel@0
|
57 output_rdf_graph.add((output_bnode, dml_ns.sample_count, Literal(sample_count)))
|
Daniel@0
|
58
|
Daniel@0
|
59 for semitone in semitone_histogram:
|
Daniel@0
|
60
|
Daniel@0
|
61 bin_bnode = BNode()
|
Daniel@0
|
62 output_rdf_graph.add((output_bnode, dml_ns.bin, bin_bnode))
|
Daniel@0
|
63 output_rdf_graph.add((bin_bnode, dml_ns.bin_number, Literal(semitone)))
|
Daniel@0
|
64 output_rdf_graph.add((bin_bnode, dml_ns.bin_value, Literal(semitone_histogram.get(semitone))))
|
Daniel@0
|
65 output_rdf_graph.add((bin_bnode, dml_ns.bin_name, Literal(semitone_labels[semitone - 1])))
|
Daniel@0
|
66
|
Daniel@0
|
67 return output_rdf_graph
|
Daniel@0
|
68
|
Daniel@0
|
69 # Parse the transform_inputs (sets of n3 files), and generate
|
Daniel@0
|
70 # a tonic-normalised semitone histogram
|
Daniel@0
|
71 def find_cla_tonic_norm_semitone_histogram(transform_inputs, input_rdf_graph):
|
Daniel@0
|
72
|
Daniel@0
|
73 sample_count = len(transform_inputs)
|
Daniel@0
|
74 semitone_hist = dict()
|
Daniel@0
|
75
|
Daniel@0
|
76 for x in range(1, 13):
|
Daniel@0
|
77
|
Daniel@0
|
78 semitone_hist[x] = 0
|
Daniel@0
|
79
|
Daniel@0
|
80 query = rdflib.plugins.sparql.prepareQuery(
|
Daniel@0
|
81 """SELECT ?silvet_input ?tonic_input
|
Daniel@0
|
82 WHERE {
|
Daniel@0
|
83 ?tonicNormSemitoneInput dml:silvetInputSetItem ?silvet_input .
|
Daniel@0
|
84 ?tonicNormSemitoneInput dml:tonicInputSetItem ?tonic_input .
|
Daniel@0
|
85 }""", initNs = { "dml": dml_ns })
|
Daniel@0
|
86
|
Daniel@0
|
87 for transform_input in transform_inputs:
|
Daniel@0
|
88
|
Daniel@0
|
89 qres = input_rdf_graph.query(query, initBindings={'tonicNormSemitoneInput': transform_input})
|
Daniel@0
|
90
|
Daniel@0
|
91 piece_semitone_hist = []
|
Daniel@0
|
92
|
Daniel@0
|
93 for row in qres:
|
Daniel@0
|
94
|
Daniel@0
|
95 piece_semitone_hist = find_semitone_histogram(row.silvet_input, perfilenorm)
|
Daniel@0
|
96 # piece_tonic = find_last_key_in_piece(row.tonic_input)
|
Daniel@0
|
97 piece_tonic = find_most_common_key_in_piece(row.tonic_input)
|
Daniel@0
|
98 piece_semitone_hist = normalise_semitone_hist_by_tonic(piece_semitone_hist, piece_tonic)
|
Daniel@0
|
99
|
Daniel@0
|
100 for x in range(1, 13):
|
Daniel@0
|
101
|
Daniel@0
|
102 semitone_hist[x] += piece_semitone_hist[x]
|
Daniel@0
|
103
|
Daniel@0
|
104 # normalise the collection histogram by duration
|
Daniel@0
|
105 hist_total = 0
|
Daniel@0
|
106
|
Daniel@0
|
107 for semitone_bin in semitone_hist:
|
Daniel@0
|
108
|
Daniel@0
|
109 hist_total += semitone_hist[semitone_bin]
|
Daniel@0
|
110
|
Daniel@0
|
111 for semitone_bin in semitone_hist:
|
Daniel@0
|
112
|
Daniel@0
|
113 semitone_hist[semitone_bin] /= hist_total
|
Daniel@0
|
114
|
Daniel@0
|
115 return (semitone_hist, sample_count)
|
Daniel@0
|
116
|
Daniel@0
|
117 def normalise_semitone_hist_by_tonic(piece_semitone_hist, piece_tonic):
|
Daniel@0
|
118
|
Daniel@0
|
119 tonic_norm_semitone_hist = dict()
|
Daniel@0
|
120
|
Daniel@0
|
121 for semitone_bin in piece_semitone_hist:
|
Daniel@0
|
122
|
Daniel@0
|
123 shifted_bin = ((semitone_bin - piece_tonic) % 12) + 1
|
Daniel@0
|
124 tonic_norm_semitone_hist[shifted_bin] = piece_semitone_hist[semitone_bin]
|
Daniel@0
|
125
|
Daniel@0
|
126 return tonic_norm_semitone_hist
|