Advertisement
diliupg

give scales chord names

Oct 22nd, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.35 KB | None | 0 0
  1. ## Find notes in all Major, Harmonic Minor and Melodic Minor scales
  2. ## return answer to querying program (find_scales_chords_TkGUI.py)
  3. ## 11/12/14
  4.  
  5. import scale_arp_chords_data as data
  6. import os
  7. import sys
  8.  
  9. #----------------------------------------------------------------------
  10. def get_major_scale(find):
  11.     """find the major scale, either # or b."""
  12.  
  13.     if find in ["C", "G", "D", "A", "E", "B", "F#", "C#"]:
  14.         """major scales - sharps"""
  15.  
  16.         sharporflat = "#"
  17.  
  18.     elif find in ["F", "Bb", "Eb", "Ab", "Db", "Gb", "Cb"]:
  19.         """major scales - flats"""
  20.  
  21.         sharporflat = "b"
  22.  
  23.     scalenotes = major_scales(find, sharporflat)
  24.  
  25.     return scalenotes
  26.  
  27. #----------------------------------------------------------------------
  28. def major_scales(find, sharporflat):
  29.     """build the major scale"""
  30.  
  31.     scaletemplate = ["C", "D", "E", "F", "G", "A", "B"]
  32.  
  33.     scale = scaletemplate
  34.  
  35.     while scale[0] != find:
  36.  
  37.         if sharporflat == "#":
  38.             scale[3] = scale[3] + "#"
  39.             newscale = scale[4:] + scale[:4]
  40.         elif sharporflat == "b":
  41.             scale[-1] = scale[-1] + "b"
  42.             newscale = scale[3:] + scale[:3]
  43.  
  44.         scale = newscale
  45.     scale.append(scale[0])
  46.  
  47.     return scale
  48.  
  49. #----------------------------------------------------------------------
  50. def pure_minor(find, scaletype):
  51.  
  52.     """return the pure minor scale"""
  53.  
  54.     scaletemplate = ["C", "D", "E", "F", "G", "A", "B"]
  55.  
  56.     x = scaletemplate.index(find[0])
  57.  
  58.     transitscale = scaletemplate[x:] + scaletemplate[:x]
  59.     relmajscale = transitscale[2]
  60.  
  61.     if scaletype == "minorsharps":
  62.  
  63.         sharporflat = "#"
  64.  
  65.     elif scaletype == "minorflats":
  66.  
  67.         if relmajscale in "BEADGC":  ## add a flat to the name
  68.             relmajscale = relmajscale + "b"
  69.             sharporflat = "b"
  70.  
  71.     newscale = major_scales(relmajscale, sharporflat)
  72.  
  73.     pureminorscale = newscale[5:-1] + newscale[:5] + newscale[5:6]
  74.  
  75.     return pureminorscale
  76.  
  77. #----------------------------------------------------------------------
  78. def harmonic_minor(scale):
  79.     """raise the 7th"""
  80.     minorscale = scale
  81.  
  82.     if minorscale[6][-1] == "#":
  83.         raiseit = minorscale[6][:-1] + "X"
  84.     elif minorscale[6][-1] == "b":
  85.         raiseit = minorscale[6][:-1]
  86.     else:
  87.         raiseit = minorscale[6] + "#"
  88.  
  89.     minorscale[6] = raiseit
  90.  
  91.     return minorscale
  92.  
  93. #----------------------------------------------------------------------
  94. def melodic_minor(scale):
  95.     """Ascending Raise the sixth and seventh
  96.    Descending lower the sixth and seventh"""
  97.     minorscale = scale
  98.     descendingpureminor = scale[:]
  99.     descendingpureminor.reverse()
  100.  
  101.     if minorscale[5][-1] == "#":
  102.         raiseit6 = minorscale[5][:-1] + "X"
  103.     elif minorscale[5][-1] == "b":
  104.         raiseit6 = minorscale[5][:-1]
  105.     else:
  106.         raiseit6 = minorscale[5] + "#"
  107.  
  108.     minorscale[5] = raiseit6  # raise the 6th
  109.  
  110.     minorscale = harmonic_minor(minorscale)  # raise the 7th. use harmonic minor function
  111.  
  112.     minorscale.extend(descendingpureminor[1:])  # add the ascending scale less the first note
  113.  
  114.     return minorscale
  115.  
  116. #----------------------------------------------------------------------
  117. def list_to_string(givenlist):
  118.     """take a list and return a string"""
  119.  
  120.     stringfromlist = ' '.join(givenlist)
  121.  
  122.     return stringfromlist
  123.  
  124. #----------------------------------------------------------------------
  125. def get_scale(find):
  126.     """"""
  127.  
  128.     if "m" not in find:  # NOT a MINOR scale. IS a MAJOR scale
  129.         """major scales - sharps"""
  130.  
  131.         scalenotes = get_major_scale(find)
  132.         scalename = find + " Major: "
  133.  
  134.     elif find[:-2] in ["A", "E", "B", "F#", "C#", "G#", "D#", "A#"]:
  135.         scaletype = "minorsharps"
  136.         pureminorscale = pure_minor(find, scaletype)
  137.  
  138.         if "hm" in find:  ## harmonic minor
  139.             scalenotes = harmonic_minor(pureminorscale)
  140.             scalename = find[:-2] + " Harmonic Minor: "
  141.         else:  ## melodic minor
  142.             scalenotes = melodic_minor(pureminorscale)
  143.             scalename = find[:-2] + " Melodic Minor: "
  144.  
  145.  
  146.     elif find[:-2] in ["D", "G", "C", "F", "Bb", "Eb", "Ab"]:
  147.         scaletype = "minorflats"
  148.         pureminorscale = pure_minor(find, scaletype)
  149.  
  150.         if "hm" in find: ## harmonic minor
  151.             scalenotes = harmonic_minor(pureminorscale)
  152.             scalename = find[:-2] + " Harmonic Minor: "
  153.         else:  ## melodic minor
  154.             scalenotes = melodic_minor(pureminorscale)
  155.             scalename = find[:-2] + " Melodic Minor: "
  156.  
  157.     return scalename, scalenotes
  158.  
  159. #----------------------------------------------------------------------
  160. def raiseaccidental(note2check):
  161.     """"""
  162.     if len(note2check) == 1:
  163.         newnote = note2check + "#"
  164.  
  165.     elif len(note2check) == 2:
  166.         if note2check[1] == "#":
  167.             newnote = note2check[0] + "X"
  168.         elif note2check[1] == "b":
  169.             newnote = note2check[0]
  170.  
  171.     return newnote
  172.  
  173. #----------------------------------------------------------------------
  174. def loweraccidental(note2check):
  175.     """"""
  176.     if len(note2check) == 1:
  177.         newnote = note2check + "b"
  178.  
  179.     elif len(note2check) == 2:
  180.         if note2check[1] == "#":
  181.             newnote = note2check[0]
  182.         elif note2check[1] == "b":
  183.             newnote = note2check + "b"
  184.  
  185.     return newnote
  186.  
  187. #----------------------------------------------------------------------
  188. def get_chord(scale, chordname):
  189.     """Chord mode"""
  190.     chordnotes = []
  191.     scalenotes = get_major_scale (scale)
  192.  
  193.     if chordname == "":
  194.         chordnotes.extend([scalenotes[0], scalenotes[2], scalenotes[4]])
  195.         chordname = scale + " major: "
  196.  
  197.     elif chordname == "m":
  198.         newnote = loweraccidental(scalenotes[2])
  199.         chordnotes.extend ([scalenotes [0], newnote, scalenotes [4]])
  200.         chordname = scale + " minor: "
  201.  
  202.     elif chordname == "sus2":
  203.         chordnotes.extend ([scalenotes [0], scalenotes [1], scalenotes [4]])
  204.         chordname = scale + " sus2: "
  205.  
  206.     elif chordname == "sus4":
  207.         chordnotes.extend ([scalenotes [0], scalenotes [3], scalenotes [4]])
  208.         chordname = scale + " sus4: "
  209.  
  210.     elif chordname == "6":
  211.         chordnotes.extend ([scalenotes [0], scalenotes [2], scalenotes [4], scalenotes[5]])
  212.         chordname = scale + " 6th: "
  213.  
  214.     elif chordname == "m6":
  215.         newnote = loweraccidental(scalenotes[2])
  216.         chordnotes.extend ([scalenotes [0], newnote, scalenotes [4], scalenotes[5]])
  217.         chordname = scale + " minor 6th: "
  218.  
  219.     elif chordname == "7":
  220.         newnote = loweraccidental(scalenotes[6])
  221.         chordnotes.extend ([scalenotes [0], scalenotes [2], scalenotes [4], newnote])
  222.         chordname = scale + " 7th: "
  223.  
  224.     elif chordname == "M7":
  225.         chordnotes.extend ([scalenotes [0], scalenotes [2], scalenotes [4], scalenotes[6]])
  226.         chordname = scale + " major 7th: "
  227.  
  228.     elif chordname == "m7":
  229.         newnote = loweraccidental(scalenotes[2])
  230.         newnote2 = loweraccidental(scalenotes[6])
  231.         chordnotes.extend ([scalenotes [0], newnote, scalenotes [4], newnote2])
  232.         chordname = scale + " minor 7th: "
  233.  
  234.     elif chordname == "mM7":
  235.         newnote = loweraccidental(scalenotes[2])
  236.         chordnotes.extend ([scalenotes [0], newnote, scalenotes [4], scalenotes[6]])
  237.         chordname = scale + " minor major 7th: "
  238.  
  239.     elif chordname == "+5":
  240.         newnote = raiseaccidental(scalenotes[4])
  241.         chordnotes.extend ([scalenotes [0], scalenotes [2], newnote])
  242.         chordname = scale + " minor 6th: "
  243.  
  244.     elif chordname == "-5":
  245.         newnote = loweraccidental(scalenotes[4])
  246.         chordnotes.extend ([scalenotes [0], scalenotes [2], newnote])
  247.         chordname = scale + " major -5: "
  248.  
  249.     elif chordname == "7+5":
  250.         newnote = raiseaccidental(scalenotes [4])
  251.         newnote2 = loweraccidental(scalenotes[6])
  252.         chordnotes.extend ([scalenotes [0], scalenotes [2], newnote, newnote2])
  253.         chordname = scale + " 7th+5: "
  254.  
  255.     elif chordname == "7-5":
  256.         newnote = loweraccidental(scalenotes [4])
  257.         newnote2 = loweraccidental(scalenotes[6])
  258.         chordnotes.extend ([scalenotes [0], scalenotes [2], newnote, newnote2])
  259.         chordname = scale + " 7th-5: "
  260.  
  261.     elif chordname == "9":
  262.         newnote = loweraccidental(scalenotes[6])
  263.         chordnotes.extend ([scalenotes [0], scalenotes [2], scalenotes [4], newnote, scalenotes [1]])
  264.         chordname = scale + " 9th: "
  265.  
  266.     elif chordname == "m9":
  267.         newnote = loweraccidental(scalenotes[2])
  268.         newnote2 = loweraccidental(scalenotes[6])
  269.         chordnotes.extend ([scalenotes [0], newnote, scalenotes [4], newnote2, scalenotes [1]])
  270.         chordname = scale + " minor9th: "
  271.  
  272.     elif chordname == "7-9":
  273.         newnote = loweraccidental(scalenotes [4])
  274.         newnote2 = loweraccidental(scalenotes[1])
  275.         chordnotes.extend ([scalenotes [0], scalenotes [2], newnote, newnote2])
  276.         chordname = scale + " 7th-9: "
  277.  
  278.     elif chordname == "7+9":
  279.         newnote = loweraccidental(scalenotes [4])
  280.         newnote2 = raiseaccidental(scalenotes[1])
  281.         chordnotes.extend ([scalenotes [0], scalenotes [2], newnote, newnote2])
  282.         chordname = scale + " 7th-9: "
  283.  
  284.     elif chordname == "11":
  285.         newnote = loweraccidental(scalenotes[6])
  286.         chordnotes.extend ([scalenotes [0], scalenotes [2], scalenotes [4], newnote, scalenotes [3]])
  287.         chordname = scale + " 11th: "
  288.  
  289.     elif chordname == "m11":
  290.         newnote = loweraccidental(scalenotes[2])
  291.         newnote2 = loweraccidental(scalenotes[6])
  292.         chordnotes.extend ([scalenotes [0], newnote, scalenotes [4], newnote2, scalenotes [3]])
  293.         chordname = scale + " minor11th: "
  294.  
  295.     elif chordname == "13":
  296.         newnote = loweraccidental(scalenotes[6])
  297.         chordnotes.extend ([scalenotes [0], scalenotes [2], scalenotes [4], newnote, scalenotes [5]])
  298.         chordname = scale + " 13th: "
  299.  
  300.     elif chordname == "m13":
  301.         newnote = loweraccidental(scalenotes[2])
  302.         newnote2 = loweraccidental(scalenotes[6])
  303.         chordnotes.extend ([scalenotes [0], newnote, scalenotes [4], newnote2, scalenotes [5]])
  304.         chordname = scale + " minor13th: "
  305.  
  306.     elif chordname == "dim":
  307.         newnote = loweraccidental(scalenotes[2])
  308.         newnote2 = loweraccidental(scalenotes[4])
  309.         chordnotes.extend ([scalenotes [0], newnote, newnote2])
  310.         chordname = scale + " diminished: "
  311.  
  312.     elif chordname == "m-5":
  313.         newnote = loweraccidental(scalenotes[2])
  314.         newnote2 = loweraccidental(scalenotes[4])
  315.         chordnotes.extend ([scalenotes [0], newnote, newnote2])
  316.         chordname = scale + " diminished: "
  317.  
  318.     elif chordname == "dim7":
  319.         newnote = loweraccidental(scalenotes[2])
  320.         newnote2 = loweraccidental(scalenotes[4])
  321.         chordnotes.extend ([scalenotes [0], newnote, newnote2, scalenotes[5]])
  322.         chordname = scale + " diminished7th: "
  323.  
  324.     else:
  325.         chordnotes.extend("No such Chord.")
  326.  
  327.     return chordname, chordnotes
  328.  
  329. #----------------------------------------------------------------------
  330. def main(find, mode):
  331.  
  332.     if mode == "Scale Mode":
  333.         if len(find) > 1:
  334.             find = find[0].upper() + find[1:]
  335.         else:
  336.             find = find.upper()
  337.  
  338.         if find in data.scalenames:
  339.  
  340.             scale_n_notes = get_scale(find)
  341.             stringfy = list_to_string (scale_n_notes[1])
  342.             totalanswer = scale_n_notes[0] + stringfy
  343.  
  344.         else:
  345.             totalanswer = "No such scale"
  346.  
  347.         return totalanswer
  348.  
  349.     elif mode == "Chord Mode":
  350.  
  351.         ## make the query variables
  352.         find = find[0].upper() + find[1:]  # capitalize the 1st. character
  353.  
  354.         if len(find) > 1 and "#" in find[1] or len(find) > 1 and "b" in find[1]:
  355.  
  356.             scale = find[:2]
  357.             chordname = find[2:]
  358.         else:
  359.             scale = find[0]
  360.             chordname = find[1:]
  361.  
  362.         ## if chord is valid get it!
  363.         if chordname in data.chordnames and scale in data.valid:
  364.  
  365.             chord_notes = get_chord(scale, chordname)
  366.             stringfy = list_to_string (chord_notes[1])
  367.             totalanswer = chord_notes[0] + stringfy
  368.  
  369.         else:
  370.             totalanswer = "No such chord"
  371.  
  372.         return totalanswer
  373.  
  374.  
  375. if __name__ == '__main__':
  376.     getscale = "A"
  377.     mode = "Scale Mode"
  378.     scale = main(getscale, mode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement