comparison dml-cla/python/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"
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
29 # map from pitch names to pitch class numbers in 0..11
30 pitch_map = { pitch_name(i,oct):i for oct in range(0,9) for i in range(0,12) }
31
32 # data conversions to a list of (pitch_name:string,duration:float) pairs representation
33
34 def transcription_from_csv(filename):
35 # we assume format time, duration, pitch, velocity, note_name
36 return csv_map_rows(filename,5,lambda row:(row[4],float(row[1])))
37
38 def transcription_from_n3(filename):
39 graph=get_rdf_graph_from_n3(filename)
40 notes = [ ( graph.value(ev, RDFS.label),
41 parse_xsd_duration(graph.value(graph.value(ev,event_ns.time), tl_ns.duration)) )
42 for ev in subject((RDF.type, af_ns.Note)) ]
43
44 def notes_histogram(notes):
45 hist = 12*[0]
46 for note in notes: hist[pitch_map[note[0]]] += note[1]
47 return hist
48
49 # Compute aggregate pitch histogram from a list of input transcriptions.
50 def aggregate(transcriptions):
51 parser_table = { 'n3':transcription_from_n3,
52 'csv':transcription_from_csv }
53
54 hist = 12*[0] # will be aggragate histogram
55 def accum(f):
56 h = notes_histogram(decode_tagged(parser_table,f))
57 total = sum(h)
58 for x in range(0, 12): hist[x] += h[x]/total
59 stats=for_each(transcriptions,accum)
60 return { 'result': discrete_hist(pitch_class_names,hist), 'stats':stats }