Skip to content

Commit

Permalink
Fixes for flot2mozzi.py script
Browse files Browse the repository at this point in the history
  • Loading branch information
leobel96 committed Dec 5, 2024
1 parent 091c319 commit 50c1e4c
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions extras/python/float2mozzi.py
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>" + "\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 <infile outfile tablename samplerate>'
print("Usage: float2mozzi.py <infile outfile tablename samplerate>")
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 <Arduino.h>'+'\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))

0 comments on commit 50c1e4c

Please sign in to comment.