def generate(self): cadence_generator = CadencesUtil() # cadences = cadence_generator.generate() cadences = cadence_generator.getLikesCadences() len_cadences = len(cadences) num_sequencias = 8 score = stream.Score() part_chords = stream.Part() part_melody = stream.Part() for i in range(0, num_sequencias): cadencia = cadences[random.randint(0, len_cadences - 1)] num_notas_array = [8, 3, 5] for i in range(0, len(cadencia)): chor = copy.deepcopy(self.chords[cadencia[i]]) num_notas = num_notas_array[i] chor.quarterLength = num_notas * .5 part_melody.append(chor) # Obtemos as notas do acorde pitches = chor.pitches velocidade_notas = random.randint(1, 2) # Iteramos as notas do acorde pra a melodia for i_notas in range(0, num_notas * velocidade_notas): idx_pitch = random.randint(0, len(pitches) - 1) note = note_mus21.Note(pitches[idx_pitch]) note.quarterLength = 0.5 / velocidade_notas part_chords.append(note) score.insert(0, part_melody) score.insert(0, part_chords) # sc.show() mf = midi.translate.streamToMidiFile(score) import datetime mf.open(('%s/musics/%s.midi' % (Configs.get_project_home(), datetime.datetime.now().strftime("%Y%m%d%H%M%S"))), "wb") mf.write() mf.close
def gen_midi_of_cadences(self): # TODO cadence_generator = CadencesUtil() cadences = cadence_generator.generate() for cadence in cadences: score = stream.Score() part = stream.Part() for i in range(0, len(cadence)): chor = copy.deepcopy(self.chords[cadence[i]]) chor.quarterLength = 2 part.append(chor) score.insert(0, part) mf = midi.translate.streamToMidiFile(score) mf.open(('%s/cadences/cad%s.midi' % Configs.get_project_home(), '-'.join(str(e) for e in cadence)), "wb") mf.write() mf.close
def index(): from os import listdir from os.path import isfile, join items = [] cadenceUtil = CadencesUtil() cadenceUtil.learnFromDataset() for f in listdir('cadences'): if isfile(join('cadences', f)) and f.endswith('.midi'): cadence_str, intervals, distances = CadencesUtil.extractDistancesFromCadence(f) dist_array = np.ndarray(buffer=np.array(distances, dtype=int), shape=(1, 2), dtype=int) predict = cadenceUtil.classify(dist_array)[0] midi_item = MIDIItem(f, url_for('static', filename=f), ('/like/%s' % f), ('/dislike/%s' % f), predict=predict) items.append(midi_item) shuffle(items) return render_template('home.html', midi_items=items)