How to use the music21.interval.Interval function in music21

To help you get started, we’ve selected a few music21 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cuthbertLab / music21 / music21 / musedata / base40.py View on Github external
try:
        simpleIntervalName = base40IntervalTable[simpleDelta]
        simpleInterval = interval.Interval(simpleIntervalName)
    except KeyError:
        raise Base40Exception('Interval not handled by Base40 ' + str(simpleDelta))

    numOctaves = abs(delta) // 40

    sgi = simpleInterval.generic  # Simple generic interval
    # Compound generic interval
    cgi = interval.GenericInterval(direction * (sgi.value + 7 * numOctaves))
    sdi = simpleInterval.diatonic  # Simple diatonic interval

    newInterval = str(sdi.specifier) + str(cgi.value)

    return interval.Interval(newInterval)
github cuthbertLab / music21 / music21 / alpha / webapps / server / zipfileapp.py View on Github external
Function that determines what command to perform on the score and returns the resulting score.
    Currently is a lookup table based on the URL,
    but will be improved and incorporated into webapps/__init__.py
    as it changes to allow for more standard music21 functions
    '''
    commandParts = command.split("/")
    if (len(commandParts) < 3):
        raise exceptions21.Music21Exception("Not enough parts on the command")

    if commandParts[1] == "transpose":
        return sc.transpose(commandParts[2])

    elif commandParts[1] == "allCMajor":
        key = sc.analyze('key')
        (p, unused_mode) = key.pitchAndMode
        intv = interval.Interval(note.Note(p),note.Note('C'))

        for ks in sc.flat.getElementsByClass('KeySignature'):
            ks.transpose(intv, inPlace = True)
        sc.transpose(intv, inPlace = True)
        return sc

    elif commandParts[1] == "addReduction":
        reductionStream = reduction(sc)
        sc.insert(0,reductionStream)
        return sc

    elif commandParts[1] == "reduction":
        reductionStream = reduction(sc)
        return reductionStream

    elif commandParts[1] == "scaleDegrees":
github cuthbertLab / music21 / music21 / chord.py View on Github external
>>> b
        
        
        
        If `inPlace` is True then rather than returning a new chord, the
        chord itself is changed.  
        
        
        >>> a.transpose(aInterval, inPlace=True)
        >>> a
        
        '''
        if hasattr(value, 'diatonic'): # its an Interval class
            intervalObj = value
        else: # try to process
            intervalObj = interval.Interval(value)

        if not inPlace:
            post = copy.deepcopy(self)
        else:
            post = self
        
        for p in post.pitches:
            # we are either operating on self or a copy; always use inPlace
            p.transpose(intervalObj, inPlace=True)
            #pitches.append(intervalObj.transposePitch(p))

        if not inPlace:
            return post
        else:
            return None
github cuthbertLab / music21 / music21 / alpha / webapps / commands.py View on Github external
def generateIntervals(numIntervals, kind=None, octaveSpacing=None):
    if kind in ['anyChords', 'majorChords', 'diatonicTriads', 'diatonicTriadInversions']:
        return generateChords(numIntervals, kind)

    sc = stream.Stream()
    for i in range(numIntervals):
        loPs = pitch.Pitch("C3").ps
        hiPs = pitch.Pitch("C#5").ps
        startPs = random.randrange(loPs ,hiPs)
        startPitch = pitch.Pitch(ps=startPs)
        numHalfSteps = random.randrange(-19, 20)
        intv = interval.ChromaticInterval(numHalfSteps)
        if kind == 'consonant':
            invType = random.choice(['m3', 'M3', 'P5', 'm6', 'M6', 'P8'])
            intv = interval.Interval(invType)
        elif kind == 'noAugDim':
            invType = random.choice(['m2', 'M2', 'm3', 'M3', 'P4', 'P5', 'm6', 'M6', 'm7', 'M7'])
            intv = interval.Interval(invType)
        elif kind == 'dissonant':
            invType = random.choice(['m2', 'M2', 'm3', 'M3', 'P4', 'P5', 'm6', 'M6', 'm7', 'M7'])
            intv = interval.Interval(invType)
        endPitch = intv.transposePitch(startPitch)

        if kind == 'diatonic':
            startPitch = pitch.Pitch(random.choice('abcdefg'))
            endPitch = pitch.Pitch(random.choice('abcdefg'))

        if octaveSpacing is not None:
            startPitch.octave = 4
            endPitch.octave = 4 - octaveSpacing
