Generate some very detailed piano key mappings (for python.) W/42 keys and good…
- User
Generate some very detailed piano key mappings (for python.) W/42 keys and good formatting.
- Assistant
Generate some very detailed piano key mappings (for python.) W/42 keys and good formatting.
Certainly! Below is a detailed Python script that maps 42 piano keys with comprehensive information for each key. The mapping includes the key number, note name, octave, frequency (in Hz), MIDI number, and whether it's a white or black key.
This script utilizes data classes for better structure and readability and automatically calculates frequencies based on the standard tuning (A4 = 440 Hz). The mapping starts from C3 and spans across 3.5 octaves.
import math
from dataclasses import dataclass
from typing import List
@dataclass
class PianoKey:
key_number: int
note: str
octave: int
frequency: float
midi_number: int
is_white_key: bool
def calculate_frequency(midi_number: int) -> float:
"""Calculate the frequency of a MIDI note number."""
return round(440.0 * (2 ** ((midi_number - 69) / 12)), 2)
def generate_piano_keys(start_midi: int, total_keys: int) -> List[PianoKey]:
"""Generate a list of PianoKey mappings."""
# Define note names in an octave
note_names = ['C', 'C#', 'D', 'D#', 'E', 'F',
'F#', 'G', 'G#', 'A', 'A#', 'B']
# Set to determine if a note is a white key
white_keys = {'C', 'D', 'E', 'F', 'G', 'A', 'B'}
piano_keys = []
for i in range(total_keys):
midi_number = start_midi + i
octave = (midi_number // 12) - 1
note_index = midi_number % 12
note = note_names[note_index]
frequency = calculate_frequency(midi_number)
is_white = note in white_keys
piano_key = PianoKey(
key_number=i + 1,
note=note,
octave=octave,
frequency=frequency,
midi_number=midi_number,
is_white_key=is_white
)
piano_keys.append(piano_key)
return piano_keys
def display_piano_keys(piano_keys: List[PianoKey]):
"""Display the piano keys in a formatted table."""
header = f"{'Key #':<6} {'Note':<4} {'Octave':<6} {'Freq (Hz)':<10} {'MIDI #':<7} {'Type':<6}"
separator = "-" * len(header)
print(header)
print(separator)
for key in piano_keys:
key_type = 'White' if key.is_white_key else 'Black'
print(f"{key.key_number:<6} {key.note:<4} {key.octave:<6} {key.frequency:<10} {key.midi_number:<7} {key_type:<6}")
if __name__ == "__main__":
START_MIDI_NUMBER = 48 # C3
TOTAL_KEYS = 42
piano_keys = generate_piano_keys(START_MIDI_NUMBER, TOTAL_KEYS)
display_piano_keys(piano_keys)
Data Class (PianoKey):
key_number: Sequential number from 1 to 42.note: Note name (e.g., C, C#, D).octave: Octave number.frequency: Frequency in Hertz, calculated based on MIDI number.midi_number: Corresponding MIDI note number.is_white_key: Boolean indicating if it's a white key.Frequency Calculation (calculate_frequency):
Generating Piano Keys (generate_piano_keys):
PianoKey instance to a list.Displaying Piano Keys (display_piano_keys):
Execution (__main__):
When you run the script, it will display a table similar to the following:
Key # Note Octave Freq (Hz) MIDI # Type
--------------------------------------------
1 C 3 130.81 48 White
2 C# 3 138.59 49 Black
3 D 3 146.83 50 White
4 D# 3 155.56 51 Black
5 E 3 164.81 52 White
6 F 3 174.61 53 White
7 F# 3 185.0 54 Black
8 G 3 196.0 55 White
9 G# 3 207.65 56 Black
10 A 3 220.0 57 White
11 A# 3 233.08 58 Black
12 B 3 246.94 59 White
13 C 4 261.63 60 White
14 C# 4 277.18 61 Black
15 D 4 293.66 62 White
16 D# 4 311.13 63 Black
17 E 4 329.63 64 White
18 F 4 349.23 65 White
19 F# 4 369.99 66 Black
20 G 4 392.0 67 White
21 G# 4 415.3 68 Black
22 A 4 440.0 69 White
23 A# 4 466.16 70 Black
24 B 4 493.88 71 White
25 C 5 523.25 72 White
26 C# 5 554.37 73 Black
27 D 5 587.33 74 White
28 D# 5 622.25 75 Black
29 E 5 659.25 76 White
30 F 5 698.46 77 White
31 F# 5 739.99 78 Black
32 G 5 784.0 79 White
33 G# 5 830.61 80 Black
34 A 5 880.0 81 White
35 A# 5 932.33 82 Black
36 B 5 987.77 83 White
37 C 6 1046.5 84 White
38 C# 6 1108.73 85 Black
39 D 6 1174.66 86 White
40 D# 6 1244.51 87 Black
41 E 6 1318.51 88 White
42 F 6 1396.91 89 White
Starting Note: By changing the START_MIDI_NUMBER, you can start the mapping from a different note. For example, START_MIDI_NUMBER = 60 starts from C4.
Total Keys: Adjust the TOTAL_KEYS variable to generate a different number of keys as per your requirement.
You can integrate this mapping into your Python projects for various purposes, such as:
Feel free to modify and expand the script to suit your specific needs!
Not yet included in a published release.