-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |