diff Syncopation models/WNBD.py @ 20:b959c2acb927

Refactored all models except for KTH, all past testing except for SG.
author csong <csong@eecs.qmul.ac.uk>
date Tue, 07 Apr 2015 19:05:07 +0100
parents 031e2ccb1fb6
children df1e7c378ee0
line wrap: on
line diff
--- a/Syncopation models/WNBD.py	Fri Apr 03 22:57:27 2015 +0100
+++ b/Syncopation models/WNBD.py	Tue Apr 07 19:05:07 2015 +0100
@@ -3,7 +3,7 @@
 Institution: Centre for Digital Music, Queen Mary University of London
 
 '''
-from BasicFuncs import repeat, get_note_indices
+from basic_functions import repeat, get_note_indices
 
 # To find the product of multiple numbers
 def cumu_multiply(numbers):
@@ -12,56 +12,72 @@
 		product = product*n
 	return product
 
-def get_syncopation(seq, subdivision_seq, strong_beat_level, postbar_seq):
+#def get_syncopation(seq, subdivision_seq, strong_beat_level, postbar_seq):
+def get_syncopation(bar, parameters = None):
 	syncopation = None
 	
-	num_beats = cumu_multiply(subdivision_seq[0:strong_beat_level+1])	# num_beats is the number of strong beats
+	binarySequence = bar.get_binary_sequence()
+	sequenceLength = len(binarySequence)
+	subdivisionSequence = bar.get_subdivision_sequence()
+	strongBeatLevel = bar.get_beat_level()
+	nextbarBinarySequence = None
+
+	if bar.get_next_bar() != None:
+		nextbarBinarySequence = bar.get_next_bar().get_binary_sequence()
+
+	numberOfBeats = cumu_multiply(subdivisionSequence[0:strongBeatLevel+1])	# numberOfBeats is the number of strong beats
 	
-	if len(seq)%num_beats != 0:
-		print 'Error: the length of sequence is not subdivable by the subdivision factor in subdivision_seq.'
+	if sequenceLength % numberOfBeats != 0:
+		print 'Error: the length of sequence is not subdivable by the subdivision factor in subdivision sequence.'
 	else:
 		# Find the indices of all the strong-beats
-		beat_indices = []
-		beat_interval = len(seq)/num_beats
-		for i in range(num_beats+1):
-			beat_indices.append(i*beat_interval)
-		if postbar_seq != None:		# if there is a postbar_seq, add another two beats index for later calculation
-			beat_indices += [len(seq)+beat_interval, len(seq)+ 2* beat_interval]
+		beatIndices = []
+		beatInterval = sequenceLength / numberOfBeats
+		for i in range(numberOfBeats+1):
+			beatIndices.append(i*beatInterval)
+		if nextbarBinarySequence != None:		# if there is a postbar_seq, add another two beats index for later calculation
+			beatIndices += [sequenceLength+beatInterval, sequenceLength+ 2* beatInterval]
 
-		note_indices = get_note_indices(seq)	# all the notes
+		noteIndices = get_note_indices(binarySequence)	# all the notes
 
 		# Calculate the WNBD measure for each note
-		def measure_pernote(note_index, nextnote_index):
+		def measure_pernote(noteIndices, nextNoteIndex):
 			# Find the nearest beats where this note locates - in [beat_indices[j], beat_indices[j+1]) 
 			j = 0
-			while note_index < beat_indices[j] or note_index >= beat_indices[j+1]:
+			while noteIndices < beatIndices[j] or noteIndices >= beatIndices[j+1]:
 				j = j + 1
 			
 			# The distance of note to nearest beat normalised by the beat interval
-			distance_nearest_beat = min(abs(note_index - beat_indices[j]), abs(note_index - beat_indices[j+1]))/float(beat_interval)
+			distanceToNearestBeat = min(abs(noteIndices - beatIndices[j]), abs(noteIndices - beatIndices[j+1]))/float(beatInterval)
 
 			# if this note is on-beat
-			if distance_nearest_beat == 0:	
+			if distanceToNearestBeat == 0:	
 				measure = 0
 			# or if this note is held on past the following beat, but ends on or before the later beat  
-			elif beat_indices[j+1] < nextnote_index <= beat_indices[j+2]:
-				measure = float(2)/distance_nearest_beat
+			elif beatIndices[j+1] < nextNoteIndex <= beatIndices[j+2]:
+				measure = float(2)/distanceToNearestBeat
 			else:
-				measure = float(1)/distance_nearest_beat
-
+				measure = float(1)/distanceToNearestBeat
 			return measure
 
 		total = 0
-		for i in range(len(note_indices)):
-			if i == len(note_indices)-1:# if this is the last note, end_time is the index of the following note in the next bar
-				if postbar_seq != None and postbar_seq != repeat([0],len(postbar_seq)):
-					nextnote_index = get_note_indices(postbar_seq)[0]+len(seq)
-				else:	# or if the next bar is none or full rest, end_time is the end of this sequence.
-					nextnote_index = len(seq)
+		for i in range(len(noteIndices)):
+			# if this is the last note, end_time is the index of the following note in the next bar
+			if i == len(noteIndices)-1:
+				# if the next bar is not none or a bar of full rest, 
+				# the nextNoteIndex is the sum of sequence length in the current bar and the noteIndex in the next bar
+				if nextbarBinarySequence != None and nextbarBinarySequence != repeat([0],len(nextbarBinarySequence)):
+					nextNoteIndex = get_note_indices(nextbarBinarySequence)[0]+sequenceLength
+				# else when the next bar is none or full rest, end_time is the end of this sequence.
+				else:
+					nextNoteIndex = sequenceLength
+			# else this is not the last note, the nextNoteIndex is the following element in the noteIndices list
 			else:
-				nextnote_index = note_indices[i+1]
-			total += measure_pernote(note_indices[i],nextnote_index)
+				nextNoteIndex = noteIndices[i+1]
+			# sum up the syncopation value for individual note at noteIndices[i]
+			total += measure_pernote(noteIndices[i],nextNoteIndex)
 
 		#syncopation = float(total) / len(note_indices)
 
+	# return the total value, leave the normalisation done in the end
 	return total