Ejemplo n.º 1
0
def modifyPitchNameWithAccidentalString(pitchNameToAlter, accidentalString):
    '''
    Given a pitch name and a length 1 accidental string (such as a sharp or flat), 
    modify the pitch accordingly. Returns the modified pitch name.
    
    >>> from music21 import *
    >>> modifyPitchNameWithAccidentalString('C#', 'N')
    'C'
    >>> modifyPitchNameWithAccidentalString('C-', 'N')
    'C'
    >>> modifyPitchNameWithAccidentalString('C', '#')
    'C#'
    >>> modifyPitchNameWithAccidentalString('C-', '+')
    'C'
    >>> modifyPitchNameWithAccidentalString('C', '-')
    'C-'
    
    OMIT_FROM_DOCS 
    This method has been implemented using the base40 system. The method
    will return an error in the extreme cases where base40 cannot be used.    
    '''
    pitchToAlter = convertToPitch(pitchNameToAlter)
    if pitchToAlter.accidental != None:
        if (pitchToAlter.accidental.alter >= 2.0) \
        and ((accidentalString == "#") or (accidentalString == "+") or (accidentalString == "/")):
            raise FiguredBassScaleException("Base40 cannot raise " \
                                            + str(pitchToAlter.name) + " by a semitone")
        elif (pitchToAlter.accidental.alter <= -2.0) and (accidentalString
                                                          == "-"):
            raise FiguredBassScaleException("Base40 cannot lower " \
                                            + str(pitchToAlter.name) + " by a semitone")

    pitchToAlter.octave = pitchToAlter.implicitOctave
    try:
        base40ToAlter = base40.pitchToBase40(pitchToAlter)
    except Base40Exception:
        raise FiguredBassScaleException("Invalid pitch name-> " +
                                        str(pitchNameToAlter))
    if (accidentalString == "#") or (accidentalString
                                     == "+") or (accidentalString == "/"):
        newPitch = base40.base40ToPitch(base40ToAlter + 1)
    elif accidentalString == "-":
        newPitch = base40.base40ToPitch(base40ToAlter - 1)
    elif accidentalString == "N":
        basePitch = pitchToAlter
        basePitch.accidental = None
        base40RootPitch = base40.pitchToBase40(basePitch)
        if base40RootPitch > base40ToAlter:
            newPitch = base40.base40ToPitch(base40ToAlter +
                                            1)  #Raise by semitone
        elif base40RootPitch < base40ToAlter:
            newPitch = base40.base40ToPitch(base40ToAlter -
                                            1)  #Lower by semitone
    else:
        raise FiguredBassScaleException("Invalid accidental string: " +
                                        accidentalString)

    return newPitch.name
Ejemplo n.º 2
0
def modifyPitchNameWithAccidentalString(pitchNameToAlter, accidentalString):
    '''
    Given a pitch name and a length 1 accidental string (such as a sharp or flat), 
    modify the pitch accordingly. Returns the modified pitch name.
    
    >>> from music21 import *
    >>> modifyPitchNameWithAccidentalString('C#', 'N')
    'C'
    >>> modifyPitchNameWithAccidentalString('C-', 'N')
    'C'
    >>> modifyPitchNameWithAccidentalString('C', '#')
    'C#'
    >>> modifyPitchNameWithAccidentalString('C-', '+')
    'C'
    >>> modifyPitchNameWithAccidentalString('C', '-')
    'C-'
    
    OMIT_FROM_DOCS 
    This method has been implemented using the base40 system. The method
    will return an error in the extreme cases where base40 cannot be used.    
    '''
    pitchToAlter = convertToPitch(pitchNameToAlter)
    if pitchToAlter.accidental != None:
        if (pitchToAlter.accidental.alter >= 2.0) \
        and ((accidentalString == "#") or (accidentalString == "+") or (accidentalString == "/")):
            raise FiguredBassScaleException("Base40 cannot raise " \
                                            + str(pitchToAlter.name) + " by a semitone")
        elif (pitchToAlter.accidental.alter <= -2.0) and (accidentalString == "-"):
            raise FiguredBassScaleException("Base40 cannot lower " \
                                            + str(pitchToAlter.name) + " by a semitone")
    
    pitchToAlter.octave = pitchToAlter.implicitOctave
    try:
        base40ToAlter = base40.pitchToBase40(pitchToAlter)
    except Base40Exception:
        raise FiguredBassScaleException("Invalid pitch name-> " + str(pitchNameToAlter))    
    if (accidentalString == "#") or (accidentalString == "+") or (accidentalString == "/"):
        newPitch = base40.base40ToPitch(base40ToAlter + 1)                
    elif accidentalString == "-":
        newPitch = base40.base40ToPitch(base40ToAlter - 1)        
    elif accidentalString == "N":
        basePitch = pitchToAlter
        basePitch.accidental = None
        base40RootPitch = base40.pitchToBase40(basePitch)
        if base40RootPitch > base40ToAlter:
            newPitch = base40.base40ToPitch(base40ToAlter + 1) #Raise by semitone
        elif base40RootPitch < base40ToAlter:
            newPitch = base40.base40ToPitch(base40ToAlter - 1) #Lower by semitone
    else:
        raise FiguredBassScaleException("Invalid accidental string: " + accidentalString)
    
    return newPitch.name
