dac_tas58xx_eq.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. /**
  3. * 15-band parametric EQ using TAS5825M on-chip biquad filters.
  4. *
  5. * Coefficient addresses and band parameters match the mrtoy-me/esphome-tas58xx
  6. * reference implementation (Book 0xAA, TAS5825M ROM Mode 1).
  7. *
  8. * Band Center (Hz) Q
  9. * 0 20 2.0
  10. * 1 31.5 2.0
  11. * 2 50 1.5
  12. * 3 80 1.5
  13. * 4 125 1.0
  14. * 5 200 1.0
  15. * 6 315 0.9
  16. * 7 500 0.9
  17. * 8 800 0.8
  18. * 9 1250 0.8
  19. * 10 2000 0.7
  20. * 11 3150 0.7
  21. * 12 5000 0.6
  22. * 13 8000 0.6
  23. * 14 16000 0.5
  24. *
  25. * Requires dac_init() to have been called first.
  26. */
  27. #include "esp_err.h"
  28. #include <stdbool.h>
  29. /** Number of EQ bands (one per on-chip biquad) */
  30. #define TAS58XX_EQ_BANDS 15
  31. /** Gain limits per band (dB) — matches reference ±15 dB range */
  32. #define TAS58XX_EQ_MAX_GAIN_DB 15.0f
  33. #define TAS58XX_EQ_MIN_GAIN_DB (-15.0f)
  34. /**
  35. * Enable or disable the on-chip parametric EQ.
  36. * Must be called after the DAC enters PLAY state to take effect.
  37. * When disabled, all biquads are bypassed (flat response).
  38. *
  39. * @param enable true to enable EQ processing, false to bypass
  40. * @return ESP_OK on success
  41. */
  42. esp_err_t tas58xx_eq_enable(bool enable);
  43. /**
  44. * Set gain for a single EQ band.
  45. * Uses pre-computed coefficient lookup tables for integer dB gains.
  46. * Coefficients are written immediately; both L and R channels are set.
  47. *
  48. * @param band Band index 0–14
  49. * @param gain_db Gain in dB (rounded to nearest integer, clamped to ±15 dB)
  50. * @return ESP_OK on success
  51. */
  52. esp_err_t tas58xx_eq_set_band(int band, float gain_db);
  53. /**
  54. * Set all 15 EQ bands in one call.
  55. *
  56. * @param gains_db Array of TAS58XX_EQ_BANDS gain values in dB
  57. * @return ESP_OK on success, or the first error encountered
  58. */
  59. esp_err_t tas58xx_eq_set_all(const float gains_db[TAS58XX_EQ_BANDS]);
  60. /**
  61. * Reset all bands to flat (0 dB gain) and enable EQ.
  62. */
  63. esp_err_t tas58xx_eq_flat(void);
  64. /**
  65. * Return the center frequency (Hz) of the given band.
  66. *
  67. * @param band Band index 0–14
  68. * @return Center frequency in Hz, or 0.0f if index is invalid
  69. */
  70. float tas58xx_eq_get_center_freq(int band);
  71. /**
  72. * Read back biquad coefficients at the expected RAM addresses and log
  73. * whether they match the default (flat) pattern. Useful for verifying
  74. * that the coefficient address table is correct for this device.
  75. *
  76. * @return ESP_OK if all addresses look valid
  77. */
  78. esp_err_t tas58xx_eq_verify_addresses(void);