Spaces:
Sleeping
Sleeping
File size: 5,706 Bytes
c1e08a0 5e84ffc b9df2e0 5e84ffc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
"""Module containing basic music theory concepts and constants."""
from enum import Enum
import numpy as np
class Notes(str, Enum):
"""Enumeration of musical notes in chromatic scale.
This class represents the twelve notes of the chromatic scale
and provides methods for note manipulation and validation.
It inherits from both str and Enum to provide string-like behavior
while maintaining the benefits of enumeration.
The str inheritance allows direct string operations on the note values,
while Enum ensures type safety and provides a defined set of valid notes.
Examples:
>>> note = Notes.C
>>> isinstance(note, str) # True
>>> note.lower() # 'c'
>>> note + 'm' # 'Cm'
"""
C = "C"
C_SHARP = "C#"
D = "D"
D_SHARP = "D#"
E = "E"
F = "F"
F_SHARP = "F#"
G = "G"
G_SHARP = "G#"
A = "A"
A_SHARP = "A#"
B = "B"
@classmethod
def get_note_index(cls, note: str) -> int:
"""Get the index of a note in the chromatic scale.
Args:
note (str): The note name to find the index for.
Returns:
int: The index of the note in the chromatic scale (0-11).
"""
return list(cls).index(cls(note))
@classmethod
def get_chromatic_scale(cls, note: str) -> list[str]:
"""Return all notes in chromatic order.
Args:
note (str): The note name to start the chromatic scale from.
Returns:
list[str]: A list of note names in chromatic order,
starting from C (e.g., ["C", "C#", "D", ...]).
"""
start_idx = cls.get_note_index(note)
all_notes = [note.value for note in cls]
return all_notes[start_idx:] + all_notes[:start_idx]
@classmethod
def convert_frequency_to_note(cls, frequency: float) -> str:
"""Convert a frequency in Hz to the nearest note name on a piano keyboard.
Args:
frequency: The frequency in Hz.
Returns:
The name of the nearest note.
"""
A4_frequency = 440.0
# Calculate the number of semitones from A4 (440Hz)
n = 12 * np.log2(frequency / A4_frequency)
# Round to the nearest semitone
n = round(n)
# Calculate octave and index of note name with respect to A4
octave = 4 + (n + 9) // 12
note_idx = (n + 9) % 12
note = cls.get_chromatic_scale(cls.C)[note_idx]
return f"{note}{octave}"
@classmethod
def convert_frequency_to_base_note(cls, frequency: float) -> str:
"""Convert frequency to base note name without octave number.
Args:
frequency: Frequency in Hz
Returns:
Base note name (e.g., 'C', 'C#', 'D')
"""
note_with_octave = cls.convert_frequency_to_note(frequency)
return note_with_octave[:-1] # Remove the octave number
class Scale:
"""Musical scale representation and operations.
This class handles scale-related operations including scale generation
and scale note calculations.
"""
SCALES = {
"major": [0, 2, 4, 5, 7, 9, 11],
"natural_minor": [0, 2, 3, 5, 7, 8, 10],
"harmonic_minor": [0, 2, 3, 5, 7, 8, 11],
"diminished": [0, 2, 3, 5, 6, 8, 9, 11],
}
@classmethod
def get_scale_notes(cls, root_note: str, scale_type: str) -> list[str]:
"""Generate scale notes from root note and scale type.
Args:
root_note: The root note of the scale.
scale_type: The type of scale (e.g., "major", "natural_minor").
Returns:
A list of note names in the scale.
Raises:
ValueError: If root_note is invalid or scale_type is not recognized.
"""
if scale_type not in cls.SCALES:
raise ValueError(f"Invalid scale type: {scale_type}")
scale_pattern = cls.SCALES[scale_type]
chromatic = Notes.get_chromatic_scale(root_note)
return [chromatic[interval % 12] for interval in scale_pattern]
class ChordTone:
"""Musical chord tone representation and operations.
This class handles chord tone-related operations
including chord tone generation and chord tone calculation.
"""
CHORD_TONES = {
"maj": [0, 4, 7, 9],
"maj7": [0, 4, 7, 11],
"min7": [0, 3, 7, 10],
"min7(b5)": [0, 3, 6, 10],
"dom7": [0, 4, 7, 10],
"dim7": [0, 3, 6, 9],
}
@classmethod
def get_chord_tones(cls, root_note: str, chord_type: str) -> list[str]:
"""Generate chord tones from root note and chord type.
Args:
root_note: The root note of the chord.
chord_type: The type of chord (e.g., "maj", "maj7").
Returns:
A list of note names in the chord.
"""
if chord_type not in cls.CHORD_TONES:
raise ValueError(f"Invalid chord type: {chord_type}")
chord_pattern = cls.CHORD_TONES[chord_type]
chromatic = Notes.get_chromatic_scale(root_note)
return [chromatic[interval] for interval in chord_pattern]
class Intervals:
"""Musical interval representation and operations.
This class handles interval-related operations
including interval generation and interval calculation.
"""
INTERVALS_MAP = {
"perfect 1st": 0,
"minor 2nd": 1,
"major 2nd": 2,
"minor 3rd": 3,
"major 3rd": 4,
"perfect 4th": 5,
"diminished 5th": 6,
"perfect 5th": 7,
"minor 6th": 8,
"major 6th": 9,
"minor 7th": 10,
"major 7th": 11,
}
|