def mutate(self, WEIGHTS, chords, count=1): """Mutates a random symbol Arguments: WEIGHTS - the default weights chords -- the possible chords count -- the number of symbols to mutate """ for _ in range(count): i = self.select() beats_left = convert_to_float(self.symbols[i - 1].length) note_weights = self.set_weights(WEIGHTS, chords) if random.randint(1, 100) < 50: self.pop(i - 1) else: beats_left -= convert_to_float(self.symbols[i - 1].mutate( WEIGHTS, note_weights, beats_left)) while beats_left > 0: if random.randint(1, 100) < 50 or len(self.symbols) == 0: s = Symbol() beats_left -= convert_to_float( s.gen(WEIGHTS, note_weights, beats_left)) if len(self.symbols) > 0: i = self.select() self.insert(i - 1, s) else: i = self.select() beats_left += convert_to_float(self.symbols[i - 1].length) beats_left -= convert_to_float(self.symbols[i - 1].mutate( WEIGHTS, note_weights, beats_left))
def gen(self, WEIGHTS, chords, key, note_count, note_bar, bars): """Generate a random list of symbols for the measure that fit in the necessary time signature Arguments: WEIGHTS -- the default weights chords -- the possible chords key -- the tune's key note_count -- needed for time signature note_bar -- needed for time signature bars -- needed for time signature """ self.symbols = [] beats_left = (note_count * 1 / bars) * note_bar note_weights = self.set_weights(WEIGHTS, chords) while beats_left > 0: s = Symbol() beats_left -= convert_to_float( s.gen(WEIGHTS, note_weights, beats_left)) self.append(s)