fork download
  1. names = 'CDEFGAB'
  2. solfege = ['Do', 'Re', 'Mi', 'Fa', 'So', 'La', 'Ti']
  3. solfege_intervals = [0, 2, 4, 5, 7, 9, 11]
  4. offsets = {
  5. 'bb': -2,
  6. 'b': -1,
  7. '': 0,
  8. '#': 1,
  9. 'x': 2,
  10. }
  11.  
  12. def note(scale, position):
  13. note_name = names[(names.index(scale[0]) + solfege.index(position)) % len(names)]
  14. tonic = dict(zip(names, solfege_intervals))[scale[0]] + offsets[scale[1:]]
  15. semitones = (tonic + dict(zip(solfege, solfege_intervals))[position]) % 12
  16. accidental = semitones - dict(zip(names, solfege_intervals))[note_name]
  17. return note_name + {v: k for k, v in offsets.items()}[accidental]
  18.  
  19. if __name__ == '__main__':
  20. print(note('C', 'Do')) # C
  21. print(note('C', 'Re')) # D
  22. print(note('C', 'Mi')) # E
  23. print(note('D', 'Mi')) # F#
  24. print(note('Bb', 'Fa')) # Eb
  25. print(note('A#', 'Fa')) # D#
  26. print(note('A#', 'Mi')) # Cx (C##)
  27. print(note('Fb', 'Fa')) # Bbb
Success #stdin #stdout 0.04s 9336KB
stdin
Standard input is empty
stdout
C
D
E
F#
Eb
D#
Cx
Bbb