audio_generate_dac_lut.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. AUDIO_DAC_BUFFER_SIZE=256
  19. AUDIO_DAC_SAMPLE_MAX=4095
  20. def plot(values):
  21. for v in values:
  22. print('0'* int(v * 80/AUDIO_DAC_SAMPLE_MAX))
  23. def to_lut(values):
  24. for v in values:
  25. print(hex(int(v)), end=", ")
  26. from math import sin, tau, pi
  27. samples=[]
  28. def sampleSine():
  29. for s in range(AUDIO_DAC_BUFFER_SIZE):
  30. samples.append((sin((s/AUDIO_DAC_BUFFER_SIZE)*tau - pi/2) + 1 )/2* AUDIO_DAC_SAMPLE_MAX)
  31. def sampleTriangle():
  32. for s in range(AUDIO_DAC_BUFFER_SIZE):
  33. if s < AUDIO_DAC_BUFFER_SIZE/2:
  34. samples.append(s/(AUDIO_DAC_BUFFER_SIZE/2) * AUDIO_DAC_SAMPLE_MAX)
  35. else:
  36. samples.append(AUDIO_DAC_SAMPLE_MAX - (s-AUDIO_DAC_BUFFER_SIZE/2)/(AUDIO_DAC_BUFFER_SIZE/2) * AUDIO_DAC_SAMPLE_MAX)
  37. #compromise between square and triangle wave,
  38. def sampleTrapezoidal():
  39. for i in range(AUDIO_DAC_BUFFER_SIZE):
  40. a=3 #slope/inclination
  41. if (i < AUDIO_DAC_BUFFER_SIZE/2):
  42. s = a * (i * AUDIO_DAC_SAMPLE_MAX/(AUDIO_DAC_BUFFER_SIZE/2)) + (1-a)*AUDIO_DAC_SAMPLE_MAX/2
  43. else:
  44. i = i - AUDIO_DAC_BUFFER_SIZE/2
  45. s = AUDIO_DAC_SAMPLE_MAX - a * (i * AUDIO_DAC_SAMPLE_MAX/(AUDIO_DAC_BUFFER_SIZE/2)) - (1-a)*AUDIO_DAC_SAMPLE_MAX/2
  46. if s < 0:
  47. s=0
  48. if s> AUDIO_DAC_SAMPLE_MAX:
  49. s=AUDIO_DAC_SAMPLE_MAX
  50. samples.append(s)
  51. #sampleSine()
  52. sampleTrapezoidal()
  53. #print(samples)
  54. plot(samples)
  55. to_lut(samples)