comparison dml-cla/python/tonic_norm_semitone_hist.py @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 # Part of DML (Digital Music Laboratory)
2 # Copyright 2014-2015 Steven Hargreaves; Samer Abdallah, University of London
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 # -*- coding: utf-8 -*-
19 __author__="hargreavess, abdallahs, wolffd"
20
21 import sys
22 from csvutils import *
23 from pitchutils import *
24 from aggregate import *
25 from rdflib import RDF, RDFS
26 from rdfutils import parse_xsd_duration, event_ns, tl_ns, af_ns
27 from n3Parser import get_rdf_graph_from_n3
28 from semitone_hist import notes_histogram, transcription_from_csv, transcription_from_n3
29 from key_tonic_hist import tonic_from_n3, tonic_from_csv
30
31 # Compute aggregate pitch histogram from a list of input transcriptions.
32 def aggregate(transcriptions_tonics,opts):
33 parser_table = { 'n3':transcription_from_n3,
34 'csv':transcription_from_csv }
35
36 hist = 12*[0] # will be aggragate histogram
37 def add_no_norm(h):
38 for x in range(0, 12): hist[x] += h[x]
39
40 def add_norm(h):
41 total=sum(h)
42 for x in range(0, 12): hist[x] += h[x]/total
43
44 if opts['normalisation']=='piece': add_to_hist=add_norm
45 else: add_to_hist=add_no_norm
46
47 def accum(f):
48 # subtract 1 from tonic because tonic_from_csv uses range 1 to 12 whilst this script uses 0 to 11
49 add_to_hist( rotate_left( notes_histogram(decode_tagged(parser_table,f['transcription'])),
50 argmax(find_tonic_histogram(f['tonic']['value'],f['duration']))))
51
52 # do_stuff
53 stats=for_each(transcriptions_tonics,accum)
54 return { 'result': discrete_hist([str(i) for i in range(1,13)],hist), 'stats':stats }
55
56 def rotate_left(x,n): return x[n:]+x[:n]
57
58 def argmax(x): return max(range(0,len(x)),key=x.__getitem__)
59
60 # Parse the qm-keydetector_tonic csv file, and generate
61 # a tonic histogram
62 def find_tonic_histogram(input_f_file,duration):
63 tonic_hist = 12*[0]
64 # ['time','keynr','label'] -> [time:float, keynr:in(range(0,12))]
65 data = csv_map_columns(input_f_file,3,[lambda r:float(r[0]), lambda r:int(r[1])-1])
66
67 # build duration weighted histogram
68 for idx in range(1,len(data[0]) ):
69 tonic_hist[data[1][idx-1]] += data[0][idx] - data[0][idx-1]
70
71 # add last tonic if duration is given
72 if duration>0:
73 tonic_hist[data[1][-1]] += duration - data[0][-1]
74
75 return tonic_hist
76