def readDefault(): if not os.path.exists(__DEFAULT_LOC) or not os.path.isfile(__DEFAULT_LOC): Out.errorMessage(ErrorMessage.SKEL_IO_ERROR_DEFAULT) raise GeneratorException() try : readSkel(__DEFAULT_LOC) except : Out.errorMessage(ErrorMessage.SKEL_IO_ERROR_DEFAULT) raise GeneratorException()
def readSkel(skel): global lines lines = [] selection = '' for ln in open(skel): if ln.startswith('---') : lines.append(selection) selection = '' else : selection += __NL selection += ln if selection : lines.append(selection) if len(lines) != __SIZE : Out.errorMessage(message=ErrorMessage.WRONG_SKELETON); raise GeneratorException()
def setDir(path : str) : """ Метод устанавливает папку Если указаный пусть файл будет брошено исключение Если же такой папки не существует она будет создана, если произошла ошибка при создании папки будет брошено исключение. :param path: пусть папки :raise: GeneratorException() """ global __directory if os.path.isdir(path) : __directory = path # print(path) elif os.path.isfile(path) : # print("Error: \""+path+"\" is not a directory.") Out.errorString("Error: \""+path+"\" is not a directory.") raise GeneratorException() else : try : os.makedirs(path) __directory = path except Exception: Out.errorMessage("Error: couldn't create directory \""+path+"\"") raise GeneratorException()
def readSkeletonFile(skel : str): if skel is None : raise GeneratorException(message='Skeleton file must not be None') if not os.path.isfile(skel): Out.errorMessage(message=ErrorMessage.CANNOT_READ_SKEL,file=skel) raise GeneratorException() Out.writeln(message=ErrorMessage.READING_SKEL, *skel ) try: file = open(skel) readSkel(skel) except : Out.errorMessage(ErrorMessage.SKEL_IO_ERROR) raise GeneratorException()
def parse(self): # We use one-token lookahead to determine which alternative to use # get_token == identifier tokNo = t.tokenizer.get_token() if tokNo == t.Tokens.IDENTIFIER.value: self.__assign = Assign.Assign() self.__assign.parse() self.__alternative = 1 # get_token == "if" elif tokNo == t.Tokens.IF.value: self.__if = If.If() self.__if.parse() self.__alternative = 2 # get_token == "while" elif tokNo == t.Tokens.WHILE.value: self.__loop = Loop.Loop() self.__loop.parse() self.__alternative = 3 # get_token == "read" elif tokNo == t.Tokens.READ.value: self.__in = In.In() self.__in.parse() self.__alternative = 4 # get_token == "write" elif tokNo == t.Tokens.WRITE.value: self.__out = Out.Out() self.__out.parse() self.__alternative = 5 else: print("Stmt: Shouldn't have gotten here ;(") # Successful error code return 0
def main(): parser = argparse.ArgumentParser(description='haspie - Harmonizing music with ASP') parser.add_argument('xml_score', metavar='XML_SCORE', help='input musicXML score for armonizing') parser.add_argument('-n', '--num_sols', metavar='N', nargs=1, default=0, type=int, help='max number of ASP solutions, by default all of them') parser.add_argument('-s', '--span', metavar='S', nargs=1, default=1, type=int, help='horizontal span to consider while harmonizing, this takes in account subdivision, by default 1') parser.add_argument('-v', '--voices', metavar='V', nargs="+", default="", help='extra instruments, these can be input by name or by numerical note range (i.e soprano,guitar,(65,90)...) to leave one of the sides of the range unespecified use 0') parser.add_argument('-S', '--show', action='store_true', default=False, help='show result in editor instead of writing it to a file in the desired format') parser.add_argument('-f', '--format', metavar='xml|pdf|midi|ly', nargs=1, default="xml", type=str, help='output file format for the result') parser.add_argument('-o', '--output', metavar='output/dir/for/file', nargs=1, default="out", type=str, help='output file name for the result') parser.add_argument('-t', '--timeout', metavar='T', nargs=1, default=0, type=int, help='maximum time (in seconds) allowed to search for optimum when searching for all optimums, by default there is no time limit') parser.add_argument('-k', '--key', metavar='A~G+-?', nargs=1, default="", help='key in which the score should be harmonized, if not specified, parser will autodetect it') parser.add_argument('-m', '--mode', metavar='major|minor', nargs=1, default="", choices=['major', 'minor'], help='mode of the scale, if not specified, parser will autodetect it') parser.add_argument('-M', '--melodious', action='store_true', default=False, help='turns on melodic preferences in ASP for a more melodic result') parser.add_argument('-6', '--sixthslink', action='store_true', default=False, help='turns on sixth-four chord linking in ASP for a more natural result (very heavy)') parser.add_argument('-a', '--all_optimums', action='store_true', default=False, help='turns on the search for all optimums when completing and not just the first found, disabled by default') parser.add_argument('-O', '--max_optimums', metavar='O', nargs=1, default=10, type=int, help='max number of optimum solutions to display in score completion, by default it\'s 10') parser.add_argument('-c', '--config', metavar='config_file_name.lp', nargs=1, default="", help='reads preference order and weights for parameters from the desired *.lp file stored in /pref folder') args = parser.parse_args() infile = args.xml_score n = args.num_sols if args.num_sols != 0: n = args.num_sols[0] opt_all = "" if args.all_optimums: opt_all = "--opt-all" mode = args.mode if args.mode != "": mode = args.mode[0] span = args.span if args.span != 1: span = args.span[0] fmt = args.format if args.format != "xml": fmt = args.format[0] if args.output != "out": final_out = args.output[0] timeout = args.timeout if args.timeout != 0: timeout = args.timeout[0] key = args.key if args.key != "": key = args.key[0] melodious = "" if args.melodious: melodious = "asp/preferences/melodious.lp" sixthslink = "" if args.sixthslink: sixthslink = "asp/preferences/sixths_link.lp" max_optimums = args.max_optimums if args.max_optimums != 10: max_optimums = args.max_optimums[0] config = args.config if args.config: config = args.config[0] asp_outfile_name = re.search('/(.*?)\.xml', infile) outname = asp_outfile_name.group(1) if args.output == "out": final_out = "./out/" + outname + "." + fmt lp_outname = outname + ".lp" xml_parser_args = ("parser/mxml_asp", infile, "-o", "asp/generated_logic_music/" + lp_outname, "-s", str(span), "-k", str(key), "-m", str(mode)) xml_parser_ret = subprocess.call(xml_parser_args) score_config = ConfigParser.ConfigParser() score_config.read('./tmp/score_meta.cfg') title = score_config.get('meta', 'title') composer = score_config.get('meta', 'composer') subdivision = score_config.get('scoredata', 'base_note') key = score_config.get('scoredata', 'key_name') key_value = score_config.get('scoredata', 'key_value') mode = score_config.get('scoredata', 'mode') last_voice = int(score_config.get('scoredata', 'last_voice')) freebeat = int(score_config.get('scoredata', 'freebeat')) base = key_to_base(key) extra_voices = "" if args.voices != "": f = open('./tmp/extra_voices.lp', 'w') extra_voices = "tmp/extra_voices.lp" i = 1 for v in args.voices: f.write("voice(" + str(last_voice+i) +").\n") name = re.search("([a-zA-Z\_\-]+)", v) vrange = re.findall("([0-9]+)", v) if name != None: f.write("voice_type("+ str(last_voice+i) + ", " + name.group(0) +").\n") elif name == None: f.write("voice_type("+ str(last_voice+i) + ", piano).\n") elif len(vrange) == 2: vrange = sorted(vrange) if (vrange[0] != "0"): f.write("voice_limit_low("+ str(last_voice+i) + ", " + vrange[0] +").\n") if (vrange[1] != "0"): f.write("voice_limit_high("+ str(last_voice+i) + ", " + vrange[1] +").\n") else: print "Voice limit for voice "+str(last_voice+1)+" has not been properly specified, please refer to usage.\n" i += 1 f.close() if xml_parser_ret != 0: sys.exit("Parsing error, stopping execution.") asp_chord_args = ("clingo", config, "asp/assign_chords.lp", "asp/include/" + mode + "_mode.lp", "asp/include/" + mode + "_chords.lp", "asp/include/chord_conversions.lp", "asp/include/measures.lp", "asp/include/voice_types.lp", extra_voices, "asp/generated_logic_music/" + lp_outname,"-n", str(n), "--const", "span=" + str(span), "--const", "base="+ str(base), "--const", "subdiv="+subdivision) asp_proc = subprocess.Popen(asp_chord_args, stdout=subprocess.PIPE) asp_chord_out = asp_proc.stdout.read() if (re.search("UNSATISFIABLE",asp_chord_out) != None): sys.exit("UNSATISFIABLE, stopping execution.") chords = ClaspChords(asp_chord_out) print chords sol_num = len(chords.chord_solutions) selected_solution = raw_input('Select a chord solution for this score (1..' + str(sol_num) +') [' + str(sol_num) + ']: ') if selected_solution == '': selected_solution = sol_num print chords.chord_solutions[int(selected_solution)-1] assig_chords = open("tmp/assigned_chords.lp", "w") assig_chords.write(HaspMusic.asp_clean_chords(chords.chord_solutions[int(selected_solution)-1].raw_ans)) assig_chords.close() asp_note_args = ("clingo", config, sixthslink, melodious, "asp/complete_score.lp", "asp/include/" + mode + "_mode.lp", "asp/include/" + mode + "_chords.lp", "asp/include/conversions.lp", "asp/include/measures.lp", "asp/include/voice_types.lp", "tmp/assigned_chords.lp", extra_voices, "asp/generated_logic_music/" + lp_outname,"-n", str(n), "--const", "span=" + str(span), "--const", "base="+ str(base), "--const", "subdiv="+subdivision) asp_proc = subprocess.Popen(asp_note_args, stdout=subprocess.PIPE) if (timeout > 0): if (args.voices != "" or freebeat == 1): t = threading.Timer( timeout, clasp_timeout, [asp_proc] ) t.start() t.join() t.cancel() asp_note_out = asp_proc.stdout.read() if (re.search("UNSATISFIABLE",asp_note_out) != None): sys.exit("UNSATISFIABLE, stopping execution.") res = ClaspResult(asp_note_out,max_optimums) print res sol_num = len(res.solutions) if sol_num > 0: if (args.voices != "" or freebeat == 1): selected_solution = raw_input('Select a solution to output (1..' + str(sol_num) +') [' + str(sol_num) + ']: ') if selected_solution == '': selected_solution = sol_num print res.solutions[int(selected_solution)-1] else: selected_solution = sol_num output = Out.solution_to_music21(res.solutions[int(selected_solution)-1], int(subdivision), span, base, int(key_value), mode, title, composer) if args.show: output.show(fmt) else: print "Writing output file to", final_out output.write(fp=final_out, fmt=fmt) else: print "Timeout was to short or something went wrong, no solutions were found.\n"
def main(): parser = argparse.ArgumentParser( description='haspie - Harmonizing music with ASP') parser.add_argument('xml_score', metavar='XML_SCORE', help='input musicXML score for armonizing') parser.add_argument( '-n', '--num_sols', metavar='N', nargs=1, default=0, type=int, help='max number of ASP solutions, by default all of them') parser.add_argument( '-s', '--span', metavar='S', nargs=1, default=1, type=int, help= 'horizontal span to consider while harmonizing, this takes in account subdivision, by default 1' ) parser.add_argument( '-v', '--voices', metavar='V', nargs="+", default="", help= 'extra instruments, these can be input by name or by numerical note range (i.e soprano,guitar,(65,90)...) to leave one of the sides of the range unespecified use 0' ) parser.add_argument( '-S', '--show', action='store_true', default=False, help= 'show result in editor instead of writing it to a file in the desired format' ) parser.add_argument('-f', '--format', metavar='xml|pdf|midi|ly', nargs=1, default="xml", type=str, help='output file format for the result') parser.add_argument('-o', '--output', metavar='output/dir/for/file', nargs=1, default="out", type=str, help='output file name for the result') parser.add_argument( '-t', '--timeout', metavar='T', nargs=1, default=0, type=int, help= 'maximum time (in seconds) allowed to search for optimum when searching for all optimums, by default there is no time limit' ) parser.add_argument( '-k', '--key', metavar='A~G+-?', nargs=1, default="", help= 'key in which the score should be harmonized, if not specified, parser will autodetect it' ) parser.add_argument( '-m', '--mode', metavar='major|minor', nargs=1, default="", choices=['major', 'minor'], help='mode of the scale, if not specified, parser will autodetect it') parser.add_argument( '-M', '--melodious', action='store_true', default=False, help='turns on melodic preferences in ASP for a more melodic result') parser.add_argument( '-6', '--sixthslink', action='store_true', default=False, help= 'turns on sixth-four chord linking in ASP for a more natural result (very heavy)' ) parser.add_argument( '-a', '--all_optimums', action='store_true', default=False, help= 'turns on the search for all optimums when completing and not just the first found, disabled by default' ) parser.add_argument( '-O', '--max_optimums', metavar='O', nargs=1, default=10, type=int, help= 'max number of optimum solutions to display in score completion, by default it\'s 10' ) parser.add_argument( '-c', '--config', metavar='config_file_name.lp', nargs=1, default="", help= 'reads preference order and weights for parameters from the desired *.lp file stored in /pref folder' ) args = parser.parse_args() infile = args.xml_score n = args.num_sols if args.num_sols != 0: n = args.num_sols[0] opt_all = "" if args.all_optimums: opt_all = "--opt-all" mode = args.mode if args.mode != "": mode = args.mode[0] span = args.span if args.span != 1: span = args.span[0] fmt = args.format if args.format != "xml": fmt = args.format[0] if args.output != "out": final_out = args.output[0] timeout = args.timeout if args.timeout != 0: timeout = args.timeout[0] key = args.key if args.key != "": key = args.key[0] melodious = "" if args.melodious: melodious = "asp/preferences/melodious.lp" sixthslink = "" if args.sixthslink: sixthslink = "asp/preferences/sixths_link.lp" max_optimums = args.max_optimums if args.max_optimums != 10: max_optimums = args.max_optimums[0] config = args.config if args.config: config = args.config[0] asp_outfile_name = re.search('/(.*?)\.xml', infile) outname = asp_outfile_name.group(1) if args.output == "out": final_out = "./out/" + outname + "." + fmt lp_outname = outname + ".lp" xml_parser_args = ("parser/mxml_asp", infile, "-o", "asp/generated_logic_music/" + lp_outname, "-s", str(span), "-k", str(key), "-m", str(mode)) xml_parser_ret = subprocess.call(xml_parser_args) score_config = ConfigParser.ConfigParser() score_config.read('./tmp/score_meta.cfg') title = score_config.get('meta', 'title') composer = score_config.get('meta', 'composer') subdivision = score_config.get('scoredata', 'base_note') key = score_config.get('scoredata', 'key_name') key_value = score_config.get('scoredata', 'key_value') mode = score_config.get('scoredata', 'mode') last_voice = int(score_config.get('scoredata', 'last_voice')) freebeat = int(score_config.get('scoredata', 'freebeat')) base = key_to_base(key) extra_voices = "" if args.voices != "": f = open('./tmp/extra_voices.lp', 'w') extra_voices = "tmp/extra_voices.lp" i = 1 for v in args.voices: f.write("voice(" + str(last_voice + i) + ").\n") name = re.search("([a-zA-Z\_\-]+)", v) vrange = re.findall("([0-9]+)", v) if name != None: f.write("voice_type(" + str(last_voice + i) + ", " + name.group(0) + ").\n") elif name == None: f.write("voice_type(" + str(last_voice + i) + ", piano).\n") elif len(vrange) == 2: vrange = sorted(vrange) if (vrange[0] != "0"): f.write("voice_limit_low(" + str(last_voice + i) + ", " + vrange[0] + ").\n") if (vrange[1] != "0"): f.write("voice_limit_high(" + str(last_voice + i) + ", " + vrange[1] + ").\n") else: print "Voice limit for voice " + str( last_voice + 1 ) + " has not been properly specified, please refer to usage.\n" i += 1 f.close() if xml_parser_ret != 0: sys.exit("Parsing error, stopping execution.") asp_chord_args = ("clingo", config, "asp/assign_chords.lp", "asp/include/" + mode + "_mode.lp", "asp/include/" + mode + "_chords.lp", "asp/include/chord_conversions.lp", "asp/include/measures.lp", "asp/include/voice_types.lp", extra_voices, "asp/generated_logic_music/" + lp_outname, "-n", str(n), "--const", "span=" + str(span), "--const", "base=" + str(base), "--const", "subdiv=" + subdivision) asp_proc = subprocess.Popen(asp_chord_args, stdout=subprocess.PIPE) asp_chord_out = asp_proc.stdout.read() if (re.search("UNSATISFIABLE", asp_chord_out) != None): sys.exit("UNSATISFIABLE, stopping execution.") chords = ClaspChords(asp_chord_out) print chords sol_num = len(chords.chord_solutions) selected_solution = raw_input( 'Select a chord solution for this score (1..' + str(sol_num) + ') [' + str(sol_num) + ']: ') if selected_solution == '': selected_solution = sol_num print chords.chord_solutions[int(selected_solution) - 1] assig_chords = open("tmp/assigned_chords.lp", "w") assig_chords.write( HaspMusic.asp_clean_chords( chords.chord_solutions[int(selected_solution) - 1].raw_ans)) assig_chords.close() asp_note_args = ("clingo", config, sixthslink, melodious, "asp/complete_score.lp", "asp/include/" + mode + "_mode.lp", "asp/include/" + mode + "_chords.lp", "asp/include/conversions.lp", "asp/include/measures.lp", "asp/include/voice_types.lp", "tmp/assigned_chords.lp", extra_voices, "asp/generated_logic_music/" + lp_outname, "-n", str(n), "--const", "span=" + str(span), "--const", "base=" + str(base), "--const", "subdiv=" + subdivision) asp_proc = subprocess.Popen(asp_note_args, stdout=subprocess.PIPE) if (timeout > 0): if (args.voices != "" or freebeat == 1): t = threading.Timer(timeout, clasp_timeout, [asp_proc]) t.start() t.join() t.cancel() asp_note_out = asp_proc.stdout.read() if (re.search("UNSATISFIABLE", asp_note_out) != None): sys.exit("UNSATISFIABLE, stopping execution.") res = ClaspResult(asp_note_out, max_optimums) print res sol_num = len(res.solutions) if sol_num > 0: if (args.voices != "" or freebeat == 1): selected_solution = raw_input('Select a solution to output (1..' + str(sol_num) + ') [' + str(sol_num) + ']: ') if selected_solution == '': selected_solution = sol_num print res.solutions[int(selected_solution) - 1] else: selected_solution = sol_num output = Out.solution_to_music21( res.solutions[int(selected_solution) - 1], int(subdivision), span, base, int(key_value), mode, title, composer) if args.show: output.show(fmt) else: print "Writing output file to", final_out output.write(fp=final_out, fmt=fmt) else: print "Timeout was to short or something went wrong, no solutions were found.\n"
def __init__(self): self.zone = 0 self.mode = "dev" global ser self.servos = [ servo.Servo(0, 0), servo.Servo(1, 0), servo.Servo(2, 0), servo.Servo(3, 0), servo.Servo(4, 0), servo.Servo(5, 0), servo.Servo(6, 0), servo.Servo(7, 0) ] self.motors = [ motor.Motor(0, 0), motor.Motor(1, 0), motor.Motor(2, 0), motor.Motor(3, 0) ] self.outputs = [ Out.Output(0, 0), Out.Output(1, 0), Out.Output(2, 0), Out.Output(3, 0), Out.Output(4, 0), Out.Output(5, 0), Out.Output(6, 0), Out.Output(7, 0), ] self.inputs = [ In.Input(0, 0), In.Input(1, 0), In.Input(2, 0), In.Input(3, 0), In.Input(4, 0), In.Input(5, 0), In.Input(6, 0), In.Input(7, 0), In.Input(8, 0), In.Input(9, 0), In.Input(10, 0), In.Input(11, 0), In.Input(12, 0), In.Input(13, 0), In.Input(14, 0), In.Input(15, 0) ] self.staff_inputs = [ Staff_In.Input(0, 0), Staff_In.Input(1, 0), Staff_In.Input(2, 0) ] self.a = ser.readline().rstrip() while str(self.a) == "": self.a = ser.readline().rstrip() print "SWITCHES ON" ''' while True: a = self.staff_inputs[0].d b = self.staff_inputs[1].d c = self.staff_inputs[2].d if a == True: if self.zone <= 2: self.zone +=1 elif self.zone == 3: self.zone = 0 print "Your zone is:" + str(self.zone) if b == True: if self.mode == "dev": self.mode = "comp" elif self.mode == "comp": self.mode = "dev" print "Your mode is:" + self.mode if c == True: print "Starting User Code..." break ''' def Timer_exit(round_length): while True: time.sleep(round_length) print "END OF ROUND, NOW EXITING CODE." thread.interrupt_main() if self.mode == "comp": thread.start_new_thread(Timer_exit, (round_length))