test.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. conv_note = note
  17. note = note.split(',')
  18. while len(note[1]) > 3:
  19. if note[1][1:3] == "##":
  20. note[1] = note[1][:1] + note[1][-1:] #remove ##
  21. conv_note = note[0] + ',' + note[1] #built new note
  22. conv_note = highes_note(conv_note, 2) #highes note by 2
  23. elif note[1][1:3] == "--":
  24. note[1] = note[1][:1] + note[1][-1:] #remove --
  25. conv_note = note[0] + ',' + note[1] #built new note
  26. conv_note = highes_note(conv_note, -2) #highes note by 2
  27. elif note[1][1:3] == "-#" or note[1][1:3] == "#-":
  28. note[1] = note[1][:1] + note[1][-1:] #remove #- or -#
  29. conv_note = note[0] + ',' + note[1] #built new note
  30. return conv_note
  31. # returns the pitch difference from two notes
  32. # assumes that last char is octave indicater
  33. def interval(note_1, note_2):
  34. notes_list = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
  35. note_1 = note_1.split(',')
  36. note_2 = note_2.split(',')
  37. octave = int(note_2[1][-1:]) - int(note_1[1][-1:])
  38. pitch = notes_list.index(note_2[1][:1]) - notes_list.index(note_1[1][:1])
  39. pitch += 12 * octave
  40. if note_1[1][1:2] == '#': pitch -= 1
  41. if note_2[1][1:2] == '-': pitch -= 1
  42. if note_1[1][1:2] == '-': pitch += 1
  43. if note_2[1][1:2] == '#': pitch += 1
  44. return pitch
  45. """ actual functions """
  46. # changes tempo
  47. def tempo(input_list, temp):
  48. working_list = []
  49. for note in input_list:
  50. duration = float (note[0:4]) * temp
  51. note = duration_to_string(duration) + note[4:]
  52. working_list.append(note)
  53. return working_list
  54. # half duration
  55. def verkleinerung(input_list):
  56. return tempo(input_list, .5)
  57. # double duration
  58. def vergrösserung(input_list):
  59. return tempo(input_list, 2)
  60. # return the note higher by a value (1 = half a step)
  61. def highes_note(note, value):
  62. note = note.split(',')
  63. notes_list = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
  64. is_up = True
  65. if value < 0: # check if value is negative and store in is_up
  66. notes_list.reverse()
  67. value = -value
  68. is_up = False
  69. pitch = note[1][:1] # first char from 2nd list argument
  70. ord_number = notes_list.index(pitch)
  71. octave = int(note[1][-1:])
  72. ord_number += value
  73. while ord_number >= 12:
  74. if is_up:
  75. octave += 1
  76. else:
  77. octave -= 1
  78. ord_number = ord_number - 12
  79. if note[1][1:2] == '#': # 2nd char from 2nd list argument
  80. higher_note = note[0] + ',' + notes_list[ord_number] + '#' + str(octave)
  81. elif note[1][1:2] == '-':
  82. higher_note = note[0] + ',' + notes_list[ord_number] + '-' + str(octave)
  83. else:
  84. higher_note = note[0] + ',' + notes_list[ord_number] + str(octave)
  85. higher_note = convert_multiple_key_signetures(higher_note)
  86. return higher_note
  87. # returns a part higher by some value
  88. def sequenz(input_list, value):
  89. working_list = []
  90. for n in input_list:
  91. note = highes_note(n, value)
  92. working_list.append(note)
  93. return working_list
  94. # mirrors notes at the first note
  95. def mirror_pitch(input_list):
  96. working_list = []
  97. mirror = input_list[0]
  98. for note in input_list:
  99. pitch = highes_note(mirror, - interval(mirror, note)) # change pitch
  100. note = note.split(',')
  101. pitch = pitch.split(',')
  102. mir_note = note[0] + ',' + pitch[1] # take duration from "note"
  103. working_list.append(mir_note)
  104. return working_list
  105. # mirrors the rythme from back to front
  106. def mirror_rythm(input_list):
  107. r_input_list = input_list[::-1]
  108. working_list = [] # revesed list
  109. for i in range (0, len(input_list)):
  110. r_note = r_input_list[i].split(',')
  111. n_note = input_list[i].split(',')
  112. working_list.append(r_note[0] + ',' + n_note[1])
  113. return working_list
  114. l = [
  115. '0.50,D4',
  116. '1.00,E4',
  117. '0.25,D4',
  118. '1.00,C4'
  119. ]
  120. """
  121. print('list')
  122. print(l)
  123. print('verkleinerung')
  124. print (verkleinerung(l))
  125. print('vergrösserung')
  126. print(vergrösserung(l))
  127. print('sequenz')
  128. print(sequenz(l, 1))
  129. """
  130. print('mirror')
  131. print (l)
  132. print(mirror_rythm(l))