def generate_pattern(progression_list, key = "C", pattern_index = 0, nb_bars=1): pattern = patterns.PATTERNS[pattern_index] t = Track() for progression in progression_list: progression = progression_to_int(progression) for p in progression : # permet d'avancer dans la progression des mesures previews_note = None p_key = get_progression_key(p, key) b = Bar(key, (4, 4)) position_note = 0 already_used=[] for pattern_note in pattern : if position_note not in already_used : is_chord = chord_length(pattern_note, pattern, position_note) if is_chord[2] : note_list = [] # c est un accord for p_note in pattern[is_chord[0]:is_chord[1]+1] : note_str = get_note_pattern(p_note, p_key) note = Note(note_str, p_note[5]) if previews_note is not None: if p_note[4]=='+': if int(note) < previews_note : note.octave_up() elif p_note[4]=='-': if int(note) > previews_note : note.octave_down() previews_note = int(note) note_list.append(note) for n in range(is_chord[0], is_chord[1]+1): already_used.append(n) b.place_notes(note_list, pattern_note[1]) else : note_str = get_note_pattern(pattern_note, p_key) note = Note(note_str, pattern_note[5]) if previews_note is not None: if pattern_note[4]=='+': if int(note) < previews_note : note.octave_up() elif pattern_note[4]=='-': if int(note) > previews_note : note.octave_down() previews_note = int(note) b.place_notes(note, pattern_note[1]) already_used.append(position_note) position_note+=1 t.add_bar(b) return t
def _set_up_metronome(self): metronome = Track() bar = Bar('C', (4, 4)) metronome.add_bar(bar) kick = MidiPercussion.BassDrum1 kick.velocity = 120 for i in range(4): bar.place_notes(kick, value.quarter) return metronome
def index(): if request.method == 'POST': keys = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] meters = [(2,2), (2,4), (3,4), (4,4)] bars = int(request.form['bars']) key = keys[int(request.form['key'])] meter = meters[int(request.form['meter'])] scale = int(request.form['scale']) composition = Composition() composition.set_author("by givemeasheet.com", "*****@*****.**") composition.set_title("%d bars exercise in %s" % (bars, key)) track = Track(Guitar()) dificulty = 3 if scale == 0: scale = scales.diatonic(key) scale_name = "Natural Major" elif scale == 1: scale = scales.natural_minor(key) scale_name = "Natural Minor" elif scale == 2: scale = scales.harmonic_minor(key) scale_name = "Minor Harmonic" elif scale == 3: scale = scales.melodic_minor(key) scale_name = "Minor Melodic" composition.subtitle = scale_name for bar in range(0,bars): bar = Bar(key, meter) while not bar.is_full(): # Random note index = randrange(dificulty) note = Note(scale[index]) possibles = get_possibles(bar) bar.place_notes(note, possibles[randrange(len(possibles))]) track.add_bar(bar) composition.add_track(track) l = LilyPond.from_Composition(composition) u = uuid.uuid1() file_name = "/var/www/givemeasheet/givemeasheet/static/sheets/%s" % u.hex LilyPond.save_string_and_execute_LilyPond(l, file_name, "-fpng") sheet="/static/sheets/%s.png" % os.path.basename(file_name) midi="/static/sheets/%s.midi" % os.path.basename(file_name) MidiFileOut.write_Composition("%s.midi" % file_name, composition) return render_template('index.html', sheet=sheet, midi=midi) else: return render_template('index.html')
def generate_transition(previous_phrase, next_phrase, nb_note_needed, pattern_index, key): previous_note = Note(previous_phrase[len(previous_phrase)-1]) next_note = Note(next_phrase[0]) selected_notes = get_note_between(previous_note, next_note, previous_phrase) temp_selected = get_note_between(previous_note, next_note, next_phrase) for i in range(len(temp_selected)): if temp_selected[i] not in selected_notes: selected_notes.append(temp_selected[i]) if (float(len(selected_notes))/abs((nb_note_needed[0]+nb_note_needed[1])))<(float(3)/4): temp_from_scale = get_note_between(previous_note, next_note, generate_blues_scale(key)) for i in range(len(temp_from_scale)): if temp_from_scale[i] not in selected_notes: selected_notes.append(temp_from_scale[i]) first_bar = Bar() last_bar = Bar() for i in range(1, nb_note_needed[0]): time = first_bar.current_beat + 1 list_compatible = get_compatible_notes(pattern_index, selected_notes, key, time) print "liste compatible : " + str(list_compatible) list_length = get_max_length_note(first_bar, nb_note_needed[0]-i) best_notes = get_best_notes_transition(list_compatible, previous_note, next_note, 0, float(i)/nb_note_needed[0]) print "best_notes : " + str(best_notes) chosen_note = best_notes[random.randint(0, len(best_notes)-1)] chosen_length = list_length[random.randint(0, len(list_length)-1)] first_bar.place_notes(chosen_note.name, chosen_length) for i in range(1, nb_note_needed[1]): time = last_bar.current_beat + 1 list_compatible = get_compatible_notes(pattern_index, selected_notes, key, time) list_length = get_max_length_note(last_bar, nb_note_needed[1]-i) best_notes = get_best_notes_transition(list_compatible, previous_note, next_note, 1, float(i)/nb_note_needed[1]) chosen_note = best_notes[random.randint(0, len(best_notes)-1)] chosen_length = list_length[random.randint(0, len(list_length)-1)] last_bar.place_notes(chosen_note.name, chosen_length) if last_bar.length - last_bar.current_beat != 0 : print("ajout de silence") space_left = 1.0 / (last_bar.length - last_bar.current_beat) last_bar.place_rest(space_left) if first_bar.length - first_bar.current_beat != 0 : print("ajout de silence") space_left = 1.0 / (first_bar.length - first_bar.current_beat) first_bar.place_rest(space_left) return [first_bar, last_bar]
def play_pattern(pattern_index, key): pattern = patterns.PATTERNS[pattern_index] fluidsynth.init("198_u20_Electric_Grand.SF2") # permet d'initialiser l'instrument previews_note = None b = Bar(key, (4, 4)) position_note = 0 already_used=[] for pattern_note in pattern : if position_note not in already_used : is_chord = chord_length(pattern_note, pattern, position_note) if is_chord[2] : note_list = [] # c est un accord for p_note in pattern[is_chord[0]:is_chord[1]+1] : note_str = get_note_pattern(p_note, key) note = Note(note_str, p_note[5]) if previews_note is not None: if p_note[4]=='+': if int(note) < previews_note : note.octave_up() elif p_note[4]=='-': if int(note) > previews_note : note.octave_down() previews_note = int(note) note_list.append(note) for n in range(is_chord[0], is_chord[1]+1): already_used.append(n) b.place_notes(note_list, pattern_note[1]) else : note_str = get_note_pattern(pattern_note, key) note = Note(note_str, pattern_note[5]) if previews_note is not None: if pattern_note[4]=='+': if int(note) < previews_note : note.octave_up() elif pattern_note[4]=='-': if int(note) > previews_note : note.octave_down() previews_note = int(note) b.place_notes(note, pattern_note[1]) already_used.append(position_note) position_note+=1 fluidsynth.play_Bar(b, 1, 60)
def toTrack(key, meter, melody): _, base = meter t = Track() for mb in melody: b = Bar(key, meter) for mn in mb.split(): nr = mn.split(':') n = nr[0] r = base if len(nr) > 1: r = int(nr[1]) ok = b.place_notes(n, r) t.add_bar(b) return t
def drum_beat(beat_probas): b = Bar() for (bass, snare, hhat) in beat_probas: p = random() if p < bass: b.place_notes(BASS, 8) elif p < bass + snare: b.place_notes(SNARE, 8) elif p < bass + snare + hhat: b.place_notes(HIHAT, 8) else: b.place_rest(8) return b
def bassline(chord, beat_probas): chord_tones = chords.from_shorthand(chord) octave = 2 b = Bar() for (bass, els, octa) in beat_probas: p = random.random() if p < bass: b.place_notes(Note(chord_tones[0], octave), 8) elif p < bass + els: b.place_notes(Note(random.choice(chord_tones[1:]), octave), 8) elif p < bass + els + octa: b.place_notes(Note(chord_tones[0], octave + 1), 8) else: b.place_rest(8) return b
class test_tied_notes(unittest.TestCase): def setUp(self): self.bar1 = Bar() self.bar2 = Bar() def test_tie_note(self): n = Note('G') m = Note('G', tie_note=n) self.bar1.place_notes('C', 2) self.bar1.place_notes('G', 4) self.bar1.place_notes(n, 4) self.bar2.place_notes(m, 4) self.bar2.place_notes('C', 2) self.assertEqual(m.tie_note(), n) self.assertEqual(n.next_note(), m) self.assertTrue(n.is_tied()) self.assertTrue(m.is_last_tied()) def test_tie_together(self): n = Note('G') m = Note('G') m.tie_together(n) self.bar1.place_notes('C', 2) self.bar1.place_notes('G', 4) self.bar1.place_notes(n, 4) self.bar2.place_notes(m, 4) self.bar2.place_notes('C', 2) self.assertEqual(m.tie_note(), n) self.assertEqual(n.next_note(), m) self.assertTrue(n.is_tied()) self.assertTrue(m.is_last_tied()) def test_not_tieable(self): def different_note(): n = Note('G') m = Note('C') m.tie_together(n) def different_channel(): n = Note('G') m = Note('G', dynamics={'channel': n.channel+1}) m.tie_together(n) self.assertRaises(NotesNotTieable, different_note) self.assertRaises(NotesNotTieable, different_channel)
class test_LilyPond(unittest.TestCase): def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ['C', 'E', 'G', 'B']) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), [ 'C', 'E', 'G', 'B', 'C', 'E', ]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E']) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C' ]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2) def test_from_Note(self): self.assertEqual(LilyPond.from_Note(Note('C'), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C#'), standalone=False), "cis'") self.assertEqual(LilyPond.from_Note(Note('C##'), standalone=False), "cisis'") self.assertEqual(LilyPond.from_Note(Note('Cb'), standalone=False), "ces'") self.assertEqual(LilyPond.from_Note(Note('Cbb'), standalone=False), "ceses'") self.assertEqual(LilyPond.from_Note(Note('C', 0), standalone=False), 'c,,,') self.assertEqual(LilyPond.from_Note(Note('C', 1), standalone=False), 'c,,') self.assertEqual(LilyPond.from_Note(Note('C', 2), standalone=False), 'c,') self.assertEqual(LilyPond.from_Note(Note('C', 3), standalone=False), 'c' ) self.assertEqual(LilyPond.from_Note(Note('C', 4), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C', 5), standalone=False), "c''") self.assertEqual(LilyPond.from_Note(Note('C', 6), standalone=False), "c'''") self.assertEqual(LilyPond.from_Note(Note('C', 7), standalone=False), "c''''") def test_from_NoteContainer(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), standalone=False), "c'") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 4, standalone=False), "c'4") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), standalone=False), "<c' e'>") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), 4, standalone=False), "<c' e'>4") # issue #37 self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 16, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 16.0, standalone=False), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(16), standalone=False), "c'16.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 0.25, standalone=False), "c'\\longa") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), 0.5, standalone=False), "c'\\breve") def test_from_Bar(self): self.assertEqual(LilyPond.from_Bar(self.commonbar), "{ \\time 4/4 \\key c \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.ebar), "{ \\time 4/4 \\key e \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.fbar), "{ \\time 6/8 \\key f \\major c'8 e'8 g'8 b'8 }") def test_from_Track(self): self.assertEqual(LilyPond.from_Track(self.track1), "{ { c'4 e'4 g'4 b'4 } }") self.assertEqual(LilyPond.from_Track(self.track2), "{ { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }" ) def test_from_Composition(self): self.assertEqual(LilyPond.from_Composition(self.composition1), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } }' ) self.assertEqual(LilyPond.from_Composition(self.composition2), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } } { { c\'4 e\'4 g\'4 b\'4 } { \\key e \\major c\'4 e\'4 g\'4 b\'4 } }' ) def test_from_Suite(self): LilyPond.from_Suite(None) def test_dotted_notes(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8), standalone=False), "c'8.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(4, 2), standalone=False), "c'4..") def test_to_pdf(self): self.assert_(LilyPond.to_pdf('{ %s }' % LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8)), 'pdftest first test')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.tbar), 'pdftest2')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.mbar), 'pdftest3'))
def generate_bar(previews_note, bar, key, mode, progression, progression_list, nb_p, pattern_index): if mode == 'mixolydien' : if progression == 'IV' : key = intervals.fourth(key, key) elif progression == 'V' : key = intervals.fifth(key, key) b = Bar(key, (4, 4)) position_note = 0 already_used=[] list_note = [] for bar_note in bar : if position_note not in already_used : is_chord = chord_length(bar_note, bar, position_note) if is_chord[2] : note_list = [] # c est un accord for p_note in bar[is_chord[0]:is_chord[1]+1] : note_str = get_note(p_note, key) note = Note(note_str, p_note[5]) if previews_note is not None: if p_note[4]=='+': if int(note) < previews_note : note.octave_up() elif p_note[4]=='-': if int(note) > previews_note : note.octave_down() current_progression = progression_list[0][nb_p] note = change_note_if_needed(bar_note[2], note, pattern_index, current_progression, bar_note, key) previews_note = int(note) #appeler best_note la aussi note_list.append(note) list_note.append(note) for n in range(is_chord[0], is_chord[1]+1): already_used.append(n) b.place_notes(note_list, bar_note[1]) else : note_str = get_note(bar_note, key) note = Note(note_str, bar_note[5]) if previews_note is not None: if bar_note[4]=='+': if int(note) < previews_note : note.octave_up() elif bar_note[4]=='-': if int(note) > previews_note : note.octave_down() # la un faut appeler le truc pour modifier la note current_progression = progression_list[0][nb_p] note = change_note_if_needed(bar_note[2], note, pattern_index, current_progression, bar_note, key) previews_note = int(note) list_note.append(note) b.place_notes(note, bar_note[1]) already_used.append(position_note) position_note+=1 return (b, previews_note, list_note)
class test_LilyPond(unittest.TestCase): def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4, 4)) self.fbar = Bar('F', (6, 8)) self.tbar = Bar('C', (4, 4)) self.mbar = Bar('C', (4, 4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ['C', 'E', 'G', 'B']) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), [ 'C', 'E', 'G', 'B', 'C', 'E', ]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ['C', 'E']) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ['G', 'B', 'C']) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2) def test_from_Note(self): self.assertEqual(LilyPond.from_Note(Note('C'), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C#'), standalone=False), "cis'") self.assertEqual(LilyPond.from_Note(Note('C##'), standalone=False), "cisis'") self.assertEqual(LilyPond.from_Note(Note('Cb'), standalone=False), "ces'") self.assertEqual(LilyPond.from_Note(Note('Cbb'), standalone=False), "ceses'") self.assertEqual(LilyPond.from_Note(Note('C', 0), standalone=False), 'c,,,') self.assertEqual(LilyPond.from_Note(Note('C', 1), standalone=False), 'c,,') self.assertEqual(LilyPond.from_Note(Note('C', 2), standalone=False), 'c,') self.assertEqual(LilyPond.from_Note(Note('C', 3), standalone=False), 'c') self.assertEqual(LilyPond.from_Note(Note('C', 4), standalone=False), "c'") self.assertEqual(LilyPond.from_Note(Note('C', 5), standalone=False), "c''") self.assertEqual(LilyPond.from_Note(Note('C', 6), standalone=False), "c'''") self.assertEqual(LilyPond.from_Note(Note('C', 7), standalone=False), "c''''") def test_from_NoteContainer(self): self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), standalone=False), "c'") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), 4, standalone=False), "c'4") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), standalone=False), "<c' e'>") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer(['C', 'E']), 4, standalone=False), "<c' e'>4") # issue #37 self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), 16, standalone=False), "c'16") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), 16.0, standalone=False), "c'16") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(16), standalone=False), "c'16.") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), 0.25, standalone=False), "c'\\longa") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), 0.5, standalone=False), "c'\\breve") def test_from_Bar(self): self.assertEqual(LilyPond.from_Bar(self.commonbar), "{ \\time 4/4 \\key c \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.ebar), "{ \\time 4/4 \\key e \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.fbar), "{ \\time 6/8 \\key f \\major c'8 e'8 g'8 b'8 }") def test_from_Track(self): self.assertEqual(LilyPond.from_Track(self.track1), "{ { c'4 e'4 g'4 b'4 } }") self.assertEqual( LilyPond.from_Track(self.track2), "{ { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }") def test_from_Composition(self): self.assertEqual( LilyPond.from_Composition(self.composition1), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } }' ) self.assertEqual( LilyPond.from_Composition(self.composition2), '\\header { title = "Untitled" composer = "" opus = "" } { { c\'4 e\'4 g\'4 b\'4 } } { { c\'4 e\'4 g\'4 b\'4 } { \\key e \\major c\'4 e\'4 g\'4 b\'4 } }' ) def test_from_Suite(self): LilyPond.from_Suite(None) def test_dotted_notes(self): self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8), standalone=False), "c'8.") self.assertEqual( LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(4, 2), standalone=False), "c'4..") def test_to_pdf(self): self.assert_( LilyPond.to_pdf( '{ %s }' % LilyPond.from_NoteContainer(NoteContainer('C'), value.dots(8)), 'pdftest first test')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.tbar), 'pdftest2')) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.mbar), 'pdftest3'))
def generate_end_bars(previews_bar, previews_bar_notes, pattern_index, key, mode): # on genere deux mesures de fin # previews_bar = format de la bdd nb_notes = get_nb_notes(previews_bar) first_bar = Bar() last_bar = Bar() last_index = len(previews_bar_notes)-1 last_note = previews_bar_notes[last_index] for i in range(1, nb_notes[0]): time = first_bar.current_beat + 1 list_compatible = get_compatible_notes(pattern_index, previews_bar_notes, key, time) list_length = get_max_length_note(first_bar, nb_notes[0]-i) best_notes = get_best_notes(list_compatible, last_note) chosen_note = best_notes[random.randint(0, len(best_notes)-1)] chosen_length = list_length[random.randint(0, len(list_length)-1)] first_bar.place_notes(chosen_note.name, chosen_length) if first_bar.length - first_bar.current_beat != 0 : print("ajout de silence") space_left = 1.0 / (first_bar.length - first_bar.current_beat) first_bar.place_rest(space_left) for i in range(1, nb_notes[1]): time = last_bar.current_beat + 1 list_compatible = get_compatible_notes(pattern_index, previews_bar_notes, key, time) list_length = get_max_length_note(last_bar, nb_notes[1]-i) best_notes = get_best_notes(list_compatible, last_note) chosen_note = best_notes[random.randint(0, len(best_notes)-1)] chosen_length = list_length[random.randint(0, len(list_length)-1)] #chosen_note.octave chord_possible = [] chord_possible.append(intervals.unison(key, key)) chord_possible.append(intervals.fourth(key, key)) chord_possible.append(intervals.fifth(key, key)) intervals_last = [] for note_possible in chord_possible : test = [] test.append(intervals.measure(note_possible, last_note.name)) test.append(intervals.measure(last_note.name, note_possible)) intervals_last.append(min(test)) for i,j in enumerate(intervals_last) : if j == min(intervals_last) : index = i break chosen_chord = chord_possible[index] if mode == "mixolydien" : chord = chords.triad(chosen_chord, key) chord_list = [] for note in chord : note_m = Note(note, chosen_note.octave) chord_list.append(note_m) last_note = note_m last_bar.place_notes(chord_list, chosen_length) else : chord = chords.triad(chosen_chord, key) chord_list = [] for note in chord : note_m = Note(note, chosen_note.octave) last_note = note_m chord_list.append(note_m) last_bar.place_notes(chord_list, chosen_length) if last_bar.length - last_bar.current_beat != 0 : print("ajout de silence") space_left = 1.0 / (last_bar.length - last_bar.current_beat) last_bar.place_rest(space_left) return [first_bar, last_bar]
def generate_pattern(progression_list, key = "C", pattern = ((1, 4, 1, "none", '=', 3), (3, 4, 2,"none", '+', 3), (5, 4, 3, "none", '+', 3), (6, 4, 4, "none", '+', 3)), nb_bars=1): #pattern = ((1, 4, 1, "none", '=', 3), (5, 4, 1, "none", '+', 3), (1, 4, 2, "none", '+', 3), (6, 4, 2, "none", '+', 3), (1, 4, 3, "none", '=', 3), (5, 4, 3, "none", '+', 3), (1, 4, 4, "none", '+', 3), (6, 4, 4, "none", '+', 3)) #pattern = ((1, 8, 1, "none", '=', 2), (5, 8, 1, "none", '+', 2), (1, 8, 1.5, "none", '=', 2), (5, 8, 1.5, "none", '+', 2), (2, 8, 2, "diese", '=', 2), (3, 8, 2.5, "none", '+', 2), (1, 8, 3, "none", '=', 2), (5, 8, 3, "none", '+', 2), (1, 8, 3.5, "none", '=', 2), (5, 8, 3.5, "none", '+', 2), (2, 8, 4, "diese", '=', 2), (3, 8, 4.5, "none", '+', 2)) #pattern = ((1, 8, 1, "none", '=', 3), (5, 8, 1, "none", '+', 3), (1, 8, 1.5, "none", '=', 3), (5, 8, 1.5, "none", '+', 3), (2, 8, 2, "diese", '-', 3), (3, 8, 2.5, "none", '+', 3), (1, 8, 3, "none", '=', 3), (5, 8, 3, "none", '+', 3), (1, 8, 3.5, "none", '=', 3), (5, 8, 3.5, "none", '+', 3), (1, 8, 4, "none", '=', 3), (6, 8, 4, "none", '+', 3), (1, 8, 4.5, "none", '=', 3), (6, 8, 4.5, "none", '+', 3)) t = Track() for progression in progression_list: progression = progression_to_int(progression) for p in progression : # permet d'avancer dans la progression des mesures previews_note = None p_key = get_progression_key(p, key) b = Bar(key, (4, 4)) position_note = 0 already_used=[] for pattern_note in pattern : if position_note not in already_used : is_chord = chord_length(pattern_note, pattern, position_note) if is_chord[2] : note_list = [] # c est un accord for p_note in pattern[is_chord[0]:is_chord[1]+1] : note_str = get_note_pattern(p_note, p_key) note = Note(note_str, p_note[5]) if previews_note is not None: if p_note[4]=='+': if int(note) < previews_note : note.octave_up() elif p_note[4]=='-': if int(note) > previews_note : note.octave_down() previews_note = int(note) note_list.append(note) for n in range(is_chord[0], is_chord[1]+1): already_used.append(n) b.place_notes(note_list, pattern_note[1]) else : note_str = get_note_pattern(pattern_note, p_key) note = Note(note_str, pattern_note[5]) if previews_note is not None: if pattern_note[4]=='+': if int(note) < previews_note : note.octave_up() elif pattern_note[4]=='-': if int(note) > previews_note : note.octave_down() previews_note = int(note) b.place_notes(note, pattern_note[1]) already_used.append(position_note) position_note+=1 t.add_bar(b) print(str(t)) #track = LilyPond.from_Track(t) #test = LilyPond.to_png(track, "left_hand") #MidiFileOut.write_Track("test_midi.mid", t) return t
class test_LilyPond(unittest.TestCase): def setUp(self): self.commonbar = Bar() self.ebar = Bar('E', (4,4)) self.fbar = Bar('F', (6,8)) self.tbar = Bar('C', (4,4)) self.mbar = Bar('C', (4,4)) for y in [self.commonbar, self.ebar, self.fbar]: map(lambda x: y + x, ["C", "E", "G", "B"]) map(lambda x: self.tbar.place_notes(NoteContainer(x), 6), ["C", "E", "G", "B", "C", "E"]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 4), ["C", "E"]) map(lambda x: self.mbar.place_notes(NoteContainer(x), 6), ["G", "B", "C"]) self.track1 = Track() self.track1 + self.commonbar self.track2 = Track() self.track2 + self.commonbar self.track2 + self.ebar self.composition1 = Composition() self.composition1.add_track(self.track1) self.composition2 = Composition() self.composition2.add_track(self.track1) self.composition2.add_track(self.track2) def test_from_Note(self): self.assertEqual(LilyPond.from_Note(Note('C')), "c'") self.assertEqual(LilyPond.from_Note(Note('C#')),"cis'") self.assertEqual(LilyPond.from_Note(Note('C##')),"cisis'") self.assertEqual(LilyPond.from_Note(Note('Cb')),"ces'") self.assertEqual(LilyPond.from_Note(Note('Cbb')),"ceses'") self.assertEqual(LilyPond.from_Note(Note('C', 0)),"c,,,") self.assertEqual(LilyPond.from_Note(Note('C', 1)),"c,,") self.assertEqual(LilyPond.from_Note(Note('C', 2)),"c,") self.assertEqual(LilyPond.from_Note(Note('C', 3)),"c") self.assertEqual(LilyPond.from_Note(Note('C', 4)),"c'") self.assertEqual(LilyPond.from_Note(Note('C', 5)),"c''") self.assertEqual(LilyPond.from_Note(Note('C', 6)), "c'''") self.assertEqual(LilyPond.from_Note(Note('C', 7)), "c''''") def test_from_NoteContainer(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C")), "c'") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 4), "c'4") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(["C", "E"])), "<c' e'>") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer(["C", "E"]), 4), "<c' e'>4") # issue #37 self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 16), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 16.0), "c'16") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(16)), "c'16.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 0.25), "c'\\longa") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), 0.5), "c'\\breve") def test_from_Bar(self): self.assertEqual(LilyPond.from_Bar(self.commonbar),\ "{ \\time 4/4 \\key c \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.ebar),\ "{ \\time 4/4 \\key e \\major c'4 e'4 g'4 b'4 }") self.assertEqual(LilyPond.from_Bar(self.fbar),\ "{ \\time 6/8 \\key f \\major c'8 e'8 g'8 b'8 }") def test_from_Track(self): self.assertEqual(LilyPond.from_Track(self.track1),\ "{ { c'4 e'4 g'4 b'4 } }") self.assertEqual(LilyPond.from_Track(self.track2),\ "{ { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }") def test_from_Composition(self): self.assertEqual(LilyPond.from_Composition(self.composition1),\ "\\header { title = \"Untitled\" composer = \"\" opus = \"\" } { { c'4 e'4 g'4 b'4 } }") self.assertEqual(LilyPond.from_Composition(self.composition2),\ "\\header { title = \"Untitled\" composer = \"\" opus = \"\" } { { c'4 e'4 g'4 b'4 } } { { c'4 e'4 g'4 b'4 } { \\key e \\major c'4 e'4 g'4 b'4 } }") def test_from_Suite(self): LilyPond.from_Suite(None) def test_dotted_notes(self): self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8)), "c'8.") self.assertEqual(LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(4, 2)), "c'4..") def test_to_pdf(self): self.assert_(LilyPond.to_pdf("{ %s }" % LilyPond.from_NoteContainer(NoteContainer("C"), value.dots(8)), "pdftest first test")) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.tbar), "pdftest2")) self.assert_(LilyPond.to_pdf(LilyPond.from_Bar(self.mbar), "pdftest3"))