def make_desordre_score(pitches): """ Makes Désordre score. """ assert len(pitches) == 2 staff_group = abjad.StaffGroup() staff_group.lilypond_type = 'PianoStaff' # build the music for hand in pitches: staff = make_desordre_staff(hand) staff_group.append(staff) # set clef and key signature to left hand staff leaf = abjad.inspect(staff_group[1]).leaf(0) clef = abjad.Clef('bass') abjad.attach(clef, leaf) key_signature = abjad.KeySignature('b', 'major') abjad.attach(key_signature, leaf) # wrap the piano staff in a score score = abjad.Score([staff_group]) return score
def __handle_set_command(self, command: SetCommand) -> List[Command]: """Handle set commands.""" try: if command.option == "clef": self.clef = abjad.Clef(command.value) elif command.option == "time": pair = command.value.split("/" if "/" in command.value else " ") self.time = abjad.TimeSignature(tuple(map(int, pair))) elif command.option == "key": self.key = abjad.KeySignature(*command.value.split(" ")) else: return [ SetStatusLineTextCommand( f"Invalid option '{command.option}'.", Position.CENTER) ] except Exception as e: return [ SetStatusLineTextCommand( f"Could not parse '{command.value}' as '{command.option}'", Position.CENTER, ) ] return [ SetStatusLineTextCommand( f"'{command.option}' set.", Position.CENTER, ) ]
def format_score(score, key_signature, time_signature): for staff in score: key_sig = abjad.KeySignature(key_signature.tonic, key_signature.mode) abjad.attach(key_sig, staff[0]) time_sig = abjad.TimeSignature(time_signature) abjad.attach(time_sig, staff[0]) abjad.attach(abjad.Clef('bass'), score[2][0])
def __init__(self, key_signature=None): import abjad if key_signature is None: key_signature = abjad.KeySignature('c', 'major') elif isinstance(key_signature, tuple): key_signature = abjad.KeySignature(*key_signature) elif isinstance(key_signature, type(self)): key_signature = key_signature.key_signature if not isinstance(key_signature, abjad.KeySignature): raise Exception(key_signature) npcs = [key_signature.tonic] for mdi in key_signature.mode.named_interval_segment[:-1]: named_pitch_class = npcs[-1] + mdi npcs.append(named_pitch_class) PitchClassSegment.__init__( self, items=npcs, item_class=abjad.NamedPitchClass, ) self._key_signature = key_signature
def test_scoretools_Inspection_get_effective_11(): r'''Attach key signature. ''' staff = abjad.Staff("c'8 d'8 e'8 f'8") key_signature = abjad.KeySignature('c', 'major') abjad.attach(key_signature, staff[0]) key_signature = abjad.inspect(staff).get_effective(abjad.KeySignature) assert key_signature == abjad.KeySignature('c', 'major') assert format(staff) == abjad.String.normalize(r''' \new Staff { \key c \major c'8 d'8 e'8 f'8 } ''') assert abjad.inspect(staff).is_well_formed()
def __initialize_score(self): """Initialize a default score.""" # internal note representation (with some defaults) self.score = abjad.Score(simultaneous=False) self.key = abjad.KeySignature("c", "major") self.clef = abjad.Clef("treble") self.time = abjad.TimeSignature((4, 4)) self.position = 0 # position within the container self.current_file_path = None # the file to which to save self.changed_since_saving = False self.previous_repeatable_command = None # the previous command (to repeat on .) self.deleted_items = [ ] # last deleted items (to be possibly pasted back)
def test_LilyPondParser__indicators__KeySignature_01(): target = abjad.Staff([abjad.Note("fs'", 1)]) key_signature = abjad.KeySignature('g', 'major') abjad.attach(key_signature, target[0]) assert format(target) == abjad.String.normalize(r""" \new Staff { \key g \major fs'1 } """) parser = abjad.parser.LilyPondParser() result = parser(format(target)) assert format(target) == format(result) and target is not result key_signatures = abjad.inspect(result[0]).indicators(abjad.KeySignature) assert len(key_signatures) == 1
def test_LilyPondParser__indicators__KeySignature_01(): target = abjad.Staff([abjad.Note("fs'", 1)]) key_signature = abjad.KeySignature("g", "major") abjad.attach(key_signature, target[0]) assert abjad.lilypond(target) == abjad.String.normalize(r""" \new Staff { \key g \major fs'1 } """) parser = abjad.parser.LilyPondParser() result = parser(abjad.lilypond(target)) assert abjad.lilypond(target) == abjad.lilypond( result) and target is not result key_signatures = abjad.get.indicators(result[0], abjad.KeySignature) assert len(key_signatures) == 1
def test_lilypondparsertools_LilyPondParser__functions__transpose_01(): pitches = ["e'", "gs'", "b'", "e''"] maker = abjad.NoteMaker() target = abjad.Staff(maker(pitches, (1, 4))) key_signature = abjad.KeySignature('e', 'major') abjad.attach(key_signature, target[0]) assert format(target) == abjad.String.normalize(r''' \new Staff { \key e \major e'4 gs'4 b'4 e''4 } ''') string = r"\transpose d e \relative c' \new Staff { \key d \major d4 fs a d }" parser = abjad.lilypondparsertools.LilyPondParser() result = parser(string) assert format(target) == format(result) and target is not result
def test_lilypondparsertools_LilyPondParser__functions__transpose_02(): pitches = ["ef'", "f'", "g'", "bf'"] maker = abjad.NoteMaker() target = abjad.Staff(maker(pitches, (1, 4))) key_signature = abjad.KeySignature('ef', 'major') abjad.attach(key_signature, target[0]) assert format(target) == abjad.String.normalize(r''' \new Staff { \key ef \major ef'4 f'4 g'4 bf'4 } ''') string = r"\transpose a c' \relative c' \new Staff { \key c \major c4 d e g }" parser = abjad.lilypondparsertools.LilyPondParser() result = parser(string) assert format(target) == format(result) and target is not result
upper_staff = ab.Staff() lower_staff = ab.Staff() staff.append(upper_staff) staff.append(lower_staff) score.append(staff) # Choose key signature key_choices = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'as', 'bs', 'cs', 'ds', 'es', 'fs', 'gs', 'af', 'bf', 'cf', 'df', 'ef', 'ff', 'gf' ] mode_choices = ['major', 'minor'] chosen_key = random.choices(key_choices)[0] chosen_mode = random.choices(mode_choices)[0] key_signature = ab.KeySignature(chosen_key, chosen_mode) # Choose time signature # time_signature_choices = [(4, 4), (3, 4), (2, 4), (2, 2), (6, 8), (3, 8), (9, 8), (12, 8), (5, 4), (6, 4)] # time_signature_probabilities = [100, 75, 67, 50, 37, 25, 20, 15, 10, 5] # time_signature = random.choices(time_signature_choices, weights = time_signature_probabilities)[0] time_signature = (3, 8) measure_duration = time_signature[0] / time_signature[1] # Choose notes and durations # Initialize inital number of notes # number_of_notes = int(np.random.normal(128, 32)) number_of_notes = 128 # Initialize duration possibilities, weighted towards lower lengths
notelist.append(ab.Rest(ab.Duration(*d))) elif len(a[2]) == 1: notelist.append( ab.Note(a[2][0] - 148, ab.Duration(*convertduration(a[3])))) elif len(a[2]) > 1: chordnotes = [] for b in a[2]: chordnotes.append(b - 148) notelist.append( ab.Chord(chordnotes, ab.Duration(*convertduration(a[3])))) return notelist # Convert chordlists to musical score using Abjad keysig = findkey(chromagram) ksignature = ab.KeySignature(*keysig) timesig = meterdetect(combinedlist) tsignature = ab.TimeSignature((timesig, 4)) def treblebass(): trebleclef = ab.Clef('treble') treblestaff = ab.Staff(listnotes(toplist)) ab.attach(trebleclef, treblestaff[0]) ab.attach(ksignature, treblestaff[0]) ab.attach(tsignature, treblestaff[0]) bassclef = ab.Clef('bass') bassstaff = ab.Staff(listnotes(bottomlist)) ab.attach(bassclef, bassstaff[0])
def ChopinPrelude20(): #Use the following libraries to access required modules. import abjad import copy # Initialize the piano score and create empty upper/lower measure lists. score = abjad.Score() piano_staff = abjad.StaffGroup([], lilypond_type='PianoStaff') upper_staff = abjad.Staff() lower_staff = abjad.Staff() piano_staff.append(upper_staff) piano_staff.append(lower_staff) score.append(piano_staff) upper_measures = [] lower_measures = copy.deepcopy(upper_measures) #Create tuples containing notes and chords for the upper and lower measures. Comments are in same listed order as each variable below. # #upper_tuple1: contains chords for first four upper measures except for the third beat in each measure. #upper_tuple_repeat1: bars 5-8 repeat themselves in bars 9-12, so only write these notes once. #upper_tuple_repeat: Concatenate upper_tuple_repeat1 twice to create the entire repeated section bars 5-12. #upper_tuple: Concatenate upper_tuple1 and upper_tuple_repeat to get just one tuple for entire upper measure. #lower_tuple1: contains chords for first four lower measures. #lower_tuple_repeat1: bars 5-8 repeat themselves in bars 9-12, so only write these notes once. #lower_tuple_repeat: Concatenate lower_tuple_repeat1 twice to create the entire repeated section bars 5-12 #lower_tuple: Concatenate lower_tuple1 and lower_tuple_repeat to get just one tuple for entire lower measure # #The third beat in each upper measure contains two voices; #voice_1_tuple1: contains notes for upper voice for first four upper measures. #voice_1_tuple_repeat1: bars 5-8 repeat themselves in bars 9-12, so only write these notes once for upper voice. #voice_1_tuple_repeat: Concatenate voice_1_tuple_repeat1 twice to create the entire repeated section bars 5-12. #voice_1_tuple: Concatenate voice_1_tuple1 and voice_1_tuple_repeat to get one tuple for entire upper measure third beat upper voice. #voice_2_tuple1: contains notes for lower voice for first four upper measures. #voice_2_tuple_repeat1: bars 5-8 repeat themselves in bars 9-12, so only write these notes once for lower voice. #voice_2_tuple_repeat: Concatenate voice_2_tuple_repeat1 twice to create the entire repeated section bars 5-12. #voice_2_tuple: Concatenate voice_2_tuple1 and voice_2_tuple_repeat to get one tuple for entire upper measure third beat lower voice. upper_tuple1 = (r"<g c' ef' g'>4 <af c' ef' af'>4 <ef g c' ef'>4", r"<ef af c' ef'>4 <f af df' f'>4 <c ef af c'>4", r"<d f b d'>4 <e g bf c' e'>4 <g c' ef'>4", r"<fs c' d'>4 <g b d' g'>4 <b d' g'>4") upper_tuple_repeat1 = (r"<ef' g' ef''>4 <ef' af' ef''>4 <d' g' d''>4", r"<c' g' c''>4 <c' d' fs' d''>4 <b d' g'>4", r"<c' g' c''>4 <af c' af'>4 <g c' ef'>4", r"<ef af c' ef'>4 <f af df' f'>4 <ef g c'>4") upper_tuple_repeat = upper_tuple_repeat1 + upper_tuple_repeat1 upper_tuple = upper_tuple1 + upper_tuple_repeat lower_tuple1 = (r"<c c,>4 <f,, f,>4 <g,, g,>4 <c, g, c>4", r"<af, af,,>4 <df, df,,>4 <ef, ef,,>4 <af, af,,>4", r"<g, g,,>4 <c, c,,>4 <f, f,,>4 <c c,>4", r"<d a, d,>4 <g, g,,>4 <d, d,,>4 <g, g,,>4") lower_tuple_repeat1 = (r"<c c,>4 <c c'>4 <b b,>4 <bf bf,>4", r"<a a,>4 <af af,>4 <g g,>4 <f f,>4", r"<ef ef,>4 <f f,>4 <b, b,,>4 <c c,>4", r"<af, af,,>4 <df, df,,>4 <g, g,,>4 <c, c,,>4") lower_tuple_repeat = lower_tuple_repeat1 + lower_tuple_repeat1 lower_tuple = lower_tuple1 + lower_tuple_repeat voice_1_tuple1 = (r"<ef' g'>8. <d' f'>16", r"<c' ef'>8. <bf df'>16", r"g'8. f'16", r"b'8. a'16") voice_1_tuple_repeat1 = (r"<d' d''>4", r"<g' b'>8. a'16", r"g'8. f'16", r"ef'8. d'16") voice_1_tuple_repeat = voice_1_tuple_repeat1 + voice_1_tuple_repeat1 voice_1_tuple = voice_1_tuple1 + voice_1_tuple_repeat voice_2_tuple1 = (r"<g b>4", r"<df ef g>4", r"<af c'>4", r"<c' d' fs'>4") voice_2_tuple_repeat1 = (r"af'8. fs'16", r"d'8. c'16", r"<g d'>4", r"<f g b>4") voice_2_tuple_repeat = voice_2_tuple_repeat1 + voice_2_tuple_repeat1 voice_2_tuple = voice_2_tuple1 + voice_2_tuple_repeat # Executing this while loop does the following: # 1: Appends upper and lower measures to the piano staff at each iteration. # 2: These measures are populated with the tuples defined above. # 3: The upper and lower voices are treated separately; a container is created for the third beat, # then is inserted into correct position in upper measures. i = 0 while i < 12: upper_measures.append(abjad.Measure((4, 4), [])) lower_measures.append(abjad.Measure((4, 4), [])) upper_measures[i].extend(upper_tuple[i]) lower_measures[i].extend(lower_tuple[i]) voice_1 = abjad.Voice(voice_1_tuple[i]) voice_2 = abjad.Voice(voice_2_tuple[i]) literal_1 = abjad.LilyPondLiteral(r"\voiceOne") literal_2 = abjad.LilyPondLiteral(r"\voiceTwo") abjad.attach(literal_1, voice_1[0]) abjad.attach(literal_2, voice_2[0]) container = abjad.Container([voice_1, voice_2], is_simultaneous=True) upper_measures[i].insert(2, container) i += 1 # Initialize final empty measure for final chord upper_measures.append(abjad.Measure((4, 4), [])) lower_measures.append(abjad.Measure((4, 4), [])) # Append the chords to the final measures upper_measures[12].append(r"<c' ef' g' c''>1") lower_measures[12].append("<c g>1") # Extend the intial piano staff to include the measures created above upper_staff.extend(upper_measures) lower_staff.extend(lower_measures) # Set and place clefs throughput piece leaf1 = abjad.inspect(lower_staff).leaf(0) leaf2 = abjad.inspect(upper_staff).leaf(0) leaf3 = abjad.inspect(upper_staff).leaf(20) leaf4 = abjad.inspect(upper_staff).leaf(43) leaf5 = abjad.inspect(upper_staff).leaf(49) leaf6 = abjad.inspect(upper_staff).leaf(68) leaf7 = abjad.inspect(upper_staff).leaf(74) abjad.attach(abjad.Clef('bass'), leaf1) abjad.attach(abjad.Clef('bass'), leaf2) abjad.attach(abjad.Clef('treble'), leaf3) abjad.attach(abjad.Clef('bass'), leaf4) abjad.attach(abjad.Clef('treble'), leaf5) abjad.attach(abjad.Clef('bass'), leaf6) abjad.attach(abjad.Clef('treble'), leaf7) # Set key signature for left and right hand key_signature = abjad.KeySignature('c', 'minor') abjad.attach(key_signature, leaf1) key_signature2 = abjad.KeySignature('c', 'minor') abjad.attach(key_signature2, leaf2) # Attach necessary dynamics to piece and insert in correct positions abjad.attach(abjad.Dynamic('ff'), upper_measures[0][0]) abjad.attach(abjad.Dynamic('p'), upper_measures[4][0]) abjad.attach(abjad.Dynamic('pp'), upper_measures[8][0]) abjad.attach(abjad.Dynamic('p'), upper_measures[12][0]) # Add bar line after measure 12 score.add_final_bar_line() # Add tempo and other markups to piece at required positions markup = abjad.Markup('Largo', direction=abjad.Up).italic() abjad.attach(markup, upper_measures[0][0]) markup2 = abjad.Markup('riten.', direction=abjad.Up).italic() abjad.attach(markup2, upper_measures[6][3]) markup3 = abjad.Markup('cresc.', direction=abjad.Down).italic() abjad.attach(markup3, upper_measures[9][1]) # Add fermata to last chord of piece fermata = abjad.Fermata() abjad.attach(fermata, upper_measures[12][0]) abjad.attach(fermata, lower_measures[12][0]) # Add accent to last chord of piece accent = abjad.Articulation('accent') abjad.attach(accent, upper_measures[12][0]) # Initialize the final lilypond file for formatting sheetmusic_file = abjad.LilyPondFile.new( music=score, global_staff_size=20, default_paper_size=('A5', 'portait'), ) # Add title, composer, etc. to the sheet sheetmusic_file.header_block.title = abjad.Markup('Prelude') sheetmusic_file.header_block.composer = abjad.Markup('Frederic Chopin') sheetmusic_file.header_block.tagline = abjad.Markup( 'Written in Python using the Abjad library and Lilypond engraving software - Wesley Olsen' ) sheetmusic_file.header_block.subsubtitle = abjad.Markup('Op. 20 No. 28') # Create Lilypond file and .pdf abjad.show(sheetmusic_file)
for scaled_staff, duplicate_index in zip(scaled_staves, reversed(range(3))): scale_factor = 2**duplicate_index staff = duplicate_music(scale_factor, scaled_staff) score.append(staff) return score def format_score(score, key_signature, time_signature): for staff in score: key_sig = abjad.KeySignature(key_signature.tonic, key_signature.mode) abjad.attach(key_sig, staff[0]) time_sig = abjad.TimeSignature(time_signature) abjad.attach(time_sig, staff[0]) abjad.attach(abjad.Clef('bass'), score[2][0]) def make_canon(melody_staff, key_signature, time_signature): # make a three-voice prolation canon from a melody scaled_staves = make_scaled_staves(melody_staff, time_signature) score = duplicate_score(scaled_staves) format_score(score, key_signature, time_signature) return score # # "Arirang" melody_staff = abjad.Staff("e'4. fs'8 e'4 a'4. b'8 a' b' cs''4 b'8 cs''16 b' a'8 fs' e'4. fs'8 e' fs' a'4. b'8 a' b' cs'' b' a' fs' e' fs' a'4. b'8 a'4 a'2.") melody_staff.extend("e''2 e''4 e'' cs'' b' cs'' b'8 cs''16 b' a'8 fs' e'4. fs'8 e' fs' a'4. b'8 a' b' cs'' b' a' fs' e' fs' a'4. b'8 a'4 a'2.") for note in melody_staff: note.written_pitch += 12 score = make_canon(melody_staff, abjad.KeySignature('d', 'major'), abjad.TimeSignature((3,4))) abjad.show(score)