test.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """ helper functions """
  2. # converts float duration to string in the form x.xx
  3. def duration_to_string(input_float_duration):
  4. input_float_duration *= 100
  5. input_int_duration = int(input_float_duration)
  6. duration = ""
  7. for i in range (0, 3):
  8. x = input_int_duration % 10
  9. duration = duration + str(x)
  10. input_int_duration = input_int_duration // 10
  11. duration = duration[::-1]
  12. duration = duration[:1] + '.' + duration[1:]
  13. return duration
  14. # converts the weird notation like -#, ## or -- to nothing, one note higher or lower respectivly
  15. def convert_multiple_key_signetures(note):
  16. while len(note) > 7:
  17. if note[5:7] == "##":
  18. note = note[:5] + note[7:]
  19. note = highes_note(note, 2)
  20. if note[5:7] == "--":
  21. note = note[:5] + note[7:]
  22. note = highes_note(note, -2)
  23. if note[5:7] == "-#" or note[5:7] == "#-":
  24. note = note[:5] + note[7:]
  25. return note
  26. # returns the pitch difference from two notes
  27. def interval(note_1, note_2):
  28. notes_list = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
  29. octave = int(note_2[-1:]) - int(note_1[-1:])
  30. pitch = notes_list.index(note_2[4:5]) - notes_list.index(note_1[4:5])
  31. pitch += 12 * octave
  32. if note_1[5:6] == '#': pitch -= 1
  33. if note_2[5:6] == '-': pitch -= 1
  34. if note_1[5:6] == '-': pitch += 1
  35. if note_2[5:6] == '#': pitch += 1
  36. return pitch
  37. """ actual functions """
  38. # changes tempo
  39. def tempo(input_list, temp):
  40. working_list = []
  41. for note in input_list:
  42. duration = float (note[0:4]) * temp
  43. note = duration_to_string(duration) + note[4:]
  44. working_list.append(note)
  45. return working_list
  46. # half duration
  47. def verkleinerung(input_list):
  48. return tempo(input_list, .5)
  49. # double duration
  50. def vergrösserung(input_list):
  51. return tempo(input_list, 2)
  52. # return the note higher by a value (1 = half a step)
  53. def highes_note(note, value):
  54. note = note.split(',')
  55. notes_list = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
  56. is_up = True
  57. if value < 0: # check if value is negative and store in is_up
  58. notes_list.reverse()
  59. value = -value
  60. is_up = False
  61. pitch = note[1][:1] # first char from 2nd list argument
  62. ord_number = notes_list.index(pitch)
  63. octave = int(note[1][-1:])
  64. ord_number += value
  65. while ord_number >= 12:
  66. if is_up:
  67. octave += 1
  68. else:
  69. octave -= 1
  70. ord_number = ord_number - 12
  71. if note[1][1:2] == '#': # 2nd char from 2nd list argument
  72. note = note[0] + notes_list[ord_number] + '#' + str(octave)
  73. elif note[1][1:2] == '-':
  74. note = note[0] + notes_list[ord_number] + '-' + str(octave)
  75. else:
  76. note = note[0] + notes_list[ord_number] + str(octave)
  77. note = convert_multiple_key_signetures(note)
  78. return note
  79. # returns a part higher by some value
  80. def sequenz(input_list, value):
  81. working_list = []
  82. for n in input_list:
  83. note = highes_note(n, value)
  84. working_list.append(note)
  85. return working_list
  86. # mirrors notes at the first note
  87. def mirror(input_list):
  88. working_list = []
  89. mirror = input_list[0]
  90. for note in input_list:
  91. note[0] = highes_note(mirror, - interval(mirror, note))
  92. working_list.append(note[0])
  93. return working_list
  94. print(highes_note('1.00,D#4', 2))