Parent directory

sample_parser.py

1344 bytes
 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)