Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

python - Music program in Jython does not run

Please excuse the faulty logic used throughout this code (I am a musician that is unable to find a program that will do what I want). This is what I am trying to accomplish in this music program using Jython:

The program generates 3000 notes (line 81). It starts by generating 10 notes (lines 83 – 105). A choice is made to select a pitch (line 85) and loops in case the pitch is out of range. After 10 pitches have been generated, it generates a sequence of pitches that are transposed (lines 107 – 189). A choice is made to select a transposition (line 109) and loops in case the transposition creates a sequence in which one of the members of the sequence is out of range. Another choice is made to select the number of pitches in the sequence (line 145). Another 10 notes are set to be generated (line 181) and loops to check if less than 3000 notes have been generated. Currently, I am getting a “no viable alternative at input ‘t’” which I am unable to correct, but as you can probably tell from the code, it is probably the least of my worries. This is my code:

from music import *

from random import *

solo = Phrase()

solo.setTempo(90)

lastFiveSoloPitchesForSequence = []

sequenceChoice = []

twoPitchSequence = []

threePitchSequence = []

fourPitchSequence = []

fivePitchSequence = []

sequenceChoice = []

t = 0

transpositionOptions = [(-2), (-1), 1, 2]

a = 0 # index for transposed first pitch in sequence
    
b = 0 # index for transposed second pitch in sequence
    
c = 0 # index for transposed third pitch in sequence
    
d = 0 # index for transposed fourth pitch in sequence
    
e = 0 # index for transposed fifth pitch in sequence 

indexOfFirstPitchInSequenceInListOfAvailablePitches = 0

firstPitchInSequence = 0

indexOfSecondPitchInSequenceInListOfAvailablePitches = 0

secondPitchInSequence = 0

indexOfThirdPitchInSequenceInListOfAvailablePitches = 0

thirdPitchInSequence = 0

indexOfFourthPitchInSequenceInListOfAvailablePitches = 0

fourthPitchInSequence = 0

indexOfFifthPitchInSequenceInListOfAvailablePitches = 0

fifthPitchInSequence = 0

sequenceOptions = [1, 2, 3, 4]

durations = []

pitches = []

pitchIndex = 0

counter = 0

numberOfNotesInSolo = 0

listOfAvailablePitches = [83, 84, 86, 87, 89, 91, 94, 96, 97, 99, 100, 102, 104]

pitch = choice(listOfAvailablePitches)

pitchIndex = listOfAvailablePitches.index(pitch)

weightedProbabilitiesForPitches = [{pitchIndex + 1} * 60 + {pitchIndex - 1} * 60 + {pitchIndex + 2} * 50 + {pitchIndex - 2} * 50 + {pitchIndex + 3} * 40 + {pitchIndex - 3} * 40 + {pitchIndex + 4} * 20 + {pitchIndex - 4} * 20 + {pitchIndex + 5} * 10 + {pitchIndex - 5} * 10]        

while numberOfNotesInSolo < 3000:
   
   while counter < 10:
   
      pitchIndex = choice(weightedProbabilitiesForPitches)         
  
      if pitchIndex < 13 and pitchIndex > -1:
      
         pitch = listOfAvailablePitches[pitchIndex]
   
         pitches.append(pitch)
   
         weightedProbabilitiesForDurations = [SN] * 150 + [EN] * 50 + [DEN] * 25 + [QN] * 0 + [DQN] * 0

         duration = choice(weightedProbabilitiesForDurations)
   
         durations.append(duration)
    
         numberOfNotesInSolo = numberOfNotesInSolo + 1
         
         counter = counter + 1
         
      else:
         
         pitchIndex = choice(weightedProbabilitiesForPitches)
   
   lastFiveSoloPitchesForSequence = [pitches[-5], pitches[-4], pitches[-3], pitches[-2], [pitches[-1]]
   
   t = choice(transpositionOptions)
   
   fivePitchSequence = [pitches[-5], pitches[-4], pitches[-3], pitches[-2], pitches[-1]]
         
   indexOfFirstPitchInSequenceInListOfAvailablePitches = listOfAvailablePitches.index{pitches[-5]}
         
   a = indexOfFirstPitchInSequenceInListOfAvailablePitches - t
         
   firstPitchInSequence = listOfAvailablePitches[a]
            
   indexOfSecondPitchInSequenceInListOfAvailablePitches = listOfAvailablePitches.index{pitches[-4]}
         
   b = indexOfSecondPitchInSequenceInListOfAvailablePitches - t
         
   secondPitchInSequence = listOfAvailablePitches(pitches[b]}
         
   indexOfThirdPitchInSequenceInListOfAvailablePitches = listOfAvailablePitches.index{pitches[-3]}
         
   c = indexOfThirdPitchInSequenceInListOfAvailablePitches - t
            
   thirdPitchInSequence = listOfAvailablePitches(pitches[c]}
         
   indexOfFourthPitchInSequenceInListOfAvailablePitches = listOfAvailablePitches.index(pitches[-2]}
         
   d = indexOfFourthPitchInSequenceInListOfAvailablePitches - t
            
   fourthPitchInSequence = listOfAvailablePitches(pitches[d]}
         
   indexOfFifthPitchInSequenceInListOfAvailablePitches = listOfAvailablePitches.index{pitches[-1]}
         
   e = indexOfFifthPitchInSequenceInListOfAvailablePitches - t
            
   fifthPitchInSequence = listOfAvailablePitches(pitches[e])
   
   if a < 13 and a > -1 and b < 13 and b > -1 and c < 13 and c > -1 and d < 13 and d > -1 and e < 13 and e > -1:
         
      sequenceChoice = choice(sequenceOptions)
   
      if sequenceChoice = 1:
    
         twoPitchSequence = [firstPitchInSequence, secondPitchInSequence]
      
         pitches.append[twoPitchSequence]
         
         durations.append[durations[-2], durations[-1]]
            
      if sequenceChoice = 2 :
    
         threePitchSequence = [firstPitchInSequence, secondPitchInSequence, thirdPitchInSequence]
            
         pitches.append[threePitchSequence]
         
         durations.append[durations[-3], durations[-2], durations[-1]]
         
      if sequenceChoice = 3:
    
         fourPitchSequence = [firstPitchInSequence, secondPitchInSequence, thirdPitchInSequence, fourthPitchInSequence]
            
         pitches.append[fourPitchSequence]
         
         durations.append[durations[-4], durations[-3], durations[-2], durations[-1]]
               
      else:
    
         fivePitchSequence = [firstPitchInSequence, secondPitchInSequence, thirdPitchInSequence, fourthPitchInSequence, fifthPitchInSequence]
         
         pitches.append[fivePitchSequence]
         
         durations.append[durations[-5], durations[-4], durations[-3], durations[-2], durations[-1]] 
         
   else:
            
      counter = 0
                                                                             
solo.addNoteList(pitches, durations)

print solo

Play.midi(solo)

Write.midi(solo, "solo.mid")
question from:https://stackoverflow.com/questions/65830166/music-program-in-jython-does-not-run

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...