diff Syncopation models/syncopation.py @ 9:c2843ef4de2c

changing filenames to Python conventions
author csong
date Fri, 03 Apr 2015 11:41:01 +0100
parents
children a3ed7d2b57d8 bc3b9022ebc4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Syncopation models/syncopation.py	Fri Apr 03 11:41:01 2015 +0100
@@ -0,0 +1,103 @@
+'''
+Author: Chunyang Song
+Institution: Centre for Digital Music, Queen Mary University of London
+
+'''
+
+
+def sync_perbar_permodel(seq, model, timesig = None, subdivision_seq = None, weight_seq = None, L_max = 5, prebar_seq = None, postbar_seq = None, strong_beat_level = None):
+	syncopation = None
+
+	if seq == None or model == None:
+		print 'Error: please indicate rhythm sequence and syncopation model.'
+
+	elif timesig == None and subdivision_seq == None:
+		print 'Error: please indicate either time signature or subdivision sequence.'
+	
+	else:
+		while subdivision_seq == None:
+			from BasicFuncs import get_subdivision_seq
+			subdivision_seq = get_subdivision_seq(timesig, L_max)
+
+		# The get_rhythm_category function is used to detect rhythm category: monorhythm or polyrhythm.
+		# For monorhythms, all prime factors of the length of minimum time-span representation of this sequence are
+		# elements of its subdivision_seq, otherwise it is polyrhythm; 
+		# e.g. prime_factors of polyrhythm 100100101010 in 4/4 is [2,3] but subdivision_seq = [1,2,2] for 4/4 
+		def get_rhythm_category():
+			rhythm_category = 'mono'
+			from BasicFuncs import get_min_timeSpan, find_prime_factors
+			for f in find_prime_factors(len(get_min_timeSpan(seq))):
+				if not (f in subdivision_seq): 
+					rhythm_category = 'poly'
+					break
+			return rhythm_category
+		
+		rhythm_category = get_rhythm_category()
+
+		if model == 'LHL':	
+			import LHL
+			if weight_seq == None:
+				weight_seq = range(0,-L_max,-1)
+			syncopation = LHL.get_syncopation(seq, subdivision_seq, weight_seq, prebar_seq, rhythm_category)
+		elif model == 'PRS':	
+			import PRS
+			syncopation = PRS.get_syncopation(seq, subdivision_seq, postbar_seq, rhythm_category)
+		elif model == 'TMC':	
+			import TMC
+			if weight_seq == None:
+				weight_seq = range(L_max+1,0,-1)
+			syncopation = TMC.get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category)
+		elif model == 'SG':		
+			import SG
+			if weight_seq == None:
+				weight_seq = range(L_max+1)
+			syncopation = SG.get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category)
+		elif model == 'KTH':
+			import KTH
+			syncopation = KTH.get_syncopation(seq, timesig, postbar_seq)
+		elif model == 'TOB':	
+			import TOB
+			syncopation = TOB.get_syncopation(seq)
+		elif model == 'WNBD':
+			import WNBD
+			if strong_beat_level == None:
+				if timesig == '4/4':
+					strong_beat_level = 2
+				else:
+					strong_beat_level = 1 
+			syncopation = WNBD.get_syncopation(seq, subdivision_seq, strong_beat_level, postbar_seq)
+
+		else:
+			print 'Error: undefined syncopation model.'
+
+	return syncopation
+
+# def syncopation_all(rhythm, model, timesig, subdivision_seq = None, weight_seq = None, L_max = 5, strong_beat_level = None):
+# 	syncopation = 0
+# 	# Chope rhythm into seq
+# 	# ...
+
+# 	for (seq_perbar in seq):
+# 		sync_perbar = syncopation_perbar(seq_perbar,model, timesig, subdivision_seq, weight_seq, L_max, strong_beat_level)
+# 		if sync_perbar != None:
+# 			syncopation = syncopation + sync_perbar
+
+# 	return syncopation
+
+
+### TESTING
+# clave = [1,0,0,1,0,0,1,0,0,0,1,0,1,0,0,0]
+# bf = [0,0,0,1,0,0,0,0,0,0,1,0]
+# rhythm = [0,1,0,1,0,1,0,1]
+# classic1 = [1,0,1,1]*3 + [1,0,0,0]
+# classic2 = [1,0,0,1]*3 + [1,0,0,0]
+# shiko = [1,0,1,1,0,1,1,0]
+# rumba = [1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0]
+# soukous = [1,0,0,1,0,0,1,0,0,0,1,1,0,0,0,0]
+# gahu = [1,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0]
+# bossanova = [1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0]
+
+# classic12 = [1,0,0,1,1,1,1,0,0,1,1,1]
+# soli = [1,0,1,0,1,0,1,0,1,1,0,1]
+
+# print sync_perbar(seq = clave, model = 'WNBD', timesig = '4/4')