Browse Source

motiv-gen now can be used with commandline arguments length, velocity and first note + some cleanup

Paola Fontana Gasio 4 years ago
parent
commit
f75b7945b6
5 changed files with 77 additions and 23 deletions
  1. 18 1
      src/gizmo_funtions.py
  2. 0 1
      src/gizmo_transforming_functions.py
  3. 43 9
      src/motiv_gen
  4. 7 3
      src/thema-gen
  5. 9 9
      src/xml-parser

+ 18 - 1
src/gizmo_funtions.py

@@ -14,7 +14,7 @@ def replace_substring_of_list_part(input_list, split_index, pattern, replace_str
     working_list = []
     working_list = []
     for sub in input_list:
     for sub in input_list:
         working_list.append((sub.split(",")[split_index]))
         working_list.append((sub.split(",")[split_index]))
-    
+
     # replace the patterns in the temporary list with the replacement strings
     # replace the patterns in the temporary list with the replacement strings
     working_list = [sub.replace(pattern, replace_str) for sub in working_list]
     working_list = [sub.replace(pattern, replace_str) for sub in working_list]
 
 
@@ -27,6 +27,23 @@ def replace_substring_of_list_part(input_list, split_index, pattern, replace_str
         output_list.append(seperator_string.join(loop_list))
         output_list.append(seperator_string.join(loop_list))
     input_list[:] = output_list
     input_list[:] = output_list
 
 
+# return true if the given pitch is in correct gizmo notation
+notes_list = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
+
+def check_pitch_gizmo_notation(pitch):
+    if pitch[0] not in notes_list:
+        return False
+    if len(pitch) == 2:
+        if pitch[1].isdigit():
+            return True
+        else:
+            return False
+    elif len(pitch) == 3:
+        if pitch[1] == '-' or pitch[1] == '#' and pitch[2].isdigit():
+            return True
+    else:
+        return False
+
 # function to write to standard output in the 'gizmo notation'
 # function to write to standard output in the 'gizmo notation'
 def gizmo_stdout(input_list):
 def gizmo_stdout(input_list):
     for item in input_list:
     for item in input_list:

+ 0 - 1
src/gizmo_transforming_functions.py

@@ -1,5 +1,4 @@
 
 
-
 """ helper functions """
 """ helper functions """
 
 
 # converts float duration to string in the form x.xx
 # converts float duration to string in the form x.xx

+ 43 - 9
src/motiv_gen

@@ -1,8 +1,9 @@
 #!/usr/bin/env python3
 #!/usr/bin/env python3
 
 
 from gizmo_transforming_functions import *
 from gizmo_transforming_functions import *
-from gizmo_funtions import gizmo_stdout
+from gizmo_funtions import gizmo_stdout, check_pitch_gizmo_notation
 
 
+import sys
 import random
 import random
 
 
 notes_list = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
 notes_list = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
@@ -30,12 +31,41 @@ def compute_note_sequenz_int():
 def half_note_duration(note):
 def half_note_duration(note):
     return verkleinerung([note])[0]
     return verkleinerung([note])[0]
 
 
+""" This should produce a motiv which can be used for further processing. There are three 
+command line arguments to change result first length in quarter notes, second velocity and for the 
+third it can be inputed a starting note"""
+
+# these are default values, TODO: write it in order to change them as commandline arguments 
+
+# get command line argument length
+try:
+    motiv_length_in_quarter_notes = int(sys.argv[1])
+    # raise error if number too small
+    if motiv_length_in_quarter_notes < 1:
+        raise ValueError("Number was too small (below 1)")
+except (IndexError, ValueError):
+    # set fallback value if no or wrong arguments given
+    motiv_length_in_quarter_notes = 4
+
+# get command line argument velocity
+try:
+    rythme_velocity = int(sys.argv[2])
+    # raise error if number too small
+    if rythme_velocity < 0 or rythme_velocity > 3:
+        raise ValueError("Number out of bounce (below 0 or over 2)")
+except (IndexError, ValueError):
+    # set fallback value if no or wrong arguments given
+    rythme_velocity = 2
+
+try:
+    first_note = str(sys.argv[3])
+    # raise error if not in correct gizmo notation
+    if not check_pitch_gizmo_notation(first_note):
+        raise ValueError("Not in correct gizmo notation")
+except (IndexError, ValueError):
+    # randomness will be applied later, set first_note to String
+    first_note = ''
 
 
-""" This should produce a motiv which can be used for further processing. By now 
-the length is determined by motiv_length_in_notes later it should be decided by 
-randomness"""
-
-motiv_length_in_quarter_notes = 4
 motiv = []
 motiv = []
 
 
 # temporarly all notes have duration 1.00 or 2.00
 # temporarly all notes have duration 1.00 or 2.00
@@ -49,7 +79,6 @@ while i < motiv_length_in_quarter_notes:
         motiv.append('1.00,')
         motiv.append('1.00,')
     i += 1
     i += 1
 
 
-
 # compute rythm
 # compute rythm
 def compute_rhytme_by_halfen(motiv):
 def compute_rhytme_by_halfen(motiv):
     i = 0
     i = 0
@@ -63,11 +92,16 @@ def compute_rhytme_by_halfen(motiv):
         i += 1
         i += 1
     return new_motiv
     return new_motiv
 
 
-for i in range (2):
+for i in range (rythme_velocity):
     motiv = compute_rhytme_by_halfen(motiv)
     motiv = compute_rhytme_by_halfen(motiv)
 
 
 # choose first note
 # choose first note
-motiv[0] = motiv[0] + random.choice(notes_list) + str(int(round(random.normalvariate(4, 0.5),0)))
+
+if len(first_note) == 0:
+    motiv[0] = motiv[0] + random.choice(notes_list) + str(int(round(random.normalvariate(4, 0.5),0)))
+else:
+    motiv[0] = motiv[0] + first_note
+    print("motiv:", motiv[0])
 
 
 # compute remaining notes
 # compute remaining notes
 for i in range (1, len(motiv)):
 for i in range (1, len(motiv)):

+ 7 - 3
src/thema-gen

@@ -27,9 +27,9 @@ except (IndexError, ValueError):
     # set fallback value if no or wrong arguments given
     # set fallback value if no or wrong arguments given
     number = 4
     number = 4
 
 
-#output_list = sequenz(stdin_list, 1)
-motiv_list = verkleinerung(stdin_list)
-#motiv_list = stdin_list
+# output_list = sequenz(stdin_list, 1)
+# motiv_list = verkleinerung(stdin_list)
+motiv_list = stdin_list
 
 
 # create lists
 # create lists
 for i in range(number):
 for i in range(number):
@@ -49,6 +49,10 @@ function_list = [
     "mirror_rythm(motiv_list)"
     "mirror_rythm(motiv_list)"
     ]
     ]
 
 
+# do not apply "verkleinerung" if the shortest note is 1/16
+if '0.25' in return_part(motiv_list, 0):
+    function_list.remove("verkleinerung(motiv_list)")
+
 def first_equals_last_pitch(input_list):
 def first_equals_last_pitch(input_list):
     is_same_pitch = not(bool(interval(input_list[0], input_list[-1])))
     is_same_pitch = not(bool(interval(input_list[0], input_list[-1])))
     return is_same_pitch
     return is_same_pitch

+ 9 - 9
src/xml-parser

@@ -16,19 +16,19 @@ def how_many_alter_signs(value):
     return alter_sign
     return alter_sign
 
 
 
 
-# Reading the data inside the xml 
-# file to a variable under the name  
+# Reading the data inside the xml
+# file to a variable under the name
 # data from standard input
 # data from standard input
 import sys
 import sys
 data = sys.stdin.read()
 data = sys.stdin.read()
 
 
-# Passing the stored data inside 
-# the beautifulsoup parser, storing  
+# Passing the stored data inside
+# the beautifulsoup parser, storing
 # the returned object
 # the returned object
 
 
-BS_data = BeautifulSoup(data, "xml") 
+BS_data = BeautifulSoup(data, "xml")
   
   
-# Finding all instances of tag  
+# Finding all instances of tag
 # 'notes'
 # 'notes'
 notes = BS_data.find_all('note')
 notes = BS_data.find_all('note')
 # reading notes and parse to 'gizmo notation'
 # reading notes and parse to 'gizmo notation'
@@ -38,8 +38,8 @@ for note in notes:
     duration = float(note.find('duration').text) / float(BS_data.find('divisions').text)
     duration = float(note.find('duration').text) / float(BS_data.find('divisions').text)
     duration = duration_to_string(duration)
     duration = duration_to_string(duration)
     pitch = note.find('pitch')
     pitch = note.find('pitch')
-    if pitch == None: # TODO: cutting out pauses does not work yet
-        next
+    if pitch == None:
+        continue
     else:
     else:
         new_note = duration + ',' + note.find('step').text
         new_note = duration + ',' + note.find('step').text
         if note.find('alter') != None:
         if note.find('alter') != None:
@@ -49,4 +49,4 @@ for note in notes:
     working_list.append(new_note)
     working_list.append(new_note)
 
 
 # print to standard output
 # print to standard output
-gizmo_stdout(working_list)   
+gizmo_stdout(working_list)