Pitch¶
Introduction¶
This module provides classes and methods for handling musical pitch data.
It includes the _PSet private class, which extends the core _Set class to specifically manage pitch-related operations.
The module also provides the Chord and Scale classes for handling chords and scales, respectively.
_PSet Class¶
The _PSet class extends the core _Set class to add pitch-specific functionality.
It adds properties and methods for handling MIDI pitches, intervals, root notes, and frequency conversions.
This class serves as the base for more specialized pitch classes like Chord and Scale.
- _PSet.__init__(values: Sequence[Real], offset: Real) None
Representation of a list of notes
- Parameters:
values (ArrayLike) – Notes.
offset (Numeric) – Offset for the notes.
- Example:
>>> a = _PSet([9, 0, 7, 1], 60) >>> print(a) _PSet = [69, 60, 67, 61]
- _PSet.pitches¶
Return the absolute pitches in MIDI format.
- Returns:
list of pitches
- Return type:
list[Numeric]
- Example:
>>> a = _PSet([9, 0, 7, 1], 60) >>> print(a.pitches) [69, 60, 67, 61]
- _PSet.intervals¶
Returns the intervals of the sequence. NB: need the root note!!
- Returns:
list of intervals
- Return type:
list[Numeric]
- Example:
>>> a = _PSet([9, 0, 7, 1], 60) >>> print(a.intervals) [9, 0, 7, 1]
- _PSet.root¶
Return the root note
- Returns:
root note
- Return type:
int
- Example:
>>> a = _PSet([9, 0, 7, 1], 60) >>> print(a.root) 60
- _PSet.to_freq(A4: float = 440) list[float]¶
Convert midi pitches in frequency
- Parameters:
A4 (float) – A4 tuning in frequence
- Returns:
list of frequencies
- Return type:
list[float]
- Example:
>>> a = _PSet([69, 60, 57, 81]) >>> print(a.to_freq()) [440.0, 261.6255653005986, 220.0, 880.0]
- _PSet.to_midi(a4: float = 440) _PSet¶
Convert frequencies in midi pitches :param a4: A4 tuning in frequence :type a4: float :return: new Self :rtype: Self
- Example:
>>> a = _PSet([440, 265, 220, 880]) >>> print(a.to_midi()) _PSet = [69.0, 64.0, 62.0, 81.0]
- classmethod _PSet.from_freq(freqs: list[float], a4: float = 440.0) Self¶
Create new pitch class form list of frequencies
- Parameters:
freqs (list[float]) – list of frequencies
a4 (float) – A4 tuning in frequence
- Returns:
new pitch istance
- Return type:
Self
- Example:
>>> a = [440, 261, 220, 880] >>> b = _PSet.from_freq(a) >>> print(b) _PSet = [69, 60, 57, 81]
- _PSet.to_range(min: int, max: int) Self¶
Move a list of notes to a specific range. If possibile without pitch change, otherwhise min if lower, max if higher.
- Parameters:
min (int) – min value of range
max (int) – max value of range
- Returns:
New Self
- Return type:
Self
- Example:
>>> a = _PSet(ScaleModel.maj, 60) >>> b = a.to_range(36, 48) >>> print(b) _PSet = [36, 38, 40, 41, 43, 45, 47]
- _PSet.micro_quanta(tone_div: int) Self¶
Quantazie to nearest tone division
- Parameters:
tone_div (int) – tone subdivision
- Returns:
self
- Return type:
Self
- Example:
>>> a = _PSet([60.1, 62.3, 63.7, 64.5, 65.9, 66.2, 67.8]) >>> b = a.micro_quanta(4) >>> print(b) _PSet = [60.0, 62.25, 63.75, 64.5, 65.75, 66.25, 67.75]
- _PSet.scale_quanta(scale: ScaleModel | Sequence[Real], unique: bool = False) Self¶
Quantize to nearest note of scale :param scale: scale model or list of intervals :type scale: ScaleModel | ArrayLike :param unique: if True, return only one note for each pitch class, default False :type unique: bool :return: new Self :rtype: Self
- Example:
>>> a = _PSet([60, 62, 63, 64, 65, 66, 67]) >>> b = a.scale_quanta(ScaleModel.maj, True) >>> print(b) _PSet = [60, 62, 64, 65, 67]
Chord Class¶
The Chord class represents a musical chord as a collection of pitches.
It extends _PSet and provides methods for creating chords from models, inverting chords, and manipulating chord notes.
Chords are defined by a list of intervals relative to a root note, and they inherit all operations from the _Set class.
from musicnpy import Chord, ChordModel
# Create a chord by specifying intervals and root
c = Chord([0, 4, 7], 60)
print(c.pitches) # [60, 64, 67]
# Create a chord from a model
maj = Chord.new(ChordModel.maj, 60)
print(maj)
- Chord.__init__(notes: Sequence[Real] = [0, 4, 7], root: Real = 0) None¶
Rapresentation of Chord
- Parameters:
notes (ArrayLike) – List of intervals or pitches
root (Numeric) – Root note
- Example:
>>> a = Chord([0, 4, 7], 60) >>> print(a) Chord = [60, 64, 67]
- classmethod Chord.new(model: ChordModel, root: Real = 0) Chord¶
Create a Chord from ChordModel database
- Parameters:
model (ChordModel) – ChordModel istance
root (Numeric) – Root note
- Returns:
new Chord instance
- Return type:
Chord
- Example:
>>> a = Chord.new(ChordModel.aug, 60) >>> print(a) Chord = [61, 65, 69]
- Chord.n_inversion(pos: int) Chord¶
Inversion of chord
- Parameters:
pos (int) – position of inversion
- Returns:
New Chord
- Return type:
Self
- Example:
>>> a = Chord([0, 1, 2, 3]) >>> b = a.n_inversion(2) >>> print(b) Chord = [2, 3, 12, 13]
- Chord.first_inv¶
First inversion
- Returns:
New Chord
- Return type:
Chord
- Example:
>>> a = Chord.new(ChordModel.maj, 60) >>> b = a.first_inv >>> print(b) Chord = [64, 67, 72]
- Chord.second_inv¶
Second inversion
- Returns:
New Chord
- Return type:
Chord
- Example:
>>> a = Chord.new(ChordModel.maj, 60) >>> b = a.second_inv >>> print(b) Chord = [67, 72, 76]
- Chord.drop(pos: int) Chord¶
Drop note at specific position by single octave, up or down
- Parameters:
pos (int) – position of drop
- Returns:
New Chord
- Return type:
Chord
- Example:
>>> a = Chord([0, 1, 2, 3, 4]).drop(3) >>> print(a) >>> Chord = [0, 1, -10, 3, 4]
- Chord.drop2¶
Drop the second highest note
- Returns:
New Chord
- Return type:
Chord
- Example:
>>> a = Chord([0, 4, 7, 10], 60).drop2 >>> print(a) >>> Chord = [60, 64, 55, 70]
- Chord.drop3¶
Drop the third highest note
- Returns:
New Chord
- Return type:
Chord
- Example:
>>> a = Chord([0, 4, 7, 10], 60).drop3 >>> print(a) >>> Chord = [60, 52, 67, 70]
- Chord.octaver(octaves: Sequence[Real]) Chord¶
Shif by octaves specific pitches of chord
- Parameters:
octaves (ArrayLike) – array of octave
- Returns:
New Chord
- Return type:
Self
- Example:
>>> a = Chord([60, 62, 63]) >>> b = a.octaver([0, 1, -1]) print(b) Chord = [60, 74, 51]
Scale Class¶
The Scale class represents a musical scale as an ordered collection of pitches.
It extends _PSet and adds harmonic and diatonic degree functionality.
Scales can be created from interval patterns or from scale models in the database.
from musicnpy import Scale, ScaleModel
# Create a scale by specifying intervals and root
s = Scale([0, 2, 4, 5, 7, 9, 11], 60)
print(s.pitches) # [60, 62, 64, 65, 67, 69, 71]
# Create a scale from a model
maj = Scale.new(ScaleModel.maj, 60)
print(maj)
- Scale.__init__(intervals: Sequence[Real], root: Real = 0, harmo: Sequence[Real] = None) None¶
Rappresentation of a musicale Scale
- Parameters:
intervals (ArrayLike) – List of intervals
root (Numeric) – Root note
harmo (ArrayLike) – List of Chord class or ChordModel
- Example:
>>> a = Scale([0, 2, 4, 5, 7, 9, 11], 60) >>> print(a) Scale = [60, 62, 64, 65, 67, 69, 71]
- classmethod Scale.new(model: ScaleModel, root: int = 60) Scale¶
Create a Scale from ScaleModel database
- Parameters:
model (ScaleModel) – scale model
root (int) – root note
- Returns:
new istance
- Return type:
Scale
- Example:
>>> a = Scale.new(ScaleModel.maj, 60) >>> print(a) Scale = [60, 62, 63, 65, 67, 68, 70]
- Scale.diatonic¶
List of ints
- Returns:
Description
- Return type:
list[int]
- Example:
>>> a = Scale([0, 2, 3, 5, 7, 8, 10], 76) >>> print(a.diatonic) [1, 2, 3, 4, 5, 6, 7]
- Scale.harmonize¶
List of chords fo the scale.
- Parameters:
self – Description
- Returns:
Description
- Return type:
list
Spectra Class¶
Spectra allow to create a collection of frequencies, that can be used to create a chord or a scale.
It also provied some class method for create spectra series.
from musicnpy import Spectra
# Create a spectra by specifying frequencies
s = Spectra([440, 880, 1320])
print(s.frequencies) # [440, 880, 1320]
- Spectra.__init__(freqs: Sequence[Real]) None¶
Rappresentation of a spectra
- Parameters:
freqs (ArrayLike) – list of frequencies
- classmethod Spectra.harm_series(fond: float, lenght: int, factor: float = 1) Spectra¶
Create harmonic series from fondamental frequency.
- Parameters:
fond (float) – fondamental frequency
lenght (int) – number of partials
factor (float) – factor of partials, default 1 (harmonic series)
a4 (float) – A4 tuning in frequence
- Returns:
new Spectra
- Return type:
Spectra
- Example:
>>> a = Spectra.harm_series(55, 8, 1) >>> print(a) Spectra = [33.0, 45.0, 52.0, 57.0, 61.0, 64.0, 67.0, 69.0]
- Spectra.ring_mod(mod: float) Spectra¶
Ring modulation of spectra with carrier
- Parameters:
carrier (_PSet) – carrier for ring modulation
mod (float) – modulation index
- Returns:
new Spectra
- Return type:
Spectra
- Example:
>>> a = Spectra.harm_series(55, 8, 1) >>> b = a.ring_mod(Scale.new(ScaleModel.maj, 60), 0.5) >>> print(b) Spectra = [15.0, 22.5, 26.0, 28.5, 30.5, 32.0, 33.5, 34.5]
- classmethod Spectra.to_ring_mod(carrier: Chord | Scale | _PSet, mod: float, a4: float = 440) Spectra¶
Ring modulation of a carrier with modulation index
- Parameters:
carrier (Chord | Scale | _PSet) – carrier for ring modulation
mod (float) – modulation index
a4 (float) – A4 tuning in frequence
- Returns:
new Spectra
- Return type:
Spectra
- Example:
>>> a = Chord([0, 4, 7], 60) >>> b = Spectra.to_ring_mod(a, 21.234) >>> print(b) Spectra = [418.61, 481.83, 523.25, 461.77, 440.0, 502.46]