view 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
line wrap: on
line source
# Part of DML (Digital Music Laboratory)
# Copyright 2014-2015 Daniel Wolff, City University
 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__="Daniel Wolff"

#Generates look-up table for possible chords relative to 

import chord2function as c2f
import csv

HUMAN_READEABLE = 1;

SAVE_CSV = 1
SAVE_PICKLE = 0
SAVE_VOCABULARY = 0

chordmapfilename = 'relative-chordmap.cmap'
chordmapfilename_csv = 'relative-chordmap.csv'
chordino_chords_vocabulary = 'relative-chord-vocabulary.txt'

# initialise chord dictionary
chord_dic = dict();

chord_dic[0] = 'N'
 # major and minor pieces are separated by chords
for mode, mode_lbl in enumerate(c2f.mode_lbls):
    for fun in range(0,len(c2f.root_funs_maj)):
        for type, type_lbl in enumerate(c2f.type_labels): 
            for base, base_lbl in enumerate(c2f.base_labels): 
                if base_lbl:
                    idx = c2f.fun2num(fun,type, base,mode)
                    if HUMAN_READEABLE:
                        chord_dic[idx] = c2f.fun2txt(fun,type, base,mode)
                    else:
                        chord_dic[idx] = idx
	
print chord_dic
T = len(chord_dic)

if SAVE_PICKLE:
	with open(chordmapfilename,'w') as fh:
		print "Writing chordmap file."
		pickle.dump(chord_dic,fh)

if SAVE_CSV:
	csvfile = open(chordmapfilename_csv, "w+b") #opens the file for updating
	w = csv.writer(csvfile, delimiter='\t')
	w.writerow(["chordid"] + ["chordlabel"])
	for chordid,chordlabel in chord_dic.items():
		w.writerow(["%s"%chordid] + ["%s"%chordlabel])		
	csvfile.close()
	print("CSV chord map file %s written."%chordmapfilename_csv)	

if SAVE_VOCABULARY:
	#creates a file with each chords on separate lines to have a vocabulary which can be used with topic modeling techniques
	#(e.g. turbotopics)
	csvfile = open(chordino_chords_vocabulary, "w+b") #opens the file for updating
	w = csv.writer(csvfile, delimiter='\t')
	for chordid,chordlabel in chord_dic.items():
		w.writerow(["%s"%chordlabel])		
	csvfile.close()
	print("Chord vocabulary file %s written."%chordino_chords_vocabulary)