Ejemplo n.º 3
0
def make_music_data(music_stream, musicdata):
    notes = all_notes(music_stream)
    notes_midi = [note.midi for note in notes]
    musicdata.notes_midi = notes_midi
    musicdata.notes = [pitchToBase40(note) for note in notes]
    musicdata.intervals = intervals_without_direction(notes)
    musicdata.intervals_midi = intervals_midi(notes)
    musicdata.intervals_with_direction = intervals_with_direction(notes)
    musicdata.intervals_classes = intervals_classes(notes)
    _durations = [float(note.duration.quarterLength) for note in notes]
    musicdata.durations = _durations
    musicdata.time_signature = get_time_signature(music_stream)
    _key = music_stream.analyze("key")
    musicdata.mode = _key.mode
    musicdata.key = _key.tonic.name
    musicdata.key_midi = _key.tonic.midi
    musicdata.ambitus = music_stream.analyze("ambitus").chromatic.directed
    musicdata.contour = get_contour(notes_midi)
    musicdata.total_duration = sum(_durations)
Ejemplo n.º 4
0
def make_segments(music, filename):
    composer = filename.split("/")[2]
    _name1 = splitext(basename(filename))[0]
    opus = haydn_opus(_name1) if composer == "haydn" else _name1
    _filename = splitext(basename(filename))[0]

    _name = opus.split("-")
    simple_name = _name[0]

    # skip flute and oboe quartets and fugue
    if simple_name in ["k285", "k298", "k370", "k546", "op001n0"]:
        return

    year = years.get(simple_name)
    key, mode = key_and_mode(composer, _name)

    n = 0
    dic = sonority_dict()
    for measure in music.chordify(addTies=False):
        if isinstance(measure, music21.stream.Measure):
            for element in measure:
                if isinstance(element, music21.chord.Chord):
                    notes = element.pitches

                    notes40 = tuple([pitchToBase40(x) % 40 for x in notes])
                    notes12 = tuple([x.midi % 12 for x in notes])
                    notes_name = tuple([x.name.replace("-", "b") for x in notes])

                    _normal_form = tuple(element.normalForm)
                    _intervals = tuple(intervals(sorted(notes)))
                    _duration = float(element.quarterLength)
                    _beat = element.beat
                    _beat_duration = element.beatDuration.quarterLength
                    _beat_strength = element.beatStrength
                    _interval_vector = tuple(element.intervalVector)
                    _prime_form = tuple(element.primeForm)
                    _measure = element.measureNumber

                    dic['order'].append(n)
                    dic['composer'].append(composer)
                    dic['opus'].append(opus)
                    dic['name'].append(simple_name)
                    dic['notes_base40'].append(notes40)
                    dic['notes_base12'].append(notes12)
                    dic['notes_name'].append(notes_name)
                    dic['prime_form'].append(_prime_form)
                    dic['normal_form'].append(_normal_form)
                    dic['interval_vector'].append(_interval_vector)
                    dic['intervals'].append(_intervals)
                    dic['prime_form_string'].append(to_string(_prime_form))
                    dic['normal_form_string'].append(to_string(_normal_form))
                    dic['interval_vector_string'].append(to_string(_interval_vector))
                    dic['intervals_string'].append(to_string(_intervals))
                    dic['beat'].append(_beat)
                    dic['duration'].append(_duration)
                    dic['measure'].append(_measure)
                    dic['beat_duration'].append(_beat_duration)
                    dic['beat_strength'].append(_beat_strength)
                    dic['year'].append(year)
                    dic['key'].append(key)
                    dic['mode'].append(mode)
                    dic['filename'].append(_filename)
                    n += 1


    return dic
Ejemplo n.º 5
0
 def make_note(n):
     return Note(n.name, pitchToBase40(n), n.duration.quarterLength)