def generate_notes(self, ngrams, initial, count, key): output = [] current = deque(initial, maxlen=self.learner.NGRAM_LEN) # Add in the key as a filter for next note # Find its index first in a list of keys frequency_list = self.FREQUENCY_LIST * 2 # filter_key gives back a list of indexes, look these up in the keys key_filter = utils.filter_by_key_major(key) key_filter = [frequency_list[i] for i in key_filter] print key_filter while len(output) < count: # Check if current is a rare ending only note if tuple(current) not in ngrams: self.NOT_FOUND_NOTE += 1 num = random.randint(0, len(ngrams) - 1) current = deque(ngrams.keys()[num], maxlen=self.learner.NGRAM_LEN) if not output: # This is the initial one, so we add # the "random" choice into it to start off. output += filter(lambda x: x in key_filter, current) else: self.FOUND_NOTE += 1 next = utils.get_result_from_distribution( ngrams[tuple(current)], filt=key_filter) # Account for the key here, unless it is a rest. if next != "REST": note = self.FREQUENCY_LIST[ self.FREQUENCY_LIST.index(next) + key] if note in key_filter: output.append(note) else: self.NOT_IN_KEY += 1 else: output.append(next) current.append(next) return output
def generate_rhythms(self, ngrams, initial, bpm, time): output = [] current = deque(initial, maxlen=self.learner.NGRAM_LEN) while sum(output) < (float(time) / 60 * bpm): # Check if current is a rare ending only rhythm if tuple(current) not in ngrams: self.NOT_FOUND_RHYTHM += 1 num = random.randint(0, len(ngrams) - 1) current = deque(ngrams.keys()[num], maxlen=self.learner.NGRAM_LEN) if not output: # This is the initial one, so we add # the "random" choice into it to start off. output += current else: self.FOUND_RHYTHM += 1 next = utils.get_result_from_distribution( ngrams[tuple(current)]) output.append(next) current.append(next) return output