From 50c1e4c6ca34ff01d0303a16054c69f987d7622c Mon Sep 17 00:00:00 2001 From: Leonardo Bellettini Date: Thu, 5 Dec 2024 22:58:01 +0000 Subject: [PATCH] Fixes for flot2mozzi.py script --- extras/python/float2mozzi.py | 91 +++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/extras/python/float2mozzi.py b/extras/python/float2mozzi.py index 23dae650e..26fa028f3 100644 --- a/extras/python/float2mozzi.py +++ b/extras/python/float2mozzi.py @@ -1,48 +1,53 @@ -## for converting 32 bit float raw files from Audacity, with values -128 to 127 int8 Mozzi table +#!/usr/bin/env python3 +""" +for converting 32 bit float raw files from Audacity, with values -128 to 127 int8 Mozzi table +""" -import sys, array, os, textwrap, math +import array +import math +import os +import sys +import textwrap + +def float2mozzi(infile, outfile, tablename, samplerate): + with open(os.path.expanduser(infile), "rb") as fin: + print("opened ", infile) + valuesetad = int( + os.path.getsize(os.path.expanduser(infile)) / 4 + ) ## adjust for number format + + valuesfromfile = array.array("f") # array of floats + valuesfromfile.fromfile(fin, valuesetad) + + values = valuesfromfile.tolist() + + with open(os.path.expanduser(outfile), "w") as fout: + fout.write("#ifndef " + tablename + "_H_" + "\n") + fout.write("#define " + tablename + "_H_" + "\n \n") + fout.write("#include " + "\n") + fout.write('#include "mozzi_pgmspace.h"' + "\n \n") + fout.write("#define " + tablename + "_NUM_CELLS " + str(len(values)) + "\n") + fout.write("#define " + tablename + "_SAMPLERATE " + str(samplerate) + "\n \n") + outstring = "CONSTTABLE_STORAGE(int8_t) " + tablename + "_DATA [] = {" + try: + for num in values: + outstring += str(math.trunc((num * 128) + 0.5)) + ", " + finally: + outstring += "};" + outstring = textwrap.fill(outstring, 80) + fout.write(outstring) + fout.write("\n \n#endif /* " + tablename + "_H_ */\n") + print("wrote ", outfile) + + return 0 + + +if __name__ == "__main__": if len(sys.argv) != 5: - print 'Usage: float2mozzi.py ' + print("Usage: float2mozzi.py ") sys.exit(1) -[infile, outfile, tablename, samplerate] = sys.argv[1:] - -def float2mozzi(infile, outfile, tablename,samplerate): - fin = open(os.path.expanduser(infile), "rb") - print "opened " + infile - valuesetad = os.path.getsize(os.path.expanduser(infile))/4 ## adjust for number format - - ##print valuesetad - valuesfromfile = array.array('f')## array of floats - try: - valuesfromfile.fromfile(fin,valuesetad) - finally: - fin.close() - - values=valuesfromfile.tolist() -## print values[0] -## print values[len(values)-1] -## print len(values) - fout = open(os.path.expanduser(outfile), "w") - fout.write('#ifndef ' + tablename + '_H_' + '\n') - fout.write('#define ' + tablename + '_H_' + '\n \n') - fout.write('#include '+'\n') - fout.write('#include "mozzi_pgmspace.h"'+'\n \n') - fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n') - fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n') - outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {' - try: - for num in values: - outstring += str(math.trunc((num*256)+0.5)) + ", " - ## outstring += str(num) + ", " - ##values.fromfile(fin, uint8_tsetad) - finally: - outstring += "};" - outstring = textwrap.fill(outstring, 80) - fout.write(outstring) - fout.write('\n \n #endif /* ' + tablename + '_H_ */\n') - fout.close() - print "wrote " + outfile - -float2mozzi(infile, outfile, tablename, samplerate) + [infile, outfile, tablename, samplerate] = sys.argv[1:] + + sys.exit(float2mozzi(infile, outfile, tablename, samplerate))