3 # Music - an utility class to deal with musical details
5 # Copyright (C) 2016 Antonio Ospite <ao2@ao2.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # The 88 piano keyboard goes from A0 (-39) to C8 (+48), see:
21 # https://en.wikipedia.org/wiki/Piano_key_frequencies
22 PIANO_88_KEYS_RANGE = range(-39, 48 + 1)
25 class SpnNote(object):
26 def __init__(self, note_number):
27 # Scientific Pitch Notation values are from -48 to +83
28 # https://en.wikipedia.org/wiki/Scientific_pitch_notation
29 if note_number < -48 or note_number > 83:
30 raise ValueError("Invalid Scientific Pitch Notation number")
32 self.note_number = note_number
33 self.name = self._name()
34 self.frequency = self._frequency()
37 note_names = ["C", "C#", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"]
39 note_offset = self.note_number % 12
40 name = note_names[note_offset]
41 octave = (self.note_number + 48 - note_offset) // 12
42 note_name = "%s%d" % (name, octave)
46 # https://en.wikipedia.org/wiki/A440_(pitch_standard)
47 return round(440 * pow(2, (self.note_number - 9) / 12), 4)
50 return "(%+3d): %-3s frequency: %9.4f" % (self.note_number,
55 class MidiNote(SpnNote):
56 def __init__(self, note_number):
57 # midi notes go from 0 to 127
58 if note_number < 0 or note_number > 127:
59 raise ValueError("Invalid midi note")
61 # In Scientific Pitch Notation C4 is 0
63 SpnNote.__init__(self, note_number - 60)
68 print("Note A0? %s" % A0)
72 print("Note C4? %s" % C4)
76 print("Note A4? %s" % A4)
80 print("Note C8? %s" % C8)
85 midi_C4 = MidiNote(60)
86 print("Midi C4? %s" % midi_C4)
91 if __name__ == "__main__":