Daniel@0: # Part of DML (Digital Music Laboratory) Daniel@0: # Copyright 2014-2015 Steven Hargreaves; Samer Abdallah, University of London Daniel@0: Daniel@0: # This program is free software; you can redistribute it and/or Daniel@0: # modify it under the terms of the GNU General Public License Daniel@0: # as published by the Free Software Foundation; either version 2 Daniel@0: # of the License, or (at your option) any later version. Daniel@0: # Daniel@0: # This program is distributed in the hope that it will be useful, Daniel@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: # GNU General Public License for more details. Daniel@0: # Daniel@0: # You should have received a copy of the GNU General Public Daniel@0: # License along with this library; if not, write to the Free Software Daniel@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: Daniel@0: # -*- coding: utf-8 -*- Daniel@0: __author__="hargreavess, abdallahs" Daniel@0: Daniel@0: import sys Daniel@0: from csvutils import * Daniel@0: from pitchutils import * Daniel@0: from aggregate import * Daniel@0: from rdflib import RDF, RDFS Daniel@0: from rdfutils import parse_xsd_duration, event_ns, tl_ns, af_ns Daniel@0: from n3Parser import get_rdf_graph_from_n3 Daniel@0: Daniel@0: # map from pitch names to pitch class numbers in 0..11 Daniel@0: pitch_map = { pitch_name(i,oct):i for oct in range(0,9) for i in range(0,12) } Daniel@0: Daniel@0: # data conversions to a list of (pitch_name:string,duration:float) pairs representation Daniel@0: Daniel@0: def transcription_from_csv(filename): Daniel@0: # we assume format time, duration, pitch, velocity, note_name Daniel@0: return csv_map_rows(filename,5,lambda row:(row[4],float(row[1]))) Daniel@0: Daniel@0: def transcription_from_n3(filename): Daniel@0: graph=get_rdf_graph_from_n3(filename) Daniel@0: notes = [ ( graph.value(ev, RDFS.label), Daniel@0: parse_xsd_duration(graph.value(graph.value(ev,event_ns.time), tl_ns.duration)) ) Daniel@0: for ev in subject((RDF.type, af_ns.Note)) ] Daniel@0: Daniel@0: def notes_histogram(notes): Daniel@0: hist = 12*[0] Daniel@0: for note in notes: hist[pitch_map[note[0]]] += note[1] Daniel@0: return hist Daniel@0: Daniel@0: # Compute aggregate pitch histogram from a list of input transcriptions. Daniel@0: def aggregate(transcriptions): Daniel@0: parser_table = { 'n3':transcription_from_n3, Daniel@0: 'csv':transcription_from_csv } Daniel@0: Daniel@0: hist = 12*[0] # will be aggragate histogram Daniel@0: def accum(f): Daniel@0: h = notes_histogram(decode_tagged(parser_table,f)) Daniel@0: total = sum(h) Daniel@0: for x in range(0, 12): hist[x] += h[x]/total Daniel@0: stats=for_each(transcriptions,accum) Daniel@0: return { 'result': discrete_hist(pitch_class_names,hist), 'stats':stats }