annotate collection_analysis/chord_sequence_mining/chordtable_relative.py @ 0:e34cf1b6fe09 tip

commit
author Daniel Wolff
date Sat, 20 Feb 2016 18:14:24 +0100
parents
children
rev   line source
Daniel@0 1 # Part of DML (Digital Music Laboratory)
Daniel@0 2 # Copyright 2014-2015 Daniel Wolff, City University
Daniel@0 3
Daniel@0 4 # This program is free software; you can redistribute it and/or
Daniel@0 5 # modify it under the terms of the GNU General Public License
Daniel@0 6 # as published by the Free Software Foundation; either version 2
Daniel@0 7 # of the License, or (at your option) any later version.
Daniel@0 8 #
Daniel@0 9 # This program is distributed in the hope that it will be useful,
Daniel@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 12 # GNU General Public License for more details.
Daniel@0 13 #
Daniel@0 14 # You should have received a copy of the GNU General Public
Daniel@0 15 # License along with this library; if not, write to the Free Software
Daniel@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 17
Daniel@0 18 #!/usr/bin/python
Daniel@0 19 # -*- coding: utf-8 -*-
Daniel@0 20 __author__="Daniel Wolff"
Daniel@0 21
Daniel@0 22 #Generates look-up table for possible chords relative to
Daniel@0 23
Daniel@0 24 import chord2function as c2f
Daniel@0 25 import csv
Daniel@0 26
Daniel@0 27 HUMAN_READEABLE = 1;
Daniel@0 28
Daniel@0 29 SAVE_CSV = 1
Daniel@0 30 SAVE_PICKLE = 0
Daniel@0 31 SAVE_VOCABULARY = 0
Daniel@0 32
Daniel@0 33 chordmapfilename = 'relative-chordmap.cmap'
Daniel@0 34 chordmapfilename_csv = 'relative-chordmap.csv'
Daniel@0 35 chordino_chords_vocabulary = 'relative-chord-vocabulary.txt'
Daniel@0 36
Daniel@0 37 # initialise chord dictionary
Daniel@0 38 chord_dic = dict();
Daniel@0 39
Daniel@0 40 chord_dic[0] = 'N'
Daniel@0 41 # major and minor pieces are separated by chords
Daniel@0 42 for mode, mode_lbl in enumerate(c2f.mode_lbls):
Daniel@0 43 for fun in range(0,len(c2f.root_funs_maj)):
Daniel@0 44 for type, type_lbl in enumerate(c2f.type_labels):
Daniel@0 45 for base, base_lbl in enumerate(c2f.base_labels):
Daniel@0 46 if base_lbl:
Daniel@0 47 idx = c2f.fun2num(fun,type, base,mode)
Daniel@0 48 if HUMAN_READEABLE:
Daniel@0 49 chord_dic[idx] = c2f.fun2txt(fun,type, base,mode)
Daniel@0 50 else:
Daniel@0 51 chord_dic[idx] = idx
Daniel@0 52
Daniel@0 53 print chord_dic
Daniel@0 54 T = len(chord_dic)
Daniel@0 55
Daniel@0 56 if SAVE_PICKLE:
Daniel@0 57 with open(chordmapfilename,'w') as fh:
Daniel@0 58 print "Writing chordmap file."
Daniel@0 59 pickle.dump(chord_dic,fh)
Daniel@0 60
Daniel@0 61 if SAVE_CSV:
Daniel@0 62 csvfile = open(chordmapfilename_csv, "w+b") #opens the file for updating
Daniel@0 63 w = csv.writer(csvfile, delimiter='\t')
Daniel@0 64 w.writerow(["chordid"] + ["chordlabel"])
Daniel@0 65 for chordid,chordlabel in chord_dic.items():
Daniel@0 66 w.writerow(["%s"%chordid] + ["%s"%chordlabel])
Daniel@0 67 csvfile.close()
Daniel@0 68 print("CSV chord map file %s written."%chordmapfilename_csv)
Daniel@0 69
Daniel@0 70 if SAVE_VOCABULARY:
Daniel@0 71 #creates a file with each chords on separate lines to have a vocabulary which can be used with topic modeling techniques
Daniel@0 72 #(e.g. turbotopics)
Daniel@0 73 csvfile = open(chordino_chords_vocabulary, "w+b") #opens the file for updating
Daniel@0 74 w = csv.writer(csvfile, delimiter='\t')
Daniel@0 75 for chordid,chordlabel in chord_dic.items():
Daniel@0 76 w.writerow(["%s"%chordlabel])
Daniel@0 77 csvfile.close()
Daniel@0 78 print("Chord vocabulary file %s written."%chordino_chords_vocabulary)
Daniel@0 79
Daniel@0 80
Daniel@0 81
Daniel@0 82