Skip to content

Commit d1ea293

Browse files
committed
Added standard tables generating script
1 parent 30e73bd commit d1ea293

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/python3
2+
3+
"""
4+
* make_standard_tables.py
5+
*
6+
* This file is part of Mozzi.
7+
*
8+
* Copyright 2012-2024 Thomas Combriat and the Mozzi Team
9+
*
10+
* Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
11+
*
12+
* This file allows to regenerate the standard tables of Mozzi (sin, cos, cosphase, triangle and saw)
13+
"""
14+
15+
import numpy as np # numerics
16+
import textwrap
17+
import os as os
18+
#import math as math # math
19+
20+
folder = ".."+os.sep+".."+os.sep+"tables" + os.sep
21+
22+
def write_header(fout, samplerate,name):
23+
tablename = name.upper()+str(samplerate)+"_INT8"
24+
fout.write('#ifndef ' + tablename + '_H_' + '\n')
25+
fout.write('#define ' + tablename + '_H_' + '\n \n')
26+
fout.write("/**\n This table is part of Mozzi\n Generated with extras/python/make_standard_tables.py\n*/\n\n")
27+
fout.write('#include <Arduino.h>'+'\n')
28+
fout.write('#include "mozzi_pgmspace.h"'+'\n \n')
29+
fout.write('#define ' + tablename + '_NUM_CELLS '+ str(samplerate)+'\n')
30+
fout.write('#define ' + tablename + '_SAMPLERATE '+ str(samplerate)+'\n \n')
31+
fout.write('CONSTTABLE_STORAGE(int8_t) ' + tablename + '_DATA [] = {\n')
32+
33+
def write_footer(fout,samplerate,name):
34+
tablename = name.upper()+str(samplerate)+"_INT8"
35+
fout.write('\n }; \n \n #endif /* ' + tablename + '_H_ */\n')
36+
37+
38+
39+
40+
41+
#### SIN
42+
43+
SR = [256,512,1024,2048,4096,8192]
44+
name = "sin"
45+
46+
for s in SR:
47+
fout = open(folder+name+str(s)+"_int8.h","w")
48+
49+
write_header(fout,s,name)
50+
51+
t = np.linspace(0,2*np.pi,num=s,endpoint=False)
52+
outstring=""
53+
for i in range(s):
54+
outstring+=str(round(127*np.sin(t[i])))+", "
55+
56+
outstring = textwrap.fill(outstring,80)
57+
fout.write(outstring)
58+
write_footer(fout,s,name)
59+
fout.close()
60+
61+
print("Wrote:"+name.upper()+str(s)+"_INT8")
62+
63+
64+
#### COS
65+
# Note that this is actually a negative cos
66+
67+
SR = [256,512,1024,2048,4096,8192]
68+
name = "cos"
69+
70+
for s in SR:
71+
fout = open(folder+name+str(s)+"_int8.h","w")
72+
73+
write_header(fout,s,name)
74+
75+
t = np.linspace(0,2*np.pi,num=s,endpoint=False)
76+
outstring=""
77+
for i in range(s):
78+
outstring+=str(round(-127*np.cos(t[i])))+", "
79+
80+
outstring = textwrap.fill(outstring,80)
81+
fout.write(outstring)
82+
write_footer(fout,s,name)
83+
fout.close()
84+
85+
print("Wrote:"+name.upper()+str(s)+"_INT8")
86+
87+
88+
#### COSPHASE
89+
90+
SR = [256,2048,8192]
91+
name = "cosphase"
92+
93+
for s in SR:
94+
fout = open(folder+name+str(s)+"_int8.h","w")
95+
96+
write_header(fout,s,name)
97+
98+
t = np.linspace(0,2*np.pi,num=s,endpoint=False)
99+
outstring=""
100+
for i in range(s):
101+
outstring+=str(round(127*np.cos(t[i])))+", "
102+
103+
outstring = textwrap.fill(outstring,80)
104+
fout.write(outstring)
105+
write_footer(fout,s,name)
106+
fout.close()
107+
108+
print("Wrote:"+name.upper()+str(s)+"_INT8")
109+
110+
111+
112+
113+
#### TRIANGLE
114+
115+
SR = [512,1024,2048]
116+
name = "triangle"
117+
118+
for s in SR:
119+
fout = open(folder+name+str(s)+"_int8.h","w")
120+
121+
write_header(fout,s,name)
122+
123+
t = np.linspace(0,2*np.pi,num=s,endpoint=False)
124+
outstring=""
125+
for i in range(s):
126+
if (i<=s/4):
127+
outstring+=str(round(i*127./s*4))+", "
128+
elif (i<=s*3/4):
129+
outstring+=str(round(127-(i-s/4)*(127*2)/(s/2)))+", "
130+
else:
131+
outstring+=str(round(-127.+127.*(i-s*3/4)/s*4))+", "
132+
133+
outstring = textwrap.fill(outstring,80)
134+
fout.write(outstring)
135+
write_footer(fout,s,name)
136+
fout.close()
137+
138+
print("Wrote:"+name.upper()+str(s)+"_INT8")
139+
140+
141+
142+
143+
144+
#### SAW
145+
146+
SR = [256,512,1024,2048,4096,8192]
147+
name = "saw"
148+
149+
for s in SR:
150+
fout = open(folder+name+str(s)+"_int8.h","w")
151+
152+
write_header(fout,s,name)
153+
154+
t = np.linspace(0,2*np.pi,num=s,endpoint=False)
155+
outstring=""
156+
for i in range(s):
157+
outstring+=str(str(round(-127+127*2.*i/(s-1)))+", ")
158+
159+
outstring = textwrap.fill(outstring,80)
160+
fout.write(outstring)
161+
write_footer(fout,s,name)
162+
fout.close()
163+
164+
print("Wrote:"+name.upper()+str(s)+"_INT8")

0 commit comments

Comments
 (0)