You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.3 KiB

  1. #! /bin/python
  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. # print(str(waveFile.getparams()))
  21. # sys.exit()
  22. if (waveFile.getsampwidth() != 2):
  23. raise(Exception("This script currently only works with 16bit audio files"))
  24. length = waveFile.getnframes()
  25. out = "#define DAC_SAMPLE_CUSTOM_LENGTH " + str(length) + "\n\n"
  26. out += "static const dacsample_t dac_sample_custom[" + str(length) + "] = {"
  27. for i in range(0,length):
  28. if (i % 8 == 0):
  29. out += "\n "
  30. waveData = waveFile.readframes(1)
  31. data = struct.unpack("<h", waveData)
  32. out += str(int((int(data[0]) + 0x8000) / 16)) + ", "
  33. out = out[:-2]
  34. out += "\n};"
  35. print(out)