4
0

make_model_inc.py 862 B

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