rhowaldt

gchords.py

Jun 7th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.62 KB | None | 0 0
  1. #!/usr/bin/python
  2. import curses
  3. import sys
  4. sys.stdout.write("\x1b]2;G(uitar) Chords!\x07") # set the terminal window title
  5.  
  6. myscreen = curses.initscr()
  7. #myscreen.box()
  8. myscreen.keypad(1)
  9. myscreen.refresh()
  10.  
  11. maxheight,maxwidth = myscreen.getmaxyx()
  12. #myscreen.addstr(1,1,str(maxwidth)) # debug
  13. #myscreen.addstr(2,1,str(maxheight)) # debug
  14.  
  15. curses.start_color()
  16. curses.use_default_colors()
  17. curses.noecho()
  18. curses.cbreak()
  19. curses.curs_set(0)
  20. curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
  21. curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
  22.  
  23. # DEFINE DICTIONARIES
  24. note_type_1 = { # guitar_note_type = 1
  25. 1: 'C', 2: 'C#', 3: 'D', 4: 'D#', 5: 'E', 6: 'F', 7: 'F#', 8: 'G', 9: 'G#',
  26. 10: 'A', 11: 'A#', 12: 'B'}
  27.  
  28. note_type_2 = { # guitar_note_type = 2
  29. 1: 'C', 2: 'Db', 3: 'D', 4: 'Eb', 5: 'E', 6: 'F', 7: 'Gb', 8: 'G', 9: 'Ab',
  30. 10: 'A', 11: 'Bb', 12: 'B'}
  31.  
  32. pckeys = {
  33. 1: 'z', 2: 's', 3: 'x', 4: 'd', 5: 'c', 6: 'v', 7: 'g', 8: 'b', 9: 'h',
  34. 10: 'n', 11: 'j', 12: 'm'}
  35.  
  36. subnotes = { # display this, this is purely for the display of the chord-name
  37. 1: 'M(ajor)', 2: 'm(inor)', 3: 'M6', 4: 'm6', 5: '7',
  38. 6: 'M7', 7: 'm7', 8: 'mM7', 9: '9', 10: 'M9', 11: 'm9',
  39. 12: 'mM9', 13: 'M11', 14: 'm11', 15: 'mM11',
  40. 16: 'M13', 17: 'm13', 18: 'mM13',
  41. 19: 'M-add9', 20: 'm-add9', 21: 'M6-add9', 22: 'm6-add9',
  42. 23: 'dom7', 24: 'dom9', 25: 'dom11', 26: 'dom13',
  43. 27: 'sus2', 28: 'sus4', 29: '6-sus4', 30: '7-sus4', 31: 'm7-sus4',
  44. 32: '9-sus4', 33: 'm9-sus4', 34: '7b5', 35: 'm7b5', 36: '7#5', 37: 'm7#5',
  45. 38: '7b9', 39: 'm7b9', 40: '7#9', 41: '7#5b9', 42: '9#11', 43: '9b13',
  46. 44: 'dim', 45: 'dim7', 46: 'add2', 47: 'add4', 48: '7-add4',
  47. 49: '5', 50: 'aug'}
  48.  
  49. chord_formulas = { # use these, these are for calculating the chords
  50. 1: [1,3,5], 2: [1,'b3',5], 3: [1,3,5,6], 4: [1,'b3',5,6], 5: [1,3,5,'b7'],
  51. 6: [1,3,5,7], 7: [1,'b3',5,'b7'], 8: [1,'b3',5,7], 9: [1,3,5,'b7',9], 10: [1,3,5,7,9], 11: [1,'b3',5,'b7',9],
  52. 12: [1,'b3',5,7,9], 13: [1,3,5,7,9,11], 14: [1,'b3',5,'b7',9,11], 15: [1,'b3',5,7,9,11],
  53. 16: [1,5,7,9,11,13], 17: [1,'b3',5,'b7',9,11,13], 18: [1,'b3',5,7,9,11,13],
  54. 19: [1,3,5,9], 20: [1,'b3',5,9], 21: [1,3,5,6,9], 22: [1,'b3',5,6,9],
  55. 23: [1,3,5,'b7'], 24: [1,3,5,'b7',9], 25: [1,3,5,'b7',9,11], 26: [1,3,5,'b7',9,11,13],
  56. 27: [1,2,5], 28: [1,4,5], 29: [1,4,5,6], 30: [1,4,5,'b7'], 31: [1,4,5,7],
  57. 32: [1,4,5,'b7',9], 33: [1,4,5,7,9], 34: [1,3,'b5',7], 35: [1,'b3','b5','b7'],
  58. 36: [1,3,'#5','b7'], 37: [1,'b3','#5','b7'], 38: [1,3,5,'b7','b9'],
  59. 39: [1,'b3',5,'b7','b9'], 40: [1,3,5,'b7','#9'], 41: [1,3,'#5','b7','b9'],
  60. 42: [1,3,5,'b7',9,'#11'], 43: [1,3,5,'b7',9,'b13'], 44: [1,'b3','b5'],
  61. 45: [1,'b3','b5',6], 46: [1,2,3,5], 47: [1,3,4,5], 48: [1,3,4,5,'b7'],
  62. 49: [1,5], 50: [1,3,'#5']}
  63.  
  64.  
  65.  
  66. # SET DEFAULTS
  67. #chord = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0} # default to no notes displayed
  68. current_window = 1 # default to notes_section
  69. ns_cursor_vpos = 1 # notes_section cursor vertical position
  70. ns_cursor_hpos = 1 # notes_section cursor horizontal position
  71. ss_cursor_vpos = 1 # subnotes_section cursor vertical position
  72. ss_cursor_hpos = 1 # subnotes_section cursor horizontal position
  73. guitar_show_what = 0 # default to showing no notes
  74. guitarnotes = note_type_1 # default to note-type A#
  75. global_guitar_notetype = 1
  76. current_note = 1 # default to first note
  77. current_subnote = 1 # default to first subnote
  78. scale = 1 # default to major scale
  79. global_tuning = {1: 'E', 2: 'B', 3: 'G', 4: 'D', 5: 'A', 6: 'E'}
  80.  
  81. # THE GUITAR
  82. guitar_width = 64
  83. guitar_hpos = (maxwidth - guitar_width) / 2 - 4
  84. guitar = curses.newwin(8,guitar_width,0,guitar_hpos)
  85. guitar.box()
  86. guitar.refresh()
  87.  
  88. def guitar_draw_body():
  89.    vpos = 1
  90.    for x in global_tuning:
  91.       guitar.addch(vpos,0,str(global_tuning[x]),curses.A_NORMAL)
  92.       vpos = vpos + 1
  93.    guitar.vline(1,2,curses.ACS_VLINE,6) # nut
  94.    guitar.vline(1,3,curses.ACS_VLINE,6) # nut
  95.  
  96.    fret_hpos = 7
  97.    for fret in range(1,15):
  98.       guitar.vline(1,fret_hpos,curses.ACS_VLINE,6)
  99.       fret_hpos = fret_hpos + 4
  100.  
  101. def guitar_draw_strings(): # takes a dict of 6 fretboard-positions
  102.    string_vpos = 1
  103.    for string in range(1,7):
  104.       guitar.hline(string_vpos,4,'-',(guitar_width - 5))
  105.       string_vpos = string_vpos + 1
  106.  
  107. # BUILD THE NOTES ON THE NECK
  108. def build_necknotes(tuning):
  109.    # build arrays of form [stringnumber,fretnumber,notename] inside a dict.
  110.    note_count = 0
  111.    string_count = 0
  112.    count = 1
  113.    neck_notes_list = {}
  114.    for x in tuning:
  115.       fret_count = 1
  116.       string_count = string_count + 1
  117.       for y in guitarnotes:
  118.          if guitarnotes[y] == tuning[x]:
  119.             note_count = y
  120.             neck_notes_list[count] = [string_count,0,tuning[x]] # fret 0 holds the root-notes
  121.             count = count + 1
  122.             break
  123.       for q in range(1,16):
  124.          if note_count < 12:
  125.             note_count = note_count + 1
  126.          else:
  127.             note_count = 1
  128.          neck_notes_list[count] = [string_count,fret_count,str(guitarnotes[note_count])]
  129.          fret_count = fret_count + 1
  130.          count = count + 1
  131.      
  132.    return neck_notes_list
  133.    
  134. global_necknotes = build_necknotes(global_tuning)
  135.  
  136. # PRINT THE NOTES ON THE NECK
  137. def print_necknotes(necknotes):
  138.    hpos = 5
  139.    vpos = 1
  140.    for i in necknotes:
  141.       if necknotes[i][1] != 0: # don't print the root-notes
  142.          guitar.addstr(vpos,hpos,necknotes[i][2])
  143.          if hpos < 61:
  144.             hpos = hpos + 4
  145.          else:
  146.             hpos = 5
  147.             vpos = vpos + 1
  148.  
  149. def draw_guitar(the_id,key):
  150.    guitar.clear()
  151.    global guitar_show_what
  152.    global global_guitar_notetype
  153.    if the_id == 0: # do nothing, just print the guitar
  154.       send_return = 0
  155.       guitar_draw_strings()
  156.    if the_id == 2: # F2: notetype (A#/Bb)
  157.       if key == 0:
  158.          global_guitar_notetype = 1
  159.          send_return = 1
  160.       elif key == 1:
  161.          global_guitar_notetype = 0
  162.          send_return = 0
  163.    elif the_id == 3: # F3: show/hide notes
  164.       if key == 0:
  165.          guitar_show_what = 1
  166.          send_return = 1
  167.       elif key == 1:
  168.          guitar_show_what = 0
  169.          send_return = 0
  170.    
  171.    guitar_draw_strings()
  172.    if guitar_show_what == 1:
  173.       print_necknotes(global_necknotes)
  174.    guitar.box()
  175.    guitar_draw_body()
  176.    build_chord(current_note,current_subnote)
  177.    guitar.refresh()
  178.    return send_return # return changed value
  179.  
  180. #draw_guitar(0,0)
  181.  
  182. # SHORTHAND BOX
  183. shorthand = curses.newwin(8,6,0,(guitar_hpos + guitar_width + 1))
  184. shorthand.box()
  185. shorthand.refresh()
  186.  
  187. def build_shorthand(notes):
  188.    ()
  189.  
  190. # NOTES SECTION
  191. def print_notes():
  192.    notes = {}
  193.    x = 1
  194.    while (x <= 12):
  195.       notes[x] = guitarnotes[x]
  196.       x = x + 1
  197.    
  198.    notes_section.clear()
  199.    
  200.    note_hpos = 1
  201.    for i in notes:
  202.       notes_section.addstr(1,note_hpos,notes[i])
  203.       note_hpos = note_hpos + len(notes[i]) + 2
  204.    
  205.    notes_section.refresh()
  206.  
  207. notes_section_hpos = (maxwidth - 46) / 2 + 2
  208. notes_section = myscreen.derwin(3,46,8,notes_section_hpos)
  209. print_notes()
  210. myscreen.addstr(9,(notes_section_hpos - 9),'[Notes]',curses.A_BOLD)
  211. notes_section.chgat(1,1,2,curses.A_REVERSE)
  212. notes_section.refresh()
  213. # END OF NOTES SECTION
  214.  
  215. # SUBNOTES SECTION
  216. def print_subnotes():
  217.    column_width = 15
  218.    subnote_hpos = 1
  219.    subnote_vpos = 1
  220.    sn = 1
  221.    while (sn <= 50):
  222.       sn_len = len(subnotes[sn])
  223.       if subnote_vpos == (11): # if too high,
  224.          subnote_hpos = subnote_hpos + column_width  # jump to the right
  225.          subnote_vpos = 1 # and start over at the top
  226.       subnotes_section.addstr(subnote_vpos,subnote_hpos,subnotes[sn])
  227.       subnote_vpos = subnote_vpos + 1
  228.       sn = sn + 1
  229.  
  230.    subnotes_section.box()
  231.    subnotes_section.addstr(0,5,'[Subnotes]')
  232.  
  233. # determine horizontal position of the subnotes_section
  234. if maxwidth <= 75:
  235.    subnotes_section_hpos = 3
  236. elif maxwidth > 75:
  237.    subnotes_section_hpos = (maxwidth - 75) / 2
  238.  
  239. subnotes_section = myscreen.derwin(12,75,11,subnotes_section_hpos)
  240. print_subnotes()
  241. subnotes_section.chgat(1,1,13,curses.A_REVERSE)
  242. subnotes_section.refresh()
  243. # END OF SUBNOTES SECTION
  244.  
  245. # SCALE SECTION
  246. def build_scale(root,m):
  247.    scale = {1: guitarnotes[root]}
  248.    index = root
  249.    scale_formula = [2,2,1,2,2,2,1,2,2,1,2,2,2] # Major scale
  250.    if m == 1: # Major scale
  251.       mjn = 'Major'
  252.       scale_formula = [2,2,1,2,2,2,1,2,2,1,2,2,2]
  253.    elif m == 2: # Natural Minor scale
  254.       mjn = 'Natural Minor'
  255.       scale_formula = [2,1,2,2,1,2,2,2,1,2,2,1,2]
  256.    elif m == 3: # Harmonic Minor scale
  257.       mjn = 'Harmonic Minor'
  258.       scale_formula = [2,1,2,2,1,3,1,2,1,2,2,1,3]
  259.    elif m == 4: # Melodic Minor scale
  260.       mjn = 'Melodic Minor'
  261.       scale_formula = [2,1,2,2,2,2,1,2,1,2,2,2,2]
  262.    
  263.    scaletext = mjn + ' scale of ' + scale[1] + ': '
  264.    
  265.    num = 2
  266.    for i in scale_formula:
  267.       if (index + i) <= len(guitarnotes):
  268.          index = index + i
  269.       else:
  270.          index = (index + i) - len(guitarnotes)
  271.       scale[num] = guitarnotes[index] # add to dictionary
  272.       num = num + 1
  273.  
  274.    scale_section.clear() # erase the content of the scale section
  275.    scale_hpos = len(scaletext) + 1 # start printing the scale
  276.    count = 0
  277.    for x in scale:
  278.       if count < 7:
  279.          scale_section.addstr(1,scale_hpos,scale[x])
  280.          scale_hpos = scale_hpos + len(scale[x]) + 1
  281.          count = count + 1
  282.  
  283.    scale_section.addstr(1,1,scaletext)
  284.    #scale_section.box()
  285.    scale_section.refresh()
  286.  
  287. scale_section = myscreen.derwin(3,75,22,subnotes_section_hpos)
  288.  
  289. def build_major_scale(root):
  290.    scale = {1: guitarnotes[root]}
  291.    key_reference = {1: root} # keeps a reference of the relation of scale-position to notes-position
  292.    index = root
  293.    scale_formula = [2,2,1,2,2,2,1,2,2,1,2,2,2] # Major scale
  294.  
  295.    num = 2
  296.    for i in scale_formula:
  297.       if (index + i) <= len(guitarnotes):
  298.          index = index + i
  299.       else:
  300.          index = (index + i) - len(guitarnotes)
  301.       scale[num] = guitarnotes[index] # add to dictionary
  302.       key_reference[num] = index
  303.       num = num + 1    
  304.  
  305.    return scale,key_reference
  306.  
  307. build_scale(current_note,scale)
  308. # END OF SCALE SECTION
  309.  
  310. # CHORD SECTION
  311. def build_chord(root,chordtype):
  312.    chord = []
  313.    chord_formula = chord_formulas[chordtype]
  314.    scale,key_reference = build_major_scale(root)
  315.    global string_map
  316.    string_map = []
  317.  
  318.    for z in chord_formula:
  319.       if isinstance(z, int) == True: # if the formula holds an integer, fine
  320.          chord.append(scale[z])
  321.          string_map.append(key_reference[z])
  322.       elif z[0] == 'b': # if the formula holds a 'b'-note, get 1 pianokey below that note in the scale
  323.          key = int(z[1])
  324.          if key_reference[key] > 1:
  325.             note = str(guitarnotes[key_reference[key] - 1])
  326.             string_map.append(key_reference[key] - 1)
  327.          elif key_reference[key] == 1: # 1-1=12 (go back around)
  328.             note = str(guitarnotes[12])
  329.             string_map.append(12)
  330.  
  331.          chord.append(note)
  332.  
  333.       elif z[0] == '#': # if the formula has a '#'-note, get 1 pianokey above that note in the scale
  334.          key = int(z[1])
  335.          if key_reference[key] < 12:
  336.             note = str(guitarnotes[key_reference[key] + 1])
  337.             string_map.append(key_reference[key] + 1)        
  338.          elif key_reference[key] == 12: # 12+1=1 (go back around)
  339.             note = str(guitarnotes[1])
  340.             string_map.append(1)
  341.  
  342.          chord.append(note)
  343.  
  344.    chord_section.clear()
  345.    
  346.    chord_hpos = 2
  347.    count = 0
  348.    for y in chord:
  349.       chord_space = (len(guitarnotes[root]) + len(subnotes[chordtype]) + 1)
  350.       chord_section.addstr(1,1,guitarnotes[root] + ' ' + subnotes[chordtype] + ': ')
  351.       chord_section.addstr(1,(1 + chord_space + chord_hpos),y)
  352.       chord_hpos = chord_hpos + len(y) + 1
  353.  
  354.    #chord_section.box()
  355.    chord_section.refresh()
  356.    print_subnotes()
  357.    #guitar_draw_strings()
  358.    print_chord(chord,"regular")
  359.  
  360. #chords_open = { # '': [[1,],[2,],[3,][4,],[5,],[6,]]
  361. #'C': [[1,0],[2,1],[3,0],[4,2],[5,3],[6,0]], 'D': [[1,2],[2,3],[3,2],[4,0],[5,'x'],[6,'x']], 'E': [[1,0],[2,0],[3,1][4,2],[5,2],[6,0]],
  362. #'F': [[1,'x'],[2,1],[3,2][4,3],[5,3],[6,'x']], 'G': [[1,3],[2,0],[3,0][4,0],[5,2],[6,3]], 'A': [[1,0],[2,2],[3,2][4,2],[5,0],[6,0]],
  363. #'B': [[1,2],[2,4],[3,4][4,4],[5,'x'],[6,'x']], 'Dm': [[1,1],[2,3],[3,2][4,0],[5,'x'],[6,'x']], 'Em': [[1,0],[2,0],[3,0][4,2],[5,2],[6,0]],
  364. #'Fm': [[1,4],[2,6],[3,5][4,3],[5,'x'],[6,'x']], 'Am': [[1,0],[2,1],[3,2][4,2],[5,0],[6,0]], 'Bm': [[1,2],[2,3],[3,4][4,4],[5,'x'],[6,'x']],
  365. #'C7': [[1,0],[2,1],[3,3][4,2],[5,3],[6,0]], 'D7': [[1,2],[2,1],[3,2][4,0],[5,'x'],[6,'x']], 'E7': [[1,0],[2,0],[3,1][4,0],[5,2],[6,0]],
  366. #'F7': [[1,'x'],[2,1],[3,2][4,1],[5,3],[6,'x']], 'G7': [[1,1],[2,0],[3,0][4,0],[5,2],[6,3]], 'A7': [[1,0],[2,2],[3,0][4,2],[5,0],[6,'x']],
  367. #'B7': [[1,2],[2,0],[3,2][4,1],[5,2],[6,'x']], 'D#': [[1,3],[2,4],[3,3][4,1],[5,'x'],[6,'x']], 'F#': [[1,6],[2,7],[3,6][4,4],[5,'x'],[6,'x']],
  368. #'C6': [[1,0],[2,1],[3,0][4,2],[5,0],[6,0]], 'D6': [[1,2],[2,3],[3,2][4,4],[5,'x'],[6,'x']], 'E6': [[1,0],[2,2],[3,1][4,2],[5,2],[6,'x']],
  369. #'F6': [[1,'x'],[2,1],[3,2][4,0],[5,'x'],[6,1]], 'G6': [[1,'x'],[2,3],[3,4][4,2],[5,'x'],[6,3]], 'A6': [[1,2],[2,2],[3,2][4,2],[5,0],[6,'x']],
  370. #'B6': [[1,4],[2,4],[3,4][4,4],[5,'x'],[6,'x']], 'Cm7': [[1,'x'],[2,1],[3,3][4,1],[5,3],[6,'x']], 'Dm7': [[1,1],[2,1],[3,2][4,0],[5,'x'],[6,'x']],
  371. #'Em7': [[1,0],[2,0],[3,0][4,0],[5,2],[6,0]], 'Fm7': [[1,1],[2,1],[3,1][4,1],[5,'x'],[6,'x']], 'Gm7': [[1,3],[2,3],[3,3][4,3],[5,'x'],[6,'x']],
  372. #'Am7': [[1,0],[2,1],[3,0][4,2],[5,0],[6,'x']], 'Bm7': [[1,2],[2,3],[3,2][4,0],[5,2],[6,'x']], 'Asus2': [[1,0],[2,0],[3,2][4,2],[5,0],[6,'x']]}
  373.  
  374. chords_barre = {}
  375.  
  376. # PRINT THE CHORD ON THE NECK/FRETBOARD
  377. def print_chord(newchord,var): # takes the chord-array and a variation (regular, barre, all, etc)
  378. # global necknotes = [string,fret,note]
  379.    chord_on_neck = []
  380.    for x in global_necknotes:
  381.       if global_necknotes[x][2] in newchord:
  382.          chord_on_neck.append(global_necknotes[x])
  383.  
  384.    if var == "all":
  385.       for q in chord_on_neck:
  386.          if q[1] != 0:
  387.             hpos = 1 + (q[1] * 4)
  388.             guitar.chgat(q[0],hpos,len(q[2]),curses.A_REVERSE)
  389.  
  390.    if var == "regular":
  391.       count = 1
  392.       chord_check = []
  393.       from operator import itemgetter
  394.       chord_on_neck.sort(key=itemgetter(0))
  395.       for p,sublist in enumerate(chord_on_neck):
  396.          if count == sublist[0]: # iterate over all strings
  397.             shorthand.addstr(count,2,str(sublist[1])) # add fretnumbers on stringpositions in shorthand-box
  398.             chord_check.append(sublist[2]) # build a list of the chords that were found
  399.             if sublist[1] != 0: # unless the note has been found on an open string:
  400.                hpos = 1 + (sublist[1] * 4) # get the correct fret
  401.                guitar.chgat(sublist[0],hpos,len(sublist[2]),curses.A_REVERSE) # make the position bold
  402.             count = count + 1
  403.  
  404.       # check for missing notes:
  405.       missing = []
  406.       count_chk = 1
  407.       for i in newchord:
  408.          if i not in chord_check:
  409.             missing.append(i)
  410.       # if we are indeed missing something:
  411.       if len(missing) > 0:
  412.          for m in missing:
  413.             for z,sublist in enumerate(chord_on_neck):
  414.                myscreen.addstr(41+count_chk,1,str(sublist))
  415.                if m == sublist[2] and sublist[1] < 5:
  416.                   #missing.remove(m)
  417.                   shorthand.addstr(count_chk,2,str(sublist[1]))
  418.                   hpos = 1 + (sublist[1] * 4)
  419.                   guitar.chgat(sublist[0],hpos,len(sublist[2]),curses.A_REVERSE)
  420.                   break
  421. # STILL NOT WORKING, TRY AM9 or something like that.
  422.  
  423.    myscreen.addstr(30,1,str(chord_on_neck))
  424.    myscreen.addstr(40,1,str(chord_check))
  425.    myscreen.addstr(41,1,str(missing))
  426.    guitar.refresh()
  427.    shorthand.refresh()
  428.  
  429. #print_chord(chord,0)
  430. chord_section = myscreen.derwin(4,75,24,subnotes_section_hpos)
  431. draw_guitar(0,0)
  432. #build_chord(current_note,current_subnote)
  433. print_subnotes()
  434. subnotes_section.chgat(1,1,13,curses.A_REVERSE)
  435. subnotes_section.refresh()
  436.  
  437. # END OF CHORD SECTION
  438.  
  439. # NAVIGATION
  440. def tab(win,vpos,hpos): # function for tabbing between notes and subnotes sections
  441.    if win == 1: # current window = notes_section
  442.       subnotes_section.chgat(vpos,hpos,13,curses.A_REVERSE)
  443.       subnotes_section.addstr(0,5,'[Subnotes]',curses.A_BOLD) # bold title
  444.       myscreen.addstr(9,(notes_section_hpos - 9),'[Notes]',curses.A_NORMAL) # unbold title
  445.       notes_section.refresh()
  446.       subnotes_section.refresh()
  447.       return 2
  448.    elif win == 2: # current window = subnotes_section
  449.       notes_section.chgat(vpos,hpos,2,curses.A_REVERSE)
  450.       subnotes_section.addstr(0,5,'[Subnotes]',curses.A_NORMAL) # unbold title
  451.       myscreen.addstr(9,(notes_section_hpos - 9),'[Notes]',curses.A_BOLD) # bold title
  452.       notes_section.refresh()
  453.       subnotes_section.refresh()
  454.       return 1
  455.  
  456. def navigate_notes_section(vpos,hpos,note,key):
  457.    if key == 'up' or key == 'down':
  458.       () # can't go up or down from the notes section
  459.    elif key == 'right':
  460.       if note < 12:
  461.          hpos = hpos + 2 + len(guitarnotes[note])
  462.          note = note + 1
  463.       else:
  464.          note = 1
  465.          hpos = 1
  466.    elif key == 'left':
  467.       if note > 1:
  468.          note = note - 1
  469.          hpos = hpos - 2 - len(guitarnotes[note])
  470.       else:
  471.          note = 12
  472.          hpos = 39
  473.  
  474.    notes_section.clrtoeol() # erase the content, then redraw it
  475.    print_notes()
  476.    notes_section.chgat(vpos,hpos,2,curses.A_REVERSE)
  477.    notes_section.refresh()
  478.    return vpos,hpos,note
  479.  
  480. def navigate_subnotes_section(vpos,hpos,subnote,key): # function for moving between values
  481.    attr = curses.A_BOLD
  482.    if key == 'up':
  483.       if vpos > 1: # if we can still go up
  484.          vpos = vpos - 1 # go up
  485.          subnote = subnote - 1 # go one subnote back
  486.       else: # if we cannot go up
  487.          vpos = 10 # go to column bottom...
  488.          if hpos > 15: # check to see if we're in the 1st column
  489.             hpos = hpos - 15 # move a column back
  490.             subnote = subnote - 1 # move a subnote back
  491.          else: # if we're in the first column
  492.             hpos = 61 # jump to last column
  493.             subnote = 50 # jump to last subnote
  494.    elif key == 'down':
  495.       if vpos < 10: # if we can still go down
  496.          vpos = vpos + 1 # go down
  497.          subnote = subnote + 1 # go one subnote forward
  498.       else: # if we cannot go down
  499.          vpos = 1 # go to column top
  500.          if hpos < 61: # if we're note in the last column
  501.             hpos = hpos + 15 # move a column forward
  502.             subnote = subnote + 1 # move a subnote forward
  503.          else: # if we're in the last column
  504.             hpos = 1 # jump to first column
  505.             subnote = 1 # jump to first subnote
  506.    elif key == 'left':
  507.       if hpos > 15: # if we're not in the first column
  508.          hpos = hpos - 15 # move a column back
  509.          subnote = subnote - 10
  510.       else: # if we're in the first column
  511.          hpos = 61 # jump to last column
  512.          subnote = subnote + 40 # jump to last subnote in last column
  513.    elif key == 'right':
  514.       if hpos < 61: # if we're not in the last column
  515.          hpos = hpos + 15 # move a column to the right
  516.          subnote = subnote + 10
  517.       else:
  518.          hpos = 1 # jump to first column
  519.          subnote = subnote - 40 # jump to first subnote        
  520.    elif key == 'refresh':
  521.       if current_window == 1:
  522.          attr = curses.A_NORMAL
  523.       else:
  524.          attr = curses.A_BOLD
  525.  
  526.    subnotes_section.clrtobot() # erase the content, then redraw it
  527.    print_subnotes()
  528.    subnotes_section.addstr(0,5,'[Subnotes]',attr)
  529.    subnotes_section.chgat(vpos,hpos,13,curses.A_REVERSE)
  530.    subnotes_section.refresh()
  531.    return vpos,hpos,subnote
  532. # END OF NAVIGATION
  533.  
  534. # CAPTURE KEYPRESSES
  535. while True:
  536.    key = myscreen.getch()
  537.    if key == ord('q'): break # 'q' = quit
  538.    elif key == ord('\t'): # TAB: move between notes_section and subnotes_section
  539.       if current_window == 1:
  540.          current_window = tab(current_window,ss_cursor_vpos,ss_cursor_hpos)
  541.       elif current_window == 2:
  542.          current_window = tab(current_window,ns_cursor_vpos,ns_cursor_hpos)
  543.    elif key == curses.KEY_UP:
  544.       if current_window == 1:
  545.          ns_cursor_vpos,ns_cursor_hpos,current_note = navigate_notes_section(ns_cursor_vpos,ns_cursor_hpos,current_note,'up')
  546.       elif current_window == 2:
  547.          ss_cursor_vpos,ss_cursor_hpos,current_subnote = navigate_subnotes_section(ss_cursor_vpos,ss_cursor_hpos,current_subnote,'up')
  548.    elif key == curses.KEY_DOWN:
  549.       if current_window == 1:
  550.          ns_cursor_vpos,ns_cursor_hpos,current_note = navigate_notes_section(ns_cursor_vpos,ns_cursor_hpos,current_note,'down')
  551.       elif current_window == 2:
  552.          ss_cursor_vpos,ss_cursor_hpos,current_subnote = navigate_subnotes_section(ss_cursor_vpos,ss_cursor_hpos,current_subnote,'down')
  553.    elif key == curses.KEY_LEFT:
  554.       if current_window == 1:
  555.          ns_cursor_vpos,ns_cursor_hpos,current_note = navigate_notes_section(ns_cursor_vpos,ns_cursor_hpos,current_note,'left')
  556.       elif current_window == 2:
  557.          ss_cursor_vpos,ss_cursor_hpos,current_subnote = navigate_subnotes_section(ss_cursor_vpos,ss_cursor_hpos,current_subnote,'left')
  558.    elif key == curses.KEY_RIGHT:
  559.       if current_window == 1:
  560.          ns_cursor_vpos,ns_cursor_hpos,current_note = navigate_notes_section(ns_cursor_vpos,ns_cursor_hpos,current_note,'right')
  561.       elif current_window == 2:
  562.          ss_cursor_vpos,ss_cursor_hpos,current_subnote = navigate_subnotes_section(ss_cursor_vpos,ss_cursor_hpos,current_subnote,'right')
  563.    elif key == ord('\n'): # ENTER
  564.       draw_guitar(0,0)
  565.       build_scale(current_note,scale)
  566.       ss_cursor_vpos,ss_cursor_hpos,current_subnote = navigate_subnotes_section(ss_cursor_vpos,ss_cursor_hpos,current_subnote,'refresh')
  567.    elif key == curses.KEY_F2: # F2: switch notetype (A#/Bb)
  568.       caller_id = 2 # the caller-id for this function
  569.       if guitarnotes == note_type_1:
  570.          guitarnotes = note_type_2
  571.          global_necknotes = build_necknotes(global_tuning)
  572.          global_guitar_notetype = draw_guitar(caller_id,0)
  573.       elif guitarnotes == note_type_2:
  574.          guitarnotes = note_type_1
  575.          global_necknotes = build_necknotes(global_tuning)
  576.          global_guitar_notetype = draw_guitar(caller_id,1)
  577.       print_notes()
  578.       navigate_notes_section(ns_cursor_vpos,ns_cursor_hpos,current_note,'up') # to refresh the notes section
  579.       build_scale(current_note,scale)
  580.       build_chord(current_note,current_subnote)
  581.       ss_cursor_vpos,ss_cursor_hpos,current_subnote = navigate_subnotes_section(ss_cursor_vpos,ss_cursor_hpos,current_subnote,'refresh')
  582.    elif key == curses.KEY_F3: # F3: toggle display of notes on/off
  583.       caller_id = 3
  584.       guitar_show_what = draw_guitar(caller_id,guitar_show_what)
  585.  
  586. # _TODO: make it resizable.
  587. curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment