Browse Source

new files: thema-extender and song-gen

Paola Fontana Gasio 4 years ago
parent
commit
05bd0df377
4 changed files with 218 additions and 6 deletions
  1. 10 0
      src/gizmo_funtions.py
  2. 24 6
      src/motiv_gen
  3. 127 0
      src/song-gen
  4. 57 0
      src/thema-extender

+ 10 - 0
src/gizmo_funtions.py

@@ -63,3 +63,13 @@ def stdin():
     for line in sys.stdin:
     for line in sys.stdin:
         stdin_list.append(line.rstrip())
         stdin_list.append(line.rstrip())
     return stdin_list
     return stdin_list
+
+# this funcion takes a text and return every char that occuers once in a list
+def diff_char(text):
+    char_list = []
+    for char in text:
+        if char in char_list:
+            continue
+        else:
+            char_list.append(char)
+    return char_list

+ 24 - 6
src/motiv_gen

@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 #!/usr/bin/env python3
 
 
 from gizmo_transforming_functions import *
 from gizmo_transforming_functions import *
-from gizmo_funtions import gizmo_stdout, check_pitch_gizmo_notation
+from gizmo_funtions import gizmo_stdout, check_pitch_gizmo_notation, gizmo_write2file
 
 
 import sys
 import sys
 import random
 import random
@@ -35,11 +35,24 @@ def half_note_duration(note):
 command line arguments to change result first length in quarter notes, second velocity and for the
 command line arguments to change result first length in quarter notes, second velocity and for the
 third it can be inputed a starting note"""
 third it can be inputed a starting note"""
 
 
-# these are default values, TODO: write it in order to change them as commandline arguments 
+
+try:
+    write2file_str = str(sys.argv[1])
+    # raise error if not 'file' or stout
+    if write2file_str == 'file' or write2file_str == 'stout':
+        if write2file_str == 'file':
+            write2file = True
+        else:
+            write2file = False
+    else:
+        raise ValueError("wrong attribute")
+except (IndexError, ValueError):
+    # set fallback value
+    write2file = False
 
 
 # get command line argument length
 # get command line argument length
 try:
 try:
-    motiv_length_in_quarter_notes = int(sys.argv[1])
+    motiv_length_in_quarter_notes = int(sys.argv[2])
     # raise error if number too small
     # raise error if number too small
     if motiv_length_in_quarter_notes < 1:
     if motiv_length_in_quarter_notes < 1:
         raise ValueError("Number was too small (below 1)")
         raise ValueError("Number was too small (below 1)")
@@ -49,7 +62,7 @@ except (IndexError, ValueError):
 
 
 # get command line argument velocity
 # get command line argument velocity
 try:
 try:
-    rythme_velocity = int(sys.argv[2])
+    rythme_velocity = int(sys.argv[3])
     # raise error if number too small
     # raise error if number too small
     if rythme_velocity < 0 or rythme_velocity > 3:
     if rythme_velocity < 0 or rythme_velocity > 3:
         raise ValueError("Number out of bounce (below 0 or over 2)")
         raise ValueError("Number out of bounce (below 0 or over 2)")
@@ -57,8 +70,9 @@ except (IndexError, ValueError):
     # set fallback value if no or wrong arguments given
     # set fallback value if no or wrong arguments given
     rythme_velocity = random.randint(0,2)
     rythme_velocity = random.randint(0,2)
 
 
+# get command line argument first note
 try:
 try:
-    first_note = str(sys.argv[3])
+    first_note = str(sys.argv[4])
     # raise error if not in correct gizmo notation
     # raise error if not in correct gizmo notation
     if not check_pitch_gizmo_notation(first_note):
     if not check_pitch_gizmo_notation(first_note):
         raise ValueError("Not in correct gizmo notation")
         raise ValueError("Not in correct gizmo notation")
@@ -66,6 +80,7 @@ except (IndexError, ValueError):
     # randomness will be applied later, set first_note to String
     # randomness will be applied later, set first_note to String
     first_note = ''
     first_note = ''
 
 
+
 motiv = []
 motiv = []
 
 
 # temporarly all notes have duration 1.00 or 2.00
 # temporarly all notes have duration 1.00 or 2.00
@@ -110,4 +125,7 @@ for i in range (1, len(motiv)):
     note = motiv[i].split(',')[1]
     note = motiv[i].split(',')[1]
     motiv[i] = duration + ',' + note
     motiv[i] = duration + ',' + note
 
 
-gizmo_stdout(motiv)
+if write2file:
+    gizmo_write2file(motiv, 'gen-motiv')
+else:
+    gizmo_stdout(motiv)

+ 127 - 0
src/song-gen

@@ -0,0 +1,127 @@
+#!/usr/bin/env python3
+
+from gizmo_funtions import *
+import sys
+import os
+from os import path
+
+""" commandline argument"""
+
+# get commandline argument song_kind
+try:
+    song_kind = str(sys.argv[1])
+    # raise error if not letter only
+    if not song_kind.isalpha():
+        raise ValueError("Does not contain only letters")
+    else:
+        song_kind = song_kind.upper()
+except (IndexError, ValueError):
+    # set fallback value if no or wrong arguments given
+    song_kind = 'ABA'
+
+
+"""optional commandline arguments"""
+
+# get commandline argument motiv's which already are determined
+try:
+    already_given = str(sys.argv[2])
+    # raise exception if not letter only
+    if not already_given.isalpha():
+        raise ValueError("Does not contain only letters")
+    else:
+        already_given =  already_given.upper()
+except (IndexError, ValueError):
+    # set fallback value
+    already_given = ''
+
+already_given = diff_char(already_given)
+
+# check if given instances are stored correctly
+working_list = already_given[:]
+for i in already_given:
+    name = 'motiv-' + i
+    if not path.exists(name):
+        working_list.remove(i)
+already_given = working_list
+
+# format the remaining (motiv) argument in a list;
+# i) cut away first two arguments if they are given
+try:
+    motiv_commandline_argument = sys.argv[1:]
+    working_list = motiv_commandline_argument[:]
+    motiv_commandline_argument_list = []
+    s = 0
+    while motiv_commandline_argument[s].isalpha() or motiv_commandline_argument[s] == 'new':
+        if motiv_commandline_argument[s] == 'new':
+            motiv_commandline_argument_list = ['']
+        working_list = working_list[1:]
+        s += 1
+    motiv_commandline_argument = working_list[:]
+
+    # ii) built list
+    argument = ''
+    for i in motiv_commandline_argument:
+        if i == 'new':
+            motiv_commandline_argument_list.append(argument[:-1])
+            argument = ''
+        else:
+            argument += i + ' '
+    motiv_commandline_argument_list.append(argument)
+except (IndexError):
+    motiv_commandline_argument_list = []
+
+# iii) add empty arguments if not enough are given
+
+# get a list of motiv's that have to get computed
+diff_motiv_list = diff_char(song_kind)
+# substract already given motives
+for i in already_given:
+    diff_motiv_list.remove(i)
+length = len(diff_motiv_list) - len(motiv_commandline_argument_list)
+if length < 0:
+    motiv_commandline_argument_list = motiv_commandline_argument_list[:len(diff_motiv_list)]
+else:
+    for i in range(length):
+        motiv_commandline_argument_list.append('')
+
+""" functions """
+# stout thema with given motiv file name
+def thema_gen(motiv_file_name):
+    helper = "python src/thema-extender < " + motiv_file_name
+    os.system(helper)
+    return
+
+# save a file with a newly generated motiv under the given name
+def motiv_gen(motiv_commandline_argument, name):
+    helper_motiv = "python src/motiv_gen file " + motiv_commandline_argument
+    helper_rename = "mv gen-motiv " + name
+    os.system(helper_motiv)
+    os.system(helper_rename)
+    return
+
+"""this program should produce a little song without accompaniment yet"""
+
+
+# compute missing motives
+for i in range(len(diff_motiv_list)):
+    name = 'motiv-' + diff_motiv_list[i]
+    motiv_gen(motiv_commandline_argument_list[i], name)
+
+for i in song_kind:
+    name = 'motiv-' + i
+    thema_gen(name)
+    # print("========== separation ==========")
+
+"""
+os.system("python src/motiv_gen true")
+os.system("python src/thema-extender < gen-motiv")
+os.system("mv gen-motiv motiv-2") # rename file
+
+print("========== separation ==========")
+
+os.system("python src/motiv_gen true")
+os.system("python src/thema-extender < gen-motiv")
+
+os.system("python src/motiv_gen true")
+os.system("python src/thema-extender < motiv-2")
+"""

+ 57 - 0
src/thema-extender

@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+
+from gizmo_transforming_functions import *
+from gizmo_funtions import *
+import random
+import sys
+import os
+
+# read from standard input
+motiv_list = stdin()
+
+# get commandline argument near_value
+try:
+    near_value = int(sys.argv[1])
+    # raise error if number too small
+    if near_value < 1:
+        raise ValueError("Number was too small (below 1)")
+except (IndexError, ValueError):
+    # set fallback value if no or wrong arguments given
+    near_value = 2
+
+# get commandline argument length
+try:
+    length = int(sys.argv[2])
+    # raise error if number too small
+    if length < 1:
+        raise ValueError("Number was too small (below 1)")
+except (IndexError, ValueError):
+    # set fallback value if no or wrong arguments given
+    length = 2
+
+gizmo_write2file(motiv_list, 'temp-file-1')
+os.system("python src/thema-gen < temp-file-1")
+# print('========== separation ============')
+for j in range (length):
+    new_motiv = motiv_list
+    for i in range(near_value):
+        # list of functions to apply to motiv
+        function_list = [
+            "verkleinerung(motiv_list)",
+            "krebs(motiv_list)",
+            "sequenz(motiv_list, 5)",
+            "mirror_pitch(motiv_list)",
+            "mirror_rythm(motiv_list)",
+            ]
+        if '0.25' in return_part(motiv_list, 0):
+            function_list.remove("verkleinerung(motiv_list)")
+        # print(motiv_list)
+        exec("motiv_list = %s" % (new_motiv))
+        # print(motiv_list)
+
+    gizmo_write2file(motiv_list, 'temp-file-2')
+    os.system("python src/thema-gen < temp-file-2")
+    # print('========== separation ============')
+
+gizmo_write2file(motiv_list, 'temp-file-1')
+os.system("python src/thema-gen < temp-file-1")