예제 #1
0
def notate_score(musician_names, instrument_names, music):
    score = Score()

    for musician_name, instrument_name in zip(musician_names,
                                              instrument_names):
        instrument = get_instrument(instrument_name)
        instrument.partName = instrument.instrumentName
        instrument.partAbbreviation = instrument.instrumentAbbreviation

        parts = []
        part = Part()
        parts.append(part)
        part.insert(0, instrument)

        score.insert(0, part)
        score.insert(0, StaffGroup(parts))

        notes = music[musician_name]

        for pitches in notes:
            if not pitches or pitches == 'stop':
                note = Rest()
            elif len(pitches) == 1:
                pitch = Pitch(pitches[0] + 60)
                note = Note(pitch)
            else:
                note = Chord(notes=[Pitch(p + 60) for p in pitches])

            duration = Duration()
            duration.fill([4.0])
            note.duration = duration

            part.append(note)

    score.show('musicxml', '/Applications/Sibelius 7.5.app')
예제 #2
0
def make_music21_score(
    part_names=('violin', 'flute', 'oboe', 'clarinet', 'alto_saxophone',
                'trumpet', 'bass', 'percussion'),
    title='Title',
    composer='Jonathan Marmor',
    time_signature=None,
    starting_tempo_bpm=60,
    starting_tempo_quarter_duration=1.0,
    timestamp=None,
):
    if not timestamp:
        timestamp = datetime.datetime.utcnow()
    metadata = Metadata()
    metadata.title = title
    metadata.composer = composer
    metadata.date = timestamp.strftime('%Y/%m/%d')

    score = Score()
    score.insert(0, metadata)

    for part_name in part_names:

        instrument_name, instrument_number = parse_part_name(part_name)

        instrument = instrument_data[instrument_name]

        part = Part()

        metronome_mark = MetronomeMark(
            number=starting_tempo_bpm,
            referent=Duration(starting_tempo_quarter_duration))
        part.append(metronome_mark)

        if time_signature:
            # Should be a string like '12/8'
            music21_time_signature = TimeSignature(time_signature)
            part.append(music21_time_signature)

        m21_instrument = instrument['class']()
        m21_instrument.partName = instrument['name']
        m21_instrument.partAbbreviation = instrument['abbreviation']

        if instrument_number > 1:
            m21_instrument.partName = '{} {}'.format(instrument['name'],
                                                     instrument_number)
            m21_instrument.partAbbreviation = '{} {}'.format(
                instrument['abbreviation'], instrument_number)

        part.insert(0, m21_instrument)

        clef = instrument.get('clef')
        if clef:
            part.append(clef())

        score.insert(0, part)

    return score
def write_notation_cell(music, path, event_index):
    score = Score()

    metadata = Metadata()
    metadata.title = ''
    metadata.composer = ''
    score.insert(0, metadata)

    layout = ScoreLayout()
    layout.scalingMillimeters = 1.25
    layout.scalingTenths = 40
    score.insert(0, layout)

    for musician in music:
        instrument_name = musician['instrument']
        instrument = get_instrument(instrument_name)
        instrument.partName = instrument.instrumentName
        if instrument.instrumentName is 'Violoncello':
            instrument.partName = 'Cello'
        instrument.partAbbreviation = instrument.instrumentAbbreviation

        parts = []
        part = Part()
        parts.append(part)
        part.insert(0, instrument)

        score.insert(0, part)
        # score.insert(0, StaffGroup(parts))

        for event in musician['music']:
            pitches = event['pitches']
            dur = event['duration']
            # if not pitches or pitches == 'stop':
            #     note = Rest()
            if len(pitches) == 1:
                pitch = Pitch(pitches[0] + 60)
                note = Note(pitch)
            else:
                note = Chord(notes=[Pitch(p + 60) for p in pitches])

            duration = Duration()
            duration.fill([dur])
            note.duration = duration

            part.append(note)

    file_path = os.path.join(path, str(event_index).zfill(2))
    musicxml_file_path = file_path + '.xml'
    png_output_file_path = file_path + '.png'

    score.write('musicxml', musicxml_file_path)

    write_png_with_musescore(musicxml_file_path, png_output_file_path, dpi=600)
예제 #4
0
def setup_parts(instruments, score):
    parts = []
    for instrument in instruments:
        part = Part()
        m21_instrument = get_instrument(instrument['name'])

        part.insert(0, m21_instrument)
        if instrument.get('clef') is 'bass':
            part.insert(0, BassClef())

        parts.append(part)
        score.insert(0, part)

    score.insert(0, StaffGroup(parts))

    return parts
예제 #5
0
def generate_song(chord_length=15,show_symbols=False):
   '''Generate a random chord progression with a piano/bass comp and
      return as a Music21 score. Default length of 15 chord changes
      (plus the fixed ones at start/end) makes a score about one A4 page.'''
   # Start with a blank score
   score = Score()
   # TODO: Add swing rhythm indicator without having to do it manually
   # in musescore (how to with music21?)

   # Add tracks/intstruments - names etc will be set automatically
   piano = Part()
   piano.insert(0, Piano())
   score.insert(0,piano)

   bass = Part()
   bass.insert(0, AcousticBass())
   score.insert(0,bass)

   #hihat = Part()   TODO drum kit

   # Get a random progression
   prog = ProgressionGenerator()
   prog.generate(chord_length)

   # Go through the progression, adding a comp for each chord
   for chord_choice in prog.chords:
      roman = RomanNumeral(chord_choice)   # Convert string into a generic Roman I/IV/etc chord

      # Duration = eights until the next chord change.
      # at least 1 bar on "important" chords (I,IV,V)
      if chord_choice in ('Imaj7', 'IVmaj7', 'V7'):
         duration = random.choice((8,8,8,8,10,10,12,12,14,16))
      else: # 1 bar or less on "minor" (pun intended) chords
         duration = random.choice((2,4,4,4,6,6,8,8,8,8))

      add_piano_riff(roman, duration, piano, show_symbols)
      add_bass_walk(roman, duration, bass)
      # TODO drum part

   # ending riff on last bar or two
   duration = random.choice((8,8,16))
   add_piano_closing(RomanNumeral('Imaj7'), duration, piano, show_symbols)
   add_bass_closing(RomanNumeral('Imaj7'), duration, bass)
   return score
예제 #6
0
from music21.instrument import fromString as get_instrument
from music21.clef import BassClef

timestamp = datetime.datetime.utcnow()
metadata = Metadata()
metadata.title = 'The Title'
metadata.composer = 'Jonathan Marmor'
metadata.date = timestamp.strftime('%Y/%m/%d')

score = Score()
score.insert(0, metadata)

part = Part()
parts = [part]

oboe = get_instrument('oboe')
part.insert(0, oboe)
score.insert(0, part)
score.insert(0, StaffGroup(parts))

for dur in [[1, .5], [.25], [.25, 2]]:
    pitch = Pitch(60)
    note = Note(pitch)
    duration = Duration()
    duration.fill(dur)
    note.duration = duration

    part.append(note)

score.show('musicxml', '/Applications/Sibelius 7.5.app')