#!/usr/bin/env python3 """Turns a model file into a C++ include (one byte array) that LYNX compiles in, so the model needs no file next to the program. Used for Basic Pitch's nmp.onnx (third_party/basic-pitch/README.md). usage: tools/make_model_inc.py """ import hashlib, sys data = open(sys.argv[1], 'rb').read() name = sys.argv[2] lines = [', '.join(str(b) for b in data[i:i + 40]) + ',' for i in range(0, len(data), 40)] with open(sys.argv[3], 'w', newline='\n') as f: f.write('// Generated by tools/make_model_inc.py from %s (%d bytes, SHA-256 %s). Do not edit.\n' % (sys.argv[1].replace('\\', '/').split('/')[-1], len(data), hashlib.sha256(data).hexdigest())) f.write('alignas (16) static const unsigned char %s[%d] = {\n' % (name, len(data))) f.write('\n'.join(lines)) f.write('\n};\n')