github cuthbertLab / music21 / music21 / interval.py View on Github external
def testIntervalMicrotonesA(self):
        from music21 import interval, pitch

        i = interval.Interval('m3')
        self.assertEqual(i.chromatic.cents, 300)
        self.assertEqual(i.cents, 300.0)

        i = interval.Interval('p5')
        self.assertEqual(i.chromatic.cents, 700)
        self.assertEqual(i.cents, 700.0)

        i = interval.Interval(8)
        self.assertEqual(i.chromatic.cents, 800)
        self.assertEqual(i.cents, 800.0)

        i = interval.Interval(8.5)
        self.assertEqual(i.chromatic.cents, 850.0)
        self.assertEqual(i.cents, 850.0)

        i = interval.Interval(5.25)  # a sharp p4
        self.assertEqual(i.cents, 525.0)
        # we can subtract the two to get an offset
        self.assertEqual(i.cents, 525.0)
        self.assertEqual(str(i), '')
github cuthbertLab / music21 / music21 / scale.py View on Github external
def _buildNetwork(self):
        '''
        '''
        srcList = ['M2', 'M2', 'm2', 'M2', 'M2', 'M2', 'm2']
        intervalList = srcList[5:] + srcList[:5] # a to A
        self.tonicDegree = 1
        self.dominantDegree = 5
        self._net = intervalNetwork.BoundIntervalNetwork(intervalList, 
                        octaveDuplicating=self.octaveDuplicating,
                        pitchSimplification=None)

        # raise the seventh in all directions
        # 7 here is scale step/degree, not node id
        self._alteredDegrees[7] = {'direction': intervalNetwork.DIRECTION_BI, 
                               'interval': interval.Interval('a1')}
github cuthbertLab / music21 / music21 / expressions.py View on Github external
def __init__(self):
        super().__init__()
        self.direction = ''  # up or down
        self.size = None  # interval.Interval (General, etc.) class
        self.quarterLength = 0.125  # 32nd note default
        self.size = interval.Interval(2)
github cuthbertLab / music21 / music21 / interval.py View on Github external
>>> p = pitch.Pitch('C#4')
        >>> di.transposePitch(p)
        

        Previous pitch was unchanged.  inPlace=True changes that.

        >>> p
        
        >>> di.transposePitch(p, inPlace=True)
        >>> p
        


        Changed in v.6 -- added inPlace
        '''
        fullIntervalObject = Interval(diatonic=self, chromatic=self.getChromatic())
        return fullIntervalObject.transposePitch(p, inPlace=inPlace)
github cuthbertLab / music21 / music21 / alpha / counterpoint / species.py View on Github external
continue
            if self.isParallelUnison(prevNote, note1, prevFirmus, currFirmus):
                continue

            if self.isParallelFifth(prevNote, note1, prevFirmus, currFirmus):
                continue

            if self.isParallelOctave(prevNote, note1, prevFirmus, currFirmus):
                continue
            if self.isHiddenFifth(prevNote, note1, prevFirmus, currFirmus):
                continue

            if self.isHiddenOctave(prevNote, note1, prevFirmus, currFirmus):
                continue

            if interval.Interval(currFirmus, note1).direction < 0:
                continue
            if interval.Interval(currFirmus, note1).generic.value > 10:
                continue
            environLocal.printDebug(["adding: ", note1.name, note1.octave])
            valid.append(note1)
        return valid
github czhuang / ChordRipple / chord2vec / music21_chord_tools.py View on Github external
def root_interval(sym, sym_next):
    # print sym, sym_next
    ch = harmony.ChordSymbol(sym)
    ch_next = harmony.ChordSymbol(sym_next)
    ch_root = ch.findRoot()
    ch_next_root = ch_next.findRoot()
    intvl = interval.Interval(ch_root, ch_next_root)
    semitones = intvl.chromatic.semitones
    return semitones