aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rwxr-xr-xutil/audio_generate_dac_lut.py67
-rwxr-xr-xutil/sample_parser.py39
-rwxr-xr-xutil/wavetable_parser.py40
3 files changed, 146 insertions, 0 deletions
diff --git a/util/audio_generate_dac_lut.py b/util/audio_generate_dac_lut.py
new file mode 100755
index 000000000..c31ba3d7e
--- /dev/null
+++ b/util/audio_generate_dac_lut.py
@@ -0,0 +1,67 @@
1#!/usr/bin/env python3
2#
3# Copyright 2020 JohSchneider
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17#
18
19AUDIO_DAC_BUFFER_SIZE=256
20AUDIO_DAC_SAMPLE_MAX=4095
21
22def plot(values):
23 for v in values:
24 print('0'* int(v * 80/AUDIO_DAC_SAMPLE_MAX))
25
26def to_lut(values):
27 for v in values:
28 print(hex(int(v)), end=", ")
29
30
31from math import sin, tau, pi
32
33samples=[]
34
35def sampleSine():
36 for s in range(AUDIO_DAC_BUFFER_SIZE):
37 samples.append((sin((s/AUDIO_DAC_BUFFER_SIZE)*tau - pi/2) + 1 )/2* AUDIO_DAC_SAMPLE_MAX)
38
39def sampleTriangle():
40 for s in range(AUDIO_DAC_BUFFER_SIZE):
41 if s < AUDIO_DAC_BUFFER_SIZE/2:
42 samples.append(s/(AUDIO_DAC_BUFFER_SIZE/2) * AUDIO_DAC_SAMPLE_MAX)
43 else:
44 samples.append(AUDIO_DAC_SAMPLE_MAX - (s-AUDIO_DAC_BUFFER_SIZE/2)/(AUDIO_DAC_BUFFER_SIZE/2) * AUDIO_DAC_SAMPLE_MAX)
45
46#compromise between square and triangle wave,
47def sampleTrapezoidal():
48 for i in range(AUDIO_DAC_BUFFER_SIZE):
49 a=3 #slope/inclination
50 if (i < AUDIO_DAC_BUFFER_SIZE/2):
51 s = a * (i * AUDIO_DAC_SAMPLE_MAX/(AUDIO_DAC_BUFFER_SIZE/2)) + (1-a)*AUDIO_DAC_SAMPLE_MAX/2
52 else:
53 i = i - AUDIO_DAC_BUFFER_SIZE/2
54 s = AUDIO_DAC_SAMPLE_MAX - a * (i * AUDIO_DAC_SAMPLE_MAX/(AUDIO_DAC_BUFFER_SIZE/2)) - (1-a)*AUDIO_DAC_SAMPLE_MAX/2
55
56 if s < 0:
57 s=0
58 if s> AUDIO_DAC_SAMPLE_MAX:
59 s=AUDIO_DAC_SAMPLE_MAX
60 samples.append(s)
61
62
63#sampleSine()
64sampleTrapezoidal()
65#print(samples)
66plot(samples)
67to_lut(samples)
diff --git a/util/sample_parser.py b/util/sample_parser.py
new file mode 100755
index 000000000..70e193aee
--- /dev/null
+++ b/util/sample_parser.py
@@ -0,0 +1,39 @@
1#!/usr/bin/env python3
2#
3# Copyright 2019 Jack Humbert
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17#
18
19import wave, struct, sys
20
21waveFile = wave.open(sys.argv[1], 'r')
22# print(str(waveFile.getparams()))
23# sys.exit()
24
25if (waveFile.getsampwidth() != 2):
26 raise(Exception("This script currently only works with 16bit audio files"))
27
28length = waveFile.getnframes()
29out = "#define DAC_SAMPLE_CUSTOM_LENGTH " + str(length) + "\n\n"
30out += "static const dacsample_t dac_sample_custom[" + str(length) + "] = {"
31for i in range(0,length):
32 if (i % 8 == 0):
33 out += "\n "
34 waveData = waveFile.readframes(1)
35 data = struct.unpack("<h", waveData)
36 out += str(int((int(data[0]) + 0x8000) / 16)) + ", "
37out = out[:-2]
38out += "\n};"
39print(out)
diff --git a/util/wavetable_parser.py b/util/wavetable_parser.py
new file mode 100755
index 000000000..be0f01f7b
--- /dev/null
+++ b/util/wavetable_parser.py
@@ -0,0 +1,40 @@
1#!/usr/bin/env python3
2#
3# Copyright 2019 Jack Humbert
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17#
18
19import wave, struct, sys
20
21waveFile = wave.open(sys.argv[1], 'r')
22
23length = waveFile.getnframes()
24out = "#define DAC_WAVETABLE_CUSTOM_LENGTH " + str(int(length / 256)) + "\n\n"
25out += "static const dacsample_t dac_wavetable_custom[" + str(int(length / 256)) + "][256] = {"
26for i in range(0,length):
27 if (i % 8 == 0):
28 out += "\n "
29 if (i % 256 == 0):
30 out = out[:-2]
31 out += "{\n "
32 waveData = waveFile.readframes(1)
33 data = struct.unpack("<h", waveData)
34 out += str(int((int(data[0]) + 0x8000) / 16)) + ", "
35 if (i % 256 == 255):
36 out = out[:-2]
37 out += "\n },"
38out = out[:-1]
39out += "\n};"
40print(out)