annotate synpy/WNBD.py @ 76:90b68f259541 tip

updated parameter_setter to be able to find the TimeSignature.pkl file without putting it in the pwd
author christopherh <christopher.harte@eecs.qmul.ac.uk>
date Wed, 13 May 2015 09:27:36 +0100
parents ef891481231e
children
rev   line source
christopher@45 1 '''
christopher@45 2 Author: Chunyang Song
christopher@45 3 Institution: Centre for Digital Music, Queen Mary University of London
christopher@45 4
christopher@45 5 '''
christopher@45 6 from basic_functions import repeat, get_note_indices
christopher@45 7
christopher@45 8 # To find the product of multiple numbers
christopher@45 9 def cumu_multiply(numbers):
christopher@45 10 product = 1
christopher@45 11 for n in numbers:
christopher@45 12 product = product*n
christopher@45 13 return product
christopher@45 14
christopher@45 15 def get_syncopation(bar, parameters = None):
christopher@45 16 syncopation = None
christopher@45 17
christopher@45 18 noteSequence = bar.get_note_sequence()
christopher@45 19 barTicks = bar.get_bar_ticks()
christopher@45 20 subdivisionSequence = bar.get_subdivision_sequence()
christopher@45 21 strongBeatLevel = bar.get_beat_level()
christopher@45 22
christopher@45 23 nextbarNoteSequence = None
christopher@45 24 if bar.get_next_bar() != None:
christopher@45 25 nextbarNoteSequence = bar.get_next_bar().get_note_sequence()
christopher@45 26
christopher@45 27 # calculate each strong beat ticks
christopher@45 28 numberOfBeats = cumu_multiply(subdivisionSequence[:strongBeatLevel+1])
christopher@45 29 beatIntervalTicks = barTicks/numberOfBeats
christopher@45 30 # beatsTicks represents the ticks for all the beats in the current bar and the first two beats in the next bar
christopher@45 31 beatsTicks = [i*beatIntervalTicks for i in range(numberOfBeats+2)]
christopher@45 32 #print beatsTicks
christopher@45 33 totalSyncopation = 0
christopher@45 34 for note in noteSequence:
christopher@45 35 # print note.to_string()
christopher@45 36 # find such beatIndex such that note.startTime is located between (including) beatsTicks[beatIndex] and (not including) beatsTicks[beatIndex+1]
christopher@45 37 beatIndex = 0
christopher@45 38 while note.startTime < beatsTicks[beatIndex] or note.startTime >= beatsTicks[beatIndex+1]:
christopher@45 39 beatIndex += 1
christopher@45 40
christopher@45 41 # print beatIndex
christopher@45 42 # calculate the distance of this note to its nearest beat
christopher@45 43 distanceToBeatOnLeft = abs(note.startTime - beatsTicks[beatIndex])/float(beatIntervalTicks)
christopher@45 44 distanceToBeatOnRight = abs(note.startTime - beatsTicks[beatIndex+1])/float(beatIntervalTicks)
christopher@45 45 distanceToNearestBeat = min(distanceToBeatOnLeft,distanceToBeatOnRight)
christopher@45 46 # print distanceToNearestBeat
christopher@45 47
christopher@45 48 # calculate the WNBD measure for this note, and add to total syncopation value for this bar
christopher@45 49 if distanceToNearestBeat == 0:
christopher@45 50 totalSyncopation += 0
christopher@45 51 # or if this note is held on past the following beat, but ends on or before the later beat
christopher@45 52 elif beatsTicks[beatIndex+1] < note.startTime+note.duration <= beatsTicks[beatIndex+2]:
christopher@45 53 totalSyncopation += float(2)/distanceToNearestBeat
christopher@45 54 else:
christopher@45 55 totalSyncopation += float(1)/distanceToNearestBeat
christopher@45 56 # print totalSyncopation
christopher@45 57
christopher@45 58 return totalSyncopation
christopher@45 59
christopher@45 60 #def get_syncopation(seq, subdivision_seq, strong_beat_level, postbar_seq):
christopher@45 61 # def get_syncopation(bar, parameters = None):
christopher@45 62 # syncopation = None
christopher@45 63
christopher@45 64 # binarySequence = bar.get_binary_sequence()
christopher@45 65 # sequenceLength = len(binarySequence)
christopher@45 66 # subdivisionSequence = bar.get_subdivision_sequence()
christopher@45 67 # strongBeatLevel = bar.get_beat_level()
christopher@45 68 # nextbarBinarySequence = None
christopher@45 69
christopher@45 70 # if bar.get_next_bar() != None:
christopher@45 71 # nextbarBinarySequence = bar.get_next_bar().get_binary_sequence()
christopher@45 72
christopher@45 73 # numberOfBeats = cumu_multiply(subdivisionSequence[0:strongBeatLevel+1]) # numberOfBeats is the number of strong beats
christopher@45 74
christopher@45 75 # if sequenceLength % numberOfBeats != 0:
christopher@45 76 # print 'Error: the length of sequence is not subdivable by the subdivision factor in subdivision sequence.'
christopher@45 77 # else:
christopher@45 78 # # Find the indices of all the strong-beats
christopher@45 79 # beatIndices = []
christopher@45 80 # beatInterval = sequenceLength / numberOfBeats
christopher@45 81 # for i in range(numberOfBeats+1):
christopher@45 82 # beatIndices.append(i*beatInterval)
christopher@45 83 # if nextbarBinarySequence != None: # if there is a postbar_seq, add another two beats index for later calculation
christopher@45 84 # beatIndices += [sequenceLength+beatInterval, sequenceLength+ 2* beatInterval]
christopher@45 85
christopher@45 86 # noteIndices = get_note_indices(binarySequence) # all the notes
christopher@45 87
christopher@45 88 # # Calculate the WNBD measure for each note
christopher@45 89 # def measure_pernote(noteIndices, nextNoteIndex):
christopher@45 90 # # Find the nearest beats where this note locates - in [beat_indices[j], beat_indices[j+1])
christopher@45 91 # j = 0
christopher@45 92 # while noteIndices < beatIndices[j] or noteIndices >= beatIndices[j+1]:
christopher@45 93 # j = j + 1
christopher@45 94
christopher@45 95 # # The distance of note to nearest beat normalised by the beat interval
christopher@45 96 # distanceToNearestBeat = min(abs(noteIndices - beatIndices[j]), abs(noteIndices - beatIndices[j+1]))/float(beatInterval)
christopher@45 97
christopher@45 98 # # if this note is on-beat
christopher@45 99 # if distanceToNearestBeat == 0:
christopher@45 100 # measure = 0
christopher@45 101 # # or if this note is held on past the following beat, but ends on or before the later beat
christopher@45 102 # elif beatIndices[j+1] < nextNoteIndex <= beatIndices[j+2]:
christopher@45 103 # measure = float(2)/distanceToNearestBeat
christopher@45 104 # else:
christopher@45 105 # measure = float(1)/distanceToNearestBeat
christopher@45 106 # return measure
christopher@45 107
christopher@45 108 # total = 0
christopher@45 109 # for i in range(len(noteIndices)):
christopher@45 110 # # if this is the last note, end_time is the index of the following note in the next bar
christopher@45 111 # if i == len(noteIndices)-1:
christopher@45 112 # # if the next bar is not none or a bar of full rest,
christopher@45 113 # # the nextNoteIndex is the sum of sequence length in the current bar and the noteIndex in the next bar
christopher@45 114 # if nextbarBinarySequence != None and nextbarBinarySequence != repeat([0],len(nextbarBinarySequence)):
christopher@45 115 # nextNoteIndex = get_note_indices(nextbarBinarySequence)[0]+sequenceLength
christopher@45 116 # # else when the next bar is none or full rest, end_time is the end of this sequence.
christopher@45 117 # else:
christopher@45 118 # nextNoteIndex = sequenceLength
christopher@45 119 # # else this is not the last note, the nextNoteIndex is the following element in the noteIndices list
christopher@45 120 # else:
christopher@45 121 # nextNoteIndex = noteIndices[i+1]
christopher@45 122 # # sum up the syncopation value for individual note at noteIndices[i]
christopher@45 123 # total += measure_pernote(noteIndices[i],nextNoteIndex)
christopher@45 124
christopher@45 125 # #syncopation = float(total) / len(note_indices)
christopher@45 126
christopher@45 127 # # return the total value, leave the normalisation done in the end
christopher@45 128 # return total