-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotation.py
More file actions
1225 lines (951 loc) · 35.5 KB
/
notation.py
File metadata and controls
1225 lines (951 loc) · 35.5 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Music notation utilities"""
import re
import numpy as np
from numba import jit
from collections import Counter
from .intervals import INTERVALS
from .._cache import cache
from ..util.exceptions import ParameterError
from typing import Dict, List, Iterable, Union, overload
from ..util.decorators import vectorize
from .._typing import _ScalarOrSequence, _FloatLike_co, _SequenceLike, _IterableLike
__all__ = [
"key_to_degrees",
"key_to_notes",
"mela_to_degrees",
"mela_to_svara",
"thaat_to_degrees",
"list_mela",
"list_thaat",
"fifths_to_note",
"interval_to_fjs",
]
THAAT_MAP = dict(
bilaval=[0, 2, 4, 5, 7, 9, 11],
khamaj=[0, 2, 4, 5, 7, 9, 10],
kafi=[0, 2, 3, 5, 7, 9, 10],
asavari=[0, 2, 3, 5, 7, 8, 10],
bhairavi=[0, 1, 3, 5, 7, 8, 10],
kalyan=[0, 2, 4, 6, 7, 9, 11],
marva=[0, 1, 4, 6, 7, 9, 11],
poorvi=[0, 1, 4, 6, 7, 8, 11],
todi=[0, 1, 3, 6, 7, 8, 11],
bhairav=[0, 1, 4, 5, 7, 8, 11],
)
# Enumeration will start from 1
MELAKARTA_MAP = {
k: i
for i, k in enumerate(
[
"kanakangi",
"ratnangi",
"ganamurthi",
"vanaspathi",
"manavathi",
"tanarupi",
"senavathi",
"hanumathodi",
"dhenuka",
"natakapriya",
"kokilapriya",
"rupavathi",
"gayakapriya",
"vakulabharanam",
"mayamalavagaula",
"chakravakom",
"suryakantham",
"hatakambari",
"jhankaradhwani",
"natabhairavi",
"keeravani",
"kharaharapriya",
"gaurimanohari",
"varunapriya",
"mararanjini",
"charukesi",
"sarasangi",
"harikambhoji",
"dheerasankarabharanam",
"naganandini",
"yagapriya",
"ragavardhini",
"gangeyabhushani",
"vagadheeswari",
"sulini",
"chalanatta",
"salagam",
"jalarnavam",
"jhalavarali",
"navaneetham",
"pavani",
"raghupriya",
"gavambodhi",
"bhavapriya",
"subhapanthuvarali",
"shadvidhamargini",
"suvarnangi",
"divyamani",
"dhavalambari",
"namanarayani",
"kamavardhini",
"ramapriya",
"gamanasrama",
"viswambhari",
"syamalangi",
"shanmukhapriya",
"simhendramadhyamam",
"hemavathi",
"dharmavathi",
"neethimathi",
"kanthamani",
"rishabhapriya",
"latangi",
"vachaspathi",
"mechakalyani",
"chitrambari",
"sucharitra",
"jyotisvarupini",
"dhatuvardhini",
"nasikabhushani",
"kosalam",
"rasikapriya",
],
1,
)
}
# Pre-compiled regular expressions for note and key parsing
KEY_RE = re.compile(
r"^(?P<tonic>[A-Ga-g])"
r"(?P<accidental>[#♯𝄪b!♭𝄫♮n]*)"
r":((?P<scale>(maj|min)(or)?)|(?P<mode>(((ion|dor|phryg|lyd|mixolyd|aeol|locr)(ian)?)|phr|mix|aeo|loc)))$"
)
NOTE_RE = re.compile(
r"^(?P<note>[A-Ga-g])"
r"(?P<accidental>[#♯𝄪b!♭𝄫♮n]*)"
r"(?P<octave>[+-]?\d+)?"
r"(?P<cents>[+-]\d+)?$"
)
# A dictionary converting the tonic name to the associated major key, e.g. C Dorian uses the notes of the Bb major scale, hence MAJOR_DICT['dor']['C'] = 'B♭'
MAJOR_DICT = {
'ion': {'C': 'C', 'D': 'D', 'E': 'E', 'F': 'F', 'G': 'G', 'A': 'A', 'B': 'B'},
'dor': {'C': 'B♭', 'D': 'C', 'E': 'D', 'F': 'E♭', 'G': 'F', 'A': 'G', 'B': 'A'},
'phr': {'C': 'A♭', 'D': 'B♭', 'E': 'C', 'F': 'D♭', 'G': 'E♭', 'A': 'F', 'B': 'G'},
'lyd': {'C': 'G', 'D': 'A', 'E': 'B', 'F': 'C', 'G': 'D', 'A': 'E', 'B': 'F♯'},
'mix': {'C': 'F', 'D': 'G', 'E': 'A', 'F': 'B♭', 'G': 'C', 'A': 'D', 'B': 'E'},
'aeo': {'C': 'E♭', 'D': 'F', 'E': 'G', 'F': 'A♭', 'G': 'B♭', 'A': 'C', 'B': 'D'},
'loc': {'C': 'D♭', 'D': 'E♭', 'E': 'F', 'F': 'G♭', 'G': 'A♭', 'A': 'B♭', 'B': 'C'}
}
OFFSET_DICT = { "ion": 0, "dor": 1, "phr": 2, "lyd": 3, "mix": 4, "aeo": 5, "loc": 6 }
ACC_MAP = {"#": 1, "♮": 0, "": 0, "n": 0, "b": -1, "!": -1, "♯": 1, "♭": -1, "𝄪": 2, "𝄫": -2}
def thaat_to_degrees(thaat: str) -> np.ndarray:
"""Construct the svara indices (degrees) for a given thaat
Parameters
----------
thaat : str
The name of the thaat
Returns
-------
indices : np.ndarray
A list of the seven svara indices (starting from 0=Sa)
contained in the specified thaat
See Also
--------
key_to_degrees
mela_to_degrees
list_thaat
Examples
--------
>>> librosa.thaat_to_degrees('bilaval')
array([ 0, 2, 4, 5, 7, 9, 11])
>>> librosa.thaat_to_degrees('todi')
array([ 0, 1, 3, 6, 7, 8, 11])
"""
return np.asarray(THAAT_MAP[thaat.lower()])
def mela_to_degrees(mela: Union[str, int]) -> np.ndarray:
"""Construct the svara indices (degrees) for a given melakarta raga
Parameters
----------
mela : str or int
Either the name or integer index ([1, 2, ..., 72]) of the melakarta raga
Returns
-------
degrees : np.ndarray
A list of the seven svara indices (starting from 0=Sa)
contained in the specified raga
See Also
--------
thaat_to_degrees
key_to_degrees
list_mela
Examples
--------
Melakarta #1 (kanakangi):
>>> librosa.mela_to_degrees(1)
array([0, 1, 2, 5, 7, 8, 9])
Or using a name directly:
>>> librosa.mela_to_degrees('kanakangi')
array([0, 1, 2, 5, 7, 8, 9])
"""
if isinstance(mela, str):
index = MELAKARTA_MAP[mela.lower()] - 1
elif 0 < mela <= 72:
index = mela - 1
else:
raise ParameterError(f"mela={mela} must be in range [1, 72]")
# always have Sa [0]
degrees = [0]
# Fill in Ri and Ga
lower = index % 36
if 0 <= lower < 6:
# Ri1, Ga1
degrees.extend([1, 2])
elif 6 <= lower < 12:
# Ri1, Ga2
degrees.extend([1, 3])
elif 12 <= lower < 18:
# Ri1, Ga3
degrees.extend([1, 4])
elif 18 <= lower < 24:
# Ri2, Ga2
degrees.extend([2, 3])
elif 24 <= lower < 30:
# Ri2, Ga3
degrees.extend([2, 4])
else:
# Ri3, Ga3
degrees.extend([3, 4])
# Determine Ma
if index < 36:
# Ma1
degrees.append(5)
else:
# Ma2
degrees.append(6)
# always have Pa [7]
degrees.append(7)
# Determine Dha and Ni
upper = index % 6
if upper == 0:
# Dha1, Ni1
degrees.extend([8, 9])
elif upper == 1:
# Dha1, Ni2
degrees.extend([8, 10])
elif upper == 2:
# Dha1, Ni3
degrees.extend([8, 11])
elif upper == 3:
# Dha2, Ni2
degrees.extend([9, 10])
elif upper == 4:
# Dha2, Ni3
degrees.extend([9, 11])
else:
# Dha3, Ni3
degrees.extend([10, 11])
return np.array(degrees)
@cache(level=10)
def mela_to_svara(
mela: Union[str, int], *, abbr: bool = True, unicode: bool = True
) -> List[str]:
"""Spell the Carnatic svara names for a given melakarta raga
This function exists to resolve enharmonic equivalences between
pitch classes:
- Ri2 / Ga1
- Ri3 / Ga2
- Dha2 / Ni1
- Dha3 / Ni2
For svara outside the raga, names are chosen to preserve orderings
so that all Ri precede all Ga, and all Dha precede all Ni.
Parameters
----------
mela : str or int
the name or numerical index of the melakarta raga
abbr : bool
If `True`, use single-letter svara names: S, R, G, ...
If `False`, use full names: Sa, Ri, Ga, ...
unicode : bool
If `True`, use unicode symbols for numberings, e.g., Ri\u2081
If `False`, use low-order ASCII, e.g., Ri1.
Returns
-------
svara : list of strings
The svara names for each of the 12 pitch classes.
See Also
--------
key_to_notes
mela_to_degrees
list_mela
Examples
--------
Melakarta #1 (Kanakangi) uses R1, G1, D1, N1
>>> librosa.mela_to_svara(1)
['S', 'R₁', 'G₁', 'G₂', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'N₁', 'N₂', 'N₃']
#19 (Jhankaradhwani) uses R2 and G2 so the third svara are Ri:
>>> librosa.mela_to_svara(19)
['S', 'R₁', 'R₂', 'G₂', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'N₁', 'N₂', 'N₃']
#31 (Yagapriya) uses R3 and G3, so third and fourth svara are Ri:
>>> librosa.mela_to_svara(31)
['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'N₁', 'N₂', 'N₃']
#34 (Vagadheeswari) uses D2 and N2, so Ni1 becomes Dha2:
>>> librosa.mela_to_svara(34)
['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'D₂', 'N₂', 'N₃']
#36 (Chalanatta) uses D3 and N3, so Ni2 becomes Dha3:
>>> librosa.mela_to_svara(36)
['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'D₂', 'D₃', 'N₃']
# You can also query by raga name instead of index:
>>> librosa.mela_to_svara('chalanatta')
['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'D₂', 'D₃', 'N₃']
"""
# The following will be constant for all ragas
svara_map = [
"Sa",
"Ri\u2081",
"", # Ri2/Ga1
"", # Ri3/Ga2
"Ga\u2083",
"Ma\u2081",
"Ma\u2082",
"Pa",
"Dha\u2081",
"", # Dha2/Ni1
"", # Dha3/Ni2
"Ni\u2083",
]
if isinstance(mela, str):
mela_idx = MELAKARTA_MAP[mela.lower()] - 1
elif 0 < mela <= 72:
mela_idx = mela - 1
else:
raise ParameterError(f"mela={mela} must be in range [1, 72]")
# Determine Ri2/Ga1
lower = mela_idx % 36
if lower < 6:
# First six will have Ri1/Ga1
svara_map[2] = "Ga\u2081"
else:
# All others have either Ga2/Ga3
# So we'll call this Ri2
svara_map[2] = "Ri\u2082"
# Determine Ri3/Ga2
if lower < 30:
# First thirty should get Ga2
svara_map[3] = "Ga\u2082"
else:
# Only the last six have Ri3
svara_map[3] = "Ri\u2083"
upper = mela_idx % 6
# Determine Dha2/Ni1
if upper == 0:
# these are the only ones with Ni1
svara_map[9] = "Ni\u2081"
else:
# Everyone else has Dha2
svara_map[9] = "Dha\u2082"
# Determine Dha3/Ni2
if upper == 5:
# This one has Dha3
svara_map[10] = "Dha\u2083"
else:
# Everyone else has Ni2
svara_map[10] = "Ni\u2082"
if abbr:
t_abbr = str.maketrans({"a": "", "h": "", "i": ""})
svara_map = [s.translate(t_abbr) for s in svara_map]
if not unicode:
t_uni = str.maketrans({"\u2081": "1", "\u2082": "2", "\u2083": "3"})
svara_map = [s.translate(t_uni) for s in svara_map]
return list(svara_map)
def list_mela() -> Dict[str, int]:
"""List melakarta ragas by name and index.
Melakarta raga names are transcribed from [#]_, with the exception of #45
(subhapanthuvarali).
.. [#] Bhagyalekshmy, S. (1990).
Ragas in Carnatic music.
South Asia Books.
Returns
-------
mela_map : dict
A dictionary mapping melakarta raga names to indices (1, 2, ..., 72)
Examples
--------
>>> librosa.list_mela()
{'kanakangi': 1,
'ratnangi': 2,
'ganamurthi': 3,
'vanaspathi': 4,
...}
See Also
--------
mela_to_degrees
mela_to_svara
list_thaat
"""
return MELAKARTA_MAP.copy()
def list_thaat() -> List[str]:
"""List supported thaats by name.
Returns
-------
thaats : list
A list of supported thaats
Examples
--------
>>> librosa.list_thaat()
['bilaval',
'khamaj',
'kafi',
'asavari',
'bhairavi',
'kalyan',
'marva',
'poorvi',
'todi',
'bhairav']
See Also
--------
list_mela
thaat_to_degrees
"""
return list(THAAT_MAP.keys())
@overload
def __note_to_degree(key: str) -> int:
...
@overload
def __note_to_degree(key: _IterableLike[str]) -> np.ndarray:
...
@overload
def __note_to_degree(key: Union[str, _IterableLike[str], Iterable[str]]) -> Union[int, np.ndarray]:
...
def __note_to_degree(key: Union[str, _IterableLike[str], Iterable[str]]) -> Union[int,np.ndarray]:
"""Take a note name and return the degree of that note (e.g. 'C#' -> 1). We allow possibilities like "C#b".
>>> librosa.__note_to_degree('B#')
0
>>> librosa.__note_to_degree('D♮##b')
3
>>> librosa.__note_to_degree(['B#','D♮##b'])
array([0,3])
"""
if not isinstance(key, str):
return np.array([__note_to_degree(n) for n in key])
match = NOTE_RE.match(key)
if not match:
raise ParameterError(f"Improper key format: {key:s}")
letter = match.group('note').upper()
accidental = match.group('accidental')
pitch_map = {"C": 0, "D": 2, "E": 4, "F": 5, "G": 7, "A": 9, "B": 11}
counter = Counter(accidental)
return (pitch_map[letter]+sum([ACC_MAP[acc] * counter[acc] for acc in ACC_MAP]))%12
@overload
def __simplify_note(key: str, additional_acc: str =..., unicode: bool= ...) -> str:
...
@overload
def __simplify_note(key: _IterableLike[str], additional_acc: str=..., unicode: bool = ... ) -> np.ndarray:
...
@overload
def __simplify_note(key: Union[str, _IterableLike[str], Iterable[str]], additional_acc: str =..., unicode: bool = ...) -> Union[str, np.ndarray]:
...
def __simplify_note(key: Union[str, _IterableLike[str], Iterable[str]], additional_acc: str='', unicode: bool = True) -> Union[str, np.ndarray]:
"""Take in a note name and simplify by canceling sharp-flat pairs, and doubling accidentals as appropriate.
>>> librosa.__simplify_note('C♭♯')
'C'
>>> librosa.__simplify_note('C♭♭♭')
'C♭𝄫'
>>> librosa.__simplify_note(['C♭♯', 'C♭♭♭'])
array(['C', 'C♭𝄫'], dtype='<U3')
"""
if not isinstance(key,str):
return np.array([__simplify_note(n+additional_acc, unicode=unicode) for n in key])
match = NOTE_RE.match(key+additional_acc)
if not match:
raise ParameterError(f"Improper key format: {key:s}")
letter = match.group('note').upper()
accidental = match.group('accidental')
counter = Counter(accidental)
offset = sum([ACC_MAP[acc] * counter[acc] for acc in ACC_MAP])
simplified_note = letter
if offset>=0:
simplified_note += "♯"*(offset%2)+ "𝄪"*(offset//2)
else:
simplified_note += "♭"*(offset%2)+ "𝄫"*(abs(offset)//2)
if not unicode:
translations = str.maketrans({"♯": "#", "𝄪": "##", "♭": "b", "𝄫": "bb", "♮": "n"})
simplified_note = simplified_note.translate(translations)
return simplified_note
def __mode_to_key(signature: str, unicode: bool = True) -> str:
"""Translate a mode (eg D:dorian) into its equivalent major key. If unicode==True, return the accidentals as unicode symbols, regardless of nature of accidentals in signature. Otherwise, return accidentals as ASCII symbols.
>>> librosa.__mode_to_key('Db:loc')
'E𝄫:maj'
>>> librosa.__mode_to_key('D♭:loc', unicode = False)
'Ebb:maj'
"""
match = KEY_RE.match(signature)
if not match:
raise ParameterError("Improper format: {:s}".format(signature))
if match.group('scale') or not match.group("mode"):
# We're already fine here, but let's pass the key through __simpify_note() to ensure good formatting.
signature = __simplify_note(match.group("tonic").upper()+match.group('accidental'), unicode=unicode)+(':'+match.group("scale") if match.group("scale") else '')
return signature
# We have a mode, time to translate
mode = match.group("mode").lower()[:3]
# Get the relative major
tonic = MAJOR_DICT[mode][match.group("tonic").upper()]
return __simplify_note(tonic+match.group("accidental"), unicode = unicode)+":maj"
@cache(level=10)
def key_to_notes(key: str, *, unicode: bool = True, natural: bool= False) -> List[str]:
"""List all 12 note names in the chromatic scale, as spelled according to
a given key (major or minor) or mode (see below for details and accepted abbreviations).
This function exists to resolve enharmonic equivalences between different
spellings for the same pitch (e.g. C♯ vs D♭), and is primarily useful when producing
human-readable outputs (e.g. plotting) for pitch content.
Note names are decided by the following rules:
1. If the tonic of the key has an accidental (sharp or flat), that accidental will be
used consistently for all notes.
2. If the tonic does not have an accidental, accidentals will be inferred to minimize
the total number used for diatonic scale degrees.
3. If there is a tie (e.g., in the case of C:maj vs A:min), sharps will be preferred.
Parameters
----------
key : string
Must be in the form TONIC:key. Tonic must be upper case (``CDEFGAB``),
key must be lower-case
(``major``, ``minor``, ``ionian``, ``dorian``, ``phrygian``, ``lydian``, ``mixolydian``, ``aeolian``, ``locrian``).
The following abbreviations are supported for the modes: either the first three letters of the mode name
(e.g. "mix") or the mode name without "ian" (e.g. "mixolyd").
Both ``major`` and ``maj`` are supported as mode abbreviations.
Single and multiple accidentals (``b!♭`` for flat, ``#♯`` for sharp, ``𝄪𝄫`` for double-accidentals, or any combination thereof) are supported.
Examples: ``C:maj, C:major, Dbb:min, A♭:min, D:aeo, E𝄪:phryg``.
unicode : bool
If ``True`` (default), use Unicode symbols (♯𝄪♭𝄫)for accidentals.
If ``False``, Unicode symbols will be mapped to low-order ASCII representations::
♯ -> #, 𝄪 -> ##, ♭ -> b, 𝄫 -> bb, ♮ -> n
natural : bool
If ``True'', mark natural accidentals with a natural symbol (♮).
If ``False`` (default), do not print natural symbols.
For example, `note_to_degrees('D:maj')[0]` is `C` if `natural=False` (default) and `C♮` if `natural=True`.
Returns
-------
notes : list
``notes[k]`` is the name for semitone ``k`` (starting from C)
under the given key. All chromatic notes (0 through 11) are
included.
See Also
--------
midi_to_note
Examples
--------
`C:maj` will use all sharps
>>> librosa.key_to_notes('C:maj')
['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
`A:min` has the same notes
>>> librosa.key_to_notes('A:min')
['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
`A♯:min` will use sharps, but spell note 0 (`C`) as `B♯`
>>> librosa.key_to_notes('A#:min')
['B♯', 'C♯', 'D', 'D♯', 'E', 'E♯', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
`G♯:maj` will use a double-sharp to spell note 7 (`G`) as `F𝄪`:
>>> librosa.key_to_notes('G#:maj')
['B♯', 'C♯', 'D', 'D♯', 'E', 'E♯', 'F♯', 'F𝄪', 'G♯', 'A', 'A♯', 'B']
`F♭:min` will use double-flats
>>> librosa.key_to_notes('Fb:min')
['D𝄫', 'D♭', 'E𝄫', 'E♭', 'F♭', 'F', 'G♭', 'A𝄫', 'A♭', 'B𝄫', 'B♭', 'C♭']
`G:loc` uses flats
>>> librosa.key_to_notes('G:loc')
['C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭', 'A', 'B♭', 'B']
If `natural=True`, print natural accidentals.
>>> librosa.key_to_notes('G:loc', natural=True)
['C', 'D♭', 'D♮', 'E♭', 'E♮', 'F', 'G♭', 'G', 'A♭', 'A♮', 'B♭', 'B♮']
>>> librosa.key_to_notes('D:maj', natural=True)
['C♮', 'C♯', 'D', 'D♯', 'E', 'F♮', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
>>> librosa.key_to_notes('G#:maj', unicode = False, natural = True)
['B#', 'C#', 'Dn', 'D#', 'En', 'E#', 'F#', 'F##', 'G#', 'An', 'A#', 'B']
We can combine this with ``key_to_degrees`` to get the notes for a given scale:
>>> notes = librosa.key_to_notes('D:maj')
>>> degrees = librosa.key_to_degrees('D:maj')
>>> print([notes[d] for d in degrees])
['D', 'E', 'F♯', 'G', 'A', 'B', 'C♯']
"""
# Parse the key signature
match = KEY_RE.match(key)
if not match:
raise ParameterError(f"Improper key format: {key:s}")
pitch_map = {"C": 0, "D": 2, "E": 4, "F": 5, "G": 7, "A": 9, "B": 11}
tonic = match.group("tonic").upper()
accidental = match.group("accidental")
offset = sum([ACC_MAP[acc] for acc in accidental])
if match.group('mode') or not match.group('scale'):
equiv = __mode_to_key(key)
return key_to_notes(equiv, unicode=unicode, natural = natural)
scale = match.group("scale")[:3].lower()
multiple = abs(offset)>=2
#If multiple accidentals, we use recursion, then cycle through so that the enharmonic equivalent of C is at the beginning again.
if multiple:
sign_map = {+1: "♯", -1: "♭"}
additional_acc = sign_map[np.sign(offset)]
intermediate_notes = key_to_notes(tonic+additional_acc*(abs(offset)-1)+':'+scale, natural = False)
notes = [__simplify_note(note, additional_acc) for note in intermediate_notes]
degrees = __note_to_degree(notes)
notes = np.roll(notes, shift=-np.argwhere(degrees == 0)[0])
notes = list(notes)
if not unicode:
translations = str.maketrans({"♯": "#", "𝄪": "##", "♭": "b", "𝄫": "bb", "♮": "n"})
notes = list(n.translate(translations) for n in notes)
return notes
# Determine major or minor
major = scale == "maj"
# calculate how many clockwise steps we are on CoF (== # sharps)
if major:
tonic_number = ((pitch_map[tonic] + offset) * 7) % 12
else:
tonic_number = ((pitch_map[tonic] + offset) * 7 + 9) % 12
# Decide if using flats or sharps
# Logic here is as follows:
# 1. respect the given notation for the tonic.
# Sharp tonics will always use sharps, likewise flats.
# 2. If no accidental in the tonic, try to minimize accidentals.
# 3. If there's a tie for accidentals, use sharp for major and flat for minor.
if offset < 0:
# use flats explicitly
use_sharps = False
elif offset > 0:
# use sharps explicitly
use_sharps = True
elif 0 <= tonic_number < 6:
use_sharps = True
elif tonic_number > 6:
use_sharps = False
# Basic note sequences for simple keys
notes_sharp = ["C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯", "A", "A♯", "B"]
notes_flat = ["C", "D♭", "D", "E♭", "E", "F", "G♭", "G", "A♭", "A", "B♭", "B"]
# These apply when we have >= 6 sharps
sharp_corrections = [
(5, "E♯"),
(0, "B♯"),
(7, "F𝄪"),
(2, "C𝄪"),
(9, "G𝄪"),
(4, "D𝄪"),
(11, "A𝄪"),
]
# These apply when we have >= 6 flats
flat_corrections = [
(11, "C♭"),
(4, "F♭"),
(9, "B𝄫"),
(2, "E𝄫"),
(7, "A𝄫"),
(0, "D𝄫"),
] # last would be (5, 'G𝄫')
# Apply a mod-12 correction to distinguish B#:maj from C:maj
n_sharps = tonic_number
if tonic_number == 0 and tonic == "B":
n_sharps = 12
if use_sharps:
# This will only execute if n_sharps >= 6
for n in range(0, n_sharps - 6 + 1):
index, name = sharp_corrections[n]
notes_sharp[index] = name
notes = notes_sharp
else:
n_flats = (12 - tonic_number) % 12
# This will only execute if tonic_number <= 6
for n in range(0, n_flats - 6 + 1):
index, name = flat_corrections[n]
notes_flat[index] = name
notes = notes_flat
# Apply natural signs to any note which has no other accidentals and does not appear in the scale for key.
if natural:
scale_notes = set(key_to_degrees(key))
for place, note in enumerate(notes):
if __note_to_degree(note) in scale_notes:
continue
if len(note)==1:
notes[place] = note+'♮'
# Finally, apply any unicode down-translation if necessary
if not unicode:
translations = str.maketrans({"♯": "#", "𝄪": "##", "♭": "b", "𝄫": "bb", "♮": "n"})
notes = list(n.translate(translations) for n in notes)
return notes
# I made this work even for key signatures like 'C#b#:min'
def key_to_degrees(key: str) -> np.ndarray:
"""Construct the diatonic scale degrees for a given key.
Parameters
----------
key : str
Must be in the form TONIC:key. Tonic must be upper case (``CDEFGAB``),
key must be lower-case
(``maj``, ``min``, ``ionian``, ``dorian``, ``phrygian``, ``lydian``, ``mixolydian``, ``aeolian``, ``locrian``).
The following abbreviations are supported for the modes: either the first three letters of the mode name
(e.g. "mix") or the mode name without "ian" (e.g. "mixolyd").
Both ``major`` and ``maj`` are supported as abbreviations.
Single and multiple accidentals (``b!♭`` for flat, or ``#♯`` for sharp) are supported.
Examples: ``C:maj, C:major, Dbb:min, A♭:min, D:aeo, E𝄪:phryg``.
Returns
-------
degrees : np.ndarray
An array containing the semitone numbers (0=C, 1=C#, ... 11=B)
for each of the seven scale degrees in the given key, starting
from the tonic.
See Also
--------
key_to_notes
Examples
--------
>>> librosa.key_to_degrees('C:maj')
array([ 0, 2, 4, 5, 7, 9, 11])
>>> librosa.key_to_degrees('C#:maj')
array([ 1, 3, 5, 6, 8, 10, 0])
>>> librosa.key_to_degrees('A:min')
array([ 9, 11, 0, 2, 4, 5, 7])
>>> librosa.key_to_degrees('A:min')
array([ 9, 11, 0, 2, 4, 5, 7])
"""
notes = dict(
maj=np.array([0, 2, 4, 5, 7, 9, 11]), min=np.array([0, 2, 3, 5, 7, 8, 10])
)
match = KEY_RE.match(key)
if not match:
raise ParameterError(f"Improper key format: {key:s}")
if match.group('mode') or not match.group('scale'):
equiv = __mode_to_key(key)
offset = OFFSET_DICT[match.group('mode')[:3]]
return np.roll(key_to_degrees(equiv),-offset)
pitch_map = {"C": 0, "D": 2, "E": 4, "F": 5, "G": 7, "A": 9, "B": 11}
tonic = match.group("tonic").upper()
accidental = match.group("accidental")
counts = Counter(accidental)
offset = sum([ACC_MAP[acc]*counts[acc] for acc in ACC_MAP])
scale = match.group("scale")[:3].lower()
return (notes[scale] + pitch_map[tonic] + offset) % 12
@cache(level=10)
def fifths_to_note(*, unison: str, fifths: int, unicode: bool = True) -> str:
"""Calculate the note name for a given number of perfect fifths
from a specified unison.
This function is primarily intended as a utility routine for
Functional Just System (FJS) notation conversions.
This function does not assume the "circle of fifths" or equal temperament,
so 12 fifths will not generally produce a note of the same pitch class
due to the accumulation of accidentals.
Parameters
----------
unison : str
The name of the starting (unison) note, e.g., 'C' or 'Bb'.
Unicode accidentals are supported.
fifths : integer
The number of perfect fifths to deviate from unison.
unicode : bool
If ``True`` (default), use Unicode symbols (♯𝄪♭𝄫)for accidentals.
If ``False``, accidentals will be encoded as low-order ASCII representations::
♯ -> #, 𝄪 -> ##, ♭ -> b, 𝄫 -> bb
Returns
-------
note : str
The name of the requested note
Examples
--------
>>> librosa.fifths_to_note(unison='C', fifths=6)
'F♯'
>>> librosa.fifths_to_note(unison='G', fifths=-3)
'B♭'
>>> librosa.fifths_to_note(unison='Eb', fifths=11, unicode=False)
'G#'
"""
# Starting the circle of fifths at F makes accidentals easier to count
COFMAP = "FCGDAEB"
acc_map = {
"#": 1,
"": 0,
"b": -1,
"!": -1,
"♯": 1,
"𝄪": 2,
"♭": -1,
"𝄫": -2,
"♮": 0,
"n": 0
}
if unicode:
acc_map_inv = {1: "♯", 2: "𝄪", -1: "♭", -2: "𝄫", 0: ""}
else:
acc_map_inv = {1: "#", 2: "##", -1: "b", -2: "bb", 0: ""}
match = NOTE_RE.match(unison)
if not match:
raise ParameterError(f"Improper note format: {unison:s}")
# Find unison in the alphabet
pitch = match.group("note").upper()
# Find the number of accidentals to start from
offset = np.sum([acc_map[o] for o in match.group("accidental")])
# Find the raw target note
circle_idx = COFMAP.index(pitch)
raw_output = COFMAP[(circle_idx + fifths) % 7]