|
1 | | -## for converting 32 bit float raw files from Audacity, with values -128 to 127 int8 Mozzi table |
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +for converting 32 bit float raw files from Audacity, with values -128 to 127 int8 Mozzi table |
| 4 | +""" |
2 | 5 |
|
3 | | -import sys, array, os, textwrap, math |
| 6 | +import array |
| 7 | +import math |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import textwrap |
4 | 11 |
|
| 12 | + |
| 13 | +def float2mozzi(infile, outfile, tablename, samplerate): |
| 14 | + with open(os.path.expanduser(infile), "rb") as fin: |
| 15 | + print("opened ", infile) |
| 16 | + valuesetad = int( |
| 17 | + os.path.getsize(os.path.expanduser(infile)) / 4 |
| 18 | + ) ## adjust for number format |
| 19 | + |
| 20 | + valuesfromfile = array.array("f") # array of floats |
| 21 | + valuesfromfile.fromfile(fin, valuesetad) |
| 22 | + |
| 23 | + values = valuesfromfile.tolist() |
| 24 | + |
| 25 | + with open(os.path.expanduser(outfile), "w") as fout: |
| 26 | + fout.write("#ifndef " + tablename + "_H_" + "\n") |
| 27 | + fout.write("#define " + tablename + "_H_" + "\n \n") |
| 28 | + fout.write("#include <Arduino.h>" + "\n") |
| 29 | + fout.write('#include "mozzi_pgmspace.h"' + "\n \n") |
| 30 | + fout.write("#define " + tablename + "_NUM_CELLS " + str(len(values)) + "\n") |
| 31 | + fout.write("#define " + tablename + "_SAMPLERATE " + str(samplerate) + "\n \n") |
| 32 | + outstring = "CONSTTABLE_STORAGE(int8_t) " + tablename + "_DATA [] = {" |
| 33 | + try: |
| 34 | + for num in values: |
| 35 | + outstring += str(math.trunc((num * 128) + 0.5)) + ", " |
| 36 | + finally: |
| 37 | + outstring += "};" |
| 38 | + outstring = textwrap.fill(outstring, 80) |
| 39 | + fout.write(outstring) |
| 40 | + fout.write("\n \n#endif /* " + tablename + "_H_ */\n") |
| 41 | + print("wrote ", outfile) |
| 42 | + |
| 43 | + return 0 |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
5 | 47 | if len(sys.argv) != 5: |
6 | | - print 'Usage: float2mozzi.py <infile outfile tablename samplerate>' |
| 48 | + print("Usage: float2mozzi.py <infile outfile tablename samplerate>") |
7 | 49 | sys.exit(1) |
8 | 50 |
|
9 | | -[infile, outfile, tablename, samplerate] = sys.argv[1:] |
10 | | - |
11 | | -def float2mozzi(infile, outfile, tablename,samplerate): |
12 | | - fin = open(os.path.expanduser(infile), "rb") |
13 | | - print "opened " + infile |
14 | | - valuesetad = os.path.getsize(os.path.expanduser(infile))/4 ## adjust for number format |
15 | | - |
16 | | - ##print valuesetad |
17 | | - valuesfromfile = array.array('f')## array of floats |
18 | | - try: |
19 | | - valuesfromfile.fromfile(fin,valuesetad) |
20 | | - finally: |
21 | | - fin.close() |
22 | | - |
23 | | - values=valuesfromfile.tolist() |
24 | | -## print values[0] |
25 | | -## print values[len(values)-1] |
26 | | -## print len(values) |
27 | | - fout = open(os.path.expanduser(outfile), "w") |
28 | | - fout.write('#ifndef ' + tablename + '_H_' + '\n') |
29 | | - fout.write('#define ' + tablename + '_H_' + '\n \n') |
30 | | - fout.write('#include <Arduino.h>'+'\n') |
31 | | - fout.write('#include "mozzi_pgmspace.h"'+'\n \n') |
32 | | - fout.write('#define ' + tablename + '_NUM_CELLS '+ str(len(values))+'\n') |
33 | | - fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n') |
34 | | - outstring = 'CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {' |
35 | | - try: |
36 | | - for num in values: |
37 | | - outstring += str(math.trunc((num*256)+0.5)) + ", " |
38 | | - ## outstring += str(num) + ", " |
39 | | - ##values.fromfile(fin, uint8_tsetad) |
40 | | - finally: |
41 | | - outstring += "};" |
42 | | - outstring = textwrap.fill(outstring, 80) |
43 | | - fout.write(outstring) |
44 | | - fout.write('\n \n #endif /* ' + tablename + '_H_ */\n') |
45 | | - fout.close() |
46 | | - print "wrote " + outfile |
47 | | - |
48 | | -float2mozzi(infile, outfile, tablename, samplerate) |
| 51 | + [infile, outfile, tablename, samplerate] = sys.argv[1:] |
| 52 | + |
| 53 | + sys.exit(float2mozzi(infile, outfile, tablename, samplerate)) |
0 commit comments