wavetable_parser.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. import wave, struct, sys
  19. waveFile = wave.open(sys.argv[1], 'r')
  20. length = waveFile.getnframes()
  21. out = "#define DAC_WAVETABLE_CUSTOM_LENGTH " + str(int(length / 256)) + "\n\n"
  22. out += "static const dacsample_t dac_wavetable_custom[" + str(int(length / 256)) + "][256] = {"
  23. for i in range(0,length):
  24. if (i % 8 == 0):
  25. out += "\n "
  26. if (i % 256 == 0):
  27. out = out[:-2]
  28. out += "{\n "
  29. waveData = waveFile.readframes(1)
  30. data = struct.unpack("<h", waveData)
  31. out += str(int((int(data[0]) + 0x8000) / 16)) + ", "
  32. if (i % 256 == 255):
  33. out = out[:-2]
  34. out += "\n },"
  35. out = out[:-1]
  36. out += "\n};"
  37. print(out)