def print_module(fn_pch2: str, mod_id: int, loc: Location): if not fn_pch2.lower().endswith('.pch2'): print("error: patch file should have extension '.pch2'") exit(-1) data = ProjectData() path = os.path.abspath(os.path.expanduser(fn_pch2)) p = parse_pch2(data, path) m = p.find_module(mod_id, loc) if m is None: print('error: cannot find module with id {} in the {} location'.format( mod_id, loc.short_str())) exit(-1) udo = Udo(p, m) params_midi = p.find_mod_params(loc, mod_id) params_mapped = udo.get_params() assert params_midi.num_params == len(params_mapped) tbl = [['Type', 'Raw', 'Mapped']] for raw, mapped in zip(params_midi.values, params_mapped): tbl.append(['Parameter', str(raw), str(mapped)]) for mode in m.modes: tbl.append(['Mode', str(mode), '']) print('Patch: {}'.format(fn_pch2)) print('Details of the module:\n{}'.format(m)) print() print(tabulate(tbl, headers='firstrow', tablefmt='simple'))
def parse_module_list(blob: bitarray, patch: Patch): bits = BitArrayStream(blob) loc, num_modules = bits.read_ints([2, 8]) num_modules, = unpack('>B', blob[2:10].tobytes()) for i in range(num_modules): mod_type, mod_id = bits.read_ints([8, 8]) hpos, vpos, color = bits.read_ints([7, 7, 8]) _, _n = bits.read_ints([8, 4]) _ = bits.read_ints([6] * _n) mod = Module(patch.data, Location.from_int(loc), mod_type, mod_id) patch.modules.append(mod)
def parse_module_parameters(blob: bitarray, patch: Patch): bits = BitArrayStream(blob) loc, num_modules, num_variations = bits.read_ints([2, 8, 8]) for imod in range(num_modules): mod_id, num_params = bits.read_ints([8, 7]) mod_params = ModuleParameters(Location.from_int(loc), mod_id, num_params) for ivar in range(num_variations): var, *p_list = bits.read_ints([8] + [7] * num_params) if var == patch.description.active_variation: mod_params.values.extend(p_list) # TODO variations support patch.mod_params.append(mod_params)
def _gen_instr(self, loc: Location) -> str: s = StringIO() s.write('; --------------------\n') s.write('; {} AREA\n'.format(loc.short_str())) s.write('instr {}\n'.format(2 - loc.value)) statements = [udo.get_statement_parts() for udo in self.udos if udo.mod.location == loc] table_head = ('; Module', 'Parameters', 'Modes', 'Inlets', 'Outlets') table_str = tabulate(statements, table_head, tablefmt='plain') s.write(table_str) s.write('\nendin\n') return s.getvalue()
def parse_cable_list(blob: bitarray, patch: Patch): bits = BitArrayStream(blob) loc, _skip_, num_cables = bits.read_ints([2, 14, 8]) for i in range(num_cables): color, module_from, jack_from, cable_type, module_to, jack_to = bits.read_ints( [3, 8, 6, 1, 8, 6]) c = Cable(Location.from_int(loc), CableType.from_int(cable_type), CableColor.from_int(color), module_from, jack_from, module_to, jack_to) if c.type == CableType.IN_TO_IN: # By some reason, in2in cables have sources swapped back with destinations... c.module_from = module_to c.module_to = module_from c.jack_from = jack_to c.jack_to = jack_from patch.cables.append(c)
def parse_location(loc: int): return Location.from_int(loc >> 6)
def main(): arg_parser = argparse.ArgumentParser( prog='pch2csd', description='convert Clavia Nord Modular G2 patches to the Csound code', epilog='Version {}, homepage: {}'.format(__version__, __homepage__)) arg_parser.add_argument('arg', metavar='arg', nargs='?', default='patch.pch2', help='a pch2 file path or an UDO numerical ID') arg_parser.add_argument('-d', '--debug', action='store_const', const=True, help='print a stack trace in case of error') group = arg_parser.add_mutually_exclusive_group() group.add_argument('-p', '--print', action='store_const', const=True, help='parse the patch file and print its content') group.add_argument('-m', '--mod-print', nargs=2, metavar=('module_id', '{voice,fx}'), help='print extensive information about the module in' 'the file {arg}. You should provide two values:' 'an integer module id and an area.') group.add_argument('-c', '--check-udo', action='store_const', const=True, help="validate the UDO template file (overrides '-p')") group.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__) group.add_argument('-e', action='store_const', const=True, help='show the elephant and exit') args = arg_parser.parse_args() if args.check_udo: try: type_id = int(args.arg) validate_udo(type_id) except ValueError: print( "you should pass the integer as the 'arg' parameter when using '--check-udo'" ) elif args.mod_print: print_module(args.arg, int(args.mod_print[0]), Location.from_str(args.mod_print[1])) elif args.print: print_pch2(args.arg) elif args.e: show_elephant() else: if args.arg == 'gen_udo_status_doc': gen_udo_status_doc() else: try: convert_pch2(args.arg) except Exception as e: print(e) if args.debug: import traceback _, _, tb = sys.exc_info() print() print('-----------') traceback.print_tb(tb, file=sys.stdout)