Example #1
0
    def update(self, streamObj):
        r'''
        Given a Stream object, update attributes with stored objects.
        '''
        from music21 import key
        from music21 import meter
        from music21 import tempo

        environLocal.printDebug(['RichMetadata: update(): start'])

        flat = streamObj.flat.sorted

        self.keySignatureFirst = None
        self.keySignatures = []
        self.tempoFirst = None
        self.tempos = []
        self.timeSignatureFirst = None
        self.timeSignatures = []

        # We combine element searching into a single loop to prevent
        # multiple traversals of the flattened stream.
        for element in flat:
            if isinstance(element, meter.TimeSignature):
                ratioString = element.ratioString
                if ratioString not in self.timeSignatures:
                    self.timeSignatures.append(ratioString)
            elif isinstance(element, key.KeySignature):
                keySignatureString = str(element)
                if keySignatureString not in self.keySignatures:
                    self.keySignatures.append(keySignatureString)
            elif isinstance(element, tempo.TempoIndication):
                tempoIndicationString = str(element)
                if tempoIndicationString not in self.tempos:
                    self.tempos.append(tempoIndicationString)

        if len(self.timeSignatures):
            self.timeSignatureFirst = self.timeSignatures[0]
        if len(self.keySignatures):
            self.keySignatureFirst = self.keySignatures[0]
        if len(self.tempos):
            self.tempoFirst = self.tempos[0]

#        for element in flat:
#            pitches = ()
#            if isinstance(element, note.Note):
#                pitches = (element.pitch,)
#            elif isinstance(element, chord.Chord):
#                pitches = element.pitches
#            for pitch in pitches:
#                if self.pitchHighest is None:
#                    self.pitchHighest = pitch
#                if self.pitchLowest is None:
#                    self.pitchLowest = pitch
#                if pitch.ps < self.pitchLowest.ps:
#                    self.pitchLowest = pitch
#                elif self.pitchHighest.ps < pitch.ps:
#                    self.pitchHighest = pitch
#        self.pitchLowest = str(self.pitchLowest)
#        self.pitchHighest = str(self.pitchHighest)

        self.noteCount = len(flat.notesAndRests)
        self.quarterLength = flat.highestTime

        # commenting out temporarily due to memory error
        # with corpus/beethoven/opus132.xml
        #         # must be a method-level import

        #         environLocal.printDebug(
        #             ['RichMetadata: update(): calling discrete.Ambitus(streamObj)'])
        #
        from music21.analysis import discrete
        self.ambitus = None
        self.pitchHighest = None
        self.pitchLowest = None
        analysisObject = discrete.Ambitus(streamObj)
        psRange = analysisObject.getPitchSpan(streamObj)
        if psRange is not None:  # may be none if no pitches are stored
            # presently, these are numbers; convert to pitches later
            self.pitchLowest = str(psRange[0])
            self.pitchHighest = str(psRange[1])
        self.ambitus = analysisObject.getSolution(streamObj)
Example #2
0
    def update(self, streamObj):
        r'''
        Given a Stream object, update attributes with stored objects.

        >>> rmd = metadata.RichMetadata()
        >>> rmd.keySignatureFirst is None
        True
        >>> rmd.sourcePath
        ''

        >>> b = corpus.parse('bwv66.6')
        >>> rmd.update(b)
        >>> rmd.keySignatureFirst
        3
        >>> rmd.sourcePath
        'bach/bwv66.6.mxl'
        >>> rmd.numberOfParts
        4
        '''
        from music21 import key
        from music21 import meter
        from music21 import tempo

        environLocal.printDebug(['RichMetadata: update(): start'])

        flat = streamObj.flat.sorted

        self.numberOfParts = len(streamObj.parts)
        self.keySignatureFirst = None
        self.keySignatures = []
        self.tempoFirst = None
        self.tempos = []
        self.timeSignatureFirst = None
        self.timeSignatures = []

        self.sourcePath = self.getSourcePath(streamObj)

        # We combine element searching into a single loop to prevent
        # multiple traversals of the flattened stream.
        for element in flat:
            if isinstance(element, meter.TimeSignature):
                ratioString = element.ratioString
                if ratioString not in self.timeSignatures:
                    self.timeSignatures.append(ratioString)
            elif isinstance(element, key.KeySignature):
                if element.sharps not in self.keySignatures:
                    self.keySignatures.append(element.sharps)
            elif isinstance(element, tempo.TempoIndication):
                tempoIndicationString = str(element)
                if tempoIndicationString not in self.tempos:
                    self.tempos.append(tempoIndicationString)

        if self.timeSignatures:
            self.timeSignatureFirst = self.timeSignatures[0]
        if self.keySignatures:
            self.keySignatureFirst = self.keySignatures[0]
        if self.tempos:
            self.tempoFirst = self.tempos[0]

#        for element in flat:
#            pitches = ()
#            if isinstance(element, note.Note):
#                pitches = (element.pitch,)
#            elif isinstance(element, chord.Chord):
#                pitches = element.pitches
#            for pitch in pitches:
#                if self.pitchHighest is None:
#                    self.pitchHighest = pitch
#                if self.pitchLowest is None:
#                    self.pitchLowest = pitch
#                if pitch.ps < self.pitchLowest.ps:
#                    self.pitchLowest = pitch
#                elif self.pitchHighest.ps < pitch.ps:
#                    self.pitchHighest = pitch
#        self.pitchLowest = str(self.pitchLowest)
#        self.pitchHighest = str(self.pitchHighest)

        self.noteCount = len(flat.notesAndRests)
        self.quarterLength = flat.highestTime

        # commenting out temporarily due to memory error
        # with corpus/beethoven/opus132.xml
        #         # must be a method-level import

        #         environLocal.printDebug(
        #             ['RichMetadata: update(): calling discrete.Ambitus(streamObj)'])
        #
        from music21.analysis import discrete
        self.ambitus = None
        self.pitchHighest = None
        self.pitchLowest = None
        analysisObject = discrete.Ambitus(streamObj)
        psRange = analysisObject.getPitchSpan(streamObj)
        if psRange is not None:  # may be none if no pitches are stored
            # presently, these are numbers; convert to pitches later
            self.pitchLowest = psRange[0].nameWithOctave
            self.pitchHighest = psRange[1].nameWithOctave
        ambitusInterval = analysisObject.getSolution(streamObj)
        self.ambitus = AmbitusShort(
            semitones=ambitusInterval.semitones,
            diatonic=ambitusInterval.diatonic.simpleName,
            pitchLowest=self.pitchLowest,
            pitchHighest=self.pitchHighest,
